// 全局布局组件 - 包含左侧菜单和头部 import React, { useState, useEffect } from 'react'; import { Layout, Menu, Typography, Avatar, Dropdown, Badge, Space } from 'antd'; import { DashboardOutlined, ShoppingOutlined, FileTextOutlined, UserOutlined, TruckOutlined, AlertOutlined, AuditOutlined, DollarOutlined, BarChartOutlined, SettingOutlined, TeamOutlined, MenuFoldOutlined, MenuUnfoldOutlined, BellOutlined, DownOutlined, ShopOutlined, ScheduleOutlined, WalletOutlined, SafetyCertificateOutlined, GlobalOutlined, TrophyOutlined, RobotOutlined, SwapOutlined, ThunderboltOutlined, } from '@ant-design/icons'; import { Link, useLocation, history, Outlet } from 'umi'; const { Header, Sider, Content } = Layout; const { Title, Text } = Typography; // 菜单项配置 const menuItems = [ // 核心业务 { key: 'core', icon: , label: '核心业务', children: [ { key: '/Product', label: 商品管理, }, { key: '/Orders', label: 订单管理, }, { key: '/Logistics', label: 物流管理, }, { key: '/AfterSales', label: 售后服务, }, ], }, // 商户与贸易 { key: 'merchant', icon: , label: '商户与贸易', children: [ { key: '/Merchant', label: 商户管理, }, { key: '/B2B', label: B2B贸易, }, ], }, // 营销与增长 { key: 'marketing', icon: , label: '营销与增长', children: [ { key: '/Ad', label: 广告管理, }, { key: '/DynamicPricing', label: 动态定价, }, { key: '/StrategyMarketplace', label: 策略市场, }, ], }, // 数据分析 { key: 'analytics', icon: , label: '数据分析', children: [ { key: '/Analytics', label: 数据分析, }, { key: '/Leaderboard', label: 收益排行榜, }, { key: '/ArbitrageMonitor', label: 套利监控, }, ], }, // 智能运营 { key: 'ai', icon: , label: '智能运营', children: [ { key: '/AutoPilot', label: AI托管, }, { key: '/TaskCenter', label: 任务中心, }, ], }, // 合规与安全 { key: 'compliance', icon: , label: '合规与安全', children: [ { key: '/Compliance', label: 合规管理, }, { key: '/Blacklist', label: 黑名单管理, }, ], }, // 财务管理 { key: '/Finance', icon: , label: 财务管理, }, // 系统管理 { key: 'system', icon: , label: '系统管理', children: [ { key: '/User', label: 用户管理, }, { key: '/Role', label: 角色管理, }, { key: '/Settings', label: 系统设置, }, ], }, ]; // 用户菜单项 const userMenuItems = [ { key: 'profile', label: '个人中心', }, { key: 'settings', label: '账号设置', }, { type: 'divider', }, { key: 'logout', label: '退出登录', }, ]; const MainLayout: React.FC = () => { const location = useLocation(); const [collapsed, setCollapsed] = useState(false); const [selectedKeys, setSelectedKeys] = useState(['/']); // 根据当前路径设置选中的菜单项 useEffect(() => { const pathname = location.pathname; // 找到匹配的菜单项 let matchedKey: string | undefined; // 递归查找匹配的菜单项 const findMatchedItem = (items: any[]) => { for (const item of items) { if (item.children) { // 检查子菜单项 for (const child of item.children) { if (child.key === '/') { if (pathname === '/') { matchedKey = child.key; return true; } } else if (pathname.startsWith(child.key as string)) { matchedKey = child.key; return true; } } // 递归检查子菜单 if (findMatchedItem(item.children)) { return true; } } else { // 检查当前菜单项 if (item.key === '/') { if (pathname === '/') { matchedKey = item.key; return true; } } else if (pathname.startsWith(item.key as string)) { matchedKey = item.key; return true; } } } return false; }; findMatchedItem(menuItems); if (matchedKey) { setSelectedKeys([matchedKey]); } }, [location.pathname]); // 从 localStorage 读取菜单折叠状态 useEffect(() => { const savedCollapsed = localStorage.getItem('sidebar_collapsed'); if (savedCollapsed !== null) { setCollapsed(savedCollapsed === 'true'); } }, []); // 保存菜单折叠状态到 localStorage const handleCollapse = (value: boolean) => { setCollapsed(value); localStorage.setItem('sidebar_collapsed', String(value)); }; const handleUserMenuClick = ({ key }: { key: string }) => { if (key === 'logout') { // 处理登出逻辑 localStorage.removeItem('token'); history.push('/Auth/LoginPage'); } else if (key === 'profile') { history.push('/Settings/ProfileSettings'); } else if (key === 'settings') { history.push('/Settings'); } }; return ( {/* 左侧菜单栏 */} {/* Logo 区域 */} {collapsed ? ( C ) : ( Crawlful Hub )} {/* 菜单 */} {/* 右侧内容区域 */} {/* 顶部Header */} {/* 左侧:折叠按钮 */} handleCollapse(!collapsed)} onMouseEnter={(e) => { e.currentTarget.style.background = '#f0f0f0'; }} onMouseLeave={(e) => { e.currentTarget.style.background = 'transparent'; }} > {collapsed ? : } {/* 右侧:通知和用户 */} {/* 通知图标 */} {/* 用户下拉菜单 */} } /> {!collapsed && ( <> 管理员 > )} {/* 主内容区域 */} ); }; export default MainLayout;