// request.js import Notify from '../miniprogram_npm/@vant/weapp/notify/notify'; // 配置项 const config = { // baseURL: 'http://192.168.1.18:8901', // 基础URL baseURL: 'https://www.xsip.cn', // 基础URL timeout: 300000, // 超时时间(单位ms) header: { 'content-type': 'application/json', // 默认请求头 // 其他全局请求头... }, }; const apiUrl = { develop: 'https://www.xsip.cn/test', // 开发环境地址 trial: 'https://www.xsip.cn/test', // 测试环境地址 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 request(url, method = 'GET', data = {}, header = {},isLogin=true) { return new Promise((resolve, reject) => { var str = '' if(method === 'GET'){ str ='?' + Object.keys(data).map(key => `${key}=${encodeURIComponent(data[key])}`).join('&') } wx.request({ url: config.baseURL + url + str, method: method.toUpperCase(), data: method === 'GET' ? {} : data, // GET请求时将data置为空对象 header: { ...config.header, ...header, "Authorization": wx.getStorageSync('token') // "Cookie": wx.getStorageSync('token') }, timeout: config.timeout, success: function(res){ var obj = getPages() wx.hideLoading() // 根据业务逻辑处理成功响应 if (res.data && res.data.code === 200) { // 假设服务器返回code为200表示成功 resolve(res.data); } else if(res.data && !res.data.code){ resolve(res.data); } else { switch(res.data.code){ case 901: resolve(res.data); break; case 401: if(isLogin){ // Notify({ type: 'danger', message: '未登录',top:'90' }); obj.message = '未登录' wx.navigateTo({ url: '/pages/login/login', }) } wx.setStorageSync('token', '') break; case 606://未开通会员或会员过期 wx.showModal({ title: '', content: '前往会员中心开通会员', complete: (res) => { if (res.cancel) {} if (res.confirm) { wx.navigateTo({ url: '/pages/memberCenter/menberCenter2', }) } } }) break; // case 607://已开通会员且超过额度 // 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 || '未知错误'); } // if($stopWuxRefresher()){ // $stopWuxRefresher() // } }, 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); // if($stopWuxRefresher()){ // $stopWuxRefresher() // } }, }); }); } function upload(url="/fileManager/uploadNormalFile", path = '', header = {},formData={}) { return new Promise((resolve, reject) => { wx.uploadFile({ url: config.baseURL + url, //仅为示例,非真实的接口地址 filePath: path, name: 'multipartFile', formData: 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 || '未知错误'); } // if($stopWuxRefresher()){ // $stopWuxRefresher() // } }, 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 = { get: (url, data,isLogin, header) => request(url, 'GET', data, header,isLogin), post: (url, data,isLogin, header) => request(url, 'POST', data, header,isLogin), // 可以继续添加其他方法,如put, delete等 // ... upload: ( path,formData,url, header) => upload(url, path, header,formData), };