/** * PerToolBox Front - 公共 JavaScript */ // ========== 全局变量 ========== let currentUser = null; // ========== 工具函数 ========== function getToken() { return localStorage.getItem('token'); } function setToken(token) { if (token) { localStorage.setItem('token', token); } else { localStorage.removeItem('token'); } } function isLoggedIn() { return !!getToken(); } // ========== API 请求封装 ========== async function apiRequest(endpoint, options = {}) { // 使用 window.API_BASE,如果未定义则使用默认值 const baseUrl = window.API_BASE || '/api/v1'; const url = endpoint.startsWith('http') ? endpoint : `${baseUrl}${endpoint}`; const headers = { 'Content-Type': 'application/json', ...options.headers }; const token = getToken(); if (token) { headers['Authorization'] = `Bearer ${token}`; } try { const response = await fetch(url, { ...options, headers }); if (response.status === 401) { setToken(null); if (!window.location.pathname.includes('/login.php')) { window.location.href = '/login.php'; } return null; } const data = await response.json(); if (!response.ok) { throw new Error(data.detail || data.message || '请求失败'); } return data; } catch (error) { console.error('API 请求错误:', error); throw error; } } // ========== 热度上报 ========== async function recordUsage(toolName) { try { await apiRequest(`/tool/usage?tool_name=${toolName}`, { method: 'POST' }); console.log(`✅ 热度上报: ${toolName}`); } catch (error) { console.warn('热度上报失败:', error); } } // ========== 获取用户信息 ========== async function loadUserInfo() { if (!isLoggedIn()) { updateUserUI(null); return null; } try { const user = await apiRequest('/user/profile'); currentUser = user; updateUserUI(user); return user; } catch (error) { console.error('获取用户信息失败:', error); setToken(null); updateUserUI(null); return null; } } // ========== 更新侧边栏 UI ========== function updateUserUI(user) { const userInfoDiv = document.getElementById('userInfo'); const profileLink = document.getElementById('profileLink'); const logoutBtn = document.getElementById('logoutBtn'); const loginLink = document.getElementById('loginLink'); if (user) { const displayName = user.username || user.phone || user.email || '用户'; if (userInfoDiv) { userInfoDiv.innerHTML = `