组织架构优化
This commit is contained in:
@@ -1,166 +1,160 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import React, { useState } from 'react';
|
||||
import {
|
||||
Table,
|
||||
Button,
|
||||
Modal,
|
||||
Form,
|
||||
Input,
|
||||
Select,
|
||||
TreeSelect,
|
||||
Space,
|
||||
Popconfirm,
|
||||
message,
|
||||
Card,
|
||||
Typography
|
||||
Table, Button, Typography, Space, Modal, Form, Tree, message,
|
||||
Input, Popconfirm, Select
|
||||
} from 'antd';
|
||||
import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons';
|
||||
import type { MenuItem, RoleItem } from '@/types/menu';
|
||||
import {
|
||||
PlusOutlined, EditOutlined, DeleteOutlined, SettingOutlined
|
||||
} from '@ant-design/icons';
|
||||
|
||||
const { Title } = Typography;
|
||||
const { Option } = Select;
|
||||
|
||||
// 模拟系统角色
|
||||
const roleOptions: RoleItem[] = [
|
||||
{ label: '系统管理员', value: 'admin' },
|
||||
{ label: '操作员', value: 'operator' },
|
||||
{ label: '观察员', value: 'viewer' },
|
||||
];
|
||||
|
||||
// 模拟菜单数据
|
||||
const mockMenuData: MenuItem[] = [
|
||||
// 1. 完整菜单树
|
||||
const menuData = [
|
||||
{
|
||||
id: '1',
|
||||
name: '控制台',
|
||||
path: '/',
|
||||
parentId: null,
|
||||
sort: 1,
|
||||
roles: ['admin', 'operator', 'viewer'],
|
||||
children: [],
|
||||
id: 1,
|
||||
title: '控制台',
|
||||
path: '/dashboard',
|
||||
parentId: 0,
|
||||
children: [
|
||||
{ id: 11, title: '概览', path: '/dashboard/overview', parentId: 1 },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
name: '设备管理',
|
||||
path: '/devices',
|
||||
parentId: null,
|
||||
sort: 2,
|
||||
roles: ['admin', 'operator'],
|
||||
children: [],
|
||||
id: 2,
|
||||
title: '设备管理',
|
||||
path: '/device',
|
||||
parentId: 0,
|
||||
children: [
|
||||
{ id: 21, title: '机器人管理', path: '/device/robot', parentId: 2 },
|
||||
{ id: 22, title: '视频监控', path: '/device/video', parentId: 2 },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: '3',
|
||||
name: '视频监控',
|
||||
path: '/video',
|
||||
parentId: '2',
|
||||
sort: 1,
|
||||
roles: ['admin', 'operator', 'viewer'],
|
||||
children: [],
|
||||
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 },
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
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);
|
||||
// 2. 角色列表
|
||||
const roleList = [
|
||||
{ name: '平台admin', key: 'admin' },
|
||||
{ name: '区域机构管理员', key: 'region_admin' },
|
||||
{ name: '单个场站管理员', key: 'site_admin' },
|
||||
{ name: '场站员工', key: 'site_staff' },
|
||||
];
|
||||
|
||||
// 加载菜单
|
||||
useEffect(() => {
|
||||
setLoading(true);
|
||||
setTimeout(() => {
|
||||
setMenuList(mockMenuData);
|
||||
setLoading(false);
|
||||
}, 300);
|
||||
}, []);
|
||||
|
||||
// 打开新增
|
||||
const handleAdd = () => {
|
||||
form.resetFields();
|
||||
setTitle('新增菜单');
|
||||
setCurrentRecord(null);
|
||||
setModalVisible(true);
|
||||
// 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);
|
||||
});
|
||||
};
|
||||
loop(menus);
|
||||
return arr;
|
||||
};
|
||||
|
||||
// 存储角色权限
|
||||
const rolePermissionMap = {};
|
||||
|
||||
export default function Roles() {
|
||||
const [menus, setMenus] = useState(flattenMenu(menuData));
|
||||
const [menuTree, setMenuTree] = useState(menuData);
|
||||
|
||||
// 新增/编辑菜单
|
||||
const [menuModal, setMenuModal] = useState(false);
|
||||
const [menuForm] = Form.useForm();
|
||||
const [editMenu, setEditMenu] = useState(null);
|
||||
|
||||
// 权限配置
|
||||
const [permModal, setPermModal] = useState(false);
|
||||
const [selectedRole, setSelectedRole] = useState('');
|
||||
const [checkedKeys, setCheckedKeys] = useState([]);
|
||||
|
||||
// 打开编辑
|
||||
const handleEdit = (record: MenuItem) => {
|
||||
form.setFieldsValue(record);
|
||||
setTitle('编辑菜单');
|
||||
setCurrentRecord(record);
|
||||
setModalVisible(true);
|
||||
const handleEdit = (record) => {
|
||||
setEditMenu(record);
|
||||
menuForm.setFieldsValue(record);
|
||||
setMenuModal(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) {
|
||||
// 保存菜单(新增+编辑)
|
||||
const handleSaveMenu = () => {
|
||||
menuForm.validateFields().then(values => {
|
||||
if (editMenu) {
|
||||
// 编辑
|
||||
setMenuList(prev => prev.map(m => m.id === currentRecord.id ? params : m));
|
||||
setMenus(menus.map(m => m.id === editMenu.id ? { ...m, ...values } : m));
|
||||
message.success('编辑成功');
|
||||
} else {
|
||||
// 新增
|
||||
setMenuList(prev => [...prev, params]);
|
||||
message.success('新增成功');
|
||||
const newMenu = {
|
||||
...values,
|
||||
id: Date.now(),
|
||||
parentId: 0,
|
||||
};
|
||||
setMenus([...menus, newMenu]);
|
||||
message.success('新增菜单成功');
|
||||
}
|
||||
setModalVisible(false);
|
||||
setMenuModal(false);
|
||||
});
|
||||
};
|
||||
|
||||
// 表格列
|
||||
// 删除菜单
|
||||
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);
|
||||
};
|
||||
|
||||
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: '菜单名称', dataIndex: 'title', key: 'title', width: 180 },
|
||||
{ title: '路由地址', dataIndex: 'fullPath', key: 'fullPath' },
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
width: 220,
|
||||
render: (_, record) => (
|
||||
<Space>
|
||||
<Button type="link" icon={<EditOutlined />} onClick={() => handleEdit(record)}>编辑</Button>
|
||||
<Button type="link" icon={<EditOutlined />} onClick={() => handleEdit(record)}>
|
||||
编辑
|
||||
</Button>
|
||||
<Popconfirm
|
||||
title="确定删除?"
|
||||
title="确定删除该菜单?"
|
||||
onConfirm={() => handleDelete(record.id)}
|
||||
>
|
||||
<Button type="link" danger icon={<DeleteOutlined />}>删除</Button>
|
||||
<Button type="link" danger icon={<DeleteOutlined />}>
|
||||
删除
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
),
|
||||
@@ -168,75 +162,87 @@ const MenuList: React.FC = () => {
|
||||
];
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<div className="flex justify-between mb-4">
|
||||
<Title level={5}>菜单管理</Title>
|
||||
<Button type="primary" icon={<PlusOutlined />} onClick={handleAdd}>新增菜单</Button>
|
||||
<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>
|
||||
</div>
|
||||
|
||||
<Table
|
||||
rowKey="id"
|
||||
loading={loading}
|
||||
columns={columns}
|
||||
dataSource={menuList}
|
||||
dataSource={menus}
|
||||
rowKey="id"
|
||||
pagination={false}
|
||||
bordered
|
||||
/>
|
||||
|
||||
{/* 菜单 新增/编辑 弹窗 */}
|
||||
<Modal
|
||||
title={title}
|
||||
open={modalVisible}
|
||||
onCancel={() => setModalVisible(false)}
|
||||
onOk={handleSubmit}
|
||||
maskClosable={false}
|
||||
title={editMenu ? '编辑菜单' : '新增菜单'}
|
||||
open={menuModal}
|
||||
onCancel={() => setMenuModal(false)}
|
||||
onOk={handleSaveMenu}
|
||||
width={500}
|
||||
>
|
||||
<Form form={form} layout="vertical">
|
||||
<Form.Item
|
||||
name="name"
|
||||
label="菜单名称"
|
||||
rules={[{ required: true, message: '请输入菜单名称' }]}
|
||||
>
|
||||
<Input placeholder="请输入" />
|
||||
<Form form={menuForm} layout="vertical">
|
||||
<Form.Item name="title" label="菜单名称" rules={[{ required: true }]}>
|
||||
<Input placeholder="请输入菜单名称" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
name="path"
|
||||
label="路由地址"
|
||||
rules={[{ required: true, message: '请输入路由' }]}
|
||||
>
|
||||
<Input placeholder="/devices" />
|
||||
<Form.Item name="path" label="路由地址" rules={[{ required: true }]}>
|
||||
<Input placeholder="/system/xxx" />
|
||||
</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;
|
||||
{/* 权限配置弹窗 */}
|
||||
<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>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user