const AIReport = (() => { // 生成AI报表卡片的HTML const generateAICard = () => { return `
AI
AI 智能报表
AI大模型
点击生成智能报表
AI将为您分析生产数据并生成洞察
`; }; // 生成报表 const generateReport = async () => { console.log('generateReport 被调用'); const placeholderEl = document.getElementById('ai-report-placeholder'); const loadingEl = document.getElementById('ai-report-loading'); const thinkingEl = document.getElementById('ai-report-thinking'); const resultEl = document.getElementById('ai-report-result'); const timeEl = document.getElementById('ai-report-time'); // 显示思考过程 if (placeholderEl) placeholderEl.style.display = 'none'; if (loadingEl) loadingEl.style.display = 'none'; if (resultEl) resultEl.style.display = 'none'; // 确保思考过程容器可见 if (thinkingEl) { thinkingEl.style.display = 'flex'; thinkingEl.style.flexDirection = 'column'; thinkingEl.style.flex = '1'; } // 清空思考内容 const thinkingContent = document.getElementById('thinking-content'); console.log('thinkingContent元素:', thinkingContent); if (thinkingContent) { thinkingContent.innerHTML = `
正在思考中,请稍候...
`; console.log('设置思考中提示成功'); } else { console.error('找不到thinking-content元素'); } try { // 直接调用分析API,从返回的thinking字段逐步显示 console.log('开始调用AI API...'); const response = await fetch('/api/ai/analyze', { method: 'POST', headers: { 'Content-Type': 'application/json' } }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const reportData = await response.json(); console.log('AI响应数据:', reportData); // 如果有思考过程,逐字显示 if (reportData.thinking && thinkingContent) { const thinking = reportData.thinking; let displayedText = ''; const chars = thinking.split(''); // 先清空提示文字 thinkingContent.innerHTML = ''; // 逐字显示,速度较慢 for (let i = 0; i < chars.length; i++) { displayedText += chars[i]; thinkingContent.innerHTML = `
${displayedText}
`; thinkingContent.scrollTop = thinkingContent.scrollHeight; // 每3个字符延迟30ms,让显示更慢 if (i % 3 === 0) { await new Promise(resolve => setTimeout(resolve, 30)); } } // 思考过程显示完成后,等待2秒再显示结果 await new Promise(resolve => setTimeout(resolve, 2000)); } // 显示结果 if (thinkingEl) thinkingEl.style.display = 'none'; if (resultEl) { resultEl.style.display = 'block'; renderReport(reportData); } if (timeEl) { const now = new Date(); timeEl.textContent = `更新于 ${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`; } } catch (error) { console.error('调用AI API失败:', error); // 使用模拟数据作为后备 console.log('使用模拟数据...'); // 模拟思考过程 if (thinkingContent) { thinkingContent.innerHTML = `
1. 数据概览:正在读取生产数据...
2. 数据概览:发现总产量数据,分析良品率...
3. 规律发现:分析平台分布规律...
4. 原因推断:分析质量问题和根本原因...
5. 结论形成:生成生产建议和预测...
`; } // 延迟后显示模拟结果 setTimeout(() => { if (thinkingEl) thinkingEl.style.display = 'none'; if (resultEl) { resultEl.style.display = 'block'; resultEl.innerHTML = `

📊 生产总览

总产量
15,423
↑ vs 上周
良品率
98.5%
稳定

📈 平台分布

拼多多 8,934 (57.9%)
圆通 6,489 (42.1%)

💡 AI洞察

`; } if (timeEl) { const now = new Date(); timeEl.textContent = `更新于 ${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')} (模拟)`; } }, 2000); } }; // 渲染真实报表数据 const renderReport = (data) => { const html = `
${data.thinking ? `

🤔 AI思考过程

${data.thinking}
` : ''}

📊 生产总览

总产量
${data.summary?.totalProduction?.toLocaleString() || 'N/A'}
${data.summary?.trend === 'up' ? '↑' : '↓'} vs 上周
良品率
${data.summary?.goodRate || 'N/A'}
稳定

📈 平台分布

${data.platforms ? Object.entries(data.platforms).map(([platform, info]) => `
${platform === 'pdd' ? '拼多多' : '圆通'} ${info.count?.toLocaleString() || 'N/A'} (${info.percentage || 'N/A'}%)
`).join('') : '
暂无平台数据
'} ${data.summary?.insights ? `

💡 AI洞察

` : ''}
`; const resultEl = document.getElementById('ai-report-result'); if (resultEl) { resultEl.innerHTML = html; } }; // 导出报表 const exportReport = () => { const resultEl = document.getElementById('ai-report-result'); if (!resultEl || resultEl.style.display === 'none') { alert('请先生成报表'); return; } const textContent = resultEl.innerText || resultEl.textContent; const blob = new Blob([`智能生产报表\n生成时间:${new Date().toLocaleString()}\n\n${textContent}`], { type: 'text/plain' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `AI生产报表_${new Date().toISOString().split('T')[0]}.txt`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); }; // 暴露方法 return { generateAICard, generateReport, exportReport }; })(); // 添加样式 const style = document.createElement('style'); style.textContent = ` @keyframes spin { to { transform: rotate(360deg); } } `; document.head.appendChild(style);