import React, { useState } from 'react'; import { Layout, Menu, Avatar, Dropdown, Space } from 'antd'; import { useNavigate, useLocation } from 'react-router-dom'; import { useSelector, useDispatch } from 'react-redux'; import { DashboardOutlined, GiftOutlined, CalendarOutlined, TeamOutlined, BarChartOutlined, SettingOutlined, UserOutlined, LogoutOutlined, MenuFoldOutlined, MenuUnfoldOutlined, } from '@ant-design/icons'; import { logout } from '../store/slices/authSlice'; const { Header, Sider, Content } = Layout; const LayoutComponent = ({ children }) => { const [collapsed, setCollapsed] = useState(false); const [openKeys, setOpenKeys] = useState([]); const navigate = useNavigate(); const location = useLocation(); const dispatch = useDispatch(); const { user } = useSelector((state) => state.auth); const menuItems = [ { key: '/dashboard', icon: , label: '主控制台', }, { key: '/points/rules', icon: , label: '申报项目设置', }, { key: '/activities', icon: , label: '活动列表', }, { key: '/volunteers', icon: , label: '志愿者列表', }, { key: '/volunteers/points', icon: , label: '积分查询', }, { key: '/statistics/overview', icon: , label: '数据概览', }, { key: 'system', icon: , label: '系统管理', children: [ { key: '/system/admins', icon: , label: '管理员管理', }, { key: '/system/users', icon: , label: '用户管理', }, { key: '/system/permissions', icon: , label: '权限管理', }, { key: '/system/settings', icon: , label: '系统设置', }, ], }, ]; const handleMenuClick = ({ key }) => { navigate(key); }; const handleLogout = () => { dispatch(logout()); navigate('/login'); }; const userMenuItems = [ { key: 'profile', icon: , label: '个人资料', }, { type: 'divider', }, { key: 'logout', icon: , label: '退出登录', onClick: handleLogout, }, ]; const getSelectedKeys = () => { const pathname = location.pathname; return [pathname]; }; const handleOpenChange = (keys) => { setOpenKeys(keys); }; // 当路由变化时,自动展开对应的菜单 React.useEffect(() => { const pathname = location.pathname; if (pathname.startsWith('/system')) { setOpenKeys(['system']); } }, [location.pathname]); return (
{collapsed ? '志愿' : '志愿者管理系统'}
{React.createElement(collapsed ? MenuUnfoldOutlined : MenuFoldOutlined, { className: 'trigger', onClick: () => setCollapsed(!collapsed), style: { fontSize: '18px', padding: '0 24px', cursor: 'pointer' }, })}
} /> {user?.name || '管理员'}
{children}
); }; export default LayoutComponent;