// 配置项 import Notify from '../miniprogram_npm/@vant/weapp/notify/notify'; const token = wx.getStorageSync('token') const config = { // baseURL: 'http://192.168.1.6:8802', // 基础URL baseURL: 'https://www.xsip.cn', // 基础URL timeout: 300000, // 超时时间(单位ms) header: { 'content-type': 'application/json', // 默认请求头 // 其他全局请求头... }, }; const apiUrl = { develop: 'https://www.xsip.cn', // 开发环境地址 trial: 'https://www.xsip.cn', // 测试环境地址 release: 'https://www.xsip.cn' // 生产环境地址 }; const accountInfo = wx.getAccountInfoSync(); const envVersion = accountInfo.miniProgram.envVersion; config.baseURL = apiUrl[envVersion]; function getPages(){ var obj = { type: 'danger' } if(wx.getStorageSync('isComponent')){ obj.top = wx.getStorageSync('height') } return obj } // 封装请求方法 function upload(url="/fileManager/uploadNormalFile", path = '', header = {},formData={}) { return new Promise((resolve, reject) => { wx.uploadFile({ url: config.baseURL + url, //仅为示例,非真实的接口地址 filePath: path, name: 'files', formData: { sourceId:5, ...formData }, header: { ...config.header, ...header, "Authorization": wx.getStorageSync('token') }, timeout: config.timeout, success: function(res){ wx.hideLoading() var obj = getPages() if(res.data){ res.data = JSON.parse(res.data) } // 根据业务逻辑处理成功响应 if (res.data && res.data.code === 200) { // 假设服务器返回code为200表示成功 resolve(res.data); } else { switch(res.data.code){ case 401: if(isLogin){ // Notify({ type: 'danger', message: '未登录',top:'90' }); obj.message = '未登录' wx.navigateTo({ url: '/pages/login/login', }) } wx.setStorageSync('token', '') break; default: // obj.message = '请求失败' obj.message = res.data.message // Notify({ type: 'danger', message: '请求失败',top:'90' }); } // wx.showToast({ // title: '请求失败', // icon: 'error', // duration: 2000 // }) console.log(obj) if(obj.message){ Notify(obj) } reject(res.data.message || '未知错误'); } }, fail: function(error) { console.log(error) wx.hideLoading() var obj = getPages() // 处理请求失败 // wx.showToast({ // title: '请求失败', // icon: 'error', // duration: 2000 // }) obj.message = '请求超时' // Notify({ type: 'danger', message: '请求失败',top:'90' }); Notify(obj) reject(error); }, }); }); } // 导出请求方法 module.exports = { upload: ( path,formData,url, header) => upload(url, path, header,formData), };