// API请求工具类 class API { constructor() { this.baseURL = API_CONFIG.BASE_URL; this.timeout = API_CONFIG.TIMEOUT; } // 获取请求头 getHeaders() { const headers = { 'Content-Type': 'application/json' }; const token = localStorage.getItem(STORAGE_KEYS.TOKEN); if (token) { headers['Authorization'] = `Bearer ${token}`; } return headers; } // 通用请求方法 async request(url, options = {}) { const config = { method: options.method || 'GET', headers: this.getHeaders(), ...options }; if (options.body && typeof options.body === 'object') { config.body = JSON.stringify(options.body); } try { const response = await fetch(this.baseURL + url, config); const data = await response.json(); if (data.code === 401) { this.handleUnauthorized(); throw new Error('未授权,请重新登录'); } return data; } catch (error) { console.error('API请求错误:', error); throw error; } } // GET请求 async get(url) { return this.request(url, { method: 'GET' }); } // POST请求 async post(url, body) { return this.request(url, { method: 'POST', body }); } // PUT请求 async put(url, body) { return this.request(url, { method: 'PUT', body }); } // DELETE请求 async delete(url) { return this.request(url, { method: 'DELETE' }); } // 处理未授权情况 handleUnauthorized() { localStorage.removeItem(STORAGE_KEYS.TOKEN); localStorage.removeItem(STORAGE_KEYS.USER_INFO); window.location.href = 'login.html'; } } // 创建API实例 const api = new API(); // 工具函数 const utils = { // 显示提示消息 showMessage(message, type = 'info') { alert(message); }, // 显示成功消息 showSuccess(message) { this.showMessage(message, 'success'); }, // 显示错误消息 showError(message) { this.showMessage(message, 'error'); }, // 确认对话框 confirm(message) { return window.confirm(message); }, // 格式化日期 formatDate(dateString) { if (!dateString) return '-'; const date = new Date(dateString); return date.toLocaleDateString('zh-CN'); }, // 格式化日期时间 formatDateTime(dateString) { if (!dateString) return '-'; const date = new Date(dateString); return date.toLocaleString('zh-CN'); }, // 获取当前用户信息 getCurrentUser() { const userStr = localStorage.getItem(STORAGE_KEYS.USER_INFO); return userStr ? JSON.parse(userStr) : null; }, // 检查用户角色 hasRole(role) { const user = this.getCurrentUser(); return user && user.role === role; }, // 退出登录 logout() { if (this.confirm('确定要退出登录吗?')) { localStorage.removeItem(STORAGE_KEYS.TOKEN); localStorage.removeItem(STORAGE_KEYS.USER_INFO); window.location.href = 'login.html'; } }, // 检查登录状态 checkAuth() { const token = localStorage.getItem(STORAGE_KEYS.TOKEN); if (!token) { window.location.href = 'login.html'; return false; } return true; }, // 获取状态标签HTML getStatusBadge(status, type) { const badges = { // 工单状态 pending: '待处理', appointed: '已预约', in_progress: '进行中', completed: '已完成', cancelled: '已取消', // 支付状态 unpaid: '未支付', paid: '已支付', refunded: '已退款', // 预约状态 confirmed: '已确认', // 车辆状态 normal: '正常', in_service: '维修中' }; return badges[status] || `${status}`; }, // 获取服务类型文本 getServiceTypeText(type) { const types = { maintenance: '保养维护', repair: '维修服务', beauty: '美容服务', insurance: '保险代理' }; return types[type] || type; }, // 获取用户角色文本 getRoleText(role) { const roles = { admin: '管理员', staff: '工作人员', customer: '客户' }; return roles[role] || role; } };