/** * API 配置模块 * 提供统一的 API 基础配置和常量定义 */ // API 基础配置 window.API_CONFIG = { // 后端 API 基础 URL baseURL: 'http://124.70.146.192:8000', // API 版本 version: '/api', // 超时时间 (毫秒) timeout: 30000, // 认证相关 auth: { tokenKey: 'smart_center_token', refreshTokenKey: 'smart_center_refresh_token', tokenExpiryKey: 'smart_center_token_expiry' }, // 常用 API 端点 endpoints: { // 认证相关 login: '/auth/login', register: '/auth/register', logout: '/auth/logout', refreshToken: '/auth/refresh', // 聊天相关 chat: '/chat/send', chatMultimodal: '/chat/multimodal', // 模块相关 moduleList: '/modules/list', moduleDetail: '/modules/detail', // 用户相关 userInfo: '/user/info', userProfile: '/user/profile' }, // 请求头配置 headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' } }; // 工具函数:获取完整 API URL window.getApiUrl = function(endpoint) { return `${window.API_CONFIG.baseURL}${window.API_CONFIG.version}${endpoint}`; }; // 工具函数:获取认证 Token window.getToken = function() { return localStorage.getItem(window.API_CONFIG.auth.tokenKey); }; // 工具函数:设置认证 Token window.setToken = function(token, expiresIn = 7200) { localStorage.setItem(window.API_CONFIG.auth.tokenKey, token); const expiry = new Date().getTime() + (expiresIn * 1000); localStorage.setItem(window.API_CONFIG.auth.tokenExpiryKey, expiry.toString()); }; // 工具函数:检查 Token 是否有效 window.isTokenValid = function() { const token = window.getToken(); if (!token) return false; const expiry = localStorage.getItem(window.API_CONFIG.auth.tokenExpiryKey); if (!expiry) return false; return new Date().getTime() < parseInt(expiry); }; // 工具函数:清除 Token window.clearToken = function() { localStorage.removeItem(window.API_CONFIG.auth.tokenKey); localStorage.removeItem(window.API_CONFIG.auth.refreshTokenKey); localStorage.removeItem(window.API_CONFIG.auth.tokenExpiryKey); }; console.log('✅ API_CONFIG 已加载');