海口龙华志愿者后端管理页面
zhouxiantao
8 days ago 4def10e6567cf651ae333a2a65c497e717c06403
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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: <DashboardOutlined />,
      label: '主控制台',
    },
    {
      key: '/points/rules',
      icon: <GiftOutlined />,
      label: '申报项目设置',
    },
    {
      key: '/activities',
      icon: <CalendarOutlined />,
      label: '活动列表',
    },
    {
      key: '/volunteers',
      icon: <TeamOutlined />,
      label: '志愿者列表',
    },
    {
      key: '/volunteers/points',
      icon: <TeamOutlined />,
      label: '积分查询',
    },
    {
      key: '/statistics/overview',
      icon: <BarChartOutlined />,
      label: '数据概览',
    },
    {
      key: 'system',
      icon: <SettingOutlined />,
      label: '系统管理',
      children: [
        {
          key: '/system/admins',
          icon: <UserOutlined />,
          label: '管理员管理',
        },
        {
          key: '/system/users',
          icon: <UserOutlined />,
          label: '用户管理',
        },
        {
          key: '/system/permissions',
          icon: <SettingOutlined />,
          label: '权限管理',
        },
        {
          key: '/system/settings',
          icon: <SettingOutlined />,
          label: '系统设置',
        },
      ],
    },
  ];
 
  const handleMenuClick = ({ key }) => {
    navigate(key);
  };
 
  const handleLogout = () => {
    dispatch(logout());
    navigate('/login');
  };
 
  const userMenuItems = [
    {
      key: 'profile',
      icon: <UserOutlined />,
      label: '个人资料',
    },
    {
      type: 'divider',
    },
    {
      key: 'logout',
      icon: <LogoutOutlined />,
      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 (
    <Layout style={{ minHeight: '100vh' }}>
      <Sider trigger={null} collapsible collapsed={collapsed} theme="dark">
        <div className="logo">
          {collapsed ? '志愿' : '志愿者管理系统'}
        </div>
        <Menu
          theme="dark"
          mode="inline"
          selectedKeys={getSelectedKeys()}
          openKeys={openKeys}
          onOpenChange={handleOpenChange}
          items={menuItems}
          onClick={handleMenuClick}
        />
      </Sider>
      <Layout>
        <Header style={{ padding: 0, background: '#fff', display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
          <div style={{ display: 'flex', alignItems: 'center' }}>
            {React.createElement(collapsed ? MenuUnfoldOutlined : MenuFoldOutlined, {
              className: 'trigger',
              onClick: () => setCollapsed(!collapsed),
              style: { fontSize: '18px', padding: '0 24px', cursor: 'pointer' },
            })}
          </div>
          <div style={{ marginRight: 24 }}>
            <Dropdown menu={{ items: userMenuItems }} placement="bottomRight">
              <Space style={{ cursor: 'pointer' }}>
                <Avatar icon={<UserOutlined />} />
                <span>{user?.name || '管理员'}</span>
              </Space>
            </Dropdown>
          </div>
        </Header>
        <Content className="content-layout">
          {children}
        </Content>
      </Layout>
    </Layout>
  );
};
 
export default LayoutComponent;