/** * 后端 API 集成模块 * 用于与后端服务通信 */ export class ApiClient { constructor(baseUrl = '/api') { this.baseUrl = baseUrl; } /** * 通用 GET 请求 */ async get(endpoint) { try { const response = await fetch(`${this.baseUrl}${endpoint}`, { method: 'GET', headers: { 'Content-Type': 'application/json' } }); return await this.handleResponse(response); } catch (error) { console.error('GET 请求失败:', error); throw error; } } /** * 通用 POST 请求 */ async post(endpoint, data) { try { const response = await fetch(`${this.baseUrl}${endpoint}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); return await this.handleResponse(response); } catch (error) { console.error('POST 请求失败:', error); throw error; } } /** * 处理响应 */ async handleResponse(response) { if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return await response.json(); } /** * 使用授权码获取用户信息 * @param {string} code - 企业微信授权码 */ async getUserInfoByCode(code) { return this.post('/wecom/getUserInfo', { code }); } /** * 获取企业微信用户详情 * @param {string} userId - 用户ID */ async getWeChatUserDetail(userId) { return this.get(`/wecom/userDetail/${userId}`); } /** * 验证用户是否具有权限 * @param {string} userId - 用户ID */ async verifyUserPermission(userId) { return this.get(`/wecom/verify/${userId}`); } } export default ApiClient;