Home > front end >  Front the refresh token, continue to request before, but before the request, can't get the inte
Front the refresh token, continue to request before, but before the request, can't get the inte

Time:09-16


Encapsulates the ajax request, has a separate API module, if the token expired, the new refresh token, continue to request before, but continue to request before, don't get the return value of the interface; How to solve, the great god


Import {domain} from '/config'

Let isRefreshing=true
Let refreshLoginUrls=[]
Const wxRequest=(params={}, url)=& gt; {
Let token=dd. GetStorageSync ({key: 'token'})? "Bearer" + dd. GetStorageSync ({key: 'token'}). Data: "'
Let data=https://bbs.csdn.net/topics/params.query | | {}
Return new Promise (async (resolve, reject)=& gt; {
Await the dd. HttpRequest ({
Url: domain + url,
Method: params. Method | | 'GET',
Data: JSON. Stringify (data),
Headers: {
"The content-type" : "application/json,"
Authorization: token
},
DataType: 'json,
Success: async sucInfo=& gt; {
If (sucInfo. Data & amp; & SucInfo. Data. The resultCode==401) {
RefreshLoginUrls. Push ({params: params, url: url})
If (isRefreshing) {
IsRefreshing=false
Await refreshToken ()
}
Return the resolve (sucInfo. Data)
} else {
Return the resolve (sucInfo. Data)
}

},
Fail: failInfo=& gt; {
Return reject (failInfo)
},
})
})
}

Const refreshToken ()==& gt; {
The return of new Promise ((resolve, reject)=& gt; {
Dd. GetAuthCode ({
Success: loginCode=& gt; {
If (loginCode. AuthCode) {
WxPost ({query: {code: loginCode authCode}}, '/API/frontend/login). Then (loginInfo=& gt; {
If (loginInfo. The resultCode==200) {
Dd. SetStorage ({
Key: "token",
Data: loginInfo. Data. Access_token,
Success: ()=& gt; {
Dd. SetStorageSync ({key: 'the userInfo' data: loginInfo. Data. User_info})
RefreshLoginUrls. ForEach (item=& gt; {
WxPost (item. Params, item url). Then (res=& gt; {
Return the resolve (res) data)
})
})
RefreshLoginUrls=[]
}
})
} else {
Return reject (loginInfo. Data)
}
})
}
}
})
})
}

Const wxGet=async (params={}, url)=& gt; {
Params. Method='GET'
Return wxRequest (params, url)
}

Const wxPost=async (params={}, url)=& gt; {
Params. Method='POST'
Return wxRequest (params, url)
}

The module. Exports={
WxRequest wxGet, wxPost
}
  • Related