refactor(webui): extract API client helpers
This commit is contained in:
1 parent
3491970090
commit
a94a6d2a71
3 files changed
+97
-92
No files matched your search
@@ -1187,36 +1187,6 @@ 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");
|
||||
}
|
||||
|
||||
function renderOverviewSparkline(svgId, timeline) {
|
||||
const svg = document.getElementById(svgId);
|
||||
if (!svg) return;
|
||||
@@ -1274,65 +1244,6 @@ function renderOverviewSparkline(svgId, timeline) {
|
||||
svg.classList.add("hasData");
|
||||
}
|
||||
|
||||
async function apiGet(path) {
|
||||
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 缓存
|
||||
let _csrfToken = null;
|
||||
async function getCsrfToken() {
|
||||
if (_csrfToken) return _csrfToken;
|
||||
try {
|
||||
const res = await fetch("/api/csrf-token", { cache: "no-store" });
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
_csrfToken = data.token || "";
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("[CSRF] 获取 token 失败:", e);
|
||||
}
|
||||
return _csrfToken || "";
|
||||
}
|
||||
|
||||
async function apiPost(path, body) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshDevices() {
|
||||
const data = await apiGet("/api/devices");
|
||||
const sel = $("device");
|
||||
|
||||
+4
-3
@@ -1278,8 +1278,9 @@
|
||||
</button>
|
||||
|
||||
<!-- WebUI JS modules are loaded before the compatibility remainder. -->
|
||||
<script src="js/constants.js?v=console-20260531-split-2"></script>
|
||||
<script src="js/dom_utils.js?v=console-20260531-split-2"></script>
|
||||
<script src="app.js?v=console-20260531-split-2"></script>
|
||||
<script src="js/constants.js?v=console-20260531-split-3"></script>
|
||||
<script src="js/dom_utils.js?v=console-20260531-split-3"></script>
|
||||
<script src="js/api_client.js?v=console-20260531-split-3"></script>
|
||||
<script src="app.js?v=console-20260531-split-3"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,93 @@
|
||||
// API and connection helpers shared by WebUI classic scripts.
|
||||
// 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) {
|
||||
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 缓存
|
||||
let _csrfToken = null;
|
||||
|
||||
async function getCsrfToken() {
|
||||
if (_csrfToken) return _csrfToken;
|
||||
try {
|
||||
const res = await fetch("/api/csrf-token", { cache: "no-store" });
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
_csrfToken = data.token || "";
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("[CSRF] 获取 token 失败:", e);
|
||||
}
|
||||
return _csrfToken || "";
|
||||
}
|
||||
|
||||
async function apiPost(path, body) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user