refactor: 字典接口抛出异常(为什么会抛出异常?)无限调用接口 兼容处理
This commit is contained in:
parent
0295418f79
commit
703586123a
@ -4,6 +4,10 @@
|
|||||||
|
|
||||||
- 菜单管理 路由地址的必填项不生效
|
- 菜单管理 路由地址的必填项不生效
|
||||||
|
|
||||||
|
**REFACTOR**
|
||||||
|
|
||||||
|
- 字典接口抛出异常(为什么会抛出异常?)无限调用接口 兼容处理
|
||||||
|
|
||||||
# 1.4.0
|
# 1.4.0
|
||||||
|
|
||||||
**FEATURES**
|
**FEATURES**
|
||||||
|
@ -39,6 +39,11 @@ const { apiURL, clientId, enableEncrypt } = useAppConfig(
|
|||||||
*/
|
*/
|
||||||
let isLogoutProcessing = false;
|
let isLogoutProcessing = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 定义一个401专用异常 用于可能会用到的区分场景?
|
||||||
|
*/
|
||||||
|
export class UnauthorizedException extends Error {}
|
||||||
|
|
||||||
function createRequestClient(baseURL: string) {
|
function createRequestClient(baseURL: string) {
|
||||||
const client = new RequestClient({
|
const client = new RequestClient({
|
||||||
// 后端地址
|
// 后端地址
|
||||||
@ -228,7 +233,7 @@ function createRequestClient(baseURL: string) {
|
|||||||
case 401: {
|
case 401: {
|
||||||
// 已经在登出过程中 不再执行
|
// 已经在登出过程中 不再执行
|
||||||
if (isLogoutProcessing) {
|
if (isLogoutProcessing) {
|
||||||
throw new Error(timeoutMsg);
|
throw new UnauthorizedException(timeoutMsg);
|
||||||
}
|
}
|
||||||
isLogoutProcessing = true;
|
isLogoutProcessing = true;
|
||||||
const _msg = $t('http.loginTimeout');
|
const _msg = $t('http.loginTimeout');
|
||||||
@ -238,7 +243,7 @@ function createRequestClient(baseURL: string) {
|
|||||||
isLogoutProcessing = false;
|
isLogoutProcessing = false;
|
||||||
});
|
});
|
||||||
// 不再执行下面逻辑
|
// 不再执行下面逻辑
|
||||||
throw new Error(_msg);
|
throw new UnauthorizedException(_msg);
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
if (msg) {
|
if (msg) {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { UnauthorizedException } from '#/api/request';
|
||||||
import { dictDataInfo } from '#/api/system/dict/dict-data';
|
import { dictDataInfo } from '#/api/system/dict/dict-data';
|
||||||
import { useDictStore } from '#/store/dict';
|
import { useDictStore } from '#/store/dict';
|
||||||
|
|
||||||
@ -27,9 +28,16 @@ function fetchAndCacheDictData<T>(
|
|||||||
// 内部处理了push的逻辑 这里不用push
|
// 内部处理了push的逻辑 这里不用push
|
||||||
setDictInfo(dictName, resp, formatNumber);
|
setDictInfo(dictName, resp, formatNumber);
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch((error) => {
|
||||||
// 401时 移除字典缓存 下次登录重新获取
|
/**
|
||||||
dictRequestCache.delete(dictName);
|
* 需要判断是否为401抛出的特定异常 401清除缓存
|
||||||
|
* 其他error清除缓存会导致无限循环调用字典接口 则不做处理
|
||||||
|
*/
|
||||||
|
if (error instanceof UnauthorizedException) {
|
||||||
|
// 401时 移除字典缓存 下次登录重新获取
|
||||||
|
dictRequestCache.delete(dictName);
|
||||||
|
}
|
||||||
|
// 其他不做处理
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
// 移除请求状态缓存
|
// 移除请求状态缓存
|
||||||
|
Loading…
Reference in New Issue
Block a user