From c2e8526f8f857be5e3a8c6f2b39c5efd424de33e Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期四, 15 十月 2026 11:50:24 +0800
Subject: [PATCH] refactor(auth): 优化认证逻辑和界面组件结构
---
src/api/request.ts | 20 +++++++++++++-------
1 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/src/api/request.ts b/src/api/request.ts
index 7389c31..679b78f 100644
--- a/src/api/request.ts
+++ b/src/api/request.ts
@@ -33,10 +33,13 @@
/**
* 閲嶆柊璁よ瘉閫昏緫
*/
- async function doReAuthenticate() {
+ async function doReAuthenticate(error?: { config?: { headers?: Record<string, unknown> } }) {
console.warn('Access token or refresh token is invalid or expired. ');
const accessStore = useAccessStore();
const authStore = useAuthStore();
+ const requestToken = error?.config?.headers?.Authorization;
+ // 宸查��鍑烘垨宸插垏鎹㈣处鍙锋椂锛屾棫璇锋眰鐨� 401 涓嶅緱娓呴櫎鏂颁細璇濄��
+ if (requestToken && requestToken !== formatToken(accessStore.accessToken)) return;
accessStore.setAccessToken(null);
if (
preferences.app.loginExpiredMode === 'modal' &&
@@ -58,6 +61,9 @@
throw new Error('Refresh token is null!');
}
const resp = await refreshTokenApi(refreshToken);
+ if (accessStore.refreshToken !== refreshToken) {
+ throw new Error('鐧诲綍鐘舵�佸凡鍙樺寲锛屽拷鐣ユ棫浼氳瘽鐨勪护鐗屽埛鏂扮粨鏋�');
+ }
const newToken = resp?.data?.data?.accessToken;
// add by 鑺嬭壙锛氳繖閲屼竴瀹氳鎶涘嚭 resp.data锛屼粠鑰岃Е鍙� authenticateResponseInterceptor 涓紝鍒锋柊浠ょ墝澶辫触锛侊紒锛�
if (!newToken) {
@@ -220,13 +226,13 @@
fulfilled: (config) => {
const accessStore = useAccessStore();
// 娣诲姞绉熸埛缂栧彿
- config.headers['tenant-id'] = tenantEnable
- ? accessStore.tenantId
- : undefined;
+ if (!('tenant-id' in config.headers)) {
+ config.headers['tenant-id'] = tenantEnable ? accessStore.tenantId : undefined;
+ }
// 鍙湁鐧诲綍鏃讹紝鎵嶈缃� visit-tenant-id 璁块棶绉熸埛
- config.headers['visit-tenant-id'] = tenantEnable
- ? accessStore.visitTenantId
- : undefined;
+ if (!('visit-tenant-id' in config.headers)) {
+ config.headers['visit-tenant-id'] = tenantEnable ? accessStore.visitTenantId : undefined;
+ }
return config;
},
});
--
Gitblit v1.9.3