import './global.less'; import React from 'react'; import { ThemeProvider } from './theme'; import { history } from 'umi'; import { clearTenantCache, ensureTenantCacheTokenGuard } from '@/utils/tenantCache'; export function rootContainer(container: React.ReactNode, _args?: any) { return React.createElement(ThemeProvider, null, container); } const API_PREFIX = process.env.API_PREFIX || ''; export const request: any = { prefix: API_PREFIX, 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'); clearTenantCache(); } 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 = { ...(options?.headers as any) }; if (token) headers['Authorization'] = `Bearer ${token}`; // 确保缓存与当前 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; } // 未授权统一处理: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'); clearTenantCache(); } 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 {} }