| | |
| | | // 引入状态码statusCode |
| | | const statusCode = require('./statusCode') |
| | | // 定义请求路径, BASEURL: 普通请求API; CBASEURL: 中台API,不使用中台可不引入CBASEURL |
| | | const { |
| | | BASEURL |
| | | } = require('./config') |
| | | const config = require('./config') |
| | | // 定义默认参数 |
| | | const defaultOptions = { |
| | | data: {}, |
| | |
| | | data, |
| | | ignoreToken, |
| | | form, |
| | | isShowLoding |
| | | isShowLoding, |
| | | timeout, |
| | | header |
| | | } = _options |
| | | const app = getApp() |
| | | // 设置请求头 |
| | | let header = {} |
| | | if (form) { |
| | | header = { |
| | | 'content-type': 'application/x-www-form-urlencoded' |
| | |
| | | 'content-type': 'application/json' //自定义请求头信息 |
| | | } |
| | | } |
| | | if (!timeout) { |
| | | timeout = 60000 |
| | | } |
| | | |
| | | if (!ignoreToken) { |
| | | // 从全局变量中获取token |
| | | let token = app.globalData.token |
| | | header.Authorization = `Bearer ${token}` |
| | | } |
| | | header.tag = app.globalData.tag; |
| | | return new Promise((resolve, reject) => { |
| | | console.log("url:" + BASEURL + url); |
| | | // 获取最新的 BASEURL |
| | | let currentBaseUrl = app.globalData.baseUrl || config.BASEURL; |
| | | |
| | | console.log("url:" + currentBaseUrl + url); |
| | | if (isShowLoding) { |
| | | wx.showLoading({ |
| | | title: '通信中...', // 加载动画标题 |
| | |
| | | if (url.startsWith('http')) { |
| | | myUrl = url; |
| | | } else { |
| | | myUrl = BASEURL + url; |
| | | myUrl = currentBaseUrl + url; |
| | | } |
| | | wx.request({ |
| | | url: myUrl, |
| | | data, |
| | | header, |
| | | method, |
| | | timeout: timeout, |
| | | success: (res) => { |
| | | let { |
| | | statusCode: code |
| | |
| | | if (code === statusCode.SUCCESS) { |
| | | if (res.data.code !== "0001") { |
| | | // 统一处理请求错误 |
| | | showToast(res.data.errorMsg) |
| | | // showToast(res.data.errorMsg) |
| | | reject(res.data) |
| | | return |
| | | } |
| | |
| | | if (isShowLoding) { |
| | | wx.hideLoading(); // 隐藏加载动画 |
| | | } |
| | | |
| | | showToast(err.errMsg) |
| | | // showToast(err.errMsg) |
| | | reject(err) |
| | | } |
| | | }) |