wechat_app_template/request/index.js

49 lines
1.6 KiB
JavaScript

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.data.code == 401) || (res.data.code == 402)) { // token过期状态码
reject(res.data.msg)
// 重新登录
wx.reLaunch({
url: '/pages/login/login',
})
} else {
reject(res.data.msg)
}
},
fail: function (error) {
reject(error)
},
complete: function () {
if (data.showLoading) {
wx.hideLoading()
}
}
})
}).catch(e => {
wx.showToast({
icon: "error",
title: e || '系统错误',
})
})
}