feat: 微服务版本 logout接口在配置错误的情况返回401的提示(解决死循环调用logout接口)

This commit is contained in:
dap
2025-07-18 18:23:55 +08:00
parent 6311f8edea
commit 296899fc33
4 changed files with 56 additions and 36 deletions

View File

@@ -1,6 +1,6 @@
import { $t } from '@vben/locales';
import { message } from 'ant-design-vue';
import { message, Modal } from 'ant-design-vue';
import { useAuthStore } from '#/store';
@@ -38,11 +38,20 @@ export function commonExport(url: string, data: Record<string, any>) {
*/
export class UnauthorizedException extends Error {}
/**
* logout这种接口都返回401 抛出这个异常
*/
export class ImpossibleReturn401Exception extends Error {}
/**
* 是否已经处在登出过程中了 一个标志位
* 主要是防止一个页面会请求多个api 都401 会导致登出执行多次
*/
let isLogoutProcessing = false;
/**
* 防止 调用logout接口 logout又返回401 然后又走到Logout逻辑死循环
*/
let lockLogoutRequest = false;
/**
* 登出逻辑 两个地方用到 提取出来
@@ -50,17 +59,43 @@ let isLogoutProcessing = false;
*/
export function handleUnauthorizedLogout() {
const timeoutMsg = $t('http.loginTimeout');
/**
* lock 不再请求logout接口
* 这里已经算异常情况了
*/
if (lockLogoutRequest) {
throw new UnauthorizedException(timeoutMsg);
}
// 已经在登出过程中 不再执行
if (isLogoutProcessing) {
throw new UnauthorizedException(timeoutMsg);
}
isLogoutProcessing = true;
const userStore = useAuthStore();
userStore.logout().finally(() => {
message.error(timeoutMsg);
isLogoutProcessing = false;
});
userStore
.logout()
.catch((error) => {
/**
* logout接口返回了401
* 做Lock处理 且 该标志位不会复位(因为这种场景出现 系统已经算故障了)
* 因为这已经不符合正常的逻辑了
*/
if (error instanceof ImpossibleReturn401Exception) {
lockLogoutRequest = true;
if (import.meta.env.DEV) {
Modal.error({
title: '提示',
centered: true,
content:
'检测到你的logout接口返回了401, 去检查你的后端配置 这已经不符合正常逻辑(该提示不会在非dev环境弹出)',
});
}
}
})
.finally(() => {
message.error(timeoutMsg);
isLogoutProcessing = false;
});
// 不再执行下面逻辑
throw new UnauthorizedException(timeoutMsg);
}