// src/utils/http.js import config from './config.js'; const http = (options = {}) => { console.log(options) // 请求拦截器逻辑 const requestConfig = { url: config.BASE_URL + options.url, // 拼接完整请求路径 :cite[1]:cite[2]:cite[7] method: options.method || 'GET', data: options.params || options.data || {}, header: { // 注意:key 由 axios 的 `headers` 改为 uni.request 的 `header` :cite[1] 'Content-Type': 'application/json', // 明确设置 Content-Type,因为 uni.request 默认可能使用 'application/x-www-form-urlencoded' :cite[1] ...options.header, // 合并自定义请求头 }, timeout: options.timeout || 5000, sslVerify: false }; // 在发送请求前可以执行的操作,例如添加 token :cite[1]:cite[7]:cite[8] // 示例:添加 Token const token = uni.getStorageSync('token'); if (token) { requestConfig.header['Authorization'] = `${token}`; // 或根据你的后端要求调整 } return new Promise((resolve, reject) => { uni.request({ ...requestConfig, success: (response) => { // 响应拦截器逻辑:对响应数据做点什么 // 这里根据你的业务需求调整,例如直接返回 data :cite[1]:cite[8] resolve(response.data); // 注意:uni.request 的响应结构 response.data 才是服务端返回的数据体 :cite[1] }, fail: (error) => { // 响应错误处理 reject(error); } }); }); }; // (可选)为方便使用,可以为不同的 HTTP 方法创建快捷方法 :cite[7] ['get', 'post', 'put', 'delete'].forEach((method) => { http[method] = (url, data, options = {}) => { return http({ url, method: method.toUpperCase(), data, ...options }); }; }); export default http;