前端刷新token, 继续之前请求,调取接口返回数据。
封装了ajax请求, 有单独api模块, 如果token过期, 从新刷新token,继续之前的请求,但继续之前的请求,拿不到接口的返回值;如何解决。求大神
`import {domain} from './config'
let isRefreshing = true
let refreshLoginUrls = []
const wxRequest = (params = {}, url) => {
let token = dd.getStorageSync({key: 'token'}) ? 'Bearer ' + dd.getStorageSync({key: 'token'}).data : ''
let data = params.query || {}
return new Promise(async (resolve, reject) => {
await dd.httpRequest({
url: domain + url,
method: params.method || 'GET',
data: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
Authorization: token
},
dataType: 'json',
success:async sucInfo => {
if (sucInfo.data && sucInfo.data.resultCode == 401) {
refreshLoginUrls.push({params: params, url:url})
if (isRefreshing) {
isRefreshing = false
await refreshToken()
}
return resolve(sucInfo.data)
} else {
return resolve(sucInfo.data)
}
},
fail: failInfo => {
return reject(failInfo)
},
})
})
}
const refreshToken = () => {
return new Promise((resolve, reject) => {
dd.getAuthCode({
success: loginCode => {
if (loginCode.authCode) {
wxPost({query: {code: loginCode.authCode}}, '/api/frontend/login').then(loginInfo => {
if (loginInfo.resultCode == 200) {
dd.setStorage({
key: "token",
data: loginInfo.data.access_token,
success: () => {
dd.setStorageSync({key: 'userInfo', data:loginInfo.data.user_info })
refreshLoginUrls.forEach(item => {
wxPost(item.params, item.url).then(res => {
return resolve(res.data)
})
})
refreshLoginUrls = []
}
})
} else {
return reject(loginInfo.data)
}
})
}
}
})
})
}
const wxGet = async (params = {}, url) => {
params.method = 'GET'
return wxRequest(params, url)
}
const wxPost = async (params = {}, url) => {
params.method = 'POST'
return wxRequest(params, url)
}
module.exports = {
wxRequest, wxGet, wxPost
}``
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我项目里面刷新token是这样处理的,你看看这样能解决你的问题吗?
那就在你这个判断里面再掉你的方法