const BASE = '/panel'; export const api = { get: async (url) => { const res = await fetch(`${BASE}${url}`, { credentials: 'include' }); return res.json(); }, post: async (url, data) => { const res = await fetch(`${BASE}${url}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(data) }); return res.json(); } }; export async function initAuth() { // 登录逻辑绑定到 DOM const loginForm = document.querySelector('#login-form'); if(loginForm) { loginForm.addEventListener('submit', async (e) => { e.preventDefault(); const u = document.getElementById('username').value; const p = document.getElementById('password').value; const res = await api.post('/api/login', { username: u, password: p }); if (res.success) location.reload(); // 登录成功刷新 }); } return await api.get('/api/auth/status'); }