Files
web-Iot/src/components/SystemSetting/MenuList.tsx
2026-05-15 15:39:28 +08:00

273 lines
6.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { useState, useEffect } from 'react';
import {
Table, Button, Space, Modal, Form, Tree, message,
Input, Popconfirm, Select, Card, InputNumber, Row, Col, Tooltip
} from 'antd';
// 引入全部常用图标
import * as AllIcons from '@ant-design/icons';
import {
PlusOutlined, EditOutlined, DeleteOutlined, SettingOutlined
} from '@ant-design/icons';
import { addMenu, deleteMenu, getMenuList, updateMenu } from '../../api/mune';
import { buildPermTree, buildTreeFromFlat } from '../../lib/utils';
const { Option } = Select;
// 筛选常用图标(够用、不乱)
const iconList = Object.keys(AllIcons).filter(item => {
return item.endsWith('Outlined')
});
const rolePermissionMap = {};
export default function MenuList() {
const [rawMenuList, setRawMenuList] = useState([]);
const [menuTreeData, setMenuTreeData] = useState([]);
const [menuTree, setMenuTree] = useState([]);
const [iconModal, setIconModal] = useState(false);
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 [pagination, setPagination] = useState({
pageNum: 1,
pageSize: 9999,
total: 0
});
// 获取菜单(带分页)
const getMenuListApi = () => {
const params = {
pageNum: pagination.pageNum,
pageSize: pagination.pageSize
};
getMenuList(params).then(res => {
console.log(res, "res")
if (res.code === 200) {
const data = res.rows || [];
setRawMenuList(data);
const tree = buildTreeFromFlat(data);
setMenuTreeData(tree);
setMenuTree(buildPermTree(tree));
// 如果后台返回 total,设置总数
setPagination({
...pagination,
total: res.total || data.length
});
}
}).catch(() => {
messageApi.error('菜单加载失败');
});
};
useEffect(() => {
getMenuListApi();
}, [pagination.pageNum, pagination.pageSize]);
// 编辑
const handleEdit = (record) => {
setEditMenu(record);
menuForm.setFieldsValue({ ...record });
setMenuModal(true);
};
// 新增子菜单
const handleAddChild = (parentRecord) => {
setEditMenu(null);
menuForm.resetFields();
menuForm.setFieldsValue({
parentId: parentRecord.menuId
});
setMenuModal(true);
};
// 选择图标
const handleSelectIcon = (iconName) => {
menuForm.setFieldValue('icon', iconName);
setIconModal(false);
};
// 保存菜单
const handleSaveMenu = () => {
menuForm.validateFields().then(values => {
if (editMenu) {
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 {
addMenu(values).then(res => {
if (res.code === 200) {
messageApi.success("新增成功");
setMenuModal(false);
getMenuListApi();
} else {
messageApi.error(res.msg);
}
}).catch(() => {
messageApi.error('新增失败');
});
}
});
};
// 删除
const handleDelete = (menuId) => {
deleteMenu(menuId).then(res => {
if (res.code == 200) {
messageApi.success('删除成功');
getMenuListApi();
} else {
messageApi.error(res.msg);
}
}).catch(() => {
messageApi.error("删除失败");
});
};
const topMenus = rawMenuList.filter(m => m.parentId === 0 || !m.parentId);
const columns = [
{
title: '菜单名称',
dataIndex: 'menuName',
key: 'menuName',
},
{ title: '路由地址', dataIndex: 'path', key: 'path' },
{ title: '排序', dataIndex: 'orderNum', key: 'orderNum' },
{
title: '操作',
key: 'action',
width: 320,
render: (_, record) => (
<Space>
{record.parentId === 0 && (
<Button type="text" icon={<PlusOutlined />} onClick={() => handleAddChild(record)}>
新增子菜单
</Button>
)}
<Button type="text" icon={<EditOutlined />} onClick={() => handleEdit(record)}>编辑</Button>
<Popconfirm title="确定删除?" onConfirm={() => handleDelete(record.menuId)}>
<Button type="text" danger>删除</Button>
</Popconfirm>
</Space>
),
},
];
// 动态渲染图标
const renderIcon = (name) => {
const Icon = AllIcons[name];
return Icon ? <Icon /> : null;
};
return (
<div style={{ padding: '8px 0px' }}>
{contextHolder}
<Card title="菜单管理" style={{ borderRadius: 8 }}>
<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"
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.menuId} value={m.menuId}>{m.menuName}</Option>
))}
</Select>
</Form.Item>
<Form.Item label="菜单名称" name="menuName" rules={[{ required: true }]}>
<Input placeholder="请输入菜单名称" />
</Form.Item>
<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="选择菜单图标"
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>
</Modal>*/}
</Card>
</div>
);
}