@@ -4,7 +4,7 @@ import { useAuthStore } from '@/store/modules/auth';
4
4
import { localStg } from '@/utils/storage' ;
5
5
import { getServiceBaseURL } from '@/utils/service' ;
6
6
import { $t } from '@/locales' ;
7
- import { handleRefreshToken , showErrorMsg } from './shared' ;
7
+ import { getAuthorization , handleExpiredRequest , showErrorMsg } from './shared' ;
8
8
import type { RequestInstanceState } from './type' ;
9
9
10
10
const isHttpProxy = import . meta. env . DEV && import . meta. env . VITE_HTTP_PROXY === 'Y' ;
@@ -19,12 +19,8 @@ export const request = createFlatRequest<App.Service.Response, RequestInstanceSt
19
19
} ,
20
20
{
21
21
async onRequest ( config ) {
22
- const { headers } = config ;
23
-
24
- // set token
25
- const token = localStg . get ( 'token' ) ;
26
- const Authorization = token ? `Bearer ${ token } ` : null ;
27
- Object . assign ( headers , { Authorization } ) ;
22
+ const Authorization = getAuthorization ( ) ;
23
+ Object . assign ( config . headers , { Authorization } ) ;
28
24
29
25
return config ;
30
26
} ,
@@ -82,15 +78,13 @@ export const request = createFlatRequest<App.Service.Response, RequestInstanceSt
82
78
// when the backend response code is in `expiredTokenCodes`, it means the token is expired, and refresh token
83
79
// the api `refreshToken` can not return error code in `expiredTokenCodes`, otherwise it will be a dead loop, should return `logoutCodes` or `modalLogoutCodes`
84
80
const expiredTokenCodes = import . meta. env . VITE_SERVICE_EXPIRED_TOKEN_CODES ?. split ( ',' ) || [ ] ;
85
- if ( expiredTokenCodes . includes ( responseCode ) && ! request . state . isRefreshingToken ) {
86
- request . state . isRefreshingToken = true ;
87
-
88
- const refreshConfig = await handleRefreshToken ( response . config ) ;
89
-
90
- request . state . isRefreshingToken = false ;
81
+ if ( expiredTokenCodes . includes ( responseCode ) ) {
82
+ const success = await handleExpiredRequest ( request . state ) ;
83
+ if ( success ) {
84
+ const Authorization = getAuthorization ( ) ;
85
+ Object . assign ( response . config . headers , { Authorization } ) ;
91
86
92
- if ( refreshConfig ) {
93
- return instance . request ( refreshConfig ) as Promise < AxiosResponse > ;
87
+ return instance . request ( response . config ) as Promise < AxiosResponse > ;
94
88
}
95
89
}
96
90
0 commit comments