Files
web-Iot/src/pages/MenuList.tsx

249 lines
6.0 KiB
TypeScript
Raw Normal View History

2026-04-24 15:28:16 +08:00
import React, { useState } from 'react';
2026-04-22 09:43:55 +08:00
import {
2026-04-24 15:28:16 +08:00
Table, Button, Typography, Space, Modal, Form, Tree, message,
Input, Popconfirm, Select
2026-04-22 09:43:55 +08:00
} from 'antd';
2026-04-24 15:28:16 +08:00
import {
PlusOutlined, EditOutlined, DeleteOutlined, SettingOutlined
} from '@ant-design/icons';
2026-04-22 09:43:55 +08:00
const { Title } = Typography;
const { Option } = Select;
2026-04-24 15:28:16 +08:00
// 1. 完整菜单树
const menuData = [
2026-04-22 09:43:55 +08:00
{
2026-04-24 15:28:16 +08:00
id: 1,
title: '控制台',
path: '/dashboard',
parentId: 0,
children: [
{ id: 11, title: '概览', path: '/dashboard/overview', parentId: 1 },
],
2026-04-22 09:43:55 +08:00
},
{
2026-04-24 15:28:16 +08:00
id: 2,
title: '设备管理',
path: '/device',
parentId: 0,
children: [
{ id: 21, title: '机器人管理', path: '/device/robot', parentId: 2 },
{ id: 22, title: '视频监控', path: '/device/video', parentId: 2 },
],
2026-04-22 09:43:55 +08:00
},
{
2026-04-24 15:28:16 +08:00
id: 3,
title: '系统管理',
path: '/system',
parentId: 0,
children: [
{ id: 31, title: '用户管理', path: '/system/user', parentId: 3 },
{ id: 32, title: '角色管理', path: '/system/role', parentId: 3 },
{ id: 33, title: '菜单管理', path: '/system/menu', parentId: 3 },
],
2026-04-22 09:43:55 +08:00
},
];
2026-04-24 15:28:16 +08:00
// 2. 角色列表
const roleList = [
{ name: '平台admin', key: 'admin' },
{ name: '区域机构管理员', key: 'region_admin' },
{ name: '单个场站管理员', key: 'site_admin' },
{ name: '场站员工', key: 'site_staff' },
];
2026-04-22 09:43:55 +08:00
2026-04-24 15:28:16 +08:00
// 3. 扁平化菜单
const flattenMenu = (menus) => {
let arr = [];
const loop = (list, parentPath = '') => {
list.forEach(item => {
const fullPath = parentPath ? `${parentPath}/${item.path}` : item.path;
arr.push({ ...item, fullPath });
if (item.children?.length) loop(item.children, item.path);
});
2026-04-22 09:43:55 +08:00
};
2026-04-24 15:28:16 +08:00
loop(menus);
return arr;
};
2026-04-22 09:43:55 +08:00
2026-04-24 15:28:16 +08:00
// 存储角色权限
const rolePermissionMap = {};
export default function Roles() {
const [menus, setMenus] = useState(flattenMenu(menuData));
const [menuTree, setMenuTree] = useState(menuData);
2026-04-22 09:43:55 +08:00
2026-04-24 15:28:16 +08:00
// 新增/编辑菜单
const [menuModal, setMenuModal] = useState(false);
const [menuForm] = Form.useForm();
const [editMenu, setEditMenu] = useState(null);
2026-04-22 09:43:55 +08:00
2026-04-24 15:28:16 +08:00
// 权限配置
const [permModal, setPermModal] = useState(false);
const [selectedRole, setSelectedRole] = useState('');
const [checkedKeys, setCheckedKeys] = useState([]);
// 打开编辑
const handleEdit = (record) => {
setEditMenu(record);
menuForm.setFieldsValue(record);
setMenuModal(true);
};
// 保存菜单(新增+编辑)
const handleSaveMenu = () => {
menuForm.validateFields().then(values => {
if (editMenu) {
2026-04-22 09:43:55 +08:00
// 编辑
2026-04-24 15:28:16 +08:00
setMenus(menus.map(m => m.id === editMenu.id ? { ...m, ...values } : m));
2026-04-22 09:43:55 +08:00
message.success('编辑成功');
} else {
// 新增
2026-04-24 15:28:16 +08:00
const newMenu = {
...values,
id: Date.now(),
parentId: 0,
};
setMenus([...menus, newMenu]);
message.success('新增菜单成功');
2026-04-22 09:43:55 +08:00
}
2026-04-24 15:28:16 +08:00
setMenuModal(false);
2026-04-22 09:43:55 +08:00
});
};
2026-04-24 15:28:16 +08:00
// 删除菜单
const handleDelete = (id) => {
setMenus(menus.filter(m => m.id !== id));
message.success('删除成功');
};
// 打开权限配置
const handlePermOpen = () => {
if (!selectedRole) {
message.warning('请先选择角色');
return;
}
// 回显已保存权限
const saved = rolePermissionMap[selectedRole] || [];
setCheckedKeys(saved);
setPermModal(true);
};
// 保存权限
const handlePermSave = () => {
rolePermissionMap[selectedRole] = checkedKeys;
message.success(`【${selectedRole}】权限保存成功`);
setPermModal(false);
};
2026-04-22 09:43:55 +08:00
const columns = [
2026-04-24 15:28:16 +08:00
{ title: '菜单名称', dataIndex: 'title', key: 'title', width: 180 },
{ title: '路由地址', dataIndex: 'fullPath', key: 'fullPath' },
2026-04-22 09:43:55 +08:00
{
title: '操作',
key: 'action',
2026-04-24 15:28:16 +08:00
width: 220,
2026-04-22 09:43:55 +08:00
render: (_, record) => (
<Space>
2026-04-24 15:28:16 +08:00
<Button type="link" icon={<EditOutlined />} onClick={() => handleEdit(record)}>
编辑
</Button>
2026-04-22 09:43:55 +08:00
<Popconfirm
2026-04-24 15:28:16 +08:00
title="确定删除该菜单?"
2026-04-22 09:43:55 +08:00
onConfirm={() => handleDelete(record.id)}
>
2026-04-24 15:28:16 +08:00
<Button type="link" danger icon={<DeleteOutlined />}>
删除
</Button>
2026-04-22 09:43:55 +08:00
</Popconfirm>
</Space>
),
},
];
return (
2026-04-24 15:28:16 +08:00
<div className="space-y-4">
<div className="flex justify-between items-center">
<Title level={5} style={{ margin: 0 }}>菜单权限管理</Title>
<Space>
<Select
placeholder="选择角色"
style={{ width: 180 }}
value={selectedRole}
onChange={setSelectedRole}
>
{roleList.map(r => (
<Option key={r.key} value={r.key}>{r.name}</Option>
))}
</Select>
<Button
type="primary"
icon={<SettingOutlined />}
onClick={handlePermOpen}
>
配置权限
</Button>
<Button
type="primary"
icon={<PlusOutlined />}
onClick={() => {
setEditMenu(null);
menuForm.resetFields();
setMenuModal(true);
}}
>
新增菜单
</Button>
</Space>
2026-04-22 09:43:55 +08:00
</div>
<Table
columns={columns}
2026-04-24 15:28:16 +08:00
dataSource={menus}
rowKey="id"
2026-04-22 09:43:55 +08:00
pagination={false}
2026-04-24 15:28:16 +08:00
bordered
2026-04-22 09:43:55 +08:00
/>
2026-04-24 15:28:16 +08:00
{/* 菜单 新增/编辑 弹窗 */}
2026-04-22 09:43:55 +08:00
<Modal
2026-04-24 15:28:16 +08:00
title={editMenu ? '编辑菜单' : '新增菜单'}
open={menuModal}
onCancel={() => setMenuModal(false)}
onOk={handleSaveMenu}
width={500}
2026-04-22 09:43:55 +08:00
>
2026-04-24 15:28:16 +08:00
<Form form={menuForm} layout="vertical">
<Form.Item name="title" label="菜单名称" rules={[{ required: true }]}>
<Input placeholder="请输入菜单名称" />
2026-04-22 09:43:55 +08:00
</Form.Item>
2026-04-24 15:28:16 +08:00
<Form.Item name="path" label="路由地址" rules={[{ required: true }]}>
<Input placeholder="/system/xxx" />
2026-04-22 09:43:55 +08:00
</Form.Item>
<Form.Item name="sort" label="排序号">
<Input type="number" placeholder="数字越小越靠前" />
</Form.Item>
</Form>
</Modal>
2026-04-24 15:28:16 +08:00
{/* 权限配置弹窗 */}
<Modal
title={`【${selectedRole}】菜单权限配置`}
open={permModal}
width={520}
onCancel={() => setPermModal(false)}
onOk={handlePermSave}
>
<Tree
checkable
treeData={menuTree}
checkedKeys={checkedKeys}
onCheck={setCheckedKeys}
defaultExpandAll
fieldNames={{ title: 'title', key: 'id', children: 'children' }}
/>
</Modal>
</div>
);
}