import React, { Children, Component, JSX, Suspense } from "react"; import { Spin } from "antd"; import type { Settings as LayoutSettings } from "@ant-design/pro-components"; import { SettingDrawer } from "@ant-design/pro-components"; import type { RequestConfig, RunTimeLayoutConfig } from "@umijs/max"; import { history, Link, Navigate } from "@umijs/max"; import { AvatarDropdown, AvatarName, Footer, Question, SelectLang, } from "@/components"; import { getInfo } from "@/services/login"; import type { UserVO, TokenType, UserInfoVO } from "@/services/login/types"; import defaultSettings from "../config/defaultSettings"; import { errorConfig } from "./requestErrorConfig"; import "@ant-design/v5-patch-for-react-19"; import { getAccessToken, getRefreshToken, getTenantId } from "@/utils/auth"; import { CACHE_KEY, useCache } from "./hooks/web/useCache"; import { MenuVO } from "./services/system/menu"; import { transformBackendMenuToFlatRoutes, transformMenuToRoutes, } from "@/utils/menuUtils"; const isDev = process.env.NODE_ENV === "development"; const isDevOrTest = isDev || process.env.CI; const loginPath = "/user/login"; // 全局存储菜单数据和路由映射 /** * @see https://umijs.org/docs/api/runtime-config#getinitialstate * */ export async function getInitialState(): Promise<{ settings?: Partial; currentUser?: UserInfoVO; loading?: boolean; fetchUserInfo?: () => Promise; }> { const { wsCache } = useCache(); const fetchUserInfo = async () => { // history.push(loginPath); try { const token = getAccessToken(); if (!token) { throw new Error("No token found"); } const { data } = await getInfo(); wsCache.set(CACHE_KEY.USER, data); wsCache.set(CACHE_KEY.ROLE_ROUTERS, data.menus); // 转换菜单格式 return data; } catch (_error) { history.push(loginPath); } return undefined; }; // 如果不是登录页面,执行 const { location } = history; if ( ![loginPath, "/user/register", "/user/register-result"].includes( location.pathname ) ) { const currentUser = wsCache.get(CACHE_KEY.USER); if (getAccessToken() && !currentUser) { fetchUserInfo(); } return { fetchUserInfo, currentUser, settings: defaultSettings as Partial, }; } return { fetchUserInfo, settings: defaultSettings as Partial, }; } // ProLayout 支持的api https://procomponents.ant.design/components/layout export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState, }) => { return { actionsRender: () => [ , , ], menu: { locale: false, // 关闭国际化- }, avatarProps: { src: initialState?.currentUser?.user.avatar, title: , render: (_, avatarChildren) => ( {avatarChildren} ), }, waterMarkProps: { content: initialState?.currentUser?.user.nickname, }, footerRender: () =>