const API_BASE_URL = '/api'; class API { constructor() { this.baseUrl = API_BASE_URL; this.token = localStorage.getItem('access_token') || this.getCookie('access_token'); } getCookie(name) { const matches = document.cookie.match(new RegExp('(?:^|; )' + name + '=([^; ]*)')); return matches ? decodeURIComponent(matches[1]) : null; } setCookie(name, value, days = 30) { const expires = new Date(Date.now() + days * 864e5).toUTCString(); document.cookie = `${name}=${encodeURIComponent(value)}; expires=${expires}; path=/;SameSite=Lax`; } deleteCookie(name) { document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`; } setToken(token, remember = false) { this.token = token; if (token) { if (remember) { this.setCookie('access_token', token, 30); localStorage.setItem('access_token', token); } else { localStorage.setItem('access_token', token); this.deleteCookie('access_token'); } } else { localStorage.removeItem('access_token'); this.deleteCookie('access_token'); } } async request(endpoint, options = {}) { const url = `${this.baseUrl}${endpoint}`; const headers = { 'Content-Type': 'application/json', ...options.headers }; if (this.token) { headers['Authorization'] = `Bearer ${this.token}`; } try { const response = await fetch(url, { ...options, headers }); const data = await response.json(); if (!response.ok) { throw new Error(data.message || '请求失败'); } return data; } catch (error) { console.error('API Error:', error); throw error; } } get(endpoint, options = {}) { return this.request(endpoint, { ...options, method: 'GET' }); } post(endpoint, data, options = {}) { return this.request(endpoint, { ...options, method: 'POST', body: JSON.stringify(data) }); } put(endpoint, data, options = {}) { return this.request(endpoint, { ...options, method: 'PUT', body: JSON.stringify(data) }); } delete(endpoint, options = {}) { return this.request(endpoint, { ...options, method: 'DELETE' }); } async checkAuth() { if (!this.token) { this.token = localStorage.getItem('access_token') || this.getCookie('access_token'); } if (!this.token) return null; try { const res = await this.get('/auth/me'); return res.data; } catch { this.setToken(null, false); return null; } } getCookie(name) { const matches = document.cookie.match(new RegExp('(?:^|; )' + name + '=([^; ]*)')); return matches ? decodeURIComponent(matches[1]) : null; } async login(username, password) { const res = await this.post('/auth/login', { username, password }); if (res.data && res.data.access_token) { this.setToken(res.data.access_token); } return res.data; return res.data; } async phoneLogin(phone, code) { const res = await this.post('/auth/login/sms', { phone, code }); if (res.data && res.data.access_token) { this.setToken(res.data.access_token); } return res.data; } async register(username, password, phone) { return this.post('/auth/register', { username, password, phone }); } async sendSms(phone, type = 'login') { return this.post('/auth/sms/send', { phone, type }); } async logout() { this.setToken(null); } async getCaptcha() { return this.get('/auth/captcha'); } async sendChat(message, sessionId = null, model = null) { return this.post('/chat/send', { message, session_id: sessionId, model }); } async getWechatQr() { return this.get('/auth/wechat/qr'); } async wechatLogin(token) { this.setToken(token); } async resetPassword(phone, code, newPassword) { return this.post('/auth/reset-password', { phone, code, password: newPassword }); } async updateProfile(data) { return this.put('/user/profile', data); } async changePassword(oldPassword, newPassword) { return this.post('/auth/change-password', { old_password: oldPassword, new_password: newPassword }); } async updatePermissions(permissions) { return this.put('/user/permissions', { permissions }); } async getChatHistory(sessionId) { return this.get(`/chat/history/${sessionId}`); } async createSession() { return this.post('/chat/session'); } async getTopics() { return this.get('/chat/topics'); } async listTasks(params = {}) { const query = new URLSearchParams(params).toString(); return this.get(`/tasks/${query ? '?' + query : ''}`); } async getTask(taskId) { return this.get(`/tasks/${taskId}`); } async getTaskStatus(taskId) { return this.get(`/tasks/${taskId}/status`); } async executeWorkflow(workflowName, params) { return this.post('/workflows/execute', { workflow_name: workflowName, params }); } async getWorkflowStatus(promptId) { return this.get(`/workflows/task/${promptId}`); } async listSkills(category = null) { const query = category ? `?category=${category}` : ''; return this.get(`/skills/${query}`); } async installSkill(source) { return this.post('/skills/install', { source }); } async uninstallSkill(name) { return this.delete(`/skills/uninstall/${name}`); } async listMcpServers() { return this.get('/mcp/servers'); } async callMcpTool(serverName, toolName, toolArgs) { return this.post('/mcp/call', { server_name: serverName, tool_name: toolName, arguments: toolArgs }); } } const api = new API();