|  |  | 
 |  |  | // 引入状态码statusCode | 
 |  |  | const statusCode = require('./statusCode') | 
 |  |  | // 定义请求路径, BASEURL: 普通请求API; CBASEURL: 中台API,不使用中台可不引入CBASEURL | 
 |  |  | const { BASEURL } = require('./config') | 
 |  |  | const { | 
 |  |  |   BASEURL | 
 |  |  | } = require('./config') | 
 |  |  | // 定义默认参数 | 
 |  |  | const defaultOptions = { | 
 |  |  |   data: {}, | 
 |  |  | 
 |  |  |  * ignoreToken: <Boolean> 是否忽略token验证 | 
 |  |  |  * form: <Boolean> 是否使用formData请求 | 
 |  |  |  */ | 
 |  |  | function request (options) { | 
 |  |  |   let _options = Object.assign(defaultOptions, options) | 
 |  |  |   let { method, url, data, ignoreToken, form } = _options | 
 |  |  | function request(options) { | 
 |  |  |   // let _options = Object.assign(defaultOptions, options) | 
 |  |  |   let _options = { | 
 |  |  |     ...defaultOptions, | 
 |  |  |     ...options | 
 |  |  |   } | 
 |  |  |   let { | 
 |  |  |     method, | 
 |  |  |     url, | 
 |  |  |     data, | 
 |  |  |     ignoreToken, | 
 |  |  |     form, | 
 |  |  |     isShowLoding | 
 |  |  |   } = _options | 
 |  |  |   const app = getApp() | 
 |  |  |   // 设置请求头 | 
 |  |  |   let header = {} | 
 |  |  | 
 |  |  |     header.Authorization = `Bearer ${token}` | 
 |  |  |   } | 
 |  |  |   return new Promise((resolve, reject) => { | 
 |  |  |     console.log("url:" + BASEURL + url); | 
 |  |  |     if (isShowLoding) { | 
 |  |  |       wx.showLoading({ | 
 |  |  |         title: '通信中...', // 加载动画标题 | 
 |  |  |         mask: true, // 是否显示透明蒙层,防止触摸穿透 | 
 |  |  |       }); | 
 |  |  |     } | 
 |  |  |     let myUrl; | 
 |  |  |     if (url.startsWith('http')) { | 
 |  |  |       myUrl = url; | 
 |  |  |     } else { | 
 |  |  |       myUrl = BASEURL + url; | 
 |  |  |     } | 
 |  |  |     wx.request({ | 
 |  |  |       url: BASEURL + url, | 
 |  |  |       url: myUrl, | 
 |  |  |       data, | 
 |  |  |       header, | 
 |  |  |       method, | 
 |  |  |       success: (res) => { | 
 |  |  |         let { statusCode: code } = res | 
 |  |  |         let { | 
 |  |  |           statusCode: code | 
 |  |  |         } = res | 
 |  |  |         console.log("success  statusCode:" + code); | 
 |  |  |         if (isShowLoding) { | 
 |  |  |           wx.hideLoading(); // 隐藏加载动画 | 
 |  |  |         } | 
 |  |  |         if (code === statusCode.SUCCESS) { | 
 |  |  |           if (res.data.code !== 0) { | 
 |  |  |           if (res.data.code !== "0001") { | 
 |  |  |             // 统一处理请求错误 | 
 |  |  |             showToast(res.data.errorMsg) | 
 |  |  |             reject(res.data) | 
 |  |  | 
 |  |  |           showToast(`登录过期, 请重新刷新页面`) | 
 |  |  |           reject(res.data) | 
 |  |  |         } else { | 
 |  |  |           showToast(`请求错误${url}, CODE: ${code}`) | 
 |  |  |           // showToast(`请求错误${url}, CODE: ${code}`) | 
 |  |  |           console.log("success  请求错误:" + code); | 
 |  |  |           reject(res.data) | 
 |  |  |         } | 
 |  |  |         console.log("success  statusCode:1111111111"); | 
 |  |  |       }, | 
 |  |  |       fail: (err) => { | 
 |  |  |         console.log('%c err', 'color: red;font-weight: bold', err) | 
 |  |  |         console.log("Error  " + err); | 
 |  |  |         if (isShowLoding) { | 
 |  |  |           wx.hideLoading(); // 隐藏加载动画 | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         showToast(err.errMsg) | 
 |  |  |         reject(err) | 
 |  |  |       } | 
 |  |  | 
 |  |  | } | 
 |  |  |  | 
 |  |  | // 封装toast函数 | 
 |  |  | function showToast (title, icon='none', duration=2500, mask=false) { | 
 |  |  | function showToast(title, icon = 'none', duration = 2500, mask = false) { | 
 |  |  |   wx.showToast({ | 
 |  |  |     title: title || '', | 
 |  |  |     icon, | 
 |  |  | 
 |  |  |   }); | 
 |  |  | } | 
 |  |  |  | 
 |  |  | function get (options) { | 
 |  |  | function get(options) { | 
 |  |  |   return request({ | 
 |  |  |     method: 'GET', | 
 |  |  |     ...options | 
 |  |  |   }) | 
 |  |  | } | 
 |  |  |  | 
 |  |  | function post (options) { | 
 |  |  | function post(options) { | 
 |  |  |   // url, data = {}, ignoreToken, form | 
 |  |  |   return request({ | 
 |  |  |     method: 'POST', | 
 |  |  | 
 |  |  | } | 
 |  |  |  | 
 |  |  | module.exports = { | 
 |  |  |   request, get, post | 
 |  |  | } | 
 |  |  |   request, | 
 |  |  |   get, | 
 |  |  |   post | 
 |  |  | } |