import { ProColumns, ActionType } from '@ant-design/pro-table'; import { useRequest } from 'ahooks'; import { Button, Modal, Select, Space, Switch, Tag } from 'antd'; import React, { useRef, useState } from 'react'; import Icon, { IconImg } from '../../components/Icon'; import Truly from '../../components/Truly'; import { OPTIONS } from '../../constants'; import { AppFields, getAppSelections } from '../../services'; import { isFalse, isTrue } from '../../utils/auth'; import { decodeHtml, isSvg } from '../../utils/html'; import { getTreeFromList } from '../../utils/tree'; import EditForm from './components/EditForm'; import BaseTable from '../../components/BaseTable'; import { delApi, updateApi, getListApi, MenuFields } from './services'; import AddForm, { AddButtonForm } from './components/AddForm'; const Page: React.FC = () => { const actionRef = useRef(); const [tableList, setTableList] = useState([]); const [selectedAppId, setSelectedAppId] = useState(); const [expandedRowKeys, setExpandedRowKeys] = useState([]); const { data: appList } = useRequest(getAppSelections, { onSuccess: (appList) => { const [{ value = '' } = {}] = appList || []; setSelectedAppId(value); }, }); const onDelete = async (record: MenuFields) => { Modal.confirm({ title: '操作提示', content: `是否确认删除菜单名称为"${record.menuName}"的数据项?`, maskClosable: true, onOk: async () => { await delApi(record.menuId); actionRef.current?.reload(); }, okText: '确定', cancelText: '取消', }); }; const onBatchDelete = async (record: MenuFields) => { Modal.confirm({ title: '操作提示', content: `是否确认执行删除子项操作?`, maskClosable: true, onOk: async () => { console.log(record.children); const delApis = (record.children || []).map((item) => { return delApi(item.menuId); }); await Promise.all(delApis); actionRef.current?.reload(); }, okText: '确定', cancelText: '取消', }); }; const onExpand = () => { if (expandedRowKeys.length === 0) { const newExpandedRowKeys = tableList.map((item) => item.menuId || ''); setExpandedRowKeys(newExpandedRowKeys); } else { setExpandedRowKeys([]); } }; const columns: ProColumns[] = [ { title: '菜单名称', dataIndex: 'menuName', }, { title: '排序', dataIndex: 'orderNum', hideInSearch: true, ellipsis: true, }, { title: '菜单类型', dataIndex: 'menuType', valueType: 'select', hideInSearch: true, ellipsis: true, request: async () => { return OPTIONS.OPTIONS_MENU_TYPE; }, }, { title: '图标', dataIndex: 'icon', hideInSearch: true, ellipsis: true, render: (_, record) => { const icon = decodeHtml(record.icon || ''); return ( } > svg ); }, }, { title: '路由地址', dataIndex: 'path', hideInSearch: true, ellipsis: true, }, { title: '权限字符', dataIndex: 'perms', hideInSearch: true, ellipsis: true, }, { title: '状态', dataIndex: 'status', search: false, ellipsis: true, render: (_, record) => { if (isTrue('administration:menu:switch')) { return ( { const { status: preStatus } = record; const status = preStatus === '1' ? '2' : '1'; await updateApi({ ...record, status }); actionRef.current?.reload(); }} /> ); } else if (record.status === '1') { return 启用; } return 停用; }, }, { title: '状态', dataIndex: 'status', hideInTable: true, valueType: 'select', request: async () => { return OPTIONS.OPTIONS_AVAILABLE; }, }, { title: '创建时间', dataIndex: 'createTime', valueType: 'dateTime', hideInSearch: true, ellipsis: true, }, { title: '操作', dataIndex: 'option', valueType: 'option', hideInTable: isFalse( 'administration:menu:add', 'administration:menu:update', 'administration:menu:delete', ), width: 160, fixed: 'right', render: (_, record) => ( {record.menuType !== 'C' ? ( { actionRef.current?.reload(); }} > ) : ( item.perms, )} onOk={() => { actionRef.current?.reload(); }} > )} { actionRef.current?.reload(); }} > 0 && (record.children || []).every( (item) => (item.children || []).length === 0, ) && isTrue('administration:menu:delete') } > ), }, ]; return ( rowKey={'menuId'} actionRef={actionRef} search={false} toolBarRender={() => [ { actionRef.current?.reload(); }} > , , ]} columns={columns} pagination={false} params={{ appId: selectedAppId }} request={async (params) => { const { appId } = params || {}; if (!appId) return {}; const { data: preMenuList = [] } = await getListApi({ appId, }); const menuList = preMenuList.map((menu) => { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { children, ...rest } = menu || {}; return rest; }); setTableList(menuList); const data = getTreeFromList(menuList, { idKey: 'menuId', parentIdKey: 'parentId', }); return { data, }; }} expandable={{ expandRowByClick: false, expandedRowKeys, indentSize: 36, onExpandedRowsChange(rows: any) { setExpandedRowKeys(rows); }, }} headerTitle={