243 lines
5.3 KiB
TypeScript
243 lines
5.3 KiB
TypeScript
import React, { useState, useEffect } from 'react';
|
|
import {
|
|
Table,
|
|
Button,
|
|
Modal,
|
|
Form,
|
|
Input,
|
|
Select,
|
|
TreeSelect,
|
|
Space,
|
|
Popconfirm,
|
|
message,
|
|
Card,
|
|
Typography
|
|
} from 'antd';
|
|
import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons';
|
|
import type { MenuItem, RoleItem } from '@/types/menu';
|
|
|
|
const { Title } = Typography;
|
|
const { Option } = Select;
|
|
|
|
// 模拟系统角色
|
|
const roleOptions: RoleItem[] = [
|
|
{ label: '系统管理员', value: 'admin' },
|
|
{ label: '操作员', value: 'operator' },
|
|
{ label: '观察员', value: 'viewer' },
|
|
];
|
|
|
|
// 模拟菜单数据
|
|
const mockMenuData: MenuItem[] = [
|
|
{
|
|
id: '1',
|
|
name: '控制台',
|
|
path: '/',
|
|
parentId: null,
|
|
sort: 1,
|
|
roles: ['admin', 'operator', 'viewer'],
|
|
children: [],
|
|
},
|
|
{
|
|
id: '2',
|
|
name: '设备管理',
|
|
path: '/devices',
|
|
parentId: null,
|
|
sort: 2,
|
|
roles: ['admin', 'operator'],
|
|
children: [],
|
|
},
|
|
{
|
|
id: '3',
|
|
name: '视频监控',
|
|
path: '/video',
|
|
parentId: '2',
|
|
sort: 1,
|
|
roles: ['admin', 'operator', 'viewer'],
|
|
children: [],
|
|
},
|
|
];
|
|
|
|
const MenuList: React.FC = () => {
|
|
const [form] = Form.useForm();
|
|
const [menuList, setMenuList] = useState<MenuItem[]>([]);
|
|
const [loading, setLoading] = useState(false);
|
|
const [modalVisible, setModalVisible] = useState(false);
|
|
const [title, setTitle] = useState('新增菜单');
|
|
const [currentRecord, setCurrentRecord] = useState<MenuItem | null>(null);
|
|
|
|
// 加载菜单
|
|
useEffect(() => {
|
|
setLoading(true);
|
|
setTimeout(() => {
|
|
setMenuList(mockMenuData);
|
|
setLoading(false);
|
|
}, 300);
|
|
}, []);
|
|
|
|
// 打开新增
|
|
const handleAdd = () => {
|
|
form.resetFields();
|
|
setTitle('新增菜单');
|
|
setCurrentRecord(null);
|
|
setModalVisible(true);
|
|
};
|
|
|
|
// 打开编辑
|
|
const handleEdit = (record: MenuItem) => {
|
|
form.setFieldsValue(record);
|
|
setTitle('编辑菜单');
|
|
setCurrentRecord(record);
|
|
setModalVisible(true);
|
|
};
|
|
|
|
// 删除
|
|
const handleDelete = (id: string) => {
|
|
setMenuList(prev => prev.filter(item => item.id !== id));
|
|
message.success('删除成功');
|
|
};
|
|
|
|
// 提交保存
|
|
const handleSubmit = () => {
|
|
form.validateFields().then(values => {
|
|
const params: MenuItem = {
|
|
...values,
|
|
id: currentRecord?.id || Date.now().toString(),
|
|
};
|
|
|
|
if (currentRecord) {
|
|
// 编辑
|
|
setMenuList(prev => prev.map(m => m.id === currentRecord.id ? params : m));
|
|
message.success('编辑成功');
|
|
} else {
|
|
// 新增
|
|
setMenuList(prev => [...prev, params]);
|
|
message.success('新增成功');
|
|
}
|
|
setModalVisible(false);
|
|
});
|
|
};
|
|
|
|
// 表格列
|
|
const columns = [
|
|
{
|
|
title: '菜单名称',
|
|
dataIndex: 'name',
|
|
key: 'name',
|
|
},
|
|
{
|
|
title: '路由地址',
|
|
dataIndex: 'path',
|
|
key: 'path',
|
|
},
|
|
{
|
|
title: '父级菜单',
|
|
key: 'parentName',
|
|
render: (_, record) => {
|
|
if (!record.parentId) return '一级菜单';
|
|
const p = menuList.find(m => m.id === record.parentId);
|
|
return p?.name || '-';
|
|
},
|
|
},
|
|
{
|
|
title: '关联角色',
|
|
key: 'roles',
|
|
render: (_, record) => (
|
|
<div>
|
|
{record.roles.map(r => {
|
|
const ro = roleOptions.find(o => o.value === r);
|
|
return <span key={r} style={{ marginRight: 8 }}>{ro?.label}</span>;
|
|
})}
|
|
</div>
|
|
),
|
|
},
|
|
{
|
|
title: '操作',
|
|
key: 'action',
|
|
render: (_, record) => (
|
|
<Space>
|
|
<Button type="link" icon={<EditOutlined />} onClick={() => handleEdit(record)}>编辑</Button>
|
|
<Popconfirm
|
|
title="确定删除?"
|
|
onConfirm={() => handleDelete(record.id)}
|
|
>
|
|
<Button type="link" danger icon={<DeleteOutlined />}>删除</Button>
|
|
</Popconfirm>
|
|
</Space>
|
|
),
|
|
},
|
|
];
|
|
|
|
return (
|
|
<Card>
|
|
<div className="flex justify-between mb-4">
|
|
<Title level={5}>菜单管理</Title>
|
|
<Button type="primary" icon={<PlusOutlined />} onClick={handleAdd}>新增菜单</Button>
|
|
</div>
|
|
|
|
<Table
|
|
rowKey="id"
|
|
loading={loading}
|
|
columns={columns}
|
|
dataSource={menuList}
|
|
pagination={false}
|
|
/>
|
|
|
|
<Modal
|
|
title={title}
|
|
open={modalVisible}
|
|
onCancel={() => setModalVisible(false)}
|
|
onOk={handleSubmit}
|
|
maskClosable={false}
|
|
>
|
|
<Form form={form} layout="vertical">
|
|
<Form.Item
|
|
name="name"
|
|
label="菜单名称"
|
|
rules={[{ required: true, message: '请输入菜单名称' }]}
|
|
>
|
|
<Input placeholder="请输入" />
|
|
</Form.Item>
|
|
|
|
<Form.Item
|
|
name="path"
|
|
label="路由地址"
|
|
rules={[{ required: true, message: '请输入路由' }]}
|
|
>
|
|
<Input placeholder="/devices" />
|
|
</Form.Item>
|
|
|
|
<Form.Item name="parentId" label="上级菜单">
|
|
<TreeSelect
|
|
placeholder="请选择"
|
|
allowClear
|
|
treeData={menuList.map(m => ({
|
|
title: m.name,
|
|
value: m.id,
|
|
disabled: m.id === currentRecord?.id,
|
|
}))}
|
|
/>
|
|
</Form.Item>
|
|
|
|
<Form.Item
|
|
name="roles"
|
|
label="关联角色"
|
|
rules={[{ required: true, message: '请选择至少一个角色' }]}
|
|
>
|
|
<Select mode="multiple" placeholder="请选择">
|
|
{roleOptions.map(o => (
|
|
<Option key={o.value} value={o.value}>{o.label}</Option>
|
|
))}
|
|
</Select>
|
|
</Form.Item>
|
|
|
|
<Form.Item name="sort" label="排序号">
|
|
<Input type="number" placeholder="数字越小越靠前" />
|
|
</Form.Item>
|
|
</Form>
|
|
</Modal>
|
|
</Card>
|
|
);
|
|
};
|
|
|
|
export default MenuList;
|