// 主题初始化 (必须在渲染前执行, 防止闪白) (function(){ var s = localStorage.getItem("sensu-theme") || "dark"; document.documentElement.setAttribute("data-theme", s); })(); window.toggleTheme = function(){ var t = document.documentElement.getAttribute("data-theme") === "light" ? "dark" : "light"; document.documentElement.setAttribute("data-theme", t); localStorage.setItem("sensu-theme", t); var btn = document.querySelector(".theme-toggle"); if(btn) btn.textContent = t === "light" ? "☀️" : "🌙"; }; // 初始化检查 window.onload = async () => { // 设置按钮初始图标 var btn = document.querySelector(".theme-toggle"); if(btn) btn.textContent = document.documentElement.getAttribute("data-theme") === "light" ? "☀️" : "🌙"; try { const res = await fetch('./api/auth/status', { credentials: 'include' }); if(res.status === 401) { window.location.href = './index.html'; return; } const data = await res.json(); if(!data.authenticated) { window.location.href = './index.html'; return; } document.getElementById('uname').textContent = data.username || 'Admin'; loadPage('dashboard'); // 默认加载 } catch(e) { window.location.href = './index.html'; } }; // 路由加载器 async function loadPage(pageName) { const content = document.getElementById('page-content'); const bar = document.getElementById('progress'); // 侧边栏高亮 document.querySelectorAll('.nav-item').forEach(el => el.classList.remove('active')); document.querySelector(`.nav-item[data-page="${pageName}"]`)?.classList.add('active'); // 进度条动画 bar.classList.add('active'); bar.style.width = '0%'; await new Promise(r => requestAnimationFrame(() => { bar.style.width = '80%'; setTimeout(r, 100); })); try { // 🟢 关键修改:fetch 路径必须包含 /static/ const html = await fetch(`./static/pages/${pageName}.html`).then(r => { if(!r.ok) throw new Error('404'); return r.text(); }); content.innerHTML = html; // 动态加载对应 JS 模块 // 🟢 关键修改:script src 路径必须包含 /static/ const script = document.createElement('script'); script.src = `./static/pages/${pageName}.js?t=${Date.now()}`; script.onload = () => { // 触发模块初始化 const moduleName = pageName.charAt(0).toUpperCase() + pageName.slice(1) + 'Module'; if(window[moduleName]?.init) { window[moduleName].init(); } bar.style.width = '100%'; setTimeout(() => bar.classList.remove('active'), 200); }; script.onerror = () => { throw new Error('JS Load Failed'); }; document.head.appendChild(script); } catch(e) { content.innerHTML = `