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 } from '@umijs/max'; import { AvatarDropdown, AvatarName, Footer, Question, SelectLang, } from '@/components'; import { getInfo } from '@/services/login'; import type { UserInfoVO } from '@/services/login/types'; import defaultSettings from '../config/defaultSettings'; import { errorConfig } from './requestErrorConfig'; import '@ant-design/v5-patch-for-react-19'; import { useDictStore } from '@/hooks/stores/dict'; import { getAccessToken, getTenantId } from '@/utils/auth'; import { CACHE_KEY, useCache } from './hooks/web/useCache'; import type { MenuVO } from './services/system/menu'; import { loopMenuItem } 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; menus?: MenuVO[]; fetchUserInfo?: () => Promise; }> { const { wsCache } = useCache(); const dictStore = useDictStore(); const fetchUserInfo = async () => { // history.push(loginPath); try { const token = getAccessToken(); if (!token) { throw new Error('No token found'); } const data = await getInfo(); console.log(data, 'data'); wsCache.set(CACHE_KEY.USER, data); wsCache.set(CACHE_KEY.ROLE_ROUTERS, data.menus); if (!dictStore.getIsSetDict) { await dictStore.setDictMap(); } // 转换菜单格式 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) { await fetchUserInfo(); } const menus = wsCache.get(CACHE_KEY.ROLE_ROUTERS); return { fetchUserInfo, currentUser, menus, 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: () =>