import React, { useEffect, useRef, useState } from 'react'; import { Modal, message, Input, Form, Statistic, Button } from 'antd'; import { request } from '@/utils/http'; import config from '@/utils/config'; import { Loading3QuartersOutlined, CheckCircleOutlined, } from '@ant-design/icons'; import { PageLoading } from '@ant-design/pro-layout'; import { logEnded } from '@/utils'; enum CrontabStatus { 'running', 'idle', 'disabled', 'queued', } const { Countdown } = Statistic; const CronLogModal = ({ cron, handleCancel, visible, data, logUrl, }: { cron?: any; visible: boolean; handleCancel: () => void; data?: string; logUrl?: string; }) => { const [value, setValue] = useState('启动中...'); const [loading, setLoading] = useState(true); const [executing, setExecuting] = useState(true); const [isPhone, setIsPhone] = useState(false); const getCronLog = (isFirst?: boolean) => { if (isFirst) { setLoading(true); } request .get(logUrl ? logUrl : `${config.apiPrefix}crons/${cron.id}/log`) .then(({ code, data }) => { if ( code === 200 && localStorage.getItem('logCron') === String(cron.id) ) { const log = data as string; setValue(log || '暂无日志'); const hasNext = log && !logEnded(log) && !log.includes('重启面板') && !log.includes('任务未运行或运行失败,请尝试手动运行'); setExecuting(hasNext); setTimeout(() => { document .querySelector('#log-flag')! .scrollIntoView({ behavior: 'smooth' }); }, 1000); if (hasNext) { setTimeout(() => { getCronLog(); }, 2000); } if ( log && log.includes('重启面板') && cron.status === CrontabStatus.running ) { message.warning({ content: ( 系统将在 秒后自动刷新 ), duration: 10, }); setTimeout(() => { window.location.reload(); }, 30000); } } }) .finally(() => { if (isFirst) { setLoading(false); } }); }; const cancel = () => { localStorage.removeItem('logCron'); handleCancel(); }; const titleElement = () => { return ( <> {(executing || loading) && } {!executing && !loading && } {cron && cron.name} ); }; useEffect(() => { if (cron && cron.id && visible) { getCronLog(true); } }, [cron, visible]); useEffect(() => { if (data) { setValue(data); } }, [data]); useEffect(() => { setIsPhone(document.body.clientWidth < 768); }, []); return ( cancel()} onCancel={() => cancel()} footer={[ , ]} > {loading ? ( ) : (
          {value}
        
)}
); }; export default CronLogModal;