import React, { useEffect, useMemo, useState } from 'react'; import { Space, message, Avatar, Dropdown } from 'antd'; import { LogoutOutlined, UserOutlined, SettingOutlined, DashboardOutlined, AppstoreOutlined, FileTextOutlined, TeamOutlined, CloudServerOutlined, BugOutlined, SearchOutlined, MessageOutlined, FolderOutlined, } from '@ant-design/icons'; import { Link, useLocation, history } from 'umi'; import ProLayout, { MenuDataItem } from '@ant-design/pro-layout'; import { fetchMenuData, outLogin } from '@/services/login'; import { clearTenantCache } from '@/utils/tenantCache'; import { useTheme } from '@/theme'; import { ThemeSwitch } from '@/components/ui'; type MenuNode = { id?: number | string; name?: string; url?: string; path?: string; css?: string; children?: MenuNode[]; subMenus?: MenuNode[]; }; const BasicLayout: React.FC = (props: any) => { const location = useLocation(); const { mode, toggle } = useTheme(); const [menus, setMenus] = useState([]); const [loaded, setLoaded] = useState(false); useEffect(() => { let mounted = true; (async () => { try { const list = await fetchMenuData(); if (mounted) { setMenus(Array.isArray(list) ? (list as MenuNode[]) : []); setLoaded(true); } } catch { if (mounted) setLoaded(true); } })(); return () => { mounted = false; }; }, []); const menuDataRender = useMemo(() => { const mapPath = (m: MenuNode, childrenLen: number): string | undefined => { const cand = (m.path || m.url || '').trim(); if (!cand) return undefined; const lower = cand.toLowerCase(); if (lower.startsWith('http')) return cand; // 澶栭摼 if (lower.endsWith('.html')) return cand.startsWith('/') ? cand : `/${cand}`; // 闈欐€佽矾鐢? if (lower === '/welcome' || lower === 'welcome') return '/welcome'; // 鍏煎鍚庣鑿滃崟鏈甫 .html 鐨勫彾瀛愯矾鐢憋紝渚嬪 /system/tenant => /system/tenant.html const ensureSlash = cand.startsWith('/') ? cand : `/${cand}`; const parts = ensureSlash.split('/').filter(Boolean); if (childrenLen === 0 && parts.length >= 2) { return `${ensureSlash}.html`; } return undefined; // 浣滀负鍒嗙粍 }; const getIcon = (p?: string) => { const s = (p || '').toLowerCase(); if (s.includes('system')) return ; if (s.includes('order')) return ; if (s.includes('device')) return ; if (s.includes('log')) return ; if (s.includes('search')) return ; if (s.includes('message')) return ; if (s.includes('file')) return ; if (s.includes('user')) return ; if (s.includes('welcome')) return ; return ; }; const normalize = (menu: MenuNode, index: number, parentKey = ''): MenuDataItem => { const stableKey = menu.id || (menu.url && menu.url !== 'javascript:;' ? menu.url : '') || (menu.path && menu.path !== 'javascript:;' ? menu.path : '') || `${parentKey}-${menu.name}-${index}`; const keyStr = String(stableKey); const childrenList: MenuNode[] = (menu.children && menu.children.length ? menu.children : (menu.subMenus || [])) || []; const path = mapPath(menu, childrenList.length); const item: MenuDataItem = { key: keyStr, name: menu.name || `保存中${index + 1}`, path, icon: getIcon(path), children: childrenList.map((child: MenuNode, i: number) => normalize(child, i, keyStr)), }; return item; }; if (!loaded) return []; const list = menus && menus.length ? menus : [{ name: '首页', url: '/welcome', id: 'welcome-default' }]; const rendered = list.map((m, i) => normalize(m, i)); const hasHome = rendered.some((r) => r.path === '/welcome'); if (!hasHome) { rendered.unshift({ key: 'welcome-fixed', name: '首页', path: '/welcome', icon: }); } return rendered; }, [menus, mode, loaded]); const rightContentRender = () => ( {首页} , label: '个人中心' }, { key: 'settings', icon: , label: '设置' }, { type: 'divider' as any }, { key: 'logout', icon: , label: '退出登录', onClick: async () => { try { await outLogin(); } catch {} try { sessionStorage.removeItem('access_token'); sessionStorage.removeItem('current_user'); clearTenantCache(); } catch {} message.success('已退出登录'); history.push('/user/login'); }, }, ], }} placement="bottomRight" >
} /> 管理员
); return (
menuDataRender} menuItemRender={(menuItemProps, defaultDom) => { if (menuItemProps.isUrl || !menuItemProps.path) return defaultDom; const path = String(menuItemProps.path); if (path === 'javascript:;' || path === '#' || path.startsWith('menu-item-')) { return {defaultDom}; } return ( {defaultDom} ); }} headerTitleRender={(logo, title) => (
G
{title}
)} token={{ sider: { colorMenuBackground: 'var(--sider-bg)', colorBgMenuItemSelected: 'var(--primary-color)', colorTextMenuSelected: '#ffffff', colorTextMenu: 'var(--text-secondary)', colorBgMenuItemHover: 'var(--bg-glass)', }, header: { colorBgHeader: 'var(--header-bg)', colorHeaderTitle: 'var(--text-primary)', colorTextRightActionsItem: 'var(--text-secondary)', }, }} >
{props.children}
); }; export default BasicLayout;