// static/history.js // 页面加载完成后加载历史记录 document.addEventListener('DOMContentLoaded', function() { loadHistory(); }); async function loadHistory() { const fileList = document.getElementById('fileList'); try { const response = await fetch('/api/history', { method: 'POST' }); if (!response.ok) { throw new Error('获取历史记录失败'); } const data = await response.json(); if (data.files && data.files.length > 0) { displayFileList(data.files); } else { fileList.innerHTML = `
📁

暂无历史记录

还没有上传过STP文件

`; } } catch (error) { fileList.innerHTML = `
❌

加载失败

${error.message}

`; } } function displayFileList(files) { const fileList = document.getElementById('fileList'); fileList.innerHTML = files.map(file => `
📄 ${file.filename}
总记录: ${file.record_count} 条 | 最后上传: ${new Date(file.last_upload).toLocaleString()}

正在加载记录详情...

`).join(''); } async function toggleFileRecords(filename) { const fileItem = document.getElementById(`file-${filename}`); const recordList = document.getElementById(`records-${filename}`); const expandBtn = fileItem.querySelector('.expand-btn'); // 切换展开状态 if (fileItem.classList.contains('expanded')) { fileItem.classList.remove('expanded'); expandBtn.textContent = '➕'; return; } // 先设置展开状态 fileItem.classList.add('expanded'); expandBtn.textContent = '➖'; // 如果已经加载过数据,直接显示 if (recordList.dataset.loaded === 'true') { return; } try { const response = await fetch(`/api/history/${encodeURIComponent(filename)}`, { method: 'POST' }); if (!response.ok) { throw new Error('获取记录详情失败'); } const records = await response.json(); if (records.length > 0) { recordList.innerHTML = records.map(record => `
📅 ${new Date(record.upload_time).toLocaleString()}
${getStatusText(record.status)}
文件大小: ${formatFileSize(record.file_size)}
任务ID: ${record.task_id}
状态: ${record.status}
${record.completed_at ? `
完成时间: ${new Date(record.completed_at).toLocaleTimeString()}
` : ''}
`).join(''); } else { recordList.innerHTML = `

暂无详细记录

`; } recordList.dataset.loaded = 'true'; } catch (error) { recordList.innerHTML = `

❌ 加载失败: ${error.message}

`; } } async function viewRecordDetails(taskId) { // 先检查任务状态,如果数据不完整,提示用户重新分析 try { const response = await fetch(`/status/${taskId}`); if (response.ok) { const task = await response.json(); if (task.status === 'completed' && task.key_info) { // 数据完整,跳转到结果页面 window.location.href = `/result/${taskId}`; } else { // 数据不完整,提示用户重新分析 if (confirm('该历史记录的分析数据不完整,是否重新分析?')) { // 重新分析逻辑 await reanalyzeTask(taskId); } } } else { // 任务不存在或无法访问,跳转到结果页面尝试显示 window.location.href = `/result/${taskId}`; } } catch (error) { console.error('检查任务状态失败:', error); // 出错时直接跳转 window.location.href = `/result/${taskId}`; } } async function reanalyzeTask(taskId) { try { const response = await fetch(`/api/reanalyze/${taskId}`, { method: 'POST' }); if (response.ok) { alert('重新分析任务已启动,请稍后查看结果'); // 跳转到分析状态页面 window.location.href = `/status/${taskId}`; } else { throw new Error('重新分析失败'); } } catch (error) { alert(`重新分析失败: ${error.message}`); } } function getStatusText(status) { const statusMap = { 'completed': '已完成', 'processing': '处理中', 'failed': '失败' }; return statusMap[status] || status; } function formatFileSize(bytes) { if (!bytes || bytes === 0) return '0 Bytes'; const k = 1024; const sizes = ['Bytes', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; } // 跳转到上传页面 async function goToUpload() { try { // 上传页面需要GET请求,直接跳转 window.location.href = '/'; } catch (error) { console.error('跳转失败:', error); window.location.href = '/'; } } // 跳转到主页 async function goToHome() { try { // 主页需要GET请求,直接跳转 window.location.href = '/'; } catch (error) { console.error('跳转失败:', error); window.location.href = '/'; } }