diff --git a/templates/index.html b/templates/index.html index 8af0b6b..07eaa1d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3641,10 +3641,7 @@ reportTaskId = null; // 停止可能正在进行的轮询 - if (reportPollingInterval) { - clearInterval(reportPollingInterval); - reportPollingInterval = null; - } + stopProgressPolling(); // 向所有运行中的应用发送搜索请求(通过刷新iframe传递参数) let totalRunning = 0; @@ -5268,10 +5265,11 @@ resetReportLogsForNewTask(taskId, '检测到正在运行的报告任务,日志已重新开始'); reportTaskId = taskId; reportAutoPreviewLoaded = false; + startProgressPolling(taskId); if (window.EventSource) { openReportStream(reportTaskId); } else { - startProgressPolling(taskId); + appendReportStreamLine('浏览器不支持SSE,已切换为轮询模式', 'warn', { badge: 'SSE', force: true }); } } else if (statusData.current_task.status === 'completed') { lastCompletedReportTask = statusData.current_task; @@ -5346,13 +5344,25 @@ const downloadPdfButton = document.getElementById('downloadPdfButton'); if (!downloadButton || !downloadPdfButton) return; - if (task && task.status === 'completed' && (task.report_file_ready || task.report_file_path)) { + const htmlReady = task && task.status === 'completed' && ( + task.report_file_ready || + task.report_file_path || + task.has_result // 有内容即可允许尝试下载/预览 + ); + const pdfReady = task && task.status === 'completed' && ( + task.ir_file_ready || + task.ir_file_path || + task.report_file_ready || + task.report_file_path + ); + + if (htmlReady) { downloadButton.disabled = false; downloadButton.dataset.taskId = task.task_id; downloadButton.dataset.filename = task.report_file_name || ''; const label = task.report_file_name ? `下载HTML (${task.report_file_name})` : '下载HTML'; downloadButton.textContent = label; - downloadPdfButton.disabled = false; + downloadPdfButton.disabled = !pdfReady; downloadPdfButton.dataset.taskId = task.task_id; lastCompletedReportTask = task; } else if (!lastCompletedReportTask || (task && task.status !== 'completed')) { @@ -5631,10 +5641,13 @@ // 确保从任务开始就能读取日志 reportLogManager.start(); + // 【兜底】立即启动进度轮询,SSE连上后会自动停止 + startProgressPolling(reportTaskId); + if (window.EventSource) { openReportStream(reportTaskId); } else { - startProgressPolling(data.task_id); + appendReportStreamLine('浏览器不支持SSE,已切换为轮询模式', 'warn', { badge: 'SSE', force: true }); } } else { updateTaskProgressStatus(null, 'error', '启动失败: ' + data.error); @@ -5664,12 +5677,19 @@ // 【新函数】使用新的日志管理器 // 旧的startReportLogRefresh和stopReportLogRefresh已废弃,请使用reportLogManager - // 开始进度轮询 - function startProgressPolling(taskId) { + // 开始/停止进度轮询(SSE不可用或断开时兜底使用) + function stopProgressPolling() { if (reportPollingInterval) { clearInterval(reportPollingInterval); + reportPollingInterval = null; } + } + function startProgressPolling(taskId) { + if (!taskId) return; + stopProgressPolling(); + // 先立即拉取一次,避免长时间停留在5% + checkTaskProgress(taskId); reportPollingInterval = setInterval(() => { checkTaskProgress(taskId); }, 2000); @@ -5687,7 +5707,7 @@ // reportLogManager会自动处理轮询 if (data.task.status === 'completed') { - clearInterval(reportPollingInterval); + stopProgressPolling(); showMessage('报告生成完成!', 'success'); // 自动显示报告 @@ -5699,7 +5719,7 @@ reportTaskId = null; setGenerateButtonState(false); } else if (data.task.status === 'error') { - clearInterval(reportPollingInterval); + stopProgressPolling(); showMessage('报告生成失败: ' + data.task.error_message, 'error'); // 重置自动生成标志,允许重新尝试 @@ -5863,10 +5883,6 @@ startProgressPolling(taskId); return; } - if (reportPollingInterval) { - clearInterval(reportPollingInterval); - reportPollingInterval = null; - } if (reportEventSource && reportEventSource.__taskId === taskId) { if (reportEventSource.readyState !== EventSource.CLOSED) { return; @@ -5913,6 +5929,7 @@ if (reportTaskId) { reportLogManager.start(); } + startProgressPolling(taskId); scheduleReportStreamReconnect(taskId); }; @@ -6060,6 +6077,7 @@ break; case 'completed': appendReportStreamLine(payload.message || '任务完成', 'success'); + stopProgressPolling(); // 【修复】任务完成前强制刷新最后一次日志,确保所有日志都被读取 if (reportLogManager && reportLogManager.isRunning) { @@ -6084,6 +6102,7 @@ break; case 'cancelled': appendReportStreamLine(payload.message || '任务已取消', 'warn'); + stopProgressPolling(); safeCloseReportStream(); updateReportStreamStatus('idle'); reportTaskId = null; @@ -6091,6 +6110,7 @@ break; case 'error': appendReportStreamLine(payload.message || '任务失败', 'error', { badge: 'ERROR' }); + stopProgressPolling(); safeCloseReportStream(); updateReportStreamStatus('error'); reportTaskId = null;