import { history, useModel } from "@umijs/max"; // 运行时配置 // 全局初始化数据配置,用于 Layout 用户信息和权限初始化 // 更多信息见文档:https://umijs.org/docs/api/runtime-config#getinitialstate export async function getInitialState(): Promise<{ name: string }> { return { name: '@umijs/max' }; } /** * 配置请求 */ export const request = { requestInterceptors: [ (url: string, options: any) => { options.headers = { ...options.headers, Authorization: `${localStorage.getItem('token') ? 'Bearer ' + localStorage.getItem('token') : ''}`, }; return { url, options }; }, ], errorConfig: { errorHandler: (error: any) => { if (error.response.status === 401) { localStorage.removeItem("token"); history.push('/login'); } }, }, }; // 配置路由跳转 export function onRouteChange({ location }: any) { const token = localStorage.getItem('token'); // 正确用法:解构 state 和 actions // 未登录且不在登录页,跳转到登录页 if (!token && (location.pathname !== '/login' && location.pathname !== '/login/callback' )) { history.push('/login?redirect=' + encodeURIComponent(location.pathname)); } if(location.pathname !== '/play') { } // 已登录却访问登录页,跳转到首页 if (token && location.pathname === '/login') { history.push('/'); } }