前端刷新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
}``
阅读 164
评论
    2 个回答

    image.png
    那就在你这个判断里面再掉你的方法

    wxRequest()