|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import baseurl from '../environments.js'
- import {isWithoutToken} from './whiteList.js'
- export const request = function(data) {
- return new Promise((resolve, reject) => {
- if(data.showLoading) {
- wx.showLoading({
- title: '加载中',
- mask: true
- })
- }
- // 接口api拼接环境地址
- data.url = baseurl + data.url
- // 处理需要token的请求
- if(!isWithoutToken(data)) {
- let baseHeader = {authorization: wx.getStorageSync('token')}
- data.header = Object.assign(data.header || {}, baseHeader)
- }
- wx.request({
- ...data,
- success: function(res) {
- // 处理请求
- if(res.data.code == 0) { // 请求成功状态码
- resolve(res.data)
- } else if ((res.statusCode === 401) || (res.statusCode === 402) || (res.statusCode === 402)) { // token过期状态码
- reject(res.data.msg)
- wx.showToast({
- title: '登录过期',
- icon: 'none',
- duration: 2000
- })
- // 重新登录
- wx.reLaunch({
- url: '/pages/login/login',
- })
- } else {
- reject(res.data.msg)
- wx.showToast({
- title: res.data.msg,
- icon: 'none',
- duration: 2000
- })
- }
- },
- fail: function (error) {
- reject(error)
- },
- complete: function () {
- if (data.showLoading) {
- wx.hideLoading()
- }
- }
- })
- }).catch(e => {
- const title = e || '系统异常'
- wx.showToast({
- icon: "none",
- title: title,
- duration: 2000
- })
- })
- }
|