update code

This commit is contained in:
lychang
2025-11-20 02:27:22 +08:00
parent 8ff1b80be6
commit bf7ec6737d
10 changed files with 229 additions and 243 deletions

157
app.py
View File

@@ -1,13 +1,16 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Any from typing import Any
from flask import Flask, render_template, jsonify, request import requests
from flask import Flask, jsonify, request, render_template
import json import json
import os import os
import time import time
from chat_session import agent, ChatSessionManager from chat_session import agent, ChatSessionManager
from tools import TimeUtils, now, from_string, from_datetime, format_timestamp, DatabaseManager from file import ObjectFileManager
from tools import TimeUtils, now, from_string, from_datetime, format_timestamp, DatabaseManager, transform_template, \
report_template_render
class MessageRender: class MessageRender:
@@ -138,8 +141,8 @@ class ReportService:
"""获取按小时统计的消息量""" """获取按小时统计的消息量"""
return self.execute_query(''' return self.execute_query('''
SELECT strftime('%H', SELECT strftime('%H',
datetime(substr(time, 0, 11), 'unixepoch', ,'+8 hours')) as hour, datetime(substr(time, 0, 11), 'unixepoch', '+8 hours')) as hour,
COUNT(*) as count COUNT(*) as count
FROM message FROM message
WHERE time >= ? WHERE time >= ?
AND time <= ? AND time <= ?
@@ -187,7 +190,7 @@ class ReportService:
placeholders = ','.join(['?'] * len(message_ids)) placeholders = ','.join(['?'] * len(message_ids))
query = f""" query = f"""
SELECT m.id, m.content, m.type, m.sender, m.receiver,u.name as sender_name, SELECT m.id, m.content, m.type, m.sender, m.receiver,u.name as sender_name,
datetime(substr(time,0,11), 'unixepoch',,'+8 hours') as formatted_time datetime(substr(time,0,11), 'unixepoch','+8 hours') as formatted_time
FROM message m FROM message m
left join user u on m.sender = u.id left join user u on m.sender = u.id
WHERE m.id IN ({placeholders}) WHERE m.id IN ({placeholders})
@@ -200,9 +203,9 @@ class ReportService:
"""获取消息列表""" """获取消息列表"""
query = ''' query = '''
SELECT m.*, SELECT m.*,
u.name as sender_name, u.name as sender_name,
g.name as group_name, g.name as group_name,
datetime(substr(time, 0, 11), 'unixepoch', ,'+8 hours') as formatted_time datetime(substr(time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
FROM message m FROM message m
LEFT JOIN user u ON m.sender = u.id LEFT JOIN user u ON m.sender = u.id
LEFT JOIN "group" g ON m.receiver = g.id LEFT JOIN "group" g ON m.receiver = g.id
@@ -233,7 +236,7 @@ class ReportService:
if not group_id: if not group_id:
query = ''' query = '''
SELECT m.*, SELECT m.*,
u.name as sender_name, u.name as sender_name,
datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
FROM message m FROM message m
LEFT JOIN user u ON m.sender = u.id LEFT JOIN user u ON m.sender = u.id
@@ -246,7 +249,7 @@ class ReportService:
else: else:
query = ''' query = '''
SELECT m.*, SELECT m.*,
u.name as sender_name, u.name as sender_name,
datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
FROM message m FROM message m
LEFT JOIN user u ON m.sender = u.id LEFT JOIN user u ON m.sender = u.id
@@ -267,7 +270,7 @@ class ReportService:
AND type NOT IN ('disagree', 'agree', 'weapp', 'link') AND type NOT IN ('disagree', 'agree', 'weapp', 'link')
''', (group_id,), fetch_all=False)['count'] ''', (group_id,), fetch_all=False)['count']
lastest_time = self.execute_query(''' lastest_time = self.execute_query('''
SELECT datetime(substr(time, 0, 11), 'unixepoch', ,'+8 hours') as formatted_time SELECT datetime(substr(time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
FROM message FROM message
WHERE receiver = ? WHERE receiver = ?
AND type NOT IN ('disagree', 'agree', 'weapp', 'link') AND type NOT IN ('disagree', 'agree', 'weapp', 'link')
@@ -299,9 +302,9 @@ class ReportService:
if group_id: if group_id:
query = ''' query = '''
SELECT m.*, SELECT m.*,
u.name as sender_name, u.name as sender_name,
g.name as group_name, g.name as group_name,
datetime(substr(m.time, 0, 11), 'unixepoch', ,'+8 hours') as formatted_time datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
FROM message m FROM message m
LEFT JOIN user u ON m.sender = u.id LEFT JOIN user u ON m.sender = u.id
LEFT JOIN "group" g ON m.receiver = g.id LEFT JOIN "group" g ON m.receiver = g.id
@@ -315,9 +318,9 @@ class ReportService:
else: else:
query = ''' query = '''
SELECT m.*, SELECT m.*,
u.name as sender_name, u.name as sender_name,
g.name as group_name, g.name as group_name,
datetime(substr(m.time, 0, 11), 'unixepoch', ,'+8 hours') as formatted_time datetime(substr(m.time, 0, 11), 'unixepoch', '+8 hours') as formatted_time
FROM message m FROM message m
LEFT JOIN user u ON m.sender = u.id LEFT JOIN user u ON m.sender = u.id
LEFT JOIN "group" g ON m.receiver = g.id LEFT JOIN "group" g ON m.receiver = g.id
@@ -469,19 +472,22 @@ class ReportService:
app = Flask(__name__) app = Flask(__name__)
app.template_folder = 'templates' app.template_folder = 'templates'
# 初始化管理器 # 初始化管理器
ofm = ObjectFileManager()
db_manager = DatabaseManager('db/data', online_db_uri="zysl-log/wework/chat_msg/db/data.db") db_manager = DatabaseManager('db/data', online_db_uri="zysl-log/wework/chat_msg/db/data.db")
report_service = ReportService(db_manager) report_service = ReportService(db_manager)
message_processor = MessageRender() message_processor = MessageRender()
chat_session = ChatSessionManager(db_manager) chat_session = ChatSessionManager(db_manager)
def api_response(success=True, data=None, error = None, status_code=200): def api_response(success=True, data=None, error=None, status_code=200, content=None):
"""统一的API响应格式""" """统一的API响应格式"""
response = {'success': success} response = {'success': success}
if error: if error:
response['error'] = str(error) response['error'] = str(error)
if data: if data:
response.update(data) response.update(data)
if content:
response["data"] = content
return jsonify(response), status_code return jsonify(response), status_code
@@ -509,11 +515,13 @@ def daily_report_page():
"""日报页面""" """日报页面"""
return render_template('daily_report.html') return render_template('daily_report.html')
@app.route('/markdown') @app.route('/markdown')
def markdown_page(): def markdown_page():
"""markdown页面""" """markdown页面"""
return render_template('markdown.html') return render_template('markdown.html')
@app.route('/api/db/datetime') @app.route('/api/db/datetime')
def db_status(): def db_status():
return api_response(success=True, data={'message': chat_session.status()}) return api_response(success=True, data={'message': chat_session.status()})
@@ -670,21 +678,21 @@ def search_messages():
return api_response(success=False, error='请输入搜索关键词', status_code=400) return api_response(success=False, error='请输入搜索关键词', status_code=400)
group_id = request.args.get('group_id', type=str) group_id = request.args.get('group_id', type=str)
# 智能搜索:支持模糊匹配和精确匹配 # 智能搜索:支持模糊匹配和精确匹配
messages_data = report_service.search_messages(keyword, limit, group_id) messages_data = report_service.search_messages(keyword, limit, group_id)
# 根据搜索类型过滤消息 # 根据搜索类型过滤消息
if search_type == 'question': if search_type == 'question':
messages_data = [msg for msg in messages_data if msg.get('type') in ['text', 'mixed']] messages_data = [msg for msg in messages_data if msg.get('type') in ['text', 'mixed']]
elif search_type == 'answer': elif search_type == 'answer':
messages_data = [msg for msg in messages_data if msg.get('type') in ['text', 'mixed']] messages_data = [msg for msg in messages_data if msg.get('type') in ['text', 'mixed']]
formatted_messages = [] formatted_messages = []
for msg in messages_data: for msg in messages_data:
message = dict(msg) message = dict(msg)
message = message_processor.process_message_content(message) message = message_processor.process_message_content(message)
# 添加搜索关键词高亮 # 添加搜索关键词高亮
if keyword: if keyword:
content = message.get('content', '') content = message.get('content', '')
@@ -692,7 +700,7 @@ def search_messages():
# 简单的关键词高亮(前端可以进一步处理) # 简单的关键词高亮(前端可以进一步处理)
highlighted_content = content.replace(keyword, f'<mark>{keyword}</mark>') highlighted_content = content.replace(keyword, f'<mark>{keyword}</mark>')
message['highlighted_content'] = highlighted_content message['highlighted_content'] = highlighted_content
formatted_messages.append(message) formatted_messages.append(message)
return api_response(data={'messages': formatted_messages, 'total': len(formatted_messages)}) return api_response(data={'messages': formatted_messages, 'total': len(formatted_messages)})
@@ -707,13 +715,13 @@ def search_suggestions():
"""搜索建议API""" """搜索建议API"""
try: try:
keyword = request.args.get('keyword', '') keyword = request.args.get('keyword', '')
if not keyword: if not keyword:
return api_response(data={'suggestions': []}) return api_response(data={'suggestions': []})
# 获取热门搜索关键词 # 获取热门搜索关键词
suggestions = [] suggestions = []
# 1. 从消息内容中提取相关关键词 # 1. 从消息内容中提取相关关键词
try: try:
messages = report_service.search_messages(keyword, 10, None) messages = report_service.search_messages(keyword, 10, None)
@@ -731,7 +739,7 @@ def search_suggestions():
break break
except: except:
pass pass
# 2. 添加常见搜索关键词 # 2. 添加常见搜索关键词
common_keywords = ['信息披露', '公司治理', '业务操作', '系统填报', '政策法规', '季度报告', '股东大会'] common_keywords = ['信息披露', '公司治理', '业务操作', '系统填报', '政策法规', '季度报告', '股东大会']
for kw in common_keywords: for kw in common_keywords:
@@ -739,9 +747,9 @@ def search_suggestions():
suggestions.append(kw) suggestions.append(kw)
if len(suggestions) >= 8: if len(suggestions) >= 8:
break break
return api_response(data={'suggestions': suggestions[:8]}) return api_response(data={'suggestions': suggestions[:8]})
except Exception as e: except Exception as e:
return api_response(success=False, error=e, status_code=500) return api_response(success=False, error=e, status_code=500)
@@ -756,34 +764,34 @@ def get_qa_list():
keyword = request.args.get('keyword', '') keyword = request.args.get('keyword', '')
start_time = request.args.get('start_time', '') start_time = request.args.get('start_time', '')
end_time = request.args.get('end_time', '') end_time = request.args.get('end_time', '')
# 构建查询条件 # 构建查询条件
query_conditions = [] query_conditions = []
query_params = [] query_params = []
if group_id: if group_id:
query_conditions.append("groupId LIKE ?") query_conditions.append("groupId LIKE ?")
query_params.append(f"%{group_id}%") query_params.append(f"%{group_id}%")
if start_time and end_time: if start_time and end_time:
query_conditions.append("start_time >= ? AND end_time <= ?") query_conditions.append("start_time >= ? AND end_time <= ?")
query_params.extend([f"{start_time}000", f"{end_time}999"]) query_params.extend([f"{start_time}000", f"{end_time}999"])
# 构建查询语句 # 构建查询语句
base_query = "SELECT * FROM qa" base_query = "SELECT * FROM qa"
if query_conditions: if query_conditions:
base_query += " WHERE " + " AND ".join(query_conditions) base_query += " WHERE " + " AND ".join(query_conditions)
base_query += " ORDER BY id DESC" base_query += " ORDER BY id DESC"
# 执行查询 # 执行查询
conn = db_manager.get_connection() conn = db_manager.get_connection()
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(base_query, query_params) cursor.execute(base_query, query_params)
qa_data = cursor.fetchall() qa_data = cursor.fetchall()
conn.close() conn.close()
qa_list = [dict(qa) for qa in qa_data] qa_list = [dict(qa) for qa in qa_data]
# 如果有关键词搜索,进行内容过滤 # 如果有关键词搜索,进行内容过滤
if keyword: if keyword:
filtered_qa_list = [] filtered_qa_list = []
@@ -792,7 +800,7 @@ def get_qa_list():
if keyword.lower() in (qa.get('groupId') or '').lower(): if keyword.lower() in (qa.get('groupId') or '').lower():
filtered_qa_list.append(qa) filtered_qa_list.append(qa)
continue continue
# 检查问题内容是否匹配 # 检查问题内容是否匹配
try: try:
question_ids = json.loads(qa.get('question', '[]')) question_ids = json.loads(qa.get('question', '[]'))
@@ -805,9 +813,9 @@ def get_qa_list():
break break
except: except:
pass pass
qa_list = filtered_qa_list qa_list = filtered_qa_list
return api_response(data={'qa_list': qa_list, 'total': len(qa_list)}) return api_response(data={'qa_list': qa_list, 'total': len(qa_list)})
except Exception as e: except Exception as e:
return api_response(success=False, error=e, status_code=500) return api_response(success=False, error=e, status_code=500)
@@ -848,7 +856,8 @@ def create_qa():
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(''' cursor.execute('''
INSERT INTO qa (groupId, question, answer,start_time,end_time) VALUES (?,?,?,?,?) INSERT INTO qa (groupId, question, answer, start_time, end_time)
VALUES (?, ?, ?, ?, ?)
''', (group_id, json.dumps(question_ids), json.dumps(answer_ids), ''', (group_id, json.dumps(question_ids), json.dumps(answer_ids),
data.get('start_time', ''), data.get('end_time', ''))) data.get('start_time', ''), data.get('end_time', '')))
@@ -918,28 +927,29 @@ def delete_qa(qa_id):
except Exception as e: except Exception as e:
return api_response(success=False, error=e, status_code=500) return api_response(success=False, error=e, status_code=500)
@app.route('/api/qa/batch', methods=['DELETE']) @app.route('/api/qa/batch', methods=['DELETE'])
def batch_delete_qa(): def batch_delete_qa():
"""批量删除QA记录API""" """批量删除QA记录API"""
try: try:
data = request.get_json() data = request.get_json()
qa_ids = data.get('qa_ids', []) qa_ids = data.get('qa_ids', [])
if not qa_ids: if not qa_ids:
return api_response(success=False, error='请提供要删除的QA记录ID列表', status_code=400) return api_response(success=False, error='请提供要删除的QA记录ID列表', status_code=400)
# 检查所有记录是否存在 # 检查所有记录是否存在
for qa_id in qa_ids: for qa_id in qa_ids:
if not report_service.check_qa_exists(qa_id): if not report_service.check_qa_exists(qa_id):
return api_response(success=False, error=f'QA记录 {qa_id} 不存在', status_code=404) return api_response(success=False, error=f'QA记录 {qa_id} 不存在', status_code=404)
conn = db_manager.get_connection() conn = db_manager.get_connection()
cursor = conn.cursor() cursor = conn.cursor()
# 构建批量删除语句 # 构建批量删除语句
placeholders = ','.join(['?'] * len(qa_ids)) placeholders = ','.join(['?'] * len(qa_ids))
query = f'DELETE FROM qa WHERE id IN ({placeholders})' query = f'DELETE FROM qa WHERE id IN ({placeholders})'
cursor.execute(query, qa_ids) cursor.execute(query, qa_ids)
conn.commit() conn.commit()
conn.close() conn.close()
@@ -993,7 +1003,6 @@ def generate_daily_report():
# 获取热点话题 # 获取热点话题
recent_messages = report_service.get_recent_messages_by_time_range(start_timestamp, end_timestamp, 100) recent_messages = report_service.get_recent_messages_by_time_range(start_timestamp, end_timestamp, 100)
hot_topics = report_service.analyze_hot_topics(recent_messages) hot_topics = report_service.analyze_hot_topics(recent_messages)
print(start_timestamp,end_timestamp)
# 生成直方图 # 生成直方图
group_activity_chart = report_service.generate_group_activity_chart(group_activity_data) group_activity_chart = report_service.generate_group_activity_chart(group_activity_data)
hourly_activity_chart = report_service.generate_hourly_activity_chart(start_timestamp, end_timestamp) hourly_activity_chart = report_service.generate_hourly_activity_chart(start_timestamp, end_timestamp)
@@ -1167,25 +1176,61 @@ def generate_weekly_report():
return api_response(success=False, error=e, status_code=500) return api_response(success=False, error=e, status_code=500)
@app.route('/api/statistics', methods=['POST']) @app.route('/api/render/template', methods=['POST'])
def report_render_template():
data = request.get_json()
template = data.get('template', '')
origin_path = data.get('path', '')
file = ofm.get_file(origin_path)
with open("a.docx", 'wb') as f:
f.write(file)
if file:
template_content = transform_template(file)
response = ofm.upload_file(f"tpldev/render/{template}.template", template_content.encode('utf-8'))
print(response)
if response:
return api_response(success=True,
data={"template_name": template, "template_path": f"tpldev/render/{template}.template"},
status_code=200)
return api_response(success=False, error="上传模板失败", status_code=500)
@app.route('/api/render/insight', methods=['GET', 'POST'])
def report_render(): def report_render():
"""获取统计数据API""" """获取统计数据API"""
try: try:
data = request.get_json() if request.method == 'GET':
security_code = data.get('security_code', '') security_code = request.args.get('security_code', '')
else:
data = request.get_json()
security_code = data.get('security_code', '')
url = "http://10.0.0.39/cloud/in/api/online/run/cache" url = "http://10.0.0.39/cloud/in/api/online/run/cache"
body = { body = {
"pid": "1988527857586081794", "pid": "1988527857586081794",
"fid": "1988527857586081794", "fid": "1988527857586081794",
"secCode": security_code "secCode": security_code
} }
return api_response(data=statistics) resp = requests.post(url=url, json=body)
if resp.status_code != 200:
return api_response(success=False, error=resp.text, status_code=500)
else:
data_info = resp.json().get('data', {}).get('data', '')
data_info = data_info.replace("data:application/json,", "")
data_info = json.loads(data_info)
data_path = data_info.get('url', '')
data = requests.get(data_path)
template_content = ofm.get_file("tpldev/render/insight.template")
html = report_template_render(template_content.decode("utf-8"), data=data.json())
if request.method == 'GET':
return html
else:
return api_response(success=False, content=html, status_code=200)
except Exception as e: except Exception as e:
return api_response(success=False, error=e, status_code=500) return api_response(success=False, error=e, status_code=500)
if __name__ == '__main__': if __name__ == '__main__':
# 检查数据库文件是否存在 # 检查数据库文件是否存在
if not os.path.exists('db/data'): if not os.path.exists('db/data'):
@@ -1195,8 +1240,8 @@ if __name__ == '__main__':
print("下载完成") print("下载完成")
print("启动聊天网页服务...") print("启动聊天网页服务...")
print("访问地址: http://0.0.0.0:9000") print("访问地址: http://localhost:9000")
print("问答页面: http://0.0.0.0:9000/qa") print("问答页面: http://localhost:9000/qa")
print("周报页面: http://0.0.0.0:9000/weekly_report") print("周报页面: http://localhost:9000/weekly_report")
print("日报页面: http://0.0.0.0:9000/daily_report") print("日报页面: http://localhost:9000/daily_report")
app.run(debug=True, host='0.0.0.0', port=9000) app.run(debug=True, host='0.0.0.0', port=9000)

View File

@@ -558,13 +558,13 @@ class ChatSessionManager:
end_time = convert_to_utc(end_time_str, source_timezone="Asia/Shanghai").timestamp() end_time = convert_to_utc(end_time_str, source_timezone="Asia/Shanghai").timestamp()
res = self.dbm.execute_query(''' res = self.dbm.execute_query('''
SELECT message.time, message.content SELECT message.time, message.content
FROM message FROM message
WHERE (message.type = 'text' WHERE (message.type = 'text'
or message.type = 'mixed') or message.type = 'mixed')
AND message.time >= ? AND message.time >= ?
AND message.time <= ? AND message.time <= ?
''', (str(start_time).replace(".0", "000"), str(end_time).replace(".0", "000"))) ''', (str(start_time).replace(".0", "000"), str(end_time).replace(".0", "000")))
return res return res
@staticmethod @staticmethod
@@ -636,7 +636,7 @@ class ChatSessionManager:
group_qa: 问答数据字典,键为群ID,值为问答列表 group_qa: 问答数据字典,键为群ID,值为问答列表
""" """
data = [] data = []
index_id = self.execute_query(f"SELECT id FROM qa order by id desc limit 1",fetch_all=False)[0] index_id = self.execute_query(f"SELECT id FROM qa order by id desc limit 1", fetch_all=False)[0]
for r in group_qa: for r in group_qa:
qas = group_qa.get(r, {}) qas = group_qa.get(r, {})

BIN
db/data

Binary file not shown.

View File

@@ -25,7 +25,7 @@ class BaseFileManager(ABC):
if headers is None: if headers is None:
headers = {} headers = {}
resp = requests.put(url, data=raw, headers=headers) resp = requests.put(url, data=raw, headers=headers)
if resp.status_code != 201 or resp.status_code != 200: if resp.status_code not in [201, 200]:
return {} return {}
return resp.json() return resp.json()

View File

@@ -1,6 +1,30 @@
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]; document.getElementById('date').value = new Date().toISOString().split('T')[0];
function showLoading() { function showLoading() {
document.getElementById('loading').style.display = 'block'; document.getElementById('loading').style.display = 'block';
document.getElementById('report-container').style.display = 'none'; document.getElementById('report-container').style.display = 'none';
@@ -38,14 +62,14 @@ function generateReport() {
if (data.success) { if (data.success) {
// 保存原始Markdown内容到data属性 // 保存原始Markdown内容到data属性
document.getElementById('report-content').setAttribute('data-markdown', data.report); document.getElementById('report-content').setAttribute('data-markdown', data.report);
// 将Markdown转换为HTML并显示 // 使用专业的Markdown解析器将Markdown转换为HTML并显示
const htmlContent = markdownToHtml(data.report); const htmlContent = parseMarkdownWithTables(data.report);
document.getElementById('report-content').innerHTML = htmlContent; document.getElementById('report-content').innerHTML = htmlContent;
// 重置为预览模式 // 重置为预览模式
togglePreview('preview'); togglePreview('preview');
document.getElementById('report-container').style.display = 'block'; document.getElementById('report-container').style.display = 'block';
// 显示统计数据 // 显示统计数据
@@ -95,8 +119,8 @@ function exportReport() {
showError('请先生成日报'); showError('请先生成日报');
return; return;
} }
const blob = new Blob([reportContent], { type: 'text/markdown' }); const blob = new Blob([reportContent], {type: 'text/markdown'});
const url = URL.createObjectURL(blob); const url = URL.createObjectURL(blob);
const a = document.createElement('a'); const a = document.createElement('a');
a.href = url; a.href = url;
@@ -110,4 +134,15 @@ function exportReport() {
// 页面加载时自动生成今日日报 // 页面加载时自动生成今日日报
window.addEventListener('load', function () { window.addEventListener('load', function () {
generateTodayReport(); 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);
}
}

View File

@@ -1,132 +0,0 @@
// Markdown转HTML的简单实现
function markdownToHtml(markdown) {
// 替换标题
let html = markdown
.replace(/^# (.*$)/gm, '<h1>$1</h1>')
.replace(/^## (.*$)/gm, '<h2>$1</h2>')
.replace(/^### (.*$)/gm, '<h3>$1</h3>')
.replace(/^#### (.*$)/gm, '<h4>$1</h4>')
.replace(/^##### (.*$)/gm, '<h5>$1</h5>')
.replace(/^###### (.*$)/gm, '<h6>$1</h6>');
// 替换粗体
html = html.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
// 替换斜体
html = html.replace(/\*(.*?)\*/g, '<em>$1</em>');
// 替换代码块
html = html.replace(/```([\s\S]*?)```/g, '<pre><code>$1</code></pre>');
// 替换行内代码
html = html.replace(/`(.*?)`/g, '<code>$1</code>');
// 替换无序列表
html = html.replace(/^\s*[-*] (.*$)/gm, '<li>$1</li>');
html = html.replace(/(<li>.*<\/li>)/s, '<ul>$1</ul>');
// 替换有序列表
html = html.replace(/^\s*\d+\. (.*$)/gm, '<li>$1</li>');
html = html.replace(/(<li>.*<\/li>)/s, '<ol>$1</ol>');
// 替换引用块
html = html.replace(/^> (.*$)/gm, '<blockquote>$1</blockquote>');
// 替换链接
html = html.replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2" target="_blank">$1</a>');
// 替换图片
html = html.replace(/!\[(.*?)\]\((.*?)\)/g, '<img src="$2" alt="$1" style="max-width: 100%;">');
// 替换表格(增强实现)
html = html.replace(/\|(.+)\|\n\|([-|: ]+)\|\n((?:\|.+\|\n)*)/g, function(match, headerRow, separatorRow, bodyRows) {
// 解析表头
const headers = headerRow.split('|').filter(cell => cell.trim());
// 解析分隔行,确定对齐方式
const separators = separatorRow.split('|').filter(cell => cell.trim());
const alignments = separators.map(sep => {
if (sep.startsWith(':') && sep.endsWith(':')) return 'center';
if (sep.endsWith(':')) return 'right';
return 'left';
});
// 构建表头HTML
let tableHtml = '<table style="border-collapse: collapse; width: 100%; margin: 10px 0; border: 1px solid #ddd;">';
tableHtml += '<thead><tr style="background-color: #f5f5f5;">';
headers.forEach((header, index) => {
const alignment = alignments[index] || 'left';
tableHtml += `<th style="border: 1px solid #ddd; padding: 8px; text-align: ${alignment}; font-weight: bold;">${header.trim()}</th>`;
});
tableHtml += '</tr></thead>';
// 解析表格主体
const bodyRowsArray = bodyRows.trim().split('\n');
if (bodyRowsArray.length > 0) {
tableHtml += '<tbody>';
bodyRowsArray.forEach(row => {
const cells = row.split('|').filter(cell => cell.trim());
if (cells.length > 0) {
tableHtml += '<tr>';
cells.forEach((cell, index) => {
const alignment = alignments[index] || 'left';
tableHtml += `<td style="border: 1px solid #ddd; padding: 8px; text-align: ${alignment};">${cell.trim()}</td>`;
});
tableHtml += '</tr>';
}
});
tableHtml += '</tbody>';
}
tableHtml += '</table>';
return tableHtml;
});
// 处理简单的单行表格(向后兼容)
html = html.replace(/^\|(.+)\|$/gm, function(match) {
const cells = match.split('|').filter(cell => cell.trim());
if (cells.length > 0) {
return '<table style="border-collapse: collapse; width: 100%; margin: 10px 0; border: 1px solid #ddd;"><tr>' +
cells.map(cell => `<td style="border: 1px solid #ddd; padding: 8px;">${cell.trim()}</td>`).join('') +
'</tr></table>';
}
return match;
});
// 替换水平分隔线(---、***、___)
html = html.replace(/^\s*[-*_]{3,}\s*$/gm, '<hr>');
// 将连续的空行转换为段落
html = html.replace(/\n\n+/g, '</p><p>');
html = '<p>' + html.replace(/\n/g, '<br>') + '</p>';
// 清理多余的段落标签
html = html.replace(/<p><\/p>/g, '');
return html;
}
// 切换预览模式函数
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;
}
};

View File

@@ -1,3 +1,27 @@
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;
}
};
// 设置默认周次为当前周 // 设置默认周次为当前周
function getCurrentWeek() { function getCurrentWeek() {
const now = new Date(); const now = new Date();
@@ -8,7 +32,7 @@ function getCurrentWeek() {
} }
// 页面加载时设置默认周次 // 页面加载时设置默认周次
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function () {
document.getElementById('week').value = getCurrentWeek(); document.getElementById('week').value = getCurrentWeek();
}); });
@@ -33,42 +57,41 @@ function generateCurrentWeekReport() {
} }
function generateReport() { function generateReport() {
const week = document.getElementById('week').value; const week = document.getElementById('week').value;
if (!week) { if (!week) {
showError('请选择周次'); showError('请选择周次');
return; return;
} }
// 转换格式:YYYY-WWW 到 YYYY-WW // 转换格式:YYYY-WWW 到 YYYY-WW
const weekFormatted = week.replace('W', ''); const weekFormatted = week.replace('W', '');
showLoading(); showLoading();
fetch(`/api/reports/weekly?week=${weekFormatted}`) fetch(`/api/reports/weekly?week=${weekFormatted}`)
.then(response => response.json()) .then(response => response.json())
.then(data => { .then(data => {
hideLoading(); hideLoading();
if (data.success) { if (data.success) {
// 保存原始Markdown内容到data属性 // 保存原始Markdown内容到data属性
document.getElementById('report-content').setAttribute('data-markdown', data.report); document.getElementById('report-content').setAttribute('data-markdown', data.report);
// 将Markdown转换为HTML并显示 // 使用专业的Markdown解析器将Markdown转换为HTML并显示
const htmlContent = markdownToHtml(data.report); const htmlContent = parseMarkdownWithTables(data.report);
document.getElementById('report-content').innerHTML = htmlContent; document.getElementById('report-content').innerHTML = htmlContent;
// 重置为预览模式 // 重置为预览模式
togglePreview('preview'); togglePreview('preview');
document.getElementById('report-container').style.display = 'block'; document.getElementById('report-container').style.display = 'block';
// 显示统计数据 // 显示统计数据
if (data.statistics) { if (data.statistics) {
displayStatistics(data.statistics); displayStatistics(data.statistics);
document.getElementById('statistics').style.display = 'block'; document.getElementById('statistics').style.display = 'block';
// 显示分类图表 // 显示分类图表
if (data.statistics.category_breakdown) { if (data.statistics.category_breakdown) {
displayCategoryChart(data.statistics.category_breakdown); displayCategoryChart(data.statistics.category_breakdown);
@@ -88,17 +111,17 @@ function generateReport() {
function displayStatistics(stats) { function displayStatistics(stats) {
const statGrid = document.getElementById('stat-grid'); const statGrid = document.getElementById('stat-grid');
statGrid.innerHTML = ''; statGrid.innerHTML = '';
const statistics = [ const statistics = [
{ label: '本周消息量', value: stats.week_message_count, unit: '条' }, {label: '本周消息量', value: stats.week_message_count, unit: '条'},
{ label: '上周消息量', value: stats.last_week_message_count, unit: '条' }, {label: '上周消息量', value: stats.last_week_message_count, unit: '条'},
{ label: '消息变化率', value: stats.message_change_rate.toFixed(1), unit: '%' }, {label: '消息变化率', value: stats.message_change_rate.toFixed(1), unit: '%'},
{ label: '本周活跃用户', value: stats.week_active_users, unit: '人' }, {label: '本周活跃用户', value: stats.week_active_users, unit: '人'},
{ label: '群组数量', value: stats.group_count, unit: '个' }, {label: '群组数量', value: stats.group_count, unit: '个'},
{ label: '成员总数', value: stats.member_count, unit: '人' }, {label: '成员总数', value: stats.member_count, unit: '人'},
{ label: '问答记录数', value: stats.qa_count, unit: '条' } {label: '问答记录数', value: stats.qa_count, unit: '条'}
]; ];
statistics.forEach(stat => { statistics.forEach(stat => {
const statItem = document.createElement('div'); const statItem = document.createElement('div');
statItem.className = 'stat-item'; statItem.className = 'stat-item';
@@ -113,7 +136,7 @@ function displayStatistics(stats) {
function displayCategoryChart(categories) { function displayCategoryChart(categories) {
const categoryItems = document.getElementById('category-items'); const categoryItems = document.getElementById('category-items');
categoryItems.innerHTML = ''; categoryItems.innerHTML = '';
Object.entries(categories).forEach(([category, percentage]) => { Object.entries(categories).forEach(([category, percentage]) => {
const categoryItem = document.createElement('div'); const categoryItem = document.createElement('div');
categoryItem.className = 'category-item'; categoryItem.className = 'category-item';
@@ -132,8 +155,8 @@ function exportReport() {
showError('请先生成周报'); showError('请先生成周报');
return; return;
} }
const blob = new Blob([reportContent], { type: 'text/markdown' }); const blob = new Blob([reportContent], {type: 'text/markdown'});
const url = URL.createObjectURL(blob); const url = URL.createObjectURL(blob);
const a = document.createElement('a'); const a = document.createElement('a');
a.href = url; a.href = url;
@@ -145,13 +168,24 @@ function exportReport() {
} }
// 页面加载时自动生成本周周报 // 页面加载时自动生成本周周报
window.addEventListener('load', function() { window.addEventListener('load', function () {
generateCurrentWeekReport(); generateCurrentWeekReport();
}); });
// 支持回车键生成报告 // 支持回车键生成报告
document.getElementById('week').addEventListener('keypress', function(e) { document.getElementById('week').addEventListener('keypress', function (e) {
if (e.key === 'Enter') { if (e.key === 'Enter') {
generateReport(); 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);
}
}

View File

@@ -53,7 +53,8 @@
<div id="error" class="error" style="display: none;"></div> <div id="error" class="error" style="display: none;"></div>
</div> </div>
<script src="/static/js/markdown-render.js"></script> <script src="/static/js/markdown-parser.js"></script>
<script src="/static/js/markdown-plugin-table.js"></script>
<script src="/static/js/daily_report.js"></script> <script src="/static/js/daily_report.js"></script>
</body> </body>
</html> </html>

View File

@@ -56,7 +56,8 @@
<div id="error" class="error" style="display: none;"></div> <div id="error" class="error" style="display: none;"></div>
</div> </div>
<script src="/static/js/markdown-render.js"></script> <script src="/static/js/markdown-parser.js"></script>
<script src="/static/js/markdown-plugin-table.js"></script>
<script src="/static/js/weekly_report.js"></script> <script src="/static/js/weekly_report.js"></script>
</body> </body>
</html> </html>

View File

@@ -6,13 +6,12 @@ import sqlite3
from file import ObjectFileManager, LocalFileManager from file import ObjectFileManager, LocalFileManager
import re import re
from io import BytesIO
import html2text
import jinja2 import jinja2
import mammoth import mammoth
def transform_docx2md(file_bytes: bytes, data: dict): def transform_template(file_bytes: bytes):
html_head = """ html_head = """
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
@@ -31,8 +30,8 @@ def transform_docx2md(file_bytes: bytes, data: dict):
html_tail = """ html_tail = """
</body> </body>
</html>""" </html>"""
file_obj = BytesIO(file_bytes)
result = mammoth.convert_to_html(file_bytes) result = mammoth.convert_to_html(file_obj)
html = result.value html = result.value
html = re.sub('<img.*?>', '', html) html = re.sub('<img.*?>', '', html)
c = re.compile(r'{(.*?)}') c = re.compile(r'{(.*?)}')
@@ -62,9 +61,12 @@ def transform_docx2md(file_bytes: bytes, data: dict):
'\n{% for ' + lc[0] + ' in data.' + lc[0] + ' %}\n<tr><td><p>') '\n{% for ' + lc[0] + ' in data.' + lc[0] + ' %}\n<tr><td><p>')
else: else:
print(lc[0], lc) print(lc[0], lc)
html = jinja2.Template(html_head + html + html_tail).render(data=data) return html_head + html + html_tail
markdown = html2text.html2text(html)
return html, markdown
def report_template_render(template: str, data: dict):
html = jinja2.Template(template).render(data=data)
return html
class DatabaseManager: class DatabaseManager: