togglePreview = function (mode) { const previewBtn = document.getElementById('preview-toggle'); const sourceBtn = document.getElementById('source-toggle'); const reportContent = document.getElementById('report-content'); const markdownSource = document.getElementById('markdown-source'); const sourceContent = document.getElementById('source-content'); if (mode === 'preview') { previewBtn.classList.add('active'); sourceBtn.classList.remove('active'); reportContent.style.display = 'block'; markdownSource.style.display = 'none'; } else { previewBtn.classList.remove('active'); sourceBtn.classList.add('active'); reportContent.style.display = 'none'; markdownSource.style.display = 'block'; // 显示原始Markdown源代码 const markdown = reportContent.getAttribute('data-markdown'); sourceContent.textContent = markdown; } }; // 设置默认日期为今天 document.getElementById('date').value = new Date().toISOString().split('T')[0]; function showLoading() { document.getElementById('loading').style.display = 'block'; document.getElementById('report-container').style.display = 'none'; document.getElementById('error').style.display = 'none'; } function hideLoading() { document.getElementById('loading').style.display = 'none'; } function showError(message) { document.getElementById('error').style.display = 'block'; document.getElementById('error').textContent = message; } function generateTodayReport() { document.getElementById('date').value = new Date().toISOString().split('T')[0]; generateReport(); } function generateReport() { const date = document.getElementById('date').value; if (!date) { showError('请选择日期'); return; } showLoading(); fetch(`api/reports/daily?date=${date}`) .then(response => response.json()) .then(data => { hideLoading(); if (data.success) { // 保存原始Markdown内容到data属性 document.getElementById('report-content').setAttribute('data-markdown', data.report); // 使用专业的Markdown解析器将Markdown转换为HTML并显示 const htmlContent = parseMarkdownWithTables(data.report); document.getElementById('report-content').innerHTML = htmlContent; // 重置为预览模式 togglePreview('preview'); document.getElementById('report-container').style.display = 'block'; // 显示统计数据 if (data.statistics) { displayStatistics(data.statistics); document.getElementById('statistics').style.display = 'block'; } } else { showError('生成日报失败:' + data.error); } }) .catch(error => { hideLoading(); showError('网络错误:' + error.message); }); } function displayStatistics(stats) { const statGrid = document.getElementById('stat-grid'); statGrid.innerHTML = ''; const statistics = [ {label: '今日消息量', value: stats.today_message_count, unit: '条'}, {label: '昨日消息量', value: stats.yesterday_message_count, unit: '条'}, {label: '消息变化率', value: stats.message_change_rate.toFixed(1), unit: '%'}, {label: '今日活跃用户', value: stats.today_active_users, unit: '人'}, {label: '昨日活跃用户', value: stats.yesterday_active_users, unit: '人'}, {label: '用户变化率', value: stats.user_change_rate.toFixed(1), unit: '%'}, {label: '群组数量', value: stats.group_count, unit: '个'}, {label: '成员总数', value: stats.member_count, unit: '人'} ]; statistics.forEach(stat => { const statItem = document.createElement('div'); statItem.className = 'stat-item'; statItem.innerHTML = `
${stat.value}${stat.unit}
${stat.label}
`; statGrid.appendChild(statItem); }); } function exportReport() { const reportContent = document.getElementById('report-content').getAttribute('data-markdown'); if (!reportContent) { showError('请先生成日报'); return; } const blob = new Blob([reportContent], {type: 'text/markdown'}); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `运营日报_${document.getElementById('date').value}.md`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); } // 页面加载时自动生成今日日报 window.addEventListener('load', function () { generateTodayReport(); }); // 使用专业的Markdown解析器解析Markdown文本,支持表格 function parseMarkdownWithTables(markdown) { if (typeof MarkdownParser !== 'undefined' && typeof MergeTableParser !== 'undefined') { const parser = new MarkdownParser(); return parser.parseFull(markdown); } else { // 回退到简单的markdown渲染 return markdownToHtml(markdown); } }