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) => ( {record.parentId === 0 && ( )} handleDelete(record.menuId)}> ), }, ]; // 动态渲染图标 const renderIcon = (name) => { const Icon = AllIcons[name]; return Icon ? : null; }; return (
{contextHolder}
{/* 菜单弹窗 */} setMenuModal(false)} onOk={handleSaveMenu} destroyOnClose >
{/* 图标选择弹窗 */} {/* setIconModal(false)} footer={null} width={500} > {iconList.map(iconName => (