fix(platform): 持久化并完善斗鱼兑换结果图片
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { DouyuTaskItem } from '../api/types';
|
||||
import { douyuApi } from '../api/douyu';
|
||||
|
||||
const EXCHANGE_TASK_TYPES = new Set([
|
||||
'exchange_goods',
|
||||
@@ -124,6 +125,21 @@ function douyuImageData(task: DouyuTaskItem): ExchangeResultImageData {
|
||||
};
|
||||
}
|
||||
|
||||
export function savedExchangeImage(task: DouyuTaskItem): {
|
||||
objectKey: string;
|
||||
url: string;
|
||||
} | null {
|
||||
const saved = task.result?.exchange_result_image;
|
||||
if (!saved || typeof saved !== 'object' || Array.isArray(saved)) return null;
|
||||
const record = saved as Record<string, unknown>;
|
||||
const objectKey = typeof record.object_key === 'string' ? record.object_key : '';
|
||||
if (!objectKey) return null;
|
||||
const url = typeof record.url === 'string' && record.url
|
||||
? record.url
|
||||
: `/api/files/object?key=${encodeURIComponent(objectKey)}`;
|
||||
return { objectKey, url };
|
||||
}
|
||||
|
||||
export async function drawExchangeResultImage(
|
||||
data: ExchangeResultImageData,
|
||||
): Promise<Blob> {
|
||||
@@ -188,9 +204,9 @@ export async function drawExchangeResultImage(
|
||||
ctx.stroke();
|
||||
|
||||
// 商品名(最多两行)
|
||||
const nameLines = wrapText(ctx, info.goodsName || '未知商品', CARD_W - 56).slice(0, 2);
|
||||
ctx.fillStyle = '#ffffff';
|
||||
ctx.font = `bold 17px ${FONT_FAMILY}`;
|
||||
const nameLines = wrapText(ctx, info.goodsName || '未知商品', CARD_W - 56).slice(0, 2);
|
||||
let nameY = avatarY + avatarR + 26;
|
||||
for (const line of nameLines) {
|
||||
ctx.fillText(line, CARD_W / 2, nameY);
|
||||
@@ -259,6 +275,14 @@ export async function copyImageToClipboard(blob: Blob): Promise<void> {
|
||||
await navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })]);
|
||||
}
|
||||
|
||||
export async function fetchExchangeImageBlob(url: string): Promise<Blob> {
|
||||
const response = await fetch(url, { credentials: 'include' });
|
||||
if (!response.ok) throw new Error(`图片读取失败(HTTP ${response.status})`);
|
||||
const blob = await response.blob();
|
||||
if (!blob.type.startsWith('image/')) throw new Error('返回内容不是图片');
|
||||
return blob;
|
||||
}
|
||||
|
||||
export function clipboardImageSupported(): boolean {
|
||||
return typeof ClipboardItem !== 'undefined' && Boolean(navigator.clipboard?.write);
|
||||
}
|
||||
@@ -296,10 +320,59 @@ export async function getExchangeResultImage(
|
||||
}
|
||||
|
||||
export async function getExchangeImage(task: DouyuTaskItem): Promise<CachedImage> {
|
||||
return getExchangeResultImage(douyuImageData(task));
|
||||
const saved = savedExchangeImage(task);
|
||||
if (saved) {
|
||||
const cacheKey = `douyu:${task.id}:saved:${saved.objectKey}`;
|
||||
const cached = imageCache.get(cacheKey);
|
||||
if (cached) return cached;
|
||||
const pending = inflightImages.get(cacheKey);
|
||||
if (pending) return pending;
|
||||
const creating = fetchExchangeImageBlob(saved.url).then((blob) => {
|
||||
const entry = { url: saved.url, blob };
|
||||
imageCache.set(cacheKey, entry);
|
||||
return entry;
|
||||
});
|
||||
inflightImages.set(cacheKey, creating);
|
||||
try {
|
||||
return await creating;
|
||||
} finally {
|
||||
inflightImages.delete(cacheKey);
|
||||
}
|
||||
}
|
||||
|
||||
const cacheKey = `douyu:${task.id}:generated`;
|
||||
const cached = imageCache.get(cacheKey);
|
||||
if (cached) return cached;
|
||||
const pending = inflightImages.get(cacheKey);
|
||||
if (pending) return pending;
|
||||
const creating = (async () => {
|
||||
const blob = await drawExchangeResultImage(douyuImageData(task));
|
||||
let url = URL.createObjectURL(blob);
|
||||
try {
|
||||
const file = new File([blob], `douyu-exchange-result-${task.id}.png`, {
|
||||
type: 'image/png',
|
||||
});
|
||||
const savedImage = await douyuApi.saveExchangeResultImage(task.id, file);
|
||||
URL.revokeObjectURL(url);
|
||||
url = savedImage.url;
|
||||
} catch (error) {
|
||||
console.warn('兑换结果图片保存失败,暂时保留本地图片', error);
|
||||
}
|
||||
const entry = { url, blob };
|
||||
imageCache.set(cacheKey, entry);
|
||||
return entry;
|
||||
})();
|
||||
inflightImages.set(cacheKey, creating);
|
||||
try {
|
||||
return await creating;
|
||||
} finally {
|
||||
inflightImages.delete(cacheKey);
|
||||
}
|
||||
}
|
||||
|
||||
export function clearExchangeImageCache(): void {
|
||||
for (const entry of imageCache.values()) URL.revokeObjectURL(entry.url);
|
||||
for (const entry of imageCache.values()) {
|
||||
if (entry.url.startsWith('blob:')) URL.revokeObjectURL(entry.url);
|
||||
}
|
||||
imageCache.clear();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user