修复页面bug

This commit is contained in:
2025-09-26 18:42:41 +08:00
parent 3c604be299
commit 4c154ec746
19 changed files with 584 additions and 405 deletions
+35 -2
View File
@@ -13,6 +13,24 @@ export const request: any = {
errorConfig: {
errorHandler(error: any) {
// 统一错误处理
// 若出现 401/403 等未授权错误,清理本地态并跳转登录
try {
const status = (error?.response?.status ?? error?.data?.code ?? error?.data?.resp_code) as number | undefined;
const unauthorized = status === 401 || status === 403;
if (unauthorized) {
try {
sessionStorage.removeItem('access_token');
sessionStorage.removeItem('current_user');
} catch {}
const pathname = typeof window !== 'undefined' ? window.location.pathname : '/';
const search = typeof window !== 'undefined' ? window.location.search : '';
const current = encodeURIComponent(`${pathname}${search}`);
const to = `/user/login?redirect=${current}`;
if (!String(pathname).startsWith('/user/login')) {
history.push(to);
}
}
} catch {}
throw error;
},
},
@@ -39,6 +57,23 @@ export const request: any = {
} as any;
(response as any).data = mapped;
}
// 未授权统一处理:HTTP 状态或业务码均触发
const httpUnauthorized = (response.status === 401 || response.status === 403);
const bizCode = (data?.resp_code ?? data?.code) as number | undefined;
const bizUnauthorized = bizCode === 401 || bizCode === 403 || bizCode === 40101 || bizCode === 40100;
if (httpUnauthorized || bizUnauthorized) {
try {
sessionStorage.removeItem('access_token');
sessionStorage.removeItem('current_user');
} catch {}
const pathname = typeof window !== 'undefined' ? window.location.pathname : '/';
const search = typeof window !== 'undefined' ? window.location.search : '';
const current = encodeURIComponent(`${pathname}${search}`);
const to = `/user/login?redirect=${current}`;
if (!String(pathname).startsWith('/user/login')) {
history.push(to);
}
}
} catch (e) {
// 静默处理解析错误
}
@@ -53,8 +88,6 @@ export function onRouteChange({ location }: any) {
const isLoginPath = String(location?.pathname || '').startsWith('/user/login');
if (!token && !isLoginPath) {
history.push('/user/login');
} else if (token && isLoginPath) {
history.push('/welcome');
}
} catch {}
}