feat(system): 添加固件版本名称缓存及云端获取功能
- 在 luxsinApi.ts 中实现固件版本名称的本地缓存机制,缓存时长为30分钟 - 新增读取和写入 localStorage 缓存的函数,避免频繁调用云端API - 实现 fetchFirmwareVersionName 函数,从云端获取固件版本名称并结合本地缓存使用 - 在 SystemPage.tsx 中优先从缓存读取固件版本名称,未命中时异步调用云端接口 - 增加加载状态展示,接口异常或无数据时用本地格式化版本名称兜底显示 - 避免因缓存无效或接口异常导致版本名称缺失,提升系统页固件版本显示的稳定性和体验
This commit is contained in:
@@ -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<string | null>(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() {
|
||||
</div>
|
||||
<div className="ios-list-row">
|
||||
<span className="flex-1 text-[16px] text-white">{ui.firmwareVersion}</span>
|
||||
<span className="ios-row-value">{formatFirmwareVersionDisplay(ds?.version)}</span>
|
||||
<span className="ios-row-value inline-flex items-center">
|
||||
{verNameLoading ? (
|
||||
<Loader2 size={15} className="animate-spin" />
|
||||
) : (
|
||||
verName ?? "—"
|
||||
)}
|
||||
</span>
|
||||
</div>
|
||||
<div className="ios-list-row">
|
||||
<span className="flex-1 text-[16px] text-white">{ui.macAddress}</span>
|
||||
|
||||
Reference in New Issue
Block a user