front/src/views/site/lib/route-cover.ts
zzh c3c00eb764
All checks were successful
Build and Deploy Front / deploy (push) Successful in 11m49s
refine: 官网 AI 首页输入台布局、精选封面与信任条,顶栏 Logo 圆角方标
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-27 15:46:24 +08:00

121 lines
3.3 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 type { RouteItem } from './portal-catalog';
const warmedCoverUrls = new Set<string>();
const readyCoverUrls = new Set<string>();
const readyListeners = new Map<string, Set<() => void>>();
export function routeHasRealCover(item: RouteItem): boolean {
return Boolean(item.coverImageUrl?.trim());
}
/** 仅返回运营上传的封面;无图时不走彩色渐变兜底 */
export function resolveRouteCoverUrl(item: RouteItem): string {
return item.coverImageUrl?.trim() || '';
}
export function isRouteCoverReady(url: string): boolean {
const u = String(url || '').trim();
return Boolean(u && readyCoverUrls.has(u));
}
function notifyCoverReady(url: string) {
const set = readyListeners.get(url);
if (!set?.size) return;
readyListeners.delete(url);
for (const cb of set) {
try {
cb();
} catch {
/* ignore */
}
}
}
function markCoverReady(url: string) {
const u = String(url || '').trim();
if (!u || readyCoverUrls.has(u)) return;
readyCoverUrls.add(u);
notifyCoverReady(u);
}
/** 订阅封面就绪(命中缓存则同步回调);返回取消函数 */
export function subscribeRouteCoverReady(url: string, cb: () => void): () => void {
const u = String(url || '').trim();
if (!u) return () => {};
if (readyCoverUrls.has(u)) {
cb();
return () => {};
}
let set = readyListeners.get(u);
if (!set) {
set = new Set();
readyListeners.set(u, set);
}
set.add(cb);
return () => {
set?.delete(cb);
if (set && set.size === 0) readyListeners.delete(u);
};
}
function warmOneCover(url: string) {
const u = String(url || '').trim();
if (!u || typeof window === 'undefined') return;
if (readyCoverUrls.has(u)) return;
const finish = () => markCoverReady(u);
try {
const img = new Image();
img.decoding = 'async';
img.onload = finish;
img.onerror = finish;
img.src = u;
if (img.complete && img.naturalWidth > 0) finish();
} catch {
finish();
}
}
/**
* 把精选封面提前推进浏览器缓存。
* highPriorityUrl 额外挂 link rel=preload,优先中心卡。
*/
export function preloadRouteCovers(
urls: string[],
opts?: { highPriorityUrl?: string },
): void {
if (typeof window === 'undefined') return;
const list = [...new Set(urls.map((u) => String(u || '').trim()).filter(Boolean))];
for (const url of list) {
if (!warmedCoverUrls.has(url)) warmedCoverUrls.add(url);
warmOneCover(url);
}
const high = opts?.highPriorityUrl?.trim();
if (!high || typeof document === 'undefined') return;
const marker = 'data-oms-route-cover-preload';
const existed = Array.from(document.head.querySelectorAll(`link[${marker}="1"]`)).some(
(el) => (el as HTMLLinkElement).href === new URL(high, window.location.href).href,
);
if (existed) return;
try {
const link = document.createElement('link');
link.rel = 'preload';
link.as = 'image';
link.href = high;
link.setAttribute(marker, '1');
document.head.appendChild(link);
} catch {
/* ignore */
}
}
export function preloadFeaturedRouteCovers(items: RouteItem[], activeIndex = 0): void {
const urls = items.map(resolveRouteCoverUrl).filter(Boolean);
if (!urls.length) return;
const high =
resolveRouteCoverUrl(items[Math.max(0, Math.min(activeIndex, items.length - 1))] || items[0]) ||
urls[0];
preloadRouteCovers(urls, { highPriorityUrl: high });
}