From 9c8493b8814bea096fd2f4f7d719c16937972dba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?21=E4=B8=96=E7=BA=AA=E5=B0=8F=E5=85=AB=E8=B7=AF?= <2529156631@qq.com> Date: Thu, 17 Oct 2024 23:38:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E3=80=90token=E3=80=91:=20jw?= =?UTF-8?q?t=20token=20=E8=BF=87=E6=9C=9F=EF=BC=8C=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=BF=94=E5=9B=9E401=E7=8A=B6=E6=80=81?= =?UTF-8?q?=EF=BC=8C=E9=9D=99=E9=BB=98=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=96=B0=E7=9A=84token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CoreShop/core/utils/http.ts | 70 +++++++++++-------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/CoreCms.Net.Uni-App/CoreShop/core/utils/http.ts b/CoreCms.Net.Uni-App/CoreShop/core/utils/http.ts index a0cfcab3..4ae5d5e0 100644 --- a/CoreCms.Net.Uni-App/CoreShop/core/utils/http.ts +++ b/CoreCms.Net.Uni-App/CoreShop/core/utils/http.ts @@ -8,40 +8,50 @@ type TReqOptions = Pick; type TReqMethodOptions = Omit; type TResData = UniApp.RequestSuccessCallbackResult['data']; -// 存储未完成的请求 -const pendingRequests : any[] = []; +let isRefreshing = true; +let subscribers = []; -const request = async (url : string, { method, data, header = {} } : TReqOptions, auth : boolean = true) : Promise> => { +function onAccessTokenFetched() { + subscribers.forEach((callback) => { + callback(); + }) + subscribers = []; + isRefreshing = true; +} - let requestHeader : any = header; +function addSubscriber(callback:Function) { + subscribers.push(callback) +} - if (auth) { - if (uni.getStorageSync(UserToken)) { - requestHeader['Authorization'] = `Bearer ${uni.getStorageSync(UserToken)}`; - } else { - /** 储存当前请求 */ - pendingRequests.push({ - url, - data: { method, data, header, }, - auth, - }); - /** 重新请求拿到token */ - await handleLoginGetToken().then(() => { - pendingRequests.forEach((requestConfig) => { - return request(requestConfig.url, requestConfig.data, requestConfig.auth); - }); - pendingRequests.length = 0; - }); - return; +const request = async (url : string, { method, data, header = {} } : TReqOptions, auth : boolean = true, callBack ?: Function) : Promise> => { + return new Promise(async (resolve, _) => { + const res = await UniP.request({ + url: url.indexOf('http') >= 0 ? url : `${getRequestHost()}/${url}`, + method, + data, + header: { + ...header, + 'Authorization': `Bearer ${uni.getStorageSync(UserToken)}` + } + }); + + if (callBack) { + return callBack(res); } - } - - return UniP.request({ - url: url.indexOf('http') >= 0 ? url : `${getRequestHost()}/${url}`, - method, - data, - header: { - ...requestHeader + // 授权过期 + if (res.code === 14007) { + addSubscriber(() => { + request(url, { method, data, header }, auth, resolve) + }); + if (isRefreshing) { + await handleLoginGetToken(); + onAccessTokenFetched(); + isRefreshing = true; + } + isRefreshing = false; + } else { + isRefreshing = true; + return resolve(res); } }); }