import React, { useState, useRef, useEffect } from 'react'; import { useIntl, FormattedMessage, useAccess, history } from '@umijs/max'; import { Dropdown, FormInstance, Space } from 'antd'; import { Button, message, Modal } from 'antd'; import { ActionType, FooterToolbar, PageContainer, ProColumns, ProTable } from '@ant-design/pro-components'; import { PlusOutlined, DeleteOutlined, ExclamationCircleOutlined, DownOutlined, EditOutlined } from '@ant-design/icons'; import { getJobList, removeJob, addJob, updateJob, exportJob, runJob } from '@/services/monitor/job'; import { getDictSelectOption, getDictValueEnum } from '@/services/system/dict'; import UpdateForm from './edit'; import DetailForm from './detail'; import DictTag from '@/components/DictTag'; /** * 定时任务调度 List Page * * @author whiteshader * @date 2023-02-07 */ /** * 添加节点 * * @param fields */ const handleAdd = async (fields: API.Monitor.Job) => { const hide = message.loading('正在添加'); try { const resp = await addJob({ ...fields }); hide(); if (resp.code === 200) { message.success('添加成功'); } else { message.error(resp.msg); } return true; } catch (error) { hide(); message.error('添加失败请重试!'); return false; } }; /** * 更新节点 * * @param fields */ const handleUpdate = async (fields: API.Monitor.Job) => { const hide = message.loading('正在更新'); try { const resp = await updateJob(fields); hide(); if (resp.code === 200) { message.success('更新成功'); } else { message.error(resp.msg); } return true; } catch (error) { hide(); message.error('配置失败请重试!'); return false; } }; /** * 删除节点 * * @param selectedRows */ const handleRemove = async (selectedRows: API.Monitor.Job[]) => { const hide = message.loading('正在删除'); if (!selectedRows) return true; try { const resp = await removeJob(selectedRows.map((row) => row.jobId).join(',')); hide(); if (resp.code === 200) { message.success('删除成功,即将刷新'); } else { message.error(resp.msg); } return true; } catch (error) { hide(); message.error('删除失败,请重试'); return false; } }; const handleRemoveOne = async (selectedRow: API.Monitor.Job) => { const hide = message.loading('正在删除'); if (!selectedRow) return true; try { const params = [selectedRow.jobId]; const resp = await removeJob(params.join(',')); hide(); if (resp.code === 200) { message.success('删除成功,即将刷新'); } else { message.error(resp.msg); } return true; } catch (error) { hide(); message.error('删除失败,请重试'); return false; } }; /** * 导出数据 * */ const handleExport = async () => { const hide = message.loading('正在导出'); try { await exportJob(); hide(); message.success('导出成功'); return true; } catch (error) { hide(); message.error('导出失败,请重试'); return false; } }; const JobTableList: React.FC = () => { const formTableRef = useRef(); const [modalVisible, setModalVisible] = useState(false); const [detailModalVisible, setDetailModalVisible] = useState(false); const actionRef = useRef(); const [currentRow, setCurrentRow] = useState(); const [selectedRows, setSelectedRows] = useState([]); const [jobGroupOptions, setJobGroupOptions] = useState([]); const [statusOptions, setStatusOptions] = useState([]); const access = useAccess(); /** 国际化配置 */ const intl = useIntl(); useEffect(() => { getDictSelectOption('sys_job_group').then((data) => { setJobGroupOptions(data); }); getDictValueEnum('sys_normal_disable').then((data) => { setStatusOptions(data); }); }, []); const columns: ProColumns[] = [ { title: , dataIndex: 'jobId', valueType: 'text', hideInSearch: true, }, { title: , dataIndex: 'jobName', valueType: 'text', render: (dom, record) => { return ( { setDetailModalVisible(true); setCurrentRow(record); }} > {dom} ); }, }, { title: , dataIndex: 'jobGroup', valueType: 'text', valueEnum: jobGroupOptions, render: (_, record) => { return (); }, }, { title: , dataIndex: 'invokeTarget', valueType: 'textarea', }, { title: , dataIndex: 'cronExpression', valueType: 'text', }, { title: , dataIndex: 'status', valueType: 'select', valueEnum: statusOptions, render: (_, record) => { return (); }, }, { title: , dataIndex: 'option', width: '220px', valueType: 'option', render: (_, record) => [ , , { if (key === 'runOnce') { Modal.confirm({ title: '警告', content: '确认要立即执行一次?', okText: '确认', cancelText: '取消', onOk: async () => { const success = await runJob(record.jobId, record.jobGroup); if (success) { message.success('执行成功'); } }, }); } else if (key === 'detail') { setDetailModalVisible(true); setCurrentRow(record); } else if( key === 'log') { history.push(`/monitor/job-log/index/${record.jobId}`); } } }} > e.preventDefault()}> 更多 , ], }, ]; return (
headerTitle={intl.formatMessage({ id: 'pages.searchTable.title', defaultMessage: '信息', })} actionRef={actionRef} formRef={formTableRef} rowKey="jobId" key="jobList" search={{ labelWidth: 120, }} toolBarRender={() => [ , , , ]} request={(params) => getJobList({ ...params } as API.Monitor.JobListParams).then((res) => { const result = { data: res.rows, total: res.total, success: true, }; return result; }) } columns={columns} rowSelection={{ onChange: (_, selectedRows) => { setSelectedRows(selectedRows); }, }} />
{selectedRows?.length > 0 && ( {selectedRows.length} } > )} { let success = false; if (values.jobId) { success = await handleUpdate({ ...values } as API.Monitor.Job); } else { success = await handleAdd({ ...values } as API.Monitor.Job); } if (success) { setModalVisible(false); setCurrentRow(undefined); if (actionRef.current) { actionRef.current.reload(); } } }} onCancel={() => { setModalVisible(false); setCurrentRow(undefined); }} open={modalVisible} values={currentRow || {}} jobGroupOptions={jobGroupOptions||{}} statusOptions={statusOptions} /> { setDetailModalVisible(false); setCurrentRow(undefined); }} open={detailModalVisible} values={currentRow || {}} statusOptions={statusOptions} />
); }; export default JobTableList;