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

62 lines
1.8 KiB
TypeScript
Raw Normal View History

import './global.less';
import React from 'react';
import { ThemeProvider } from './theme';
import { history } from 'umi';
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
// 统一错误处理
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}`;
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;
}
} 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');
} else if (token && isLoginPath) {
history.push('/welcome');
}
} catch {}
}
2025-09-23 16:12:43 +08:00