feat: 数据源统一与前端预览修复

=== 后端核心 ===
- db_helper: 统一数据库访问抽象层
- system.py API:
  * 参数绑定修复: 使用 Body(embed=True) 接收 JSON
  * 添加请求日志记录
- sync.py: 仅导出 DB→JSON(备份)

=== 合规与流水线 ===
- compliance_checker: 标签检测优化(仅检查容器,避免正文误判)
- 所有脚本(creator/collector/writer/outline/research等)统一使用数据库

=== 前端改版 ===
- topics.html:
  * 创作/优化 API 路径修正
  * 预览弹窗重设计:多平台并行加载、富文本显示、单复制按钮
  * 状态中文映射(getStatusLabel)
  * 认证检查
- 所有 HTML 静态资源路径修复(移除 /static 前缀)

=== 数据一致性 ===
- 数据库状态统一为英文(pending/review/ready/published)
- 前端显示中文化映射

已测试 A03 流水线完整通过。
This commit is contained in:
lt
2026-05-07 11:25:42 +08:00
parent 8dd19a2179
commit 31d6306e3b
24 changed files with 1018 additions and 530 deletions
+42 -21
View File
@@ -41,6 +41,7 @@ logging.basicConfig(
)
logger = logging.getLogger(__name__)
@dataclass
class SustainabilitySource:
"""可持续性信息源"""
@@ -511,33 +512,53 @@ class SustainabilityCollector:
logger.info(f"保存了 {len(self.new_cases)} 个案例和 {len(self.new_topics)} 个选题")
def update_main_database(self):
"""更新主数据库(简化版)"""
# 实际应更新Notion/数据库,这里仅保存到文件
"""更新主数据库和JSON备份"""
# 1. 更新案例库 (sustainability_cases.json)
main_cases_file = DATA_DIR / "sustainability_cases.json"
main_topics_file = DATA_DIR / "sustainability_topics.json"
# 读取现有数据
existing_cases = []
existing_topics = []
if main_cases_file.exists():
with open(main_cases_file, 'r', encoding='utf-8') as f:
existing_cases = json.load(f)
if main_topics_file.exists():
with open(main_topics_file, 'r', encoding='utf-8') as f:
existing_topics = json.load(f)
# 合并新数据
try:
with open(main_cases_file, 'r', encoding='utf-8') as f:
existing_cases = json.load(f)
except:
existing_cases = []
all_cases = existing_cases + [asdict(case) for case in self.new_cases]
all_topics = existing_topics + [asdict(topic) for topic in self.new_topics]
# 保存(限制总数)
# 去重
seen = set()
unique_cases = []
for c in all_cases:
title = c.get('title', '').strip()
if title and title not in seen:
seen.add(title)
unique_cases.append(c)
unique_cases.sort(key=lambda x: x.get('collection_date', ''), reverse=True)
with open(main_cases_file, 'w', encoding='utf-8') as f:
json.dump(all_cases[:100], f, ensure_ascii=False, indent=2)
json.dump(unique_cases[:200], f, ensure_ascii=False, indent=2)
logger.info(f"案例库更新: 总计 {len(unique_cases)} 个案例 (新增 {len(self.new_cases)})")
with open(main_topics_file, 'w', encoding='utf-8') as f:
json.dump(all_topics[:50], f, ensure_ascii=False, indent=2)
# 2. 更新选题数据库 (主数据源)
try:
from db_helper import save_topics_to_db
save_topics_to_db([asdict(topic) for topic in self.new_topics])
logger.info(f"选题数据库更新: 处理了 {len(self.new_topics)} 个选题")
except Exception as e:
logger.error(f"选题数据库保存失败: {e}")
# 3. 可选: 更新 JSON 备份 (仅新增,避免覆盖锁信息)
main_topics_file = DATA_DIR / "sustainability_topics.json"
try:
if main_topics_file.exists():
with open(main_topics_file, 'r', encoding='utf-8') as f:
existing_topics = json.load(f)
else:
existing_topics = []
existing_ids = {t['id'] for t in existing_topics}
new_additions = [asdict(topic) for topic in self.new_topics if topic.id not in existing_ids]
existing_topics.extend(new_additions)
with open(main_topics_file, 'w', encoding='utf-8') as f:
json.dump(existing_topics, f, ensure_ascii=False, indent=2)
except Exception as e:
logger.error(f"JSON备份失败: {e}")
def send_wecom_notification(self):
"""发送企业微信通知"""