front/src/utils/printSessionCache.ts
zzh 68b85dadf9
All checks were successful
Build and Deploy Front / deploy (push) Successful in 10m0s
feat: 仓储表单体验、轨迹 SPA 代理修复与打印预览增强
修复 /track/pull|push 被代理误伤 404;Warning/ProviderService/Dictionary 路径改写;SkuImage 入库归一化。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-12 12:30:35 +08:00

151 lines
4.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<string, PrintUrlCacheEntry>();
const printUrlInflight = new Map<string, Promise<string>>();
const previewCache = new Map<string, PrintPreviewCacheEntry>();
const previewInflight = new Map<string, Promise<PrintPreviewCacheEntry>>();
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<PrintPreviewCacheEntry> {
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<PrintPreviewCacheEntry> {
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<PrintPreviewCacheEntry> {
const cached = peekPrintPreviewCache(rawUrl);
if (cached) return cached;
return prefetchPrintPreview(rawUrl);
}
/** 获取 printUrl(带会话缓存 + 请求去重) */
export async function fetchPrintUrlForOrder(orderId: string | number): Promise<string> {
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;
}