import config from "../js/config"
|
let clearStorage = false;
|
let timer = null;
|
const request = (params) => {
|
let fullUrl = `${config.requestCtx}${params.url}`
|
let token = wx.getStorageSync(config.tokenKey) ? wx.getStorageSync(config.tokenKey) : ''
|
return new Promise((resolve, reject) => {
|
wx.request({
|
url: fullUrl,
|
method: params.method,
|
data: params.data ? params.data : {},
|
header: {
|
'content-type': 'application/json',
|
'Authorization': `bearer ${token}`
|
},
|
success: (res) => {
|
if (res.statusCode == 200) {
|
return resolve(res.data.data)
|
} else if (res.statusCode == 401) {
|
const pages = getCurrentPages()
|
const currentPage = pages[pages.length - 1]
|
const url = currentPage.route
|
let options = currentPage.options
|
let urlargs = `/${url}?`
|
wx.clearStorageSync();
|
if (!clearStorage) {
|
clearStorage = true;
|
if (!timer) {
|
timer = setTimeout(() => {
|
clearStorage = false
|
}, 4000);
|
}
|
// urlargs = JSON.stringify(urlargs)
|
let strOpt = JSON.stringify(options)
|
let currentUrl = {
|
options: options,
|
backUrl: urlargs
|
}
|
currentUrl = JSON.stringify(currentUrl)
|
// 不去首页登录,直接调用公共登录方法
|
// wx.reLaunch({
|
// url: urlargs ? '/pages/home/home?backUrl=' + encodeURIComponent(currentUrl) : '/pages/home/home'
|
// })
|
}
|
} else {
|
wx.showToast({
|
title: res.data.msg,
|
icon: 'none'
|
})
|
reject(res.data.message)
|
}
|
},
|
fail: (res) => {
|
wx.showToast({
|
title: '接口请求错误',
|
icon: 'none'
|
})
|
reject('接口请求错误')
|
},
|
complete: () => { }
|
})
|
})
|
}
|
export default request
|