From 0a27114ea37dc2bdf5710d0812a4eb7f25c6dc16 Mon Sep 17 00:00:00 2001 From: dap <15891557205@163.com> Date: Sat, 12 Oct 2024 11:29:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=99=BB=E5=87=BA=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=9C=A8=E5=B9=B6=E5=8F=91(=E9=9D=9Eawait)?= =?UTF-8?q?=E6=83=85=E5=86=B5=E4=B8=8B=E9=87=8D=E5=A4=8D=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + apps/web-antd/src/api/request.ts | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab3d09ce..46220a4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - 登录页面 关闭租户后下拉框没有正常隐藏 - 字典管理 关闭租户不应显示`同步租户字典`按钮 - 登录日志 漏掉了登录日志日期查询 +- 登出相关逻辑在并发(非await)情况下重复执行的问题 **OTHERS** diff --git a/apps/web-antd/src/api/request.ts b/apps/web-antd/src/api/request.ts index 2bdc1c7d..989ddda1 100644 --- a/apps/web-antd/src/api/request.ts +++ b/apps/web-antd/src/api/request.ts @@ -34,8 +34,11 @@ const { apiURL, clientId, enableEncrypt } = useAppConfig( import.meta.env.PROD, ); -/** 控制是否弹窗 防止登录超时请求多个api会弹窗多次 */ -let showTimeoutToast = true; +/** + * 是否已经处在登出过程中了 一个标志位 + * 主要是防止一个页面会请求多个api 都401 会导致登出执行多次 + */ +let isLogoutProcessing = false; function createRequestClient(baseURL: string) { const client = new RequestClient({ @@ -234,18 +237,16 @@ function createRequestClient(baseURL: string) { let timeoutMsg = ''; switch (code) { case 401: { + // 已经在登出过程中 不再执行 + if (isLogoutProcessing) { + return; + } + isLogoutProcessing = true; const _msg = '登录超时, 请重新登录'; const userStore = useAuthStore(); - userStore.logout().then(() => { - /** 只弹窗一次 */ - if (showTimeoutToast) { - showTimeoutToast = false; - message.error(_msg); - /** 定时器 3s后再开启弹窗 */ - setTimeout(() => { - showTimeoutToast = true; - }, 3000); - } + userStore.logout().finally(() => { + message.error(_msg); + isLogoutProcessing = false; }); // 不再执行下面逻辑 return;