// DOM 加载完成后执行,启动入口:初始化菜单、默认页面加载 document.addEventListener("DOMContentLoaded", () => { const items = document.querySelectorAll(".menu-item"); function setActive(name) { items.forEach(i => { i.classList.toggle("active", i.dataset.page === name); }); } function bindMenu() { items.forEach(item => { item.addEventListener("click", () => { const page = item.dataset.page; setActive(page); loadPage(page); }); }); } bindMenu(); setActive("page1"); loadPage("page1"); }); function logout() { window.location.href = "/api/auth/logout"; } function setIMEI(imei) { const el = document.getElementById("imei-value"); if (el) { el.textContent = imei; } } async function fetchIMEI() { try { const res = await fetch("/api/device/imei"); if (!res.ok) { throw new Error("http error"); } const imei = await res.text(); if (!imei || imei.trim() === "") { throw new Error("empty imei"); } setIMEI(imei.trim()); return; } catch (e) { console.log("get imei failed, retrying..."); setTimeout(fetchIMEI, 3000); // 3秒后继续重试 } } function setText(id, value) { const el = document.getElementById(id); if (el) { el.textContent = value ?? "--"; } } let sysTimeTimer = null; function startSysTime() { if (sysTimeTimer !== null) { return; } function update() { const el = document.getElementById("sys-time"); if (!el) { return; } const now = new Date(); const year = now.getFullYear(); const month = String(now.getMonth() + 1).padStart(2, "0"); const day = String(now.getDate()).padStart(2, "0"); const hour = String(now.getHours()).padStart(2, "0"); const min = String(now.getMinutes()).padStart(2, "0"); const sec = String(now.getSeconds()).padStart(2, "0"); el.textContent = `${year}-${month}-${day} ${hour}:${min}:${sec}`; } update(); sysTimeTimer = setInterval(update, 1000); } document.addEventListener("DOMContentLoaded", () => { startSysTime(); }); function reboot() { if (!confirm("确定重启设备吗?")) return; fetch("/api/system/reboot", { method: "POST" }) .then(r => r.json()) .then(d => { if (d.ok) { alert("设备正在重启..."); setTimeout(logout, 1000); } else { alert("重启失败: " + d.error); } }) .catch(e => alert("请求失败: " + e)); } function loadIMEI() { fetchIMEI(); } loadIMEI();