import React, { PureComponent, Fragment, useState, useEffect } from 'react'; import { Button, notification, Modal, Table, Tag, Space, Tooltip, Dropdown, Menu, Typography, Input, } from 'antd'; import { ClockCircleOutlined, SyncOutlined, CloseCircleOutlined, FileTextOutlined, EllipsisOutlined, PlayCircleOutlined, CheckCircleOutlined, EditOutlined, StopOutlined, DeleteOutlined, } from '@ant-design/icons'; import config from '@/utils/config'; import { PageContainer } from '@ant-design/pro-layout'; import { request } from '@/utils/http'; import CronModal from './modal'; const { Text } = Typography; const { Search } = Input; enum CrontabStatus { 'running', 'idle', 'disabled', } const Crontab = () => { const columns = [ { title: '任务名', dataIndex: 'name', key: 'name', align: 'center' as const, render: (text: string, record: any) => ( {record.name || record._id} ), }, { title: '任务', dataIndex: 'command', key: 'command', align: 'center' as const, }, { title: '任务定时', dataIndex: 'schedule', key: 'schedule', align: 'center' as const, }, { title: '状态', key: 'status', dataIndex: 'status', align: 'center' as const, render: (text: string, record: any) => ( <> {record.status === CrontabStatus.idle && ( } color="default"> 空闲中 )} {record.status === CrontabStatus.running && ( } color="processing"> 运行中 )} {record.status === CrontabStatus.disabled && ( } color="error"> 已禁用 )} ), }, { title: '操作', key: 'action', align: 'center' as const, render: (text: string, record: any, index: number) => ( { runCron(record); }} > { logCron(record); }} > ), }, ]; const [width, setWdith] = useState('100%'); const [marginLeft, setMarginLeft] = useState(0); const [marginTop, setMarginTop] = useState(-72); const [value, setValue] = useState(); const [loading, setLoading] = useState(true); const [isModalVisible, setIsModalVisible] = useState(false); const [editedCron, setEditedCron] = useState(); const getCrons = (text: string = '') => { setLoading(true); request .get(`${config.apiPrefix}crons?searchValue=${text}`) .then((data: any) => { setValue(data.data.sort((a: any, b: any) => a.status - b.status)); }) .finally(() => setLoading(false)); }; const addCron = () => { setIsModalVisible(true); }; const editCron = (record: any) => { setEditedCron(record); setIsModalVisible(true); }; const delCron = (record: any) => { Modal.confirm({ title: '确认删除', content: ( <> 确认删除定时任务 {record.name} 吗 ), onOk() { request .delete(`${config.apiPrefix}crons`, { data: { _id: record._id }, }) .then((data: any) => { if (data.code === 200) { notification.success({ message: '删除成功', }); getCrons(); } else { notification.error({ message: data, }); } }); }, onCancel() { console.log('Cancel'); }, }); }; const runCron = (record: any) => { Modal.confirm({ title: '确认运行', content: ( <> 确认运行定时任务 {record.name} 吗 ), onOk() { request .get(`${config.apiPrefix}crons/${record._id}/run`) .then((data: any) => { getCrons(); }); }, onCancel() { console.log('Cancel'); }, }); }; const enabledOrDisabledCron = (record: any) => { Modal.confirm({ title: '确认禁用', content: ( <> 确认禁用定时任务 {record.name} 吗 ), onOk() { request .get( `${config.apiPrefix}crons/${record._id}/${ record.status === CrontabStatus.disabled ? 'enable' : 'disable' }`, { data: { _id: record._id }, }, ) .then((data: any) => { if (data.code === 200) { notification.success({ message: '禁用成功', }); getCrons(); } else { notification.error({ message: data, }); } }); }, onCancel() { console.log('Cancel'); }, }); }; const logCron = (record: any) => { request .get(`${config.apiPrefix}crons/${record._id}/log`) .then((data: any) => { Modal.info({ width: 650, title: `${record.name || record._id}`, content: (
              {data.data || '暂无日志'}
            
), onOk() {}, }); }); }; const MoreBtn: React.FC<{ record: any; }> = ({ record }) => ( action(key, record)}> }> 编辑 {record.isSystem !== 1 && ( <> ) : ( ) } > {record.status === CrontabStatus.disabled ? '启用' : '禁用'} }> 删除 )} } > ); const action = (key: string | number, record: any) => { switch (key) { case 'edit': editCron(record); break; case 'enableordisable': enabledOrDisabledCron(record); break; case 'delete': delCron(record); break; default: break; } }; const handleCancel = (needUpdate?: boolean) => { setIsModalVisible(false); if (needUpdate) { getCrons(); } }; const onSearch = (value: string) => { getCrons(value); }; useEffect(() => { if (document.body.clientWidth < 768) { setWdith('auto'); setMarginLeft(0); setMarginTop(0); } else { setWdith('100%'); setMarginLeft(0); setMarginTop(-72); } getCrons(); }, []); return ( , , ]} header={{ style: { padding: '4px 16px 4px 15px', position: 'sticky', top: 0, left: 0, zIndex: 20, marginTop, width, marginLeft, }, }} > ); }; export default Crontab;