// 设置默认周次为当前周
function getCurrentWeek() {
const now = new Date();
const startOfYear = new Date(now.getFullYear(), 0, 1);
const days = Math.floor((now - startOfYear) / (24 * 60 * 60 * 1000));
const weekNumber = Math.ceil((days + 1) / 7);
return `${now.getFullYear()}-W${weekNumber.toString().padStart(2, '0')}`;
}
// 页面加载时设置默认周次
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('week').value = getCurrentWeek();
});
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 generateCurrentWeekReport() {
document.getElementById('week').value = getCurrentWeek();
generateReport();
}
function generateReport() {
const week = document.getElementById('week').value;
if (!week) {
showError('请选择周次');
return;
}
// 转换格式:YYYY-WWW 到 YYYY-WW
const weekFormatted = week.replace('W', '');
showLoading();
fetch(`/api/reports/weekly?week=${weekFormatted}`)
.then(response => response.json())
.then(data => {
hideLoading();
if (data.success) {
// 保存原始Markdown内容到data属性
document.getElementById('report-content').setAttribute('data-markdown', data.report);
// 使用innerHTML显示格式化内容
document.getElementById('report-content').innerHTML = data.report.replace(/\n/g, '
');
document.getElementById('report-container').style.display = 'block';
// 显示统计数据
if (data.statistics) {
displayStatistics(data.statistics);
document.getElementById('statistics').style.display = 'block';
// 显示分类图表
if (data.statistics.category_breakdown) {
displayCategoryChart(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), 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: '条' }
];
statistics.forEach(stat => {
const statItem = document.createElement('div');
statItem.className = 'stat-item';
statItem.innerHTML = `