utils中request.js
const BASEURL= 'http://localhost:3000/api/'
// 对象参数解构
function request({
url='',
method='GET',
data={},
header={},
tip='拼命加载中。。。'
}){
const token=wx.getStorageSync('token')
if(token){
header.Authorization=token
}
return new Promise((resolve,reject)=>{
wx.showLoading({
title: tip,
})
wx.request({
url: `${BASEURL}${url}`, //仅为示例,并非真实的接口地址
method,
header,
data,
success (res) {
if(res.data.code===402){
wx.removeStorageSync('token')
wx.reLaunch({
url: '/pages/login/login',
})
}
resolve(res.data)
},
fail(err){
reject(err)
},
complete:()=>{
wx.hideLoading()
}
})
})
}
wx.$request = request
export {
request
}