refactor: 登录超时逻辑

This commit is contained in:
dap
2025-07-04 15:41:09 +08:00
parent 7535bd6096
commit 29dd4ce7f7
4 changed files with 91 additions and 42 deletions

View File

@@ -1,3 +1,9 @@
import { $t } from '@vben/locales';
import { message } from 'ant-design-vue';
import { useAuthStore } from '#/store';
import { requestClient } from './request';
/**
@@ -26,3 +32,35 @@ export function commonExport(url: string, data: Record<string, any>) {
responseType: 'blob',
});
}
/**
* 定义一个401专用异常 用于可能会用到的区分场景?
*/
export class UnauthorizedException extends Error {}
/**
* 是否已经处在登出过程中了 一个标志位
* 主要是防止一个页面会请求多个api 都401 会导致登出执行多次
*/
let isLogoutProcessing = false;
/**
* 登出逻辑 两个地方用到 提取出来
* @throws UnauthorizedException 抛出特定的异常
*/
export function handleUnauthorizedLogout() {
const timeoutMsg = $t('http.loginTimeout');
// 已经在登出过程中 不再执行
if (isLogoutProcessing) {
throw new UnauthorizedException(timeoutMsg);
}
isLogoutProcessing = true;
const userStore = useAuthStore();
userStore.logout().finally(() => {
message.error(timeoutMsg);
isLogoutProcessing = false;
});
// 不再执行下面逻辑
throw new UnauthorizedException(timeoutMsg);
}