feat(huya): 增加兑换记录结果图

This commit is contained in:
yml
2026-11-13 21:32:34 +08:00
parent 8ba947e60f
commit 7f49bc8579
4 changed files with 204 additions and 22 deletions
+69 -20
View File
@@ -8,8 +8,21 @@ const EXCHANGE_TASK_TYPES = new Set([
]);
const CARD_W = 320;
const CARD_H = 470;
const PIXEL_RATIO = 2;
const FONT_FAMILY = '"PingFang SC", "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif';
export interface ExchangeResultImageData {
key: string | number;
goodsName: string;
goodsPic?: string;
roleName?: string;
channel?: string;
accountName: string;
exchangeTime?: string;
statusText?: string;
footerText?: string;
}
export function isExchangeTask(task: DouyuTaskItem): boolean {
return EXCHANGE_TASK_TYPES.has(task.task_type);
}
@@ -55,7 +68,7 @@ function loadImage(url: string, timeoutMs = 8000): Promise<HTMLImageElement> {
img.crossOrigin = 'anonymous';
img.onload = () => resolve(img);
img.onerror = () => reject(new Error('图片加载失败'));
img.src = url;
img.src = url.startsWith('//') ? `https:${url}` : url;
window.setTimeout(() => reject(new Error('图片加载超时')), timeoutMs);
});
}
@@ -97,15 +110,32 @@ function truncate(text: string, max: number): string {
return text.length > max ? `${text.slice(0, max - 1)}…` : text;
}
export async function drawExchangeImage(task: DouyuTaskItem): Promise<Blob> {
function douyuImageData(task: DouyuTaskItem): ExchangeResultImageData {
const info = goodsOf(task);
return {
key: `douyu:${task.id}`,
goodsName: info.name || '未知商品',
goodsPic: info.pic,
roleName: info.roleName,
channel: info.channel,
accountName: info.accountName,
exchangeTime: formatTime(task.finished_at || task.created_at),
statusText: '兑换成功',
};
}
export async function drawExchangeResultImage(
data: ExchangeResultImageData,
): Promise<Blob> {
const canvas = document.createElement('canvas');
canvas.width = CARD_W;
canvas.height = CARD_H;
canvas.width = CARD_W * PIXEL_RATIO;
canvas.height = CARD_H * PIXEL_RATIO;
const ctx = canvas.getContext('2d');
if (!ctx) throw new Error('canvas 不可用');
ctx.scale(PIXEL_RATIO, PIXEL_RATIO);
const info = goodsOf(task);
const time = formatTime(task.finished_at || task.created_at);
const info = data;
const time = data.exchangeTime || '';
// 背景渐变
const grad = ctx.createLinearGradient(0, 0, CARD_W, CARD_H);
@@ -131,9 +161,9 @@ export async function drawExchangeImage(task: DouyuTaskItem): Promise<Blob> {
ctx.clip();
ctx.fillStyle = 'rgba(255,255,255,0.12)';
ctx.fillRect(CARD_W / 2 - avatarR, avatarY - avatarR, avatarR * 2, avatarR * 2);
if (info.pic) {
if (info.goodsPic) {
try {
const img = await loadImage(info.pic);
const img = await loadImage(info.goodsPic);
const size = Math.min(img.width, img.height);
ctx.drawImage(
img,
@@ -158,7 +188,7 @@ export async function drawExchangeImage(task: DouyuTaskItem): Promise<Blob> {
ctx.stroke();
// 商品名(最多两行)
const nameLines = wrapText(ctx, info.name || '未知商品', CARD_W - 56).slice(0, 2);
const nameLines = wrapText(ctx, info.goodsName || '未知商品', CARD_W - 56).slice(0, 2);
ctx.fillStyle = '#ffffff';
ctx.font = `bold 17px ${FONT_FAMILY}`;
let nameY = avatarY + avatarR + 26;
@@ -194,7 +224,7 @@ export async function drawExchangeImage(task: DouyuTaskItem): Promise<Blob> {
}
// 底部状态徽章
const badgeText = '兑换成功';
const badgeText = data.statusText || '兑换成功';
ctx.font = `bold 14px ${FONT_FAMILY}`;
const badgeW = ctx.measureText(badgeText).width + 34;
const badgeX = (CARD_W - badgeW) / 2;
@@ -205,11 +235,23 @@ export async function drawExchangeImage(task: DouyuTaskItem): Promise<Blob> {
ctx.textAlign = 'center';
ctx.fillText(badgeText, CARD_W / 2, 444);
ctx.fillStyle = 'rgba(255,255,255,0.35)';
ctx.font = `10px ${FONT_FAMILY}`;
ctx.fillText(
data.footerText || '真实兑换记录凭证 · 仅用于订单核验',
CARD_W / 2,
CARD_H - 12,
);
return new Promise<Blob>((resolve, reject) => {
canvas.toBlob((blob) => (blob ? resolve(blob) : reject(new Error('图片导出失败'))), 'image/png');
});
}
export async function drawExchangeImage(task: DouyuTaskItem): Promise<Blob> {
return drawExchangeResultImage(douyuImageData(task));
}
export async function copyImageToClipboard(blob: Blob): Promise<void> {
if (typeof ClipboardItem === 'undefined' || !navigator.clipboard?.write) {
throw new Error('当前环境不支持复制图片(需要 HTTPS)');
@@ -226,30 +268,37 @@ interface CachedImage {
blob: Blob;
}
const imageCache = new Map<number, CachedImage>();
const inflightImages = new Map<number, Promise<CachedImage>>();
const imageCache = new Map<string, CachedImage>();
const inflightImages = new Map<string, Promise<CachedImage>>();
export async function getExchangeImage(task: DouyuTaskItem): Promise<CachedImage> {
const cached = imageCache.get(task.id);
export async function getExchangeResultImage(
data: ExchangeResultImageData,
): Promise<CachedImage> {
const cacheKey = String(data.key);
const cached = imageCache.get(cacheKey);
if (cached) return cached;
const pending = inflightImages.get(task.id);
const pending = inflightImages.get(cacheKey);
if (pending) return pending;
const creating = (async () => {
const blob = await drawExchangeImage(task);
const blob = await drawExchangeResultImage(data);
const entry = { url: URL.createObjectURL(blob), blob };
const previous = imageCache.get(task.id);
const previous = imageCache.get(cacheKey);
if (previous) URL.revokeObjectURL(previous.url);
imageCache.set(task.id, entry);
imageCache.set(cacheKey, entry);
return entry;
})();
inflightImages.set(task.id, creating);
inflightImages.set(cacheKey, creating);
try {
return await creating;
} finally {
inflightImages.delete(task.id);
inflightImages.delete(cacheKey);
}
}
export async function getExchangeImage(task: DouyuTaskItem): Promise<CachedImage> {
return getExchangeResultImage(douyuImageData(task));
}
export function clearExchangeImageCache(): void {
for (const entry of imageCache.values()) URL.revokeObjectURL(entry.url);
imageCache.clear();