From 39b9875c2db3d19bb789dabb8b3d361649629b71 Mon Sep 17 00:00:00 2001 From: lychang <632945494@qq.com> Date: Fri, 26 Dec 2025 22:29:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=95=B0=E6=8D=AE=E6=88=AA?= =?UTF-8?q?=E6=AD=A2=E6=97=A52025.12.25?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v3.py | 1 - requirements.txt | 3 - script.py | 6 +- static/css/weekly_report.css | 31 +++++ static/js/h2p.js | 204 ++++++++++++++++++++++++++++++++ static/js/weekly_report_v3.js | 95 ++++++++++----- templates/weekly_report_v3.html | 18 +++ 7 files changed, 324 insertions(+), 34 deletions(-) create mode 100644 static/js/h2p.js diff --git a/api/v3.py b/api/v3.py index eee0e96..0892b3e 100644 --- a/api/v3.py +++ b/api/v3.py @@ -2,7 +2,6 @@ 主程序入口 - 集成周报生成器 """ -import os from datetime import datetime, timedelta diff --git a/requirements.txt b/requirements.txt index a49ba12..e052092 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,4 @@ mammoth pytz flask -pandas -openpyxl -jieba sqlite3 \ No newline at end of file diff --git a/script.py b/script.py index 7c15465..f7779a4 100644 --- a/script.py +++ b/script.py @@ -1,6 +1,6 @@ import json -from app import DatabaseManager +from api.lib import DatabaseManager from chat_session import ChatSessionManager apk = "wx98dfa35ad7e4b271" @@ -97,8 +97,8 @@ if __name__ == '__main__': chat_session = ChatSessionManager(db_manager) month = 12 - day_start = 18 - day_end = 25 + day_start = 25 + day_end = 26 for day in range(day_start, day_end): for hour in range(24): chat_session.process_chat_session(2025, month, day, hour) diff --git a/static/css/weekly_report.css b/static/css/weekly_report.css index ea1f33b..7585876 100644 --- a/static/css/weekly_report.css +++ b/static/css/weekly_report.css @@ -329,6 +329,37 @@ body { background: #218838; } +/* 图片导出按钮样式 */ +.image-export-btn { + background: #17a2b8; + color: white; + border: none; + padding: 8px 16px; + border-radius: 5px; + cursor: pointer; + font-size: 14px; + margin-left: 10px; + transition: all 0.3s ease; +} + +.image-export-btn:hover:not(.disabled) { + background: #138496; + transform: translateY(-1px); + box-shadow: 0 2px 5px rgba(0,0,0,0.2); +} + +.image-export-btn.disabled { + background: #6c757d; + cursor: not-allowed; + opacity: 0.6; +} + +.image-export-btn.disabled:hover { + background: #6c757d; + transform: none; + box-shadow: none; +} + /* 预览控制样式 */ .preview-controls { display: flex; diff --git a/static/js/h2p.js b/static/js/h2p.js new file mode 100644 index 0000000..ae9243d --- /dev/null +++ b/static/js/h2p.js @@ -0,0 +1,204 @@ +/** + * HTML转图片工具 + * 支持将HTML元素转换为PNG、JPEG格式的图片 + * 使用html2canvas库实现 + */ + +class HTMLToImage { + constructor(options = {}) { + this.options = { + scale: 2, // 缩放比例,提高图片清晰度 + useCORS: true, // 启用跨域图片支持 + allowTaint: false, // 不允许污染画布 + backgroundColor: '#ffffff', // 背景颜色 + width: null, // 自定义宽度 + height: null, // 自定义高度 + ...options + }; + + // 检查是否已加载html2canvas + if (typeof html2canvas === 'undefined') { + console.warn('html2canvas未加载,将动态加载...'); + this.loadHtml2Canvas(); + } + } + + /** + * 动态加载html2canvas库 + */ + loadHtml2Canvas() { + return new Promise((resolve, reject) => { + if (typeof html2canvas !== 'undefined') { + resolve(); + return; + } + + const script = document.createElement('script'); + script.src = 'https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js'; + script.onload = () => { + console.log('html2canvas加载成功'); + resolve(); + }; + script.onerror = () => { + reject(new Error('html2canvas加载失败')); + }; + document.head.appendChild(script); + }); + } + + /** + * 将HTML元素转换为图片 + * @param {HTMLElement} element - 要转换的HTML元素 + * @param {Object} options - 转换选项 + * @returns {Promise} - 返回图片的DataURL + */ + async toImage(element, options = {}) { + try { + // 确保html2canvas已加载 + if (typeof html2canvas === 'undefined') { + await this.loadHtml2Canvas(); + } + + const mergedOptions = { ...this.options, ...options }; + + const canvas = await html2canvas(element, mergedOptions); + return canvas.toDataURL('image/png'); + } catch (error) { + console.error('HTML转图片失败:', error); + throw error; + } + } + + /** + * 将HTML元素转换为JPEG图片 + * @param {HTMLElement} element - 要转换的HTML元素 + * @param {number} quality - JPEG质量 (0-1) + * @param {Object} options - 转换选项 + * @returns {Promise} - 返回图片的DataURL + */ + async toJPEG(element, quality = 0.92, options = {}) { + try { + if (typeof html2canvas === 'undefined') { + await this.loadHtml2Canvas(); + } + + const mergedOptions = { ...this.options, ...options }; + + const canvas = await html2canvas(element, mergedOptions); + return canvas.toDataURL('image/jpeg', quality); + } catch (error) { + console.error('HTML转JPEG失败:', error); + throw error; + } + } + + /** + * 下载图片 + * @param {string} dataURL - 图片的DataURL + * @param {string} filename - 文件名 + */ + downloadImage(dataURL, filename = 'image.png') { + const link = document.createElement('a'); + link.download = filename; + link.href = dataURL; + link.click(); + } + + /** + * 将图片DataURL转换为Blob对象 + * @param {string} dataURL - 图片的DataURL + * @returns {Promise} + */ + dataURLToBlob(dataURL) { + return new Promise((resolve) => { + const byteString = atob(dataURL.split(',')[1]); + const mimeString = dataURL.split(',')[0].split(':')[1].split(';')[0]; + const ab = new ArrayBuffer(byteString.length); + const ia = new Uint8Array(ab); + + for (let i = 0; i < byteString.length; i++) { + ia[i] = byteString.charCodeAt(i); + } + + resolve(new Blob([ab], { type: mimeString })); + }); + } + + /** + * 获取图片的尺寸信息 + * @param {string} dataURL - 图片的DataURL + * @returns {Promise<{width: number, height: number}>} + */ + getImageDimensions(dataURL) { + return new Promise((resolve) => { + const img = new Image(); + img.onload = () => { + resolve({ + width: img.width, + height: img.height + }); + }; + img.src = dataURL; + }); + } + + /** + * 批量转换多个元素 + * @param {HTMLElement[]} elements - 要转换的元素数组 + * @param {Object} options - 转换选项 + * @returns {Promise} - 返回图片DataURL数组 + */ + async batchConvert(elements, options = {}) { + const results = []; + + for (let i = 0; i < elements.length; i++) { + try { + const dataURL = await this.toImage(elements[i], options); + results.push(dataURL); + console.log(`第 ${i + 1} 个元素转换完成`); + } catch (error) { + console.error(`第 ${i + 1} 个元素转换失败:`, error); + results.push(null); + } + } + + return results; + } +} + +/** + * 便捷函数:快速转换并下载 + * @param {HTMLElement} element - 要转换的HTML元素 + * @param {string} filename - 文件名 + * @param {Object} options - 转换选项 + */ +async function convertAndDownload(element, filename = 'screenshot.png', options = {}) { + const converter = new HTMLToImage(options); + try { + const dataURL = await converter.toImage(element, options); + converter.downloadImage(dataURL, filename); + console.log('图片下载成功'); + return Promise.resolve(); // 返回成功的Promise + } catch (error) { + console.error('转换下载失败:', error); + return Promise.reject(error); // 返回失败的Promise + } +} + +/** + * 便捷函数:获取图片DataURL + * @param {HTMLElement} element - 要转换的HTML元素 + * @param {Object} options - 转换选项 + * @returns {Promise} + */ +async function getImageDataURL(element, options = {}) { + const converter = new HTMLToImage(options); + return await converter.toImage(element, options); +} + +// 导出到全局作用域 +if (typeof window !== 'undefined') { + window.HTMLToImage = HTMLToImage; + window.convertAndDownload = convertAndDownload; + window.getImageDataURL = getImageDataURL; +} \ No newline at end of file diff --git a/static/js/weekly_report_v3.js b/static/js/weekly_report_v3.js index 8defa94..63efbfc 100644 --- a/static/js/weekly_report_v3.js +++ b/static/js/weekly_report_v3.js @@ -132,6 +132,9 @@ function generateReport() { document.getElementById('report-container').style.display = 'block'; + // 启用图片导出按钮 + updateImageExportButtonState(); + // 显示统计数据 if (data.data.statistics) { displayStatistics(data.data.statistics); @@ -153,33 +156,6 @@ function generateReport() { }); } -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 = ` -
${stat.value}${stat.unit}
-
${stat.label}
- `; - statGrid.appendChild(statItem); - }); -} - function displayCategoryChart(categories) { const categoryItems = document.getElementById('category-items'); categoryItems.innerHTML = ''; @@ -215,6 +191,71 @@ function exportReport() { URL.revokeObjectURL(url); } +/** + * 导出报告为图片 + */ +async function exportReportAsImage() { + const exportBtn = document.getElementById('image-export-btn'); + const reportContent = document.getElementById('report-content'); + + if (!reportContent || reportContent.innerHTML.trim() === '') { + alert('请先生成周报内容'); + return; + } + + if (typeof convertAndDownload === 'undefined') { + alert('图片导出功能未加载,请刷新页面重试'); + return; + } + + try { + // 显示加载中状态 + const originalText = exportBtn.textContent; + exportBtn.disabled = true; + exportBtn.textContent = '导出中...'; + + // 获取当前周数 - 修复选择器 + const week = document.getElementById('week').value || '当前'; + const filename = `运营周报_v3_${week}.png`; + + console.log('开始导出图片...'); + console.log('目标元素:', reportContent); + console.log('文件名:', filename); + + await convertAndDownload(reportContent, filename, { + scale: 2, + backgroundColor: '#ffffff', + useCORS: true, + allowTaint: false + }); + + console.log('图片导出成功'); + alert('图片导出成功!'); + + } catch (error) { + console.error('图片导出失败:', error); + alert(`图片导出失败: ${error.message || error}`); + } finally { + // 恢复按钮状态 + exportBtn.disabled = false; + exportBtn.textContent = '导出图片'; + } +} + +// 启用/禁用图片导出按钮 +function updateImageExportButtonState() { + const reportContent = document.getElementById('report-content'); + const imageExportBtn = document.getElementById('image-export-btn'); + + if (reportContent && reportContent.innerHTML.trim()) { + imageExportBtn.disabled = false; + imageExportBtn.classList.remove('disabled'); + } else { + imageExportBtn.disabled = true; + imageExportBtn.classList.add('disabled'); + } +} + // 支持回车键生成报告 document.getElementById('week').addEventListener('keypress', function (e) { if (e.key === 'Enter') { diff --git a/templates/weekly_report_v3.html b/templates/weekly_report_v3.html index 54df8b9..b662ed5 100644 --- a/templates/weekly_report_v3.html +++ b/templates/weekly_report_v3.html @@ -12,6 +12,22 @@ .deepseek-toggle label { margin-right: 10px; } + .image-export-btn { + background-color: #28a745; + color: white; + border: none; + padding: 8px 16px; + border-radius: 4px; + cursor: pointer; + margin-left: 10px; + } + .image-export-btn:hover { + background-color: #218838; + } + .image-export-btn:disabled { + background-color: #6c757d; + cursor: not-allowed; + } @@ -46,6 +62,7 @@ + @@ -70,6 +87,7 @@ + \ No newline at end of file