import { gatewayHttpClient } from '@jdmini/api' // API路径 const API_PATH = '/mp/jd-dapaijizhang' // 请求封装 class Request { // 通用请求方法 // 网关请求。参数:路径、方法、数据、其他选项(如headers、responseType) async request(url, method = 'GET', data = {}, options = {}) { try { // 构建完整路径 const fullPath = `${API_PATH}${url}` // 合并选项 const requestOptions = { headers: { 'Content-Type': 'application/json', ...options.headers }, ...options } // 使用网关请求,参数:路径、方法、数据、其他选项 const res = await gatewayHttpClient.request(fullPath, method, data, requestOptions) // 处理响应 if (res && res.code === 200) { // 成功,返回data字段 return res.data } else if (res && res.code === 401) { // 未授权,跳转登录 wx.showToast({ title: res.message || '请重新登录', icon: 'none' }) setTimeout(() => { wx.navigateTo({ url: '/pages/login/login' }) }, 1500) throw res } else { // 其他错误,不自动显示toast,让调用者处理 throw res || { code: 500, message: '请求失败' } } } catch (error) { console.error('请求错误:', error) throw error } } // GET请求 get(url, params = {}, options = {}) { // 将params转换为查询字符串 const queryString = Object.keys(params) .map(key => `${key}=${encodeURIComponent(params[key])}`) .join('&') const finalUrl = queryString ? `${url}?${queryString}` : url return this.request(finalUrl, 'GET', {}, options) } // POST请求 post(url, data = {}, options = {}) { return this.request(url, 'POST', data, options) } // PUT请求 put(url, data = {}, options = {}) { return this.request(url, 'PUT', data, options) } // DELETE请求 delete(url, options = {}) { return this.request(url, 'DELETE', {}, options) } } // 导出实例 export default new Request()