import { postOrderPrint } from '@/api/order'; import { fetchPrintPreviewBlob, fetchPrintPreviewPdfBytes, isPrintPreviewPdf, resolvePrintPreviewPdfSource, withPreviewTimeout, } from '@/utils/printPreview'; const PRINT_URL_TTL_MS = 10 * 60 * 1000; const PREVIEW_TTL_MS = 10 * 60 * 1000; export interface PrintPreviewCacheEntry { pdfSource: string | Uint8Array | null; htmlBlob: Blob | null; createdAt: number; } interface PrintUrlCacheEntry { url: string; createdAt: number; } const printUrlCache = new Map(); const printUrlInflight = new Map>(); const previewCache = new Map(); const previewInflight = new Map>(); function normalizeUrlKey(rawUrl: string) { return String(rawUrl || '').trim(); } function isUsablePrintUrl(raw: unknown) { const value = String(raw ?? '').trim(); if (!value) return false; if (value.includes('获取中')) return false; return true; } function isFresh(createdAt: number, ttl: number) { return Date.now() - createdAt < ttl; } export function getCachedPrintUrl(orderId: string | number): string | null { const hit = printUrlCache.get(String(orderId)); if (!hit || !isFresh(hit.createdAt, PRINT_URL_TTL_MS)) { if (hit) printUrlCache.delete(String(orderId)); return null; } return hit.url; } export function setCachedPrintUrl(orderId: string | number, url: string) { const value = String(url || '').trim(); if (!isUsablePrintUrl(value)) return; printUrlCache.set(String(orderId), { url: value, createdAt: Date.now() }); } export function peekPrintPreviewCache(rawUrl: string): PrintPreviewCacheEntry | null { const key = normalizeUrlKey(rawUrl); if (!key) return null; const hit = previewCache.get(key); if (!hit || !isFresh(hit.createdAt, PREVIEW_TTL_MS)) { if (hit) previewCache.delete(key); return null; } return hit; } async function warmPdfNetwork(url: string) { if (!url.startsWith('/__cos__/')) return; try { await fetch(url, { method: 'GET', credentials: 'include' }); } catch { // ignore warm-up failure } } async function buildPreviewEntry(rawUrl: string): Promise { const raw = normalizeUrlKey(rawUrl); if (!raw) throw new Error('打印地址为空'); if (isPrintPreviewPdf(raw)) { const proxied = resolvePrintPreviewPdfSource(raw); if (proxied) { void warmPdfNetwork(proxied); return { pdfSource: proxied, htmlBlob: null, createdAt: Date.now() }; } const bytes = await withPreviewTimeout(fetchPrintPreviewPdfBytes(raw)); return { pdfSource: bytes, htmlBlob: null, createdAt: Date.now() }; } const blob = await withPreviewTimeout(fetchPrintPreviewBlob(raw)); return { pdfSource: null, htmlBlob: blob, createdAt: Date.now() }; } /** 点击后立即预取面单预览(与弹窗打开并行) */ export function prefetchPrintPreview(rawUrl: string): Promise { const key = normalizeUrlKey(rawUrl); if (!key) return Promise.reject(new Error('打印地址为空')); const cached = peekPrintPreviewCache(key); if (cached) return Promise.resolve(cached); const inflight = previewInflight.get(key); if (inflight) return inflight; const task = buildPreviewEntry(key) .then((entry) => { previewCache.set(key, entry); previewInflight.delete(key); return entry; }) .catch((error) => { previewInflight.delete(key); throw error; }); previewInflight.set(key, task); return task; } export async function getOrPrefetchPrintPreview(rawUrl: string): Promise { const cached = peekPrintPreviewCache(rawUrl); if (cached) return cached; return prefetchPrintPreview(rawUrl); } /** 获取 printUrl(带会话缓存 + 请求去重) */ export async function fetchPrintUrlForOrder(orderId: string | number): Promise { const key = String(orderId); const cached = getCachedPrintUrl(key); if (cached) return cached; const inflight = printUrlInflight.get(key); if (inflight) return inflight; const task = postOrderPrint({ id: orderId }) .then((res) => { const url = String(res?.printUrl ?? '').trim(); if (isUsablePrintUrl(url)) setCachedPrintUrl(key, url); return url; }) .finally(() => { printUrlInflight.delete(key); }); printUrlInflight.set(key, task); return task; }