import React, { useState } from 'react';
import {
Table, Button, Typography, Space, Modal, Form, Tree, message,
Input, Popconfirm, Select
} from 'antd';
import {
PlusOutlined, EditOutlined, DeleteOutlined, SettingOutlined
} from '@ant-design/icons';
const { Title } = Typography;
const { Option } = Select;
// 1. 完整菜单树
const menuData = [
{
id: 1,
title: '控制台',
path: '/dashboard',
parentId: 0,
children: [
{ id: 11, title: '概览', path: '/dashboard/overview', parentId: 1 },
],
},
{
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,
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 },
],
},
];
// 2. 角色列表
const roleList = [
{ name: '平台admin', key: 'admin' },
{ name: '区域机构管理员', key: 'region_admin' },
{ name: '单个场站管理员', key: 'site_admin' },
{ name: '场站员工', key: 'site_staff' },
];
// 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) => {
setEditMenu(record);
menuForm.setFieldsValue(record);
setMenuModal(true);
};
// 保存菜单(新增+编辑)
const handleSaveMenu = () => {
menuForm.validateFields().then(values => {
if (editMenu) {
// 编辑
setMenus(menus.map(m => m.id === editMenu.id ? { ...m, ...values } : m));
message.success('编辑成功');
} else {
// 新增
const newMenu = {
...values,
id: Date.now(),
parentId: 0,
};
setMenus([...menus, newMenu]);
message.success('新增菜单成功');
}
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: 'title', key: 'title', width: 180 },
{ title: '路由地址', dataIndex: 'fullPath', key: 'fullPath' },
{
title: '操作',
key: 'action',
width: 220,
render: (_, record) => (