From 8d1384766836e9311a3fffec715931ed0b9107d1 Mon Sep 17 00:00:00 2001 From: eafonyang Date: Wed, 9 Sep 2026 14:14:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(system):=20=E6=B7=BB=E5=8A=A0=E5=9B=BA?= =?UTF-8?q?=E4=BB=B6=E7=89=88=E6=9C=AC=E5=90=8D=E7=A7=B0=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E5=8F=8A=E4=BA=91=E7=AB=AF=E8=8E=B7=E5=8F=96=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 luxsinApi.ts 中实现固件版本名称的本地缓存机制,缓存时长为30分钟 - 新增读取和写入 localStorage 缓存的函数,避免频繁调用云端API - 实现 fetchFirmwareVersionName 函数,从云端获取固件版本名称并结合本地缓存使用 - 在 SystemPage.tsx 中优先从缓存读取固件版本名称,未命中时异步调用云端接口 - 增加加载状态展示,接口异常或无数据时用本地格式化版本名称兜底显示 - 避免因缓存无效或接口异常导致版本名称缺失,提升系统页固件版本显示的稳定性和体验 --- client/src/lib/luxsinApi.ts | 95 +++++++++++++++++++++++++++++++++ client/src/pages/SystemPage.tsx | 53 ++++++++++++++++-- 2 files changed, 144 insertions(+), 4 deletions(-) diff --git a/client/src/lib/luxsinApi.ts b/client/src/lib/luxsinApi.ts index 6fe3429..55dd541 100644 --- a/client/src/lib/luxsinApi.ts +++ b/client/src/lib/luxsinApi.ts @@ -645,6 +645,101 @@ export function buildLuxsinAudioUrl(resourcePath: string): string { return `https://api.luxsin.com.cn/audio/${path}`; } +/** + * Firmware version-name cache. `getVerName` results are stable for a given + * model+version, so we cache them in `localStorage` for 30 minutes to avoid + * hitting the cloud API every time the SystemPage is opened. + */ +const VER_NAME_CACHE_PREFIX = "luxsin:verName:"; +const VER_NAME_CACHE_TTL_MS = 30 * 60 * 1000; + +function verNameCacheKey(model: string, version: string): string { + return `${VER_NAME_CACHE_PREFIX}${model}:${version}`; +} + +/** + * Synchronously read a cached firmware version name for `model`+`version`. + * Returns `null` when there is no entry, it is malformed, or it has expired + * (expired entries are dropped so the next fetch refreshes them). Storage + * access is best-effort — any failure (private mode / quota) yields `null`. + */ +export function readCachedFirmwareVersionName( + model: string, + version: string | number, +): string | null { + const m = String(model ?? "").trim(); + const v = String(version ?? "").trim(); + if (!m || !v) return null; + const key = verNameCacheKey(m, v); + try { + const raw = localStorage.getItem(key); + if (!raw) return null; + const parsed = JSON.parse(raw) as { verName?: unknown; ts?: unknown }; + const verName = typeof parsed?.verName === "string" ? parsed.verName.trim() : ""; + const ts = typeof parsed?.ts === "number" ? parsed.ts : 0; + if (!verName || !ts) return null; + if (Date.now() - ts > VER_NAME_CACHE_TTL_MS) { + localStorage.removeItem(key); + return null; + } + return verName; + } catch { + return null; + } +} + +function writeCachedFirmwareVersionName( + model: string, + version: string, + verName: string, +): void { + try { + localStorage.setItem( + verNameCacheKey(model, version), + JSON.stringify({ verName, ts: Date.now() }), + ); + } catch { + /* storage unavailable (private mode / quota) — caching is best-effort */ + } +} + +/** + * Fetch the firmware display version name from the cloud catalog. + * `GET getVerName?model=&version=` → `{ code, msg, data: { verName } }`. + * Serves from the 30-minute `localStorage` cache when available; otherwise the + * protocol (http/https) follows the current page, matching the other + * `//api.luxsin.com.cn/audio` calls; dev goes through the Vite proxy to avoid + * Origin-based 403/CORS. Returns `null` when unavailable so callers can fall back. + */ +export async function fetchFirmwareVersionName( + model: string, + version: string | number, +): Promise { + const m = String(model ?? "").trim(); + const v = String(version ?? "").trim(); + if (!m || !v) return null; + + // Serve from the browser cache within the TTL to avoid frequent requests. + const cached = readCachedFirmwareVersionName(m, v); + if (cached) return cached; + + const query = `getVerName?model=${encodeURIComponent(m)}&version=${encodeURIComponent(v)}`; + const url = import.meta.env.DEV + ? `/luxsin-audio-api/${query}` + : `//api.luxsin.com.cn/audio/${query}`; + try { + const res = await fetch(url); + if (!res.ok) return null; + const json = (await res.json()) as { data?: { verName?: unknown } }; + const verName = json?.data?.verName; + const result = typeof verName === "string" && verName.trim() ? verName.trim() : null; + if (result) writeCachedFirmwareVersionName(m, v, result); + return result; + } catch { + return null; + } +} + /** Fetches `getBrand`; body is custom Base64 text, decodes to JSON array of `{ id, name }`. */ export async function fetchLuxsinAudioBrands(): Promise { const res = await fetch(buildLuxsinAudioUrl("getBrand")); diff --git a/client/src/pages/SystemPage.tsx b/client/src/pages/SystemPage.tsx index a84dbd7..c3ac5a9 100644 --- a/client/src/pages/SystemPage.tsx +++ b/client/src/pages/SystemPage.tsx @@ -9,11 +9,15 @@ All list rows with options → navigate to /select page ============================================================ */ import { useDevice } from "@/contexts/DeviceContext"; -import { ChevronLeft, ChevronRight } from "lucide-react"; +import { ChevronLeft, ChevronRight, Loader2 } from "lucide-react"; import { useLocation } from "wouter"; -import { useEffect } from "react"; +import { useEffect, useState } from "react"; import BottomNav from "@/components/BottomNav"; -import { formatFirmwareVersionDisplay } from "@/lib/luxsinApi"; +import { + fetchFirmwareVersionName, + formatFirmwareVersionDisplay, + readCachedFirmwareVersionName, +} from "@/lib/luxsinApi"; import { navigateToSelect } from "./SelectPage"; import localeEn from "@/locales/data-en.json"; import { DEFAULT_DEVICE_LANGUAGE, resolveDeviceLanguage, resolveLocalePack } from "@/locales/resolveLocale"; @@ -126,6 +130,41 @@ export default function SystemPage() { } }, [isConnected, api]); + // 固件版本名称:进入系统页时优先读浏览器缓存(30 分钟内命中则直接显示, + // 不再请求接口);未命中才调用云端 getVerName 接口获取;接口异常/无数据时, + // 用本地版本名称计算兜底(本地兜底结果不写缓存)。 + const [verName, setVerName] = useState(null); + const [verNameLoading, setVerNameLoading] = useState(true); + useEffect(() => { + const model = ds?.device?.trim(); + const version = ds?.version; + if (!model || version === undefined || String(version).trim() === "") { + setVerName(null); + setVerNameLoading(false); + return; + } + // 命中缓存:直接显示,不进入 loading,也不请求接口。 + const cached = readCachedFirmwareVersionName(model, version); + if (cached) { + setVerName(cached); + setVerNameLoading(false); + return; + } + let cancelled = false; + setVerNameLoading(true); + fetchFirmwareVersionName(model, version) + .then((name) => { + // 接口异常/无数据:用本地版本名称计算兜底(该结果不缓存)。 + if (!cancelled) setVerName(name ?? formatFirmwareVersionDisplay(version)); + }) + .finally(() => { + if (!cancelled) setVerNameLoading(false); + }); + return () => { + cancelled = true; + }; + }, [ds?.device, ds?.version]); + const screenLightIdx = ds?.screenLight ?? 1; const buttonLightIdx = ds?.buttonLight ?? 1; const screenOffIdx = ds?.screenOff ?? 2; @@ -212,7 +251,13 @@ export default function SystemPage() {
{ui.firmwareVersion} - {formatFirmwareVersionDisplay(ds?.version)} + + {verNameLoading ? ( + + ) : ( + verName ?? "—" + )} +
{ui.macAddress}