/** * 字典管理页面 * 与 GVA 保持一致的功能 */ import { PlusOutlined, DeleteOutlined, EditOutlined, DownloadOutlined, UploadOutlined } from '@ant-design/icons'; import { PageContainer, ProTable, ModalForm, ProFormText, ProFormSwitch } from '@ant-design/pro-components'; import type { ProColumns, ActionType } from '@ant-design/pro-components'; import { Button, message, Popconfirm, Space, Card, Row, Col, Upload, Switch } from 'antd'; import { useRef, useState } from 'react'; import { getSysDictionaryList, createSysDictionary, updateSysDictionary, deleteSysDictionary, exportSysDictionary, importSysDictionary, } from '@/services/system/dictionary'; import { getSysDictionaryDetailList, createSysDictionaryDetail, updateSysDictionaryDetail, deleteSysDictionaryDetail, } from '@/services/system/dictionaryDetail'; const DictionaryPage: React.FC = () => { const actionRef = useRef(); const detailActionRef = useRef(); const [modalVisible, setModalVisible] = useState(false); const [detailModalVisible, setDetailModalVisible] = useState(false); const [currentRow, setCurrentRow] = useState(); const [currentDetail, setCurrentDetail] = useState(); const [isEdit, setIsEdit] = useState(false); const [isDetailEdit, setIsDetailEdit] = useState(false); const [selectedDict, setSelectedDict] = useState(); const handleDelete = async (id: number) => { const res = await deleteSysDictionary({ ID: id }); if (res.code === 0) { message.success('删除成功'); actionRef.current?.reload(); } }; const handleDeleteDetail = async (id: number) => { const res = await deleteSysDictionaryDetail({ ID: id }); if (res.code === 0) { message.success('删除成功'); detailActionRef.current?.reload(); } }; const handleExport = async (id: number) => { const res = await exportSysDictionary({ ID: id }); if (res.code === 0 && res.data) { const blob = new Blob([JSON.stringify(res.data, null, 2)], { type: 'application/json' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `dictionary_${id}.json`; a.click(); message.success('导出成功'); } }; const handleImport = async (file: File) => { const reader = new FileReader(); reader.onload = async (e) => { try { const data = JSON.parse(e.target?.result as string); const res = await importSysDictionary(data); if (res.code === 0) { message.success('导入成功'); actionRef.current?.reload(); } } catch { message.error('文件格式错误'); } }; reader.readAsText(file); return false; }; const dictColumns: ProColumns[] = [ { title: 'ID', dataIndex: 'ID', search: false }, { title: '字典名称', dataIndex: 'name' }, { title: '字典类型', dataIndex: 'type' }, { title: '描述', dataIndex: 'desc', search: false }, { title: '状态', dataIndex: 'status', search: false, render: (_, record) => , }, { title: '操作', valueType: 'option', render: (_, record) => ( handleDelete(record.ID!)}> ), }, ]; const detailColumns: ProColumns[] = [ { title: 'ID', dataIndex: 'ID' }, { title: '标签', dataIndex: 'label' }, { title: '值', dataIndex: 'value' }, { title: '扩展', dataIndex: 'extend' }, { title: '排序', dataIndex: 'sort' }, { title: '状态', dataIndex: 'status', render: (_, record) => , }, { title: '操作', valueType: 'option', render: (_, record) => ( handleDeleteDetail(record.ID!)}> ), }, ]; return ( headerTitle="字典列表" actionRef={actionRef} rowKey="ID" columns={dictColumns} toolBarRender={() => [ , , ]} request={async (params) => { const res = await getSysDictionaryList({ page: params.current, pageSize: params.pageSize, ...params }); return { data: res.data?.list || [], total: res.data?.total || 0, success: res.code === 0, }; }} /> {selectedDict && ( actionRef={detailActionRef} rowKey="ID" columns={detailColumns} search={false} toolBarRender={() => [ , ]} request={async (params) => { const res = await getSysDictionaryDetailList({ page: params.current, pageSize: params.pageSize, sysDictionaryID: selectedDict.ID, }); return { data: res.data?.list || [], total: res.data?.total || 0, success: res.code === 0, }; }} /> )} { let res; if (isEdit) { res = await updateSysDictionary({ ...currentRow, ...values }); } else { res = await createSysDictionary(values); } if (res.code === 0) { message.success(isEdit ? '编辑成功' : '创建成功'); setModalVisible(false); actionRef.current?.reload(); return true; } return false; }} > { let res; if (isDetailEdit) { res = await updateSysDictionaryDetail({ ...currentDetail, ...values }); } else { res = await createSysDictionaryDetail({ ...currentDetail, ...values }); } if (res.code === 0) { message.success(isDetailEdit ? '编辑成功' : '创建成功'); setDetailModalVisible(false); detailActionRef.current?.reload(); return true; } return false; }} > ); }; export default DictionaryPage;