菜单模块增删改查
This commit is contained in:
@@ -1,50 +1,16 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import {
|
||||
Table, Button, Typography, Space, Modal, Form, Tree, message,
|
||||
Input, Popconfirm, Select
|
||||
Table, Button, Space, Modal, Form, Tree, message,
|
||||
Input, Popconfirm, Select, Card, InputNumber
|
||||
} from 'antd';
|
||||
import {
|
||||
PlusOutlined, EditOutlined, DeleteOutlined, SettingOutlined
|
||||
} from '@ant-design/icons';
|
||||
import { addMenu, deleteMenu, getMenuList, updateMenu } from '../api/mune';
|
||||
|
||||
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' },
|
||||
@@ -52,152 +18,176 @@ const roleList = [
|
||||
{ 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 buildTreeFromFlat = (data) => {
|
||||
const map = {};
|
||||
const tree = [];
|
||||
flatten.forEach(item => {
|
||||
map[item.id] = { ...item, children: [] };
|
||||
|
||||
data.forEach(item => {
|
||||
map[item.menuId] = { ...item, children: [] };
|
||||
});
|
||||
flatten.forEach(item => {
|
||||
if (item.parentId === 0) {
|
||||
tree.push(map[item.id]);
|
||||
|
||||
data.forEach(item => {
|
||||
if (item.parentId === 0 || !item.parentId) {
|
||||
tree.push(map[item.menuId]);
|
||||
} else {
|
||||
if (map[item.parentId]) {
|
||||
map[item.parentId].children.push(map[item.id]);
|
||||
map[item.parentId].children.push(map[item.menuId]);
|
||||
}
|
||||
}
|
||||
});
|
||||
return tree;
|
||||
};
|
||||
|
||||
// 存储角色权限
|
||||
// 菜单数据转 Tree组件 格式
|
||||
const buildPermTree = (treeData) => {
|
||||
const loop = (list) => {
|
||||
return list.map(item => {
|
||||
const node = {
|
||||
title: item.menuName,
|
||||
key: item.menuId,
|
||||
};
|
||||
if (item.children && item.children.length) {
|
||||
node.children = loop(item.children);
|
||||
}
|
||||
return node;
|
||||
});
|
||||
};
|
||||
return loop(treeData);
|
||||
};
|
||||
|
||||
const rolePermissionMap = {};
|
||||
|
||||
export default function Roles() {
|
||||
const [menus, setMenus] = useState(flattenMenu(menuData));
|
||||
const [menuTree, setMenuTree] = useState(buildTree(flattenMenu(menuData)));
|
||||
export default function MenuList() {
|
||||
const [rawMenuList, setRawMenuList] = useState([]);
|
||||
const [menuTreeData, setMenuTreeData] = useState([]);
|
||||
const [menuTree, setMenuTree] = useState([]);
|
||||
|
||||
// 新增/编辑菜单
|
||||
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 [messageApi, contextHolder] = message.useMessage();
|
||||
|
||||
// 菜单类型:一级/二级
|
||||
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',
|
||||
// 获取菜单
|
||||
const getMenuListApi = () => {
|
||||
getMenuList().then(res => {
|
||||
if (res.code === 200) {
|
||||
const data = res.data || [];
|
||||
setRawMenuList(data);
|
||||
// 扁平转树形 → 表格树形 & 权限树
|
||||
const tree = buildTreeFromFlat(data);
|
||||
setMenuTreeData(tree);
|
||||
setMenuTree(buildPermTree(tree));
|
||||
}
|
||||
}).catch(() => {
|
||||
messageApi.error('菜单加载失败');
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
getMenuListApi();
|
||||
}, []);
|
||||
|
||||
// 编辑
|
||||
const handleEdit = (record) => {
|
||||
console.log(record, "values11")
|
||||
|
||||
setEditMenu(record);
|
||||
menuForm.setFieldsValue({ ...record });
|
||||
setMenuModal(true);
|
||||
};
|
||||
|
||||
// 保存菜单(新增+编辑)
|
||||
// 保存菜单
|
||||
const handleSaveMenu = () => {
|
||||
menuForm.validateFields().then(values => {
|
||||
const { menuType, parentId, title, path } = values;
|
||||
console.log(values, "values")
|
||||
if (editMenu) {
|
||||
// 编辑
|
||||
setMenus(menus.map(m =>
|
||||
m.id === editMenu.id
|
||||
? {
|
||||
...m,
|
||||
title,
|
||||
path,
|
||||
parentId: menuType === 'top' ? 0 : Number(parentId)
|
||||
}
|
||||
: m
|
||||
));
|
||||
message.success('编辑成功');
|
||||
const data = {
|
||||
menuId: editMenu.menuId,
|
||||
...values
|
||||
}
|
||||
updateMenu(data).then(res => {
|
||||
if (res.code === 200) {
|
||||
messageApi.success("编辑成功");
|
||||
setMenuModal(false);
|
||||
getMenuListApi();
|
||||
} else {
|
||||
messageApi.error(res.msg);
|
||||
}
|
||||
}).catch(() => {
|
||||
messageApi.error('编辑失败');
|
||||
});
|
||||
} else {
|
||||
// 新增
|
||||
const newMenu = {
|
||||
id: Date.now(),
|
||||
title,
|
||||
path,
|
||||
parentId: menuType === 'top' ? 0 : Number(parentId),
|
||||
};
|
||||
setMenus([...menus, newMenu]);
|
||||
message.success('新增菜单成功');
|
||||
addMenu(values).then(res => {
|
||||
if (res.code === 200) {
|
||||
messageApi.success("新增成功");
|
||||
setMenuModal(false);
|
||||
getMenuListApi();
|
||||
} else {
|
||||
messageApi.error(res.msg);
|
||||
}
|
||||
}).catch(() => {
|
||||
messageApi.error('新增失败');
|
||||
});
|
||||
}
|
||||
setMenuModal(false);
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
// 删除菜单
|
||||
const handleDelete = (id) => {
|
||||
setMenus(menus.filter(m => m.id !== id));
|
||||
message.success('删除成功');
|
||||
// 删除
|
||||
const handleDelete = (menuId) => {
|
||||
deleteMenu(menuId).then(res => {
|
||||
if (res.code == 200) {
|
||||
messageApi.success('删除成功');
|
||||
getMenuListApi();
|
||||
} else {
|
||||
messageApi.error(res.msg);
|
||||
}
|
||||
}).catch(() => {
|
||||
messageApi.error("删除失败");
|
||||
|
||||
})
|
||||
|
||||
};
|
||||
|
||||
// 打开权限配置
|
||||
// 权限弹窗
|
||||
const handlePermOpen = () => {
|
||||
if (!selectedRole) {
|
||||
message.warning('请先选择角色');
|
||||
return;
|
||||
}
|
||||
const saved = rolePermissionMap[selectedRole] || [];
|
||||
setCheckedKeys(saved);
|
||||
if (!selectedRole) return messageApi.warning('请选择角色');
|
||||
setCheckedKeys(rolePermissionMap[selectedRole] || []);
|
||||
setPermModal(true);
|
||||
};
|
||||
|
||||
// 保存权限
|
||||
const handlePermSave = () => {
|
||||
rolePermissionMap[selectedRole] = checkedKeys;
|
||||
message.success(`【${selectedRole}】权限保存成功`);
|
||||
messageApi.success(`【${selectedRole}】权限保存成功`);
|
||||
setPermModal(false);
|
||||
};
|
||||
|
||||
// 一级菜单列表(用于父级选择)
|
||||
const topMenus = menus.filter(m => m.parentId === 0);
|
||||
// 上级菜单下拉选项(只展示一级)
|
||||
const topMenus = rawMenuList.filter(m => m.parentId === 0 || !m.parentId);
|
||||
|
||||
const columns = [
|
||||
{ title: '菜单名称', dataIndex: 'title', key: 'title', width: 180 },
|
||||
{ title: '路由地址', dataIndex: 'fullPath', key: 'fullPath' },
|
||||
{
|
||||
title: '菜单名称',
|
||||
dataIndex: 'menuName',
|
||||
key: 'menuName',
|
||||
// 关键:antd 表格自带缩进树形
|
||||
},
|
||||
{ title: '路由地址', dataIndex: 'path', key: 'path' },
|
||||
{ title: '排序', dataIndex: 'orderNum', key: 'orderNum' },
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
width: 220,
|
||||
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>
|
||||
<Button type="text" icon={<EditOutlined />} onClick={() => handleEdit(record)}>编辑</Button>
|
||||
<Popconfirm title="确定删除?" onConfirm={() => handleDelete(record.menuId)}>
|
||||
<Button type="text" danger>删除</Button>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
),
|
||||
@@ -205,105 +195,84 @@ export default function Roles() {
|
||||
];
|
||||
|
||||
return (
|
||||
<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
|
||||
columns={columns}
|
||||
dataSource={menus}
|
||||
rowKey="id"
|
||||
pagination={false}
|
||||
variant
|
||||
/>
|
||||
|
||||
{/* 菜单 新增/编辑 弹窗 */}
|
||||
<Modal
|
||||
title={editMenu ? '编辑菜单' : '新增菜单'}
|
||||
open={menuModal}
|
||||
onCancel={() => setMenuModal(false)}
|
||||
onOk={handleSaveMenu}
|
||||
width={500}
|
||||
>
|
||||
<Form form={menuForm} layout="vertical">
|
||||
<Form.Item name="menuType" label="菜单类型" rules={[{ required: true }]}>
|
||||
<Select placeholder="请选择菜单类型">
|
||||
<Option value="top">一级菜单</Option>
|
||||
<Option value="sub">二级菜单</Option>
|
||||
<div style={{ padding: '20px' }}>
|
||||
{contextHolder}
|
||||
<Card title="菜单权限管理" style={{ borderRadius: 8 }}>
|
||||
<div style={{ marginBottom: 16, display: 'flex', justifyContent: 'space-between' }}>
|
||||
<Space>
|
||||
<Select placeholder="选择角色" style={{ width: 180 }} value={selectedRole} onChange={setSelectedRole}>
|
||||
{roleList.map(r => <Option key={r.key} value={r.key}>{r.name}</Option>)}
|
||||
</Select>
|
||||
</Form.Item>
|
||||
<Button type="primary" icon={<SettingOutlined />} onClick={handlePermOpen}>配置权限</Button>
|
||||
</Space>
|
||||
<Button type="primary" icon={<PlusOutlined />} onClick={() => {
|
||||
setEditMenu(null);
|
||||
menuForm.resetFields();
|
||||
setMenuModal(true);
|
||||
}}>新增菜单</Button>
|
||||
</div>
|
||||
|
||||
{/* 二级菜单才显示父级选择 */}
|
||||
{menuType === 'sub' && (
|
||||
<Form.Item name="parentId" label="上级菜单" rules={[{ required: true }]}>
|
||||
<Select placeholder="请选择上级菜单">
|
||||
{/* 关键:rowKey + treeData + indentSize 开启树形表格 */}
|
||||
<Table
|
||||
columns={columns}
|
||||
dataSource={menuTreeData}
|
||||
rowKey="menuId"
|
||||
pagination={false}
|
||||
bordered
|
||||
size="middle"
|
||||
indentSize={24}
|
||||
/>
|
||||
|
||||
{/* 新增/编辑弹窗 */}
|
||||
<Modal
|
||||
title={editMenu ? '编辑菜单' : '新增菜单'}
|
||||
open={menuModal}
|
||||
width={600}
|
||||
onCancel={() => setMenuModal(false)}
|
||||
onOk={handleSaveMenu}
|
||||
destroyOnClose
|
||||
>
|
||||
<Form form={menuForm} layout="vertical">
|
||||
<Form.Item label="上级菜单" name="parentId">
|
||||
<Select placeholder="无(一级菜单)">
|
||||
<Option value={0}>无(一级菜单)</Option>
|
||||
{topMenus.map(m => (
|
||||
<Option key={m.id} value={m.id}>{m.title}</Option>
|
||||
<Option key={m.menuId} value={m.menuId}>{m.menuName}</Option>
|
||||
))}
|
||||
</Select>
|
||||
</Form.Item>
|
||||
)}
|
||||
|
||||
<Form.Item name="title" label="菜单名称" rules={[{ required: true }]}>
|
||||
<Input placeholder="请输入菜单名称" />
|
||||
</Form.Item>
|
||||
<Form.Item name="path" label="路由地址" rules={[{ required: true }]}>
|
||||
<Input placeholder="/system/xxx" />
|
||||
</Form.Item>
|
||||
<Form.Item name="sort" label="排序号">
|
||||
<Input type="number" placeholder="数字越小越靠前" />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
<Form.Item label="菜单名称" name="menuName" rules={[{ required: true }]}>
|
||||
<Input placeholder="请输入菜单名称" />
|
||||
</Form.Item>
|
||||
|
||||
{/* 权限配置弹窗 */}
|
||||
<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>
|
||||
<Form.Item label="路由地址" name="path">
|
||||
<Input placeholder="/system/menu" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="显示排序" name="orderNum">
|
||||
<InputNumber min={0} style={{ width: '100%' }} placeholder="数字越小越靠前" />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
|
||||
{/* 权限树弹窗 */}
|
||||
<Modal
|
||||
title={`【${selectedRole}】权限配置`}
|
||||
open={permModal}
|
||||
width={520}
|
||||
onCancel={() => setPermModal(false)}
|
||||
onOk={handlePermSave}
|
||||
>
|
||||
<Tree
|
||||
checkable
|
||||
treeData={menuTree}
|
||||
checkedKeys={checkedKeys}
|
||||
onCheck={setCheckedKeys}
|
||||
defaultExpandAll
|
||||
/>
|
||||
</Modal>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user