2021-03-16 16:59:21 +00:00
|
|
|
import sha1 from './sha1'
|
|
|
|
|
2021-03-07 17:09:11 +00:00
|
|
|
|
|
|
|
// 正在队列上的请求
|
|
|
|
const pendings = []
|
|
|
|
|
|
|
|
let refreshing = false
|
|
|
|
|
|
|
|
// 刷新token
|
|
|
|
const refresh_token = app => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
do_request(app, {
|
|
|
|
url: '/auth/token',
|
|
|
|
method: 'get'
|
|
|
|
}).then(res => {
|
|
|
|
// 设置登录信息
|
2021-03-27 09:19:31 +00:00
|
|
|
app.globalData.userBaseInfo.token = res.token
|
2021-03-07 17:09:11 +00:00
|
|
|
resolve(true)
|
|
|
|
}).catch(err => {
|
|
|
|
// 清空登录信息
|
|
|
|
app.globalData.login = false
|
|
|
|
app.globalData.userInfo = {}
|
|
|
|
app.globalData.loginTime = null
|
|
|
|
refreshing = false
|
|
|
|
reject()
|
|
|
|
}).then(() => {
|
|
|
|
// 停止刷新状态
|
|
|
|
refreshing = false;
|
2021-03-20 17:48:56 +00:00
|
|
|
// 执行所有的缓存的请求
|
2021-03-07 17:09:11 +00:00
|
|
|
pendings.forEach(element => {
|
|
|
|
do_request(app, element)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 实际操作函数
|
|
|
|
const do_request = (app, options) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const { data, method, url } = options
|
|
|
|
|
|
|
|
// 设置请求头
|
|
|
|
let header_options = {
|
|
|
|
'timestamp': Date.now(),
|
|
|
|
'X-Requested-With': 'XMLHttpRequest'
|
|
|
|
}
|
|
|
|
|
|
|
|
// 检查登录状态
|
|
|
|
if(app.globalData.login) {
|
2021-03-16 16:59:21 +00:00
|
|
|
const userBaseInfo = app.globalData.userBaseInfo
|
2021-03-20 17:48:56 +00:00
|
|
|
// 生成随机特征值
|
2021-03-16 16:59:21 +00:00
|
|
|
let random_code = `RandomCode [${userBaseInfo.openid}][${header_options.timestamp}][${app.globalData.appID}]`
|
|
|
|
random_code = sha1(random_code)
|
2021-03-20 17:48:56 +00:00
|
|
|
// 生成客户端签名
|
2021-03-16 16:59:21 +00:00
|
|
|
let signed = `SIGN [${userBaseInfo.postmark}][${random_code}][${userBaseInfo.token}]`
|
|
|
|
signed = sha1(signed)
|
2021-03-20 17:48:56 +00:00
|
|
|
// 填写头部验证部分
|
2021-03-16 16:59:21 +00:00
|
|
|
header_options['signed'] = signed;
|
|
|
|
header_options['postmark'] = userBaseInfo.postmark;
|
2021-03-07 17:09:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
options.url = app.globalData.baseURL + url
|
|
|
|
|
|
|
|
if(data && method === 'post') {
|
|
|
|
header_options['Content-Type'] = 'application/x-www-form-urlencoded'
|
|
|
|
}
|
2021-03-20 17:48:56 +00:00
|
|
|
|
|
|
|
// 调用微信接口进行网络请求
|
2021-03-07 17:09:11 +00:00
|
|
|
wx.request({
|
|
|
|
header: {
|
|
|
|
...header_options
|
|
|
|
},
|
|
|
|
...options,
|
|
|
|
success: function(res) {
|
|
|
|
const data = res.data
|
2021-03-20 17:48:56 +00:00
|
|
|
// 检查返回状态码
|
2021-03-07 17:09:11 +00:00
|
|
|
if(data.status >= 200 && data.status < 300) {
|
|
|
|
resolve(data.data)
|
|
|
|
} else {
|
2021-03-16 16:59:21 +00:00
|
|
|
reject(data.msg)
|
2021-03-07 17:09:11 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
fail: function(res) {
|
|
|
|
reject(res.data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 对外接口
|
|
|
|
const request = options => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const app = getApp()
|
|
|
|
// 获得登录时间
|
|
|
|
const loginTime = app.globalData.loginTime
|
|
|
|
// 检查是否达到刷新周期
|
|
|
|
if(loginTime != null && loginTime + 1000 * 60 < Date.now()) {
|
|
|
|
app.globalData.loginTime = null
|
|
|
|
refreshing = true
|
2021-03-20 17:48:56 +00:00
|
|
|
// 执行token刷新
|
2021-03-07 17:09:11 +00:00
|
|
|
refresh_token(app)
|
|
|
|
}
|
|
|
|
|
2021-03-20 17:48:56 +00:00
|
|
|
// 检查是否正在刷新
|
2021-03-07 17:09:11 +00:00
|
|
|
if(refreshing) {
|
2021-03-20 17:48:56 +00:00
|
|
|
// 缓存请求
|
2021-03-07 17:09:11 +00:00
|
|
|
pendings.push(options)
|
|
|
|
} else {
|
2021-03-20 17:48:56 +00:00
|
|
|
// 进行请求
|
2021-03-07 17:09:11 +00:00
|
|
|
do_request(app, options).then(res => {
|
|
|
|
resolve(res)
|
|
|
|
}).catch(err => {
|
|
|
|
reject(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
export default request
|