Files
SmartChat/static/js/weekly_report_v3.js
2025-12-26 08:20:45 +08:00

246 lines
9.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 周报生成器 v3版本 JavaScript
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.addEventListener('DOMContentLoaded', function () {
loadAvailableWeeks();
});
// 加载可用的周次列表
function loadAvailableWeeks() {
fetch('/v3/api/weekly_report/weeks')
.then(response => response.json())
.then(data => {
if (data.success) {
const weekSelect = document.getElementById('week');
weekSelect.innerHTML = '<option value="">请选择周次...</option>';
data.data.weeks.forEach(week => {
const option = document.createElement('option');
option.value = week.week;
option.textContent = week.display;
weekSelect.appendChild(option);
});
// 设置默认选择为当前周
const currentDate = new Date();
const year = currentDate.getFullYear();
const weekNumber = getWeekNumber(currentDate);
const currentWeek = `${year}-${weekNumber.toString().padStart(2, '0')}`;
weekSelect.value = currentWeek;
} else {
console.error('获取周次列表失败:', data.error);
}
})
.catch(error => {
console.error('网络错误:', error);
});
}
// 获取周数
function getWeekNumber(date) {
const firstDayOfYear = new Date(date.getFullYear(), 0, 1);
const pastDaysOfYear = (date - firstDayOfYear) / 86400000;
return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7);
}
function showLoading(message = '正在生成周报,请稍候...') {
document.getElementById('loading').style.display = 'block';
document.getElementById('loading-details').textContent = message;
document.getElementById('report-container').style.display = 'none';
document.getElementById('error').style.display = 'none';
}
function hideLoading() {
document.getElementById('loading').style.display = 'none';
document.getElementById('loading-details').textContent = '';
}
function showError(message) {
document.getElementById('error').style.display = 'block';
document.getElementById('error').textContent = message;
}
function generateCurrentWeekReport() {
const currentDate = new Date();
const year = currentDate.getFullYear();
const weekNumber = getWeekNumber(currentDate);
const currentWeek = `${year}-${weekNumber.toString().padStart(2, '0')}`;
document.getElementById('week').value = currentWeek;
generateReport();
}
function generateReport() {
const week = document.getElementById('week').value;
const useDeepseek = document.getElementById('use-deepseek').checked;
if (!week) {
showError('请选择周次');
return;
}
showLoading(useDeepseek ? '正在生成周报(使用DeepSeek AI增强)...' : '正在生成周报...');
fetch('/v3/api/weekly_report/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
week: week,
use_deepseek: useDeepseek
})
})
.then(response => response.json())
.then(data => {
hideLoading();
if (data.success) {
// 保存原始Markdown内容到data属性
document.getElementById('report-content').setAttribute('data-markdown', data.data.report);
// 使用专业的Markdown解析器将Markdown转换为HTML并显示
const htmlContent = parseMarkdownWithTables(data.data.report);
document.getElementById('report-content').innerHTML = htmlContent;
// 重置为预览模式
togglePreview('preview');
document.getElementById('report-container').style.display = 'block';
// 显示统计数据
if (data.data.statistics) {
displayStatistics(data.data.statistics);
document.getElementById('statistics').style.display = 'block';
// 显示分类图表
if (data.data.statistics.category_breakdown) {
displayCategoryChart(data.data.statistics.category_breakdown);
document.getElementById('category-chart').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.week_message_count, unit: '条'},
{label: '上周消息量', value: stats.last_week_message_count, unit: '条'},
{label: '消息变化率', value: stats.message_change_rate?.toFixed(1) || '0.0', unit: '%'},
{label: '本周活跃用户', value: stats.week_active_users, unit: '人'},
{label: '群组数量', value: stats.group_count, unit: '个'},
{label: '成员总数', value: stats.member_count, unit: '人'},
{label: '问答记录数', value: stats.qa_count, unit: '条'},
{label: '热点话题数', value: stats.hot_topics_count || 0, unit: '个'},
{label: '客户问题分类', value: Object.keys(stats.category_breakdown || {}).length, unit: '类'}
];
statistics.forEach(stat => {
const statItem = document.createElement('div');
statItem.className = 'stat-item';
statItem.innerHTML = `
<div class="stat-value">${stat.value}${stat.unit}</div>
<div class="stat-label">${stat.label}</div>
`;
statGrid.appendChild(statItem);
});
}
function displayCategoryChart(categories) {
const categoryItems = document.getElementById('category-items');
categoryItems.innerHTML = '';
Object.entries(categories).forEach(([category, percentage]) => {
const categoryItem = document.createElement('div');
categoryItem.className = 'category-item';
categoryItem.innerHTML = `
<span style="min-width: 120px;">${category}</span>
<div class="category-bar" style="width: ${percentage * 2}px;"></div>
<span style="min-width: 40px; text-align: right;">${percentage}%</span>
`;
categoryItems.appendChild(categoryItem);
});
}
function exportReport() {
const reportContent = document.getElementById('report-content').getAttribute('data-markdown');
if (!reportContent) {
showError('请先生成周报');
return;
}
const week = document.getElementById('week').value;
const blob = new Blob([reportContent], {type: 'text/markdown'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `运营周报_v3_${week}.md`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
// 支持回车键生成报告
document.getElementById('week').addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
generateReport();
}
});
// 使用专业的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);
}
}
// 简单的Markdown转HTML函数(备用)
function markdownToHtml(markdown) {
return markdown
.replace(/\n/g, '<br>')
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
.replace(/\*(.*?)\*/g, '<em>$1</em>')
.replace(/^#\s+(.*)$/gm, '<h1>$1</h1>')
.replace(/^##\s+(.*)$/gm, '<h2>$1</h2>')
.replace(/^###\s+(.*)$/gm, '<h3>$1</h3>')
.replace(/`(.*?)`/g, '<code>$1</code>');
}