一分钟搭建影视站,支持Docker等部署方式
fix(quality): 徽章就地显示失败原因,超时失败自动补探一次
bestZwei1bca17df9ce1
diff --git a/src/components/quality-badge.tsx b/src/components/quality-badge.tsxindex d09aae8..8a0577e 100644--- a/src/components/quality-badge.tsx+++ b/src/components/quality-badge.tsx@@ -15,12 +15,15 @@ export function QualityBadge({entry,loading,onRetry,+ showReason,className,}: {entry?: QualityEntry;loading?: boolean;/** 失败态可重试;不传则失败态退化为普通徽章 */onRetry?: () => void;+ /** 就地显示失败原因(超时/防盗链/无资源):触屏看不到 tooltip,需要可见文案 */+ showReason?: boolean;className?: string;}) {const view = describeQualityBadge(entry, loading);@@ -72,9 +75,10 @@ export function QualityBadge({className)}title={view.title}- aria-label="画质未探到,点击重试"+ aria-label={`本次没测到画质${view.reason ? `(${view.reason})` : ''},点击重试`}>- 未探到+ {view.label ?? '未测到'}+ {showReason && view.reason && <span className="ml-0.5 opacity-80">·{view.reason}</span>}</button>);}@@ -84,7 +88,8 @@ export function QualityBadge({className={cn('tag shrink-0', view.kind === 'failed' ? 'bg-warning/15 text-warning' : 'bg-chip text-faint', className)}title={view.title}>- {view.kind === 'failed' ? '未探到' : '未知'}+ {view.kind === 'failed' ? view.label ?? '未测到' : '未知'}+ {showReason && view.reason && <span className="ml-0.5 opacity-80">·{view.reason}</span>}</span>);}diff --git a/src/components/use-quality-probe.ts b/src/components/use-quality-probe.tsindex 5c68efc..f94f28d 100644--- a/src/components/use-quality-probe.ts+++ b/src/components/use-quality-probe.ts@@ -2,7 +2,7 @@import { useCallback, useEffect, useMemo, useRef, useState } from 'react';import { api } from '@/lib/client-api';-import { QUALITY_FAIL_TTL_MS, qualityKey, summarizeQuality } from '@/lib/quality';+import { QUALITY_FAIL_TTL_MS, isRetryableTimeout, qualityKey, summarizeQuality } from '@/lib/quality';import { createProbeQueue, type ProbeQueue } from '@/lib/quality-probe-queue';import { readQualities, useAppStore } from '@/lib/store';import type { QualityEntry, QualityProbeItem, QualityProbeResult, SourceConfig } from '@/lib/types';@@ -92,6 +92,10 @@ export function useQualityProbe(sources?: SourceConfig[]) {*/const qualitiesRef = useRef<Record<string, QualityEntry>>({});qualitiesRef.current = qualities;+ /** 已经自动重试过的键:超时只补一次,避免「超时→重试→超时」循环打源站 */+ const retriedRef = useRef<Set<string>>(new Set());+ /** 让延迟重试能拿到最新的 probe(probe 依赖 resolveItem,闭包会过期) */+ const probeRef = useRef<((items: QualityProbeTarget[], opts?: { force?: boolean }) => void) | null>(null);useEffect(() => {mountedRef.current = true;@@ -132,6 +136,32 @@ export function useQualityProbe(sources?: SourceConfig[]) {});}, []);+ /**+ * 超时自动补一次。+ * 失败里只有超时是「这次没够着」——冷启动、CDN 抖动、源站响应慢;+ * 同一条请求 300ms 后再打一次成功率明显更高,可避免把可用源标成「未测到」。+ * 只补一次:retriedRef 记住已重试的键,仍失败就交给用户手动重试。+ */+ const scheduleTimeoutRetry = useCallback(+ (collected: Record<string, QualityEntry>, targetByKey: Map<string, QualityProbeTarget>) => {+ const retries: QualityProbeTarget[] = [];+ for (const [key, entry] of Object.entries(collected)) {+ if (!isRetryableTimeout(entry.error)) continue;+ if (retriedRef.current.has(key)) continue;+ const target = targetByKey.get(key);+ if (!target) continue;+ retriedRef.current.add(key);+ retries.push(target);+ }+ if (retries.length === 0) return;+ setTimeout(() => {+ if (!mountedRef.current) return;+ probeRef.current?.(retries, { force: true });+ }, 300);+ },+ []+ );+/** 读取缓存并把目标登记为「进度分母」:卡片收起时也能显示最高画质 */const prime = useCallback((items: QualityProbeTarget[]) => {const list = dedupeByKey(items);@@ -184,6 +214,7 @@ export function useQualityProbe(sources?: SourceConfig[]) {if (mapped.length === 0) return;const keys = mapped.map((m) => m.key);+ const targetByKey = new Map(list.map((t) => [qualityKey(t.sourceKey, t.vodId), t]));const batch = new Set(keys);// 结果先攒着,流结束一次性写全局:逐条写会让 persist 把整个 qualityMap(最多 400 条)序列化十几遍const collected: Record<string, QualityEntry> = {};@@ -204,6 +235,8 @@ export function useQualityProbe(sources?: SourceConfig[]) {collected[key] = entry;inflightRef.current.delete(key);settledRef.current.set(key, Date.now());+ // 探成功就忘掉「已重试过」:下次再超时仍然值得补一次+ if (!entry.error) retriedRef.current.delete(key);mark([key], false);},controller.signal@@ -213,6 +246,8 @@ export function useQualityProbe(sources?: SourceConfig[]) {}).finally(() => {if (Object.keys(collected).length > 0) useAppStore.getState().setQuality(collected);+ // 超时补一次放在接管守卫之前:结果已经拿到,是否被接管都不影响这一批的补探测+ scheduleTimeoutRetry(collected, targetByKey);// 被新一批接管时跳过清理:未完成的键已由新批同步段重新调度(finally 是 microtask,必然晚于它)if (batchRef.current !== batch) return;for (const key of batch) inflightRef.current.delete(key);@@ -220,8 +255,10 @@ export function useQualityProbe(sources?: SourceConfig[]) {mark(keys, false);});},- [mark, resolveItem]+ [mark, resolveItem, scheduleTimeoutRetry]);+ // 延迟重试走的入口:scheduleTimeoutRetry 的定时器里不能直接用 probe(会捕获旧闭包)+ probeRef.current = probe;/*** 经全局队列探测(单源卡片进入视口)。diff --git a/src/lib/quality.ts b/src/lib/quality.tsindex 311843d..03a5ad0 100644--- a/src/lib/quality.ts+++ b/src/lib/quality.ts@@ -16,7 +16,25 @@ export const QUALITY_TTL_MS = 30 * 24 * 60 * 60 * 1000;* 失败原因多为瞬时(超时/限流/防盗链偶发拒绝),不能像成功结果那样存 30 天,* 否则偶发失败会被当成永久结论,用户再也看不到画质。*/-export const QUALITY_FAIL_TTL_MS = 10 * 60 * 1000;+export const QUALITY_FAIL_TTL_MS = 2 * 60 * 1000;++/**+ * 失败原因短标签:徽章上直接显示「未测到·超时」比只藏在 tooltip 里有用,+ * 触屏没有 hover,用户更需要一眼看出「该重试」还是「该换源」。+ */+export function failureReasonLabel(error?: string): string | undefined {+ if (!error) return undefined;+ if (error.includes('超时')) return '超时';+ if (/HTTP (401|403)/.test(error)) return '防盗链';+ if (error.includes('未找到播放资源')) return '无资源';+ if (error.includes('无效')) return '源无效';+ return undefined;+}++/** 超时是唯一值得自动重试的失败:冷启动/CDN 抖动,同一条请求再打一次常能成 */+export function isRetryableTimeout(error?: string): boolean {+ return !!error && error.includes('超时');+}/*** 条目是否仍在有效期内(唯一判定入口,写入与读取都走这里)。@@ -128,8 +146,10 @@ export type QualityBadgeKind = 'loading' | 'value' | 'unknown' | 'failed';export interface QualityBadgeView {kind: QualityBadgeKind;- /** 有档位时的展示文本(4K / 1080P / 720P…) */+ /** 有档位时的展示文本(4K / 1080P / 720P…);失败时为「未测到」 */label?: string;+ /** 失败原因短标签(超时 / 防盗链 / 无资源),供徽章就地展示 */+ reason?: string;/** tooltip:说明数据来源,或失败原因与下一步该做什么 */title: string;/** 只有「没够着」值得重试;源站本来就不给画质信息,重试多少次都一样 */@@ -159,9 +179,16 @@ export function describeQualityBadge(};}if (entry.error) {+ const reason = failureReasonLabel(entry.error);+ // 不用「失败/不可用」这类否定结论:探测失败只代表这次没测到,+ // 同一个源换个时刻常常能测到(实测也证明了源是可用的)return {kind: 'failed',- title: `画质探测失败:${entry.error}(可能是防盗链或源站超时),可点击重试`,+ label: '未测到',+ reason,+ title: `本次没测到画质:${entry.error}。${+ reason === '超时' ? '多半是网络抖动或源站响应慢,点击重试' : '点击可重试,也可以直接播放,播放后会自动记录实测分辨率'+ }`,retryable: true,};}