Files
gofly-platform-services/zlt-web/react-web-next/src/app.tsx
T

100 lines
3.7 KiB
TypeScript
Raw Normal View History

import './global.less';
import React from 'react';
import { ThemeProvider } from './theme';
import { history } from 'umi';
2025-10-01 03:51:53 +08:00
import { clearTenantCache, ensureTenantCacheTokenGuard } from '@/utils/tenantCache';
export function rootContainer(container: React.ReactNode, _args?: any) {
return React.createElement(ThemeProvider, null, container);
}
2025-09-23 16:12:43 +08:00
const API_PREFIX = process.env.API_PREFIX || '';
export const request: any = {
2025-09-23 16:12:43 +08:00
prefix: API_PREFIX,
errorConfig: {
errorHandler(error: any) {
2025-09-23 16:12:43 +08:00
// 统一错误处理
2025-09-26 18:42:41 +08:00
// 若出现 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');
2025-10-01 03:51:53 +08:00
clearTenantCache();
2025-09-26 18:42:41 +08:00
} 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;
},
},
requestInterceptors: [
(url: string, options: any) => {
const token = sessionStorage.getItem('access_token');
const headers: Record<string, string> = { ...(options?.headers as any) };
if (token) headers['Authorization'] = `Bearer ${token}`;
2025-10-01 03:51:53 +08:00
// 确保缓存与当前 Token 一致(Token 变化则清缓存)
ensureTenantCacheTokenGuard();
return { url, options: { ...options, headers } };
},
],
responseInterceptors: [
async (response: Response) => {
// 兼容后端 Result 结构
try {
const cloned = response.clone();
const data = await cloned.json();
if (data && typeof (data as any).resp_code !== 'undefined') {
const mapped = {
success: (data as any).resp_code === 0,
code: (data as any).resp_code,
msg: (data as any).resp_msg,
data: (data as any).data,
} as any;
(response as any).data = mapped;
}
2025-09-26 18:42:41 +08:00
// 未授权统一处理: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');
2025-10-01 03:51:53 +08:00
clearTenantCache();
2025-09-26 18:42:41 +08:00
} 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) {
// 静默处理解析错误
}
return response;
},
],
};
export function onRouteChange({ location }: any) {
try {
const token = sessionStorage.getItem('access_token');
const isLoginPath = String(location?.pathname || '').startsWith('/user/login');
if (!token && !isLoginPath) {
history.push('/user/login');
}
} catch {}
}
2025-09-23 16:12:43 +08:00