// 全局布局组件 - 包含左侧菜单和头部 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, useNavigate, Outlet } from 'react-router-dom'; import type { MenuProps } from 'antd'; const { Header, Sider, Content } = Layout; const { Title, Text } = Typography; type MenuItem = MenuProps['items'][number]; // 菜单项配置 const menuItems: MenuItem[] = [ // 核心业务 { 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: MenuItem[] = [ { key: 'profile', label: '个人中心', }, { key: 'settings', label: '账号设置', }, { key: 'divider-1', type: 'divider', }, { key: 'logout', label: '退出登录', }, ]; const MainLayout: React.FC = () => { const location = useLocation(); const navigate = useNavigate(); const [collapsed, setCollapsed] = useState(false); const [selectedKeys, setSelectedKeys] = useState(['/']); // 根据当前路径设置选中的菜单项 useEffect(() => { const pathname = location.pathname; const findKey = (items: MenuItem[]): string | undefined => { for (const item of items) { if (item && 'key' in item && typeof item.key === 'string') { if (item.key.startsWith('/') && pathname.startsWith(item.key)) { return item.key; } if ('children' in item && Array.isArray(item.children)) { const found = findKey(item.children as MenuItem[]); if (found) return found; } } } return undefined; }; const matchedKey = findKey(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'); navigate('/Auth/LoginPage'); } else if (key === 'profile') { navigate('/Settings/ProfileSettings'); } else if (key === 'settings') { navigate('/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;