Files
web-Iot/src/components/SystemSetting/MenuList.tsx

273 lines
6.7 KiB
TypeScript
Raw Normal View History

import React, { useState, useEffect } from 'react';
2026-04-22 09:43:55 +08:00
import {
2026-04-28 14:45:07 +08:00
Table, Button, Space, Modal, Form, Tree, message,
2026-04-28 15:16:50 +08:00
Input, Popconfirm, Select, Card, InputNumber, Row, Col, Tooltip
2026-04-22 09:43:55 +08:00
} from 'antd';
2026-04-28 15:16:50 +08:00
// 引入全部常用图标
import * as AllIcons from '@ant-design/icons';
2026-04-24 15:28:16 +08:00
import {
PlusOutlined, EditOutlined, DeleteOutlined, SettingOutlined
} from '@ant-design/icons';
2026-05-07 14:13:26 +08:00
import { addMenu, deleteMenu, getMenuList, updateMenu } from '../../api/mune';
import { buildPermTree, buildTreeFromFlat } from '../../lib/utils';
2026-04-22 09:43:55 +08:00
const { Option } = Select;
2026-04-28 15:16:50 +08:00
// 筛选常用图标(够用、不乱)
const iconList = Object.keys(AllIcons).filter(item => {
return item.endsWith('Outlined')
});
2026-04-28 14:45:07 +08:00
2026-04-24 15:28:16 +08:00
const rolePermissionMap = {};
2026-04-28 14:45:07 +08:00
export default function MenuList() {
const [rawMenuList, setRawMenuList] = useState([]);
const [menuTreeData, setMenuTreeData] = useState([]);
const [menuTree, setMenuTree] = useState([]);
2026-04-28 15:16:50 +08:00
const [iconModal, setIconModal] = useState(false);
2026-04-22 09:43:55 +08:00
2026-04-24 15:28:16 +08:00
const [menuModal, setMenuModal] = useState(false);
const [menuForm] = Form.useForm();
const [editMenu, setEditMenu] = useState(null);
2026-04-22 09:43:55 +08:00
2026-04-24 15:28:16 +08:00
const [permModal, setPermModal] = useState(false);
const [selectedRole, setSelectedRole] = useState('');
const [checkedKeys, setCheckedKeys] = useState([]);
2026-04-28 14:45:07 +08:00
const [messageApi, contextHolder] = message.useMessage();
2026-04-24 15:28:16 +08:00
2026-04-29 10:00:24 +08:00
// 分页状态
const [pagination, setPagination] = useState({
pageNum: 1,
2026-04-30 10:49:44 +08:00
pageSize: 9999,
2026-04-29 10:00:24 +08:00
total: 0
});
// 获取菜单(带分页)
2026-04-28 14:45:07 +08:00
const getMenuListApi = () => {
2026-04-29 10:00:24 +08:00
const params = {
pageNum: pagination.pageNum,
pageSize: pagination.pageSize
};
getMenuList(params).then(res => {
2026-04-30 10:49:44 +08:00
console.log(res, "res")
2026-04-28 14:45:07 +08:00
if (res.code === 200) {
2026-04-30 10:49:44 +08:00
const data = res.rows || [];
2026-04-28 14:45:07 +08:00
setRawMenuList(data);
const tree = buildTreeFromFlat(data);
setMenuTreeData(tree);
setMenuTree(buildPermTree(tree));
2026-04-29 10:00:24 +08:00
// 如果后台返回 total,设置总数
setPagination({
...pagination,
total: res.total || data.length
});
2026-04-28 14:45:07 +08:00
}
}).catch(() => {
messageApi.error('菜单加载失败');
});
};
2026-04-29 10:00:24 +08:00
useEffect(() => {
2026-04-28 14:45:07 +08:00
getMenuListApi();
2026-04-29 10:00:24 +08:00
}, [pagination.pageNum, pagination.pageSize]);
2026-04-28 14:45:07 +08:00
// 编辑
2026-04-24 15:28:16 +08:00
const handleEdit = (record) => {
setEditMenu(record);
2026-04-28 14:45:07 +08:00
menuForm.setFieldsValue({ ...record });
2026-04-24 15:28:16 +08:00
setMenuModal(true);
};
2026-04-28 15:16:50 +08:00
// 新增子菜单
const handleAddChild = (parentRecord) => {
setEditMenu(null);
menuForm.resetFields();
menuForm.setFieldsValue({
parentId: parentRecord.menuId
});
setMenuModal(true);
};
// 选择图标
const handleSelectIcon = (iconName) => {
menuForm.setFieldValue('icon', iconName);
setIconModal(false);
};
2026-04-28 14:45:07 +08:00
// 保存菜单
2026-04-24 15:28:16 +08:00
const handleSaveMenu = () => {
menuForm.validateFields().then(values => {
if (editMenu) {
2026-04-28 14:45:07 +08:00
const data = {
menuId: editMenu.menuId,
...values
2026-04-28 15:16:50 +08:00
};
2026-04-28 14:45:07 +08:00
updateMenu(data).then(res => {
if (res.code === 200) {
messageApi.success("编辑成功");
setMenuModal(false);
getMenuListApi();
} else {
messageApi.error(res.msg);
}
}).catch(() => {
messageApi.error('编辑失败');
});
2026-04-22 09:43:55 +08:00
} else {
2026-04-28 14:45:07 +08:00
addMenu(values).then(res => {
if (res.code === 200) {
messageApi.success("新增成功");
setMenuModal(false);
getMenuListApi();
} else {
messageApi.error(res.msg);
}
}).catch(() => {
messageApi.error('新增失败');
});
2026-04-22 09:43:55 +08:00
}
});
};
2026-04-28 14:45:07 +08:00
// 删除
const handleDelete = (menuId) => {
deleteMenu(menuId).then(res => {
if (res.code == 200) {
messageApi.success('删除成功');
getMenuListApi();
} else {
messageApi.error(res.msg);
}
}).catch(() => {
messageApi.error("删除失败");
2026-04-28 15:16:50 +08:00
});
2026-04-24 15:28:16 +08:00
};
2026-04-28 14:45:07 +08:00
const topMenus = rawMenuList.filter(m => m.parentId === 0 || !m.parentId);
2026-04-22 09:43:55 +08:00
const columns = [
2026-04-28 14:45:07 +08:00
{
title: '菜单名称',
dataIndex: 'menuName',
key: 'menuName',
},
{ title: '路由地址', dataIndex: 'path', key: 'path' },
{ title: '排序', dataIndex: 'orderNum', key: 'orderNum' },
2026-04-22 09:43:55 +08:00
{
title: '操作',
key: 'action',
2026-04-28 15:16:50 +08:00
width: 320,
2026-04-22 09:43:55 +08:00
render: (_, record) => (
<Space>
2026-04-28 15:16:50 +08:00
{record.parentId === 0 && (
<Button type="text" icon={<PlusOutlined />} onClick={() => handleAddChild(record)}>
新增子菜单
</Button>
)}
2026-04-28 14:45:07 +08:00
<Button type="text" icon={<EditOutlined />} onClick={() => handleEdit(record)}>编辑</Button>
<Popconfirm title="确定删除?" onConfirm={() => handleDelete(record.menuId)}>
<Button type="text" danger>删除</Button>
2026-04-22 09:43:55 +08:00
</Popconfirm>
</Space>
),
},
];
2026-04-28 15:16:50 +08:00
// 动态渲染图标
const renderIcon = (name) => {
const Icon = AllIcons[name];
return Icon ? <Icon /> : null;
};
2026-04-22 09:43:55 +08:00
return (
2026-05-15 15:39:28 +08:00
<div style={{ padding: '8px 0px' }}>
2026-04-28 14:45:07 +08:00
{contextHolder}
2026-04-29 09:47:01 +08:00
<Card title="菜单管理" style={{ borderRadius: 8 }}>
2026-04-28 14:45:07 +08:00
<div style={{ marginBottom: 16, display: 'flex', justifyContent: 'space-between' }}>
<Button type="primary" icon={<PlusOutlined />} onClick={() => {
setEditMenu(null);
menuForm.resetFields();
setMenuModal(true);
}}>新增菜单</Button>
</div>
<Table
columns={columns}
dataSource={menuTreeData}
rowKey="menuId"
2026-04-30 10:49:44 +08:00
pagination={false} // 关闭分页
2026-04-28 14:45:07 +08:00
bordered
size="middle"
indentSize={24}
/>
2026-04-28 15:16:50 +08:00
{/* 菜单弹窗 */}
2026-04-28 14:45:07 +08:00
<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 => (
2026-04-28 14:45:07 +08:00
<Option key={m.menuId} value={m.menuId}>{m.menuName}</Option>
))}
</Select>
</Form.Item>
2026-04-28 14:45:07 +08:00
<Form.Item label="菜单名称" name="menuName" rules={[{ required: true }]}>
<Input placeholder="请输入菜单名称" />
</Form.Item>
2026-04-22 09:43:55 +08:00
2026-04-28 14:45:07 +08:00
<Form.Item label="路由地址" name="path">
<Input placeholder="/system/menu" />
</Form.Item>
2026-04-30 13:04:17 +08:00
2026-04-28 15:16:50 +08:00
2026-04-28 14:45:07 +08:00
<Form.Item label="显示排序" name="orderNum">
<InputNumber min={0} style={{ width: '100%' }} placeholder="数字越小越靠前" />
</Form.Item>
</Form>
</Modal>
2026-04-28 15:16:50 +08:00
{/* 图标选择弹窗 */}
2026-04-30 13:04:17 +08:00
{/*<Modal
2026-04-28 15:16:50 +08:00
title="选择菜单图标"
open={iconModal}
onCancel={() => setIconModal(false)}
footer={null}
width={500}
>
<Row gutter={[12, 12]}>
{iconList.map(iconName => (
<Col span={4} key={iconName}>
<Tooltip title={iconName}>
<Button
block
icon={renderIcon(iconName)}
onClick={() => handleSelectIcon(iconName)}
/>
</Tooltip>
</Col>
))}
</Row>
2026-04-30 13:04:17 +08:00
</Modal>*/}
2026-04-28 15:16:50 +08:00
2026-04-30 10:49:44 +08:00
2026-04-28 14:45:07 +08:00
</Card>
2026-04-24 15:28:16 +08:00
</div>
);
}