import React, { useState, useEffect } from 'react'; import { Table, Button, Space, Input, Form, Modal, message, Popconfirm, Tooltip, Select } from 'antd'; import { PlusOutlined, SearchOutlined } from '@ant-design/icons'; import TemplateContainer from '@/pages/TemplateContainer'; import { request, useModel } from '@umijs/max'; import { ColumnsType, FilterValue, SorterResult, TableCurrentDataSource, TablePaginationConfig } from 'antd/es/table/interface'; import { objectToQueryString } from '@/services/services/common'; import { FormatDate } from '@/util/time'; import { GetMachineAuthorizationTypeOption, GetMachineAuthorizationTypeOptions } from '@/services/enum/machineAuthorizationEnum'; import { useFormReset } from '@/hooks/useFormReset'; import AddMachineIdAuthorization from './AddMachineIdAuthorization'; const MachineIdAuthorization: React.FC = () => { const { initialState } = useModel('@@initialState'); const [dataSource, setDataSource] = useState([]); const [loading, setLoading] = useState(false); const [modalVisible, setModalVisible] = useState(false); const [form] = Form.useForm(); const [messageApi, messageHolder] = message.useMessage(); const { setFormRef, resetForm } = useFormReset(); const [id, setId] = useState(''); const [tableParams, setTableParams] = useState({ pagination: { current: 1, pageSize: 10, showQuickJumper: true, totalBoundaryShowSizeChanger: true, }, }); useEffect(() => { QueryMachineAuthorizationCollection(tableParams, form.getFieldsValue()); }, []); async function QueryMachineAuthorizationCollection(tableParams: TableModel.TableParams, options?: any) { try { setLoading(true); let data = { ...options, page: tableParams.pagination?.current, pageSize: tableParams.pagination?.pageSize, } let query = objectToQueryString(data) let res = await request>(`/lms/Other/QueryMachineAuthorizationCollection?${query}`, { method: 'GET', }); if (res.code != 1) { throw new Error(res.message); } let resData = res.data; setDataSource(resData.collection); setTableParams({ pagination: { ...tableParams.pagination, total: resData.total } }) messageApi.success('Data fetched successfully'); } catch (error: any) { messageApi.error(error.message); } finally { setLoading(false); } } const columns: ColumnsType = [ { title: '机器码/唯一授权码', dataIndex: 'machineID', key: 'machineId', width: '200px', render: (text) => ( {text?.length > 20 ? `${text.substring(0, 20)}...` : text} ), }, { title: '授权类型', dataIndex: 'type', key: 'type', render: (text, record) => GetMachineAuthorizationTypeOption(record.type), }, { title: '授权时间', dataIndex: 'authorizedDate', key: 'authorizedDate', render: (text) => FormatDate(text), }, { title: '授权到期时间', dataIndex: 'expiryDate', key: 'expiryDate', render: (text) => FormatDate(text), }, { title: '授权码', dataIndex: 'authorizationCode', key: 'authorizationCode', render: (text) => ( {text?.length > 30 ? `${text.substring(0, 30)}...` : text} ), }, { title: '创建人', dataIndex: 'createdUser', key: 'createdUser', render: (text) => { const userName = text?.userName || ''; return ( {userName.length > 10 ? `${userName.substring(0, 10)}...` : userName} ); } }, { title: '创建时间', dataIndex: 'createdDate', key: 'createdDate', render: (text, record) => FormatDate(record.createdDate), }, { title: 'Actions', key: 'actions', render: (_, record) => ( handleRevoke(record.id)} okText="Yes" cancelText="No" > ), }, ]; const handleEdit = (record: MachineAuthorizationModel.MachineAuthorizationBase) => { setModalVisible(true); }; const handleRevoke = async (id: string) => { try { const newData = dataSource.map(item => item.id === id ? { ...item, status: 'revoked' as const } : item ); setDataSource(newData); messageApi.success('Authorization revoked successfully'); } catch (error) { messageApi.error('Failed to revoke authorization'); } }; const handleAdd = () => { form.resetFields(); setModalVisible(true); }; async function TableChangeHandle(pagination: TablePaginationConfig, filters: Record, sorter: SorterResult | SorterResult[], extra: TableCurrentDataSource): Promise { await QueryMachineAuthorizationCollection({ pagination }, form.getFieldsValue()); setTableParams({ pagination: { ...tableParams.pagination, current: pagination.current, pageSize: pagination.pageSize } }) } async function modalCancel(): Promise { setModalVisible(false); resetForm(); setId(''); // 这边调用加载数据的方法 await QueryMachineAuthorizationCollection(tableParams, form.getFieldsValue()); } return (
QueryMachineAuthorizationCollection(tableParams, values)} style={{ marginBottom: 16 }} >