From 703586123aa21021edf477871db44b44f46b8131 Mon Sep 17 00:00:00 2001 From: dap <15891557205@163.com> Date: Wed, 4 Jun 2025 19:06:18 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=AD=97=E5=85=B8=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=8A=9B=E5=87=BA=E5=BC=82=E5=B8=B8(=E4=B8=BA?= =?UTF-8?q?=E4=BB=80=E4=B9=88=E4=BC=9A=E6=8A=9B=E5=87=BA=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=3F)=E6=97=A0=E9=99=90=E8=B0=83=E7=94=A8=E6=8E=A5=E5=8F=A3=20?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++++ apps/web-antd/src/api/request.ts | 9 +++++++-- apps/web-antd/src/utils/dict.ts | 14 +++++++++++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5137a988..129c9d93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ - 菜单管理 路由地址的必填项不生效 +**REFACTOR** + +- 字典接口抛出异常(为什么会抛出异常?)无限调用接口 兼容处理 + # 1.4.0 **FEATURES** diff --git a/apps/web-antd/src/api/request.ts b/apps/web-antd/src/api/request.ts index 08d9a8f7..95229265 100644 --- a/apps/web-antd/src/api/request.ts +++ b/apps/web-antd/src/api/request.ts @@ -39,6 +39,11 @@ const { apiURL, clientId, enableEncrypt } = useAppConfig( */ let isLogoutProcessing = false; +/** + * 定义一个401专用异常 用于可能会用到的区分场景? + */ +export class UnauthorizedException extends Error {} + function createRequestClient(baseURL: string) { const client = new RequestClient({ // 后端地址 @@ -228,7 +233,7 @@ function createRequestClient(baseURL: string) { case 401: { // 已经在登出过程中 不再执行 if (isLogoutProcessing) { - throw new Error(timeoutMsg); + throw new UnauthorizedException(timeoutMsg); } isLogoutProcessing = true; const _msg = $t('http.loginTimeout'); @@ -238,7 +243,7 @@ function createRequestClient(baseURL: string) { isLogoutProcessing = false; }); // 不再执行下面逻辑 - throw new Error(_msg); + throw new UnauthorizedException(_msg); } default: { if (msg) { diff --git a/apps/web-antd/src/utils/dict.ts b/apps/web-antd/src/utils/dict.ts index dd73ef85..bd147be1 100644 --- a/apps/web-antd/src/utils/dict.ts +++ b/apps/web-antd/src/utils/dict.ts @@ -1,3 +1,4 @@ +import { UnauthorizedException } from '#/api/request'; import { dictDataInfo } from '#/api/system/dict/dict-data'; import { useDictStore } from '#/store/dict'; @@ -27,9 +28,16 @@ function fetchAndCacheDictData( // 内部处理了push的逻辑 这里不用push setDictInfo(dictName, resp, formatNumber); }) - .catch(() => { - // 401时 移除字典缓存 下次登录重新获取 - dictRequestCache.delete(dictName); + .catch((error) => { + /** + * 需要判断是否为401抛出的特定异常 401清除缓存 + * 其他error清除缓存会导致无限循环调用字典接口 则不做处理 + */ + if (error instanceof UnauthorizedException) { + // 401时 移除字典缓存 下次登录重新获取 + dictRequestCache.delete(dictName); + } + // 其他不做处理 }) .finally(() => { // 移除请求状态缓存