diff --git a/webui/app.js b/webui/app.js index 966d8ae..7c54853 100644 --- a/webui/app.js +++ b/webui/app.js @@ -428,6 +428,25 @@ function initConfigSearch() { }); } +function initFooter() { + // 版本号尝试从 /api/version 拿;拿不到就用 cache-bust 标识做 fallback + const verEl = document.getElementById("appFooterVersion"); + const buildEl = document.getElementById("appFooterBuild"); + (async () => { + try { + const data = await apiGet("/api/version"); + if (verEl && data?.version) verEl.textContent = "v" + data.version; + if (buildEl && data?.build) buildEl.textContent = data.build; + } catch (_) { + // fallback:从当前 script 的 cache-bust 提取 + const tag = (document.currentScript?.src || document.querySelector('script[src*="app.js"]')?.src || "") + .split("v=")[1] || ""; + if (verEl) verEl.textContent = tag ? tag.split("-").pop() : "—"; + if (buildEl) buildEl.textContent = "本地"; + } + })(); +} + function initTabs() { const tabButtons = Array.from(document.querySelectorAll(".tabBtn[data-target]")); const panels = Array.from(document.querySelectorAll(".panel[data-panel]")); @@ -698,7 +717,16 @@ function startShotPolling() { function setTextIfExists(id, text) { const el = $(id); if (!el) return; - el.textContent = String(text ?? "-"); + const next = String(text ?? "-"); + // 仅对 statValue 有效:值变化时短暂高亮 + if (el.classList.contains("statValue") && el.textContent !== next && next !== "-") { + el.classList.remove("isFlash"); + // 强制重排让动画重新触发 + void el.offsetWidth; + el.classList.add("isFlash"); + setTimeout(() => el.classList.remove("isFlash"), 700); + } + el.textContent = next; } function setButtonBusy(id, busy) { @@ -985,10 +1013,49 @@ function renderTimeline(containerId, timeline) { root.appendChild(legend); } +// API 连接状态:连续失败 N 次才算 error,避免轻微抖动闪烁 +let _apiFailStreak = 0; +const _apiFailThreshold = 2; +function setConnState(state) { + const dot = document.getElementById("appConnDot"); + const txt = document.getElementById("appConnText"); + if (!dot || !txt) return; + if (state === "ok") { + dot.dataset.state = "ok"; + txt.textContent = "已连接"; + dot.title = "API 通信正常"; + } else if (state === "error") { + dot.dataset.state = "error"; + txt.textContent = "连接异常"; + dot.title = "API 请求连续失败,检查后端是否运行"; + } else { + dot.dataset.state = "idle"; + txt.textContent = "就绪"; + dot.title = "等待首个 API 请求"; + } +} +function notifyApiOk() { + _apiFailStreak = 0; + setConnState("ok"); +} +function notifyApiFail() { + _apiFailStreak++; + if (_apiFailStreak >= _apiFailThreshold) setConnState("error"); +} + async function apiGet(path) { - const res = await fetch(path, { cache: "no-store" }); - if (!res.ok) throw new Error(await res.text()); - return await res.json(); + try { + const res = await fetch(path, { cache: "no-store" }); + if (!res.ok) { + notifyApiFail(); + throw new Error(await res.text()); + } + notifyApiOk(); + return await res.json(); + } catch (e) { + notifyApiFail(); + throw e; + } } // CSRF token 缓存 @@ -1008,24 +1075,31 @@ async function getCsrfToken() { } async function apiPost(path, body) { - const token = await getCsrfToken(); - const res = await fetch(path, { - method: "POST", - headers: { - "Content-Type": "application/json", - "X-CSRF-Token": token, - }, - body: JSON.stringify(body || {}), - }); - const data = await res.json(); - if (!res.ok) { - const errorMsg = data.error || data.message || res.statusText; - const error = new Error(errorMsg); - error.status = res.status; - error.data = data; - throw error; + try { + const token = await getCsrfToken(); + const res = await fetch(path, { + method: "POST", + headers: { + "Content-Type": "application/json", + "X-CSRF-Token": token, + }, + body: JSON.stringify(body || {}), + }); + const data = await res.json(); + if (!res.ok) { + notifyApiFail(); + const errorMsg = data.error || data.message || res.statusText; + const error = new Error(errorMsg); + error.status = res.status; + error.data = data; + throw error; + } + notifyApiOk(); + return data; + } catch (e) { + notifyApiFail(); + throw e; } - return data; } async function refreshDevices() { @@ -1796,6 +1870,7 @@ window.addEventListener("DOMContentLoaded", async () => { initTheme(); initTabs(); initConfigSearch(); + initFooter(); // 首屏统计数字加骨架,待第一次 pollStats 成功后移除 document.querySelectorAll(".stat .statValue").forEach((el) => { if ((el.textContent || "").trim() === "-") el.classList.add("skeleton"); diff --git a/webui/index.html b/webui/index.html index d6ecc10..89a1a65 100644 --- a/webui/index.html +++ b/webui/index.html @@ -15,7 +15,7 @@ } catch (e) {} })(); - +