import React, { useState, useEffect } 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' }, ]; // 扁平化菜单 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; }; // 转树形结构(用于Tree组件) const buildTree = (flatten) => { const map = {}; const tree = []; flatten.forEach(item => { map[item.id] = { ...item, children: [] }; }); flatten.forEach(item => { if (item.parentId === 0) { tree.push(map[item.id]); } else { if (map[item.parentId]) { map[item.parentId].children.push(map[item.id]); } } }); return tree; }; // 存储角色权限 const rolePermissionMap = {}; export default function Roles() { const [menus, setMenus] = useState(flattenMenu(menuData)); const [menuTree, setMenuTree] = useState(buildTree(flattenMenu(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 menuType = Form.useWatch('menuType', menuForm); // 每次菜单变化,重新生成树形 useEffect(() => { setMenuTree(buildTree(menus)); }, [menus]); // 打开编辑 const handleEdit = (record) => { setEditMenu(record); menuForm.setFieldsValue({ ...record, menuType: record.parentId === 0 ? 'top' : 'sub', }); setMenuModal(true); }; // 保存菜单(新增+编辑) const handleSaveMenu = () => { menuForm.validateFields().then(values => { const { menuType, parentId, title, path } = values; if (editMenu) { // 编辑 setMenus(menus.map(m => m.id === editMenu.id ? { ...m, title, path, parentId: menuType === 'top' ? 0 : Number(parentId) } : m )); message.success('编辑成功'); } else { // 新增 const newMenu = { id: Date.now(), title, path, parentId: menuType === 'top' ? 0 : Number(parentId), }; 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 topMenus = menus.filter(m => m.parentId === 0); const columns = [ { title: '菜单名称', dataIndex: 'title', key: 'title', width: 180 }, { title: '路由地址', dataIndex: 'fullPath', key: 'fullPath' }, { title: '操作', key: 'action', width: 220, render: (_, record) => ( handleDelete(record.id)} > ), }, ]; return (
菜单权限管理
{/* 菜单 新增/编辑 弹窗 */} setMenuModal(false)} onOk={handleSaveMenu} width={500} >
{/* 二级菜单才显示父级选择 */} {menuType === 'sub' && ( )}
{/* 权限配置弹窗 */} setPermModal(false)} onOk={handlePermSave} > ); }