"use strict"; /* ========================================================================== xiaobai-datahub 管理后台 · 启动壳 会话/登录/改密/登出/主题按钮/退出确认 —— 与具体布局无关,三页共用同一份。 ========================================================================== */ (function () { const C = window.Core; const { $ } = C; function show(id) { ["login-view", "change-view", "shell"].forEach((key) => { $(key).hidden = key !== id; }); } async function boot() { try { const session = await C.api("/admin/api/session"); C.state.csrf = session.csrf; $("who").textContent = session.username; if (session.must_change) { show("change-view"); return; } show("shell"); enterShell(); } catch { show("login-view"); } } $("login-form").addEventListener("submit", async (event) => { event.preventDefault(); const form = new FormData(event.target); $("login-error").hidden = true; try { const result = await C.api("/admin/api/login", { method: "POST", body: JSON.stringify({ username: form.get("username"), password: form.get("password") }), }); C.state.csrf = result.csrf; if (result.must_change) show("change-view"); else { show("shell"); enterShell(); } } catch (err) { $("login-error").hidden = false; $("login-error").textContent = err.message; } }); $("change-form").addEventListener("submit", async (event) => { event.preventDefault(); const form = new FormData(event.target); try { await C.api("/admin/api/change-password", { method: "POST", body: JSON.stringify({ current: form.get("current"), new_password: form.get("new_password") }), }); show("shell"); enterShell(); } catch (err) { $("change-error").hidden = false; $("change-error").textContent = err.message; } }); $("logout-btn").addEventListener("click", async () => { await C.api("/admin/api/logout", { method: "POST", body: "{}" }); C.Poller.stopAll(); Object.values(window.HUB_LAYOUTS || {}).forEach((impl) => { try { impl.unmount && impl.unmount(); } catch { /* ignore */ } }); show("login-view"); }); $("theme-btn").addEventListener("click", () => C.toggleTheme()); C.Bus.on("theme", (theme) => { $("theme-btn").textContent = theme === "night" ? "日间" : "夜间"; }); function updateCrumb() { const el = $("crumb"); if (!el) return; if (!C.state.online) { el.textContent = "网络已断开 · 已暂停实时"; return; } if (!C.state.visible) { el.textContent = "已切至后台 · 已暂停动效"; return; } const d = C.state.data.overview; el.textContent = d ? `8766 · ${d.trade_date} · 持续运转` : "8766 · 四源汇流 · 持续运转"; } C.Bus.on("runtime", updateCrumb); C.Bus.on("data", updateCrumb); function enterShell() { C.bootTheme(); C.applyReduced(C.REDUCE_MQ.matches); C.Poller.startAll(); updateCrumb(); C.Router.boot(); } boot(); })();