// @ts-nocheck import { logout } from '@/pages/login/apis'; import { useAccessMarkedRoutes } from '@@/plugin-access'; import { useModel } from '@@/plugin-model'; import { ProLayout } from '@ant-design/pro-components'; import { Link, Outlet, history, matchRoutes, useAppData, useLocation, useNavigate, type IRoute } from '@umijs/max'; import { useMemo } from 'react'; import Exception from './Exception'; import './Layout.css'; import Logo from './Logo'; import { getRightRenderContent } from './rightRender'; import { patchRoutes } from './runtime'; const loginPath = '/login'; // 过滤出需要显示的路由, 这里的filterFn 指 不希望显示的层级 const filterRoutes = ( routes: IRoute[], filterFn: (route: IRoute) => boolean ) => { if (routes.length === 0) { return []; } let newRoutes = []; for (const route of routes) { const newRoute = { ...route }; if (filterFn(route)) { if (Array.isArray(newRoute.routes)) { newRoutes.push(...filterRoutes(newRoute.routes, filterFn)); } } else { if (Array.isArray(newRoute.children)) { newRoute.children = filterRoutes(newRoute.children, filterFn); newRoute.routes = newRoute.children; } newRoutes.push(newRoute); } } return newRoutes; }; // 格式化路由 处理因 wrapper 导致的 菜单 path 不一致 const mapRoutes = (routes: IRoute[], role: string) => { if (routes.length === 0) { return []; } return routes.map((route) => { // 需要 copy 一份, 否则会污染原始数据 const newRoute = { ...route, role }; if (route.originPath) { newRoute.path = route.originPath; } if (Array.isArray(route.routes)) { newRoute.routes = mapRoutes(route.routes, role); } if (Array.isArray(route.children)) { newRoute.children = mapRoutes(route.children, role); } return newRoute; }); }; export default (props: any) => { const location = useLocation(); const navigate = useNavigate(); const { clientRoutes, pluginManager } = useAppData(); const initialInfo = (useModel && useModel('@@initialState')) || { initialState: undefined, loading: false, setInitialState: null }; console.log('initialInfo==========', initialInfo); const { initialState, loading, setInitialState } = initialInfo; const userConfig = { title: '', layout: 'mix', locale: true }; const formatMessage = undefined; const runtimeConfig = { ...initialInfo, logout: async (userInfo) => { console.log('logout', userInfo); await logout(); navigate(loginPath); }, notFound: 404 not found }; // 现在的 layout 及 wrapper 实现是通过父路由的形式实现的, 会导致路由数据多了冗余层级, proLayout 消费时, 无法正确展示菜单, 这里对冗余数据进行过滤操作 const newRoutes = filterRoutes( clientRoutes.filter((route) => route.id === '@@/global-layout'), (route) => { return ( (!!route.isLayout && route.id !== '@@/global-layout') || !!route.isWrapper ); } ); const role = initialState?.currentUser?.is_admin ? 'admin' : 'user'; const [route] = useAccessMarkedRoutes(mapRoutes(newRoutes, role)); console.log('clientRoutes===========', route, clientRoutes, newRoutes); patchRoutes({ routes: route.children, initialState: initialInfo.initialState }); const matchedRoute = useMemo( () => matchRoutes(route?.children || [], location.pathname)?.pop?.()?.route, [location.pathname] ); console.log('route===========', matchedRoute, route); return (