import { Button, Card, Form, Input, message, Modal, Table } from 'antd'; import React, { useEffect, useState, useRef } from 'react'; import TemplateContainer from '@/pages/TemplateContainer'; import { ExclamationCircleOutlined, PlusOutlined } from '@ant-design/icons'; import { ColumnsType, TablePaginationConfig } from 'antd/es/table'; import { DeleteRoleById, QueryRoleList } from '@/services/services/role'; import { FormatDate } from '@/util/time'; import ManageRoleModal from '../ManageRoleModal'; import { useFormReset } from '@/hooks/useFormReset'; import AddRoleForm from '../AddRoleForm'; import { FilterValue, SorterResult, TableCurrentDataSource } from 'antd/es/table/interface'; import { useModel } from '@umijs/max'; const RoleManagement: React.FC = () => { const { initialState } = useModel('@@initialState'); const [data, setData] = useState(); // 数据 const [form] = Form.useForm(); const { setFormRef, resetForm } = useFormReset(); let [loading, setLoading] = useState(true); const [roleId, setRoleId] = useState(0); const [openModal, setOpenModal] = useState(false); const [modal, contextHolder] = Modal.useModal(); const [modalTitle, setModalTitle] = useState("编辑角色"); const [type, setType] = useState("edit"); const [messageApi, messageHolder] = message.useMessage(); const [tableParams, setTableParams] = useState({ pagination: { current: 1, pageSize: 10, showQuickJumper: true, totalBoundaryShowSizeChanger: true, }, }); useEffect(() => { // 初始化加载数据 QueryRoleList(tableParams, form.getFieldsValue()) .then((res) => { setData(res.collection); setTableParams({ pagination: { ...tableParams.pagination, total: res.total } }) setLoading(false); }) .catch((error) => { messageApi.error(error.message); }).finally(() => { setLoading(false); }) }, []); async function modalCancel() { try { resetForm(); setOpenModal(false); setLoading(true); let res = await QueryRoleList(tableParams, form.getFieldsValue()); setData(res.collection); } catch (error: any) { messageApi.error(error.message); } finally { setLoading(false); } } async function QueryRoleByName(values: any): Promise { setLoading(true); try { let queryRole = await QueryRoleList(tableParams, values); setData(queryRole.collection); setTableParams({ pagination: { ...tableParams.pagination, total: queryRole.total } }) } catch (error: any) { messageApi.error(error.message); } finally { setLoading(false); } } function DeleteRole(roleId: number) { try { modal.confirm({ title: '确认删除', icon: , content: '是否确认删除选中的角色,改操作不可逆,请谨慎操作!', okText: '确认', cancelText: '取消', onOk: async () => { // 开始删除 try { await DeleteRoleById(roleId); await QueryRoleByName(form.getFieldsValue()); messageApi.success("删除角色成功"); } catch (error: any) { messageApi.error(error.message); } }, onCancel: async () => { await QueryRoleByName(form.getFieldsValue()); }, }); } catch (error) { } } function AddRole() { setModalTitle("新增角色"); setOpenModal(true); setType("add"); } const columns: ColumnsType = [ { title: 'ID', dataIndex: 'id', sorter: true, width: '100px', }, { title: '角色名', dataIndex: 'name', width: '200px', }, { title: '创建者', dataIndex: 'createdUser', width: '150px', render: (text, record) => record.createdUser?.nickName, }, { title: '更新者', dataIndex: 'updeatedUser', width: '150px', render: (text, record) => record.updeatedUser?.nickName, }, { title: '创建时间', dataIndex: 'createdTime', render: (text, record) => FormatDate(record.createdTime), width: '200px', }, { title: '更新时间', dataIndex: 'updatedTime', render: (text, record) => FormatDate(record.updatedTime), width: '200px', }, { title: '备注', dataIndex: 'remark', }, { title: '操作', width: '120px', render: (text, record) => (
), }, ]; async function handleTableChange(pagination: TablePaginationConfig, filters: Record, sorter: SorterResult | SorterResult[], extra: TableCurrentDataSource): Promise { setLoading(true); try { let queryRole = await QueryRoleList({ pagination }, form.getFieldsValue()); setData(queryRole.collection); setTableParams({ pagination: { ...pagination, total: queryRole.total } }) } catch (error: any) { messageApi.error(error.message); } finally { setLoading(false); } } return (
columns={columns} rowKey={(record) => record.id} dataSource={data} pagination={tableParams.pagination} loading={loading} onChange={handleTableChange} />
{ type === "edit" ? : } {contextHolder} {messageHolder}
) } export default RoleManagement;