// API配置 const API_CONFIG = { // 开发环境API地址 dev: { baseUrl: 'http://localhost:8080' }, // 生产环境API地址 prod: { baseUrl: 'http://43.143.204.137:8080' } }; // 获取当前环境 const isProduction = process.env.NODE_ENV === 'production'; // 导出当前环境的API基础URL export const BASE_URL = isProduction ? API_CONFIG.prod.baseUrl : API_CONFIG.dev.baseUrl; /** * 发送HTTP请求的通用方法 * @param {Object} options 请求选项 * @returns {Promise} Promise对象 */ export const request = (options) => { return new Promise((resolve, reject) => { const token = uni.getStorageSync('token') || ''; console.log(`发起请求: ${options.method || 'GET'} ${options.url}`, options.data); // 显示加载提示 if (options.showLoading !== false) { uni.showLoading({ title: options.loadingText || '加载中...', mask: true }); } // 请求开始时间 const startTime = Date.now(); const requestTask = uni.request({ url: BASE_URL + options.url, method: options.method || 'GET', data: options.data || {}, header: { 'Content-Type': 'application/json', 'Authorization': token ? `Bearer ${token}` : '', ...options.header }, timeout: options.timeout || 30000, // 请求超时时间,默认30秒 success: (res) => { // 请求结束时间和耗时 const endTime = Date.now(); const duration = endTime - startTime; console.log(`请求响应: ${options.url}, 耗时: ${duration}ms`, res); // 二维码接口特殊处理,即使状态码不是200,只要有数据就算成功 if (options.url.indexOf('/api/qrcode/generate') !== -1) { // 检查是否有二维码数据 if (res.data && res.data.qrCodeUrl) { console.log(`二维码接口请求成功: ${options.url}`, res.data); resolve(res.data); return; } } // 统一处理响应 if (res.statusCode === 200) { // 修改判断逻辑,适应后端返回格式 if (res.data && (res.data.code === 0 || res.data.code === 200 || res.data.success === true)) { console.log(`请求成功: ${options.url}`, res.data); resolve(res.data); } else { // 业务错误 console.error(`业务错误: ${options.url}`, res.data); uni.showToast({ icon: 'none', title: res.data.message || res.data.msg || '请求失败' }); reject(res.data); } } else if (res.statusCode === 401) { // 未授权,需要登录 console.error(`未授权: ${options.url}`, res); uni.showToast({ icon: 'none', title: '请先登录' }); // 可以在这里处理登录逻辑 reject(res); } else { // 其他HTTP错误 console.error(`HTTP错误: ${options.url} (${res.statusCode})`, res); uni.showToast({ icon: 'none', title: `请求失败(${res.statusCode})` }); reject(res); } }, fail: (err) => { console.error(`请求失败: ${options.url}`, err); uni.showToast({ icon: 'none', title: err.errMsg?.includes('timeout') ? '网络请求超时' : '网络请求失败' }); reject(err); }, complete: () => { // 关闭loading if (options.showLoading !== false) { uni.hideLoading(); } // 可能的重试逻辑 if (options.retry && options.retryCount > 0 && options.retryCondition) { // 省略重试逻辑实现... } } }); // 记录requestTask,方便后续可能的取消操作 if (options.requestTaskCallback) { options.requestTaskCallback(requestTask); } }); }; // 导出API接口 export const API = { // 行程相关接口 trip: { create: (data) => request({ url: '/api/trip/create', method: 'POST', data }), update: (data) => request({ url: '/api/trip/update', method: 'POST', data }), delete: (id) => request({ url: `/api/trip/delete/${id}`, method: 'POST' }), detail: (id) => request({ url: `/api/trip/detail/${id}`, method: 'GET' }), list: () => request({ url: '/api/trip/list', method: 'GET' }), sync: (data) => request({ url: '/api/trip/sync', method: 'POST', data }), generateQrCode: (data) => request({ url: '/api/qrcode/generate', method: 'POST', data }) } };