feat: version introduction support db source with json fallback

This commit is contained in:
wxy
2026-03-26 11:50:36 +08:00
parent c13c3846d1
commit 9d2f3aa8f9
2 changed files with 79 additions and 11 deletions

View File

@@ -2,7 +2,7 @@ from datetime import datetime, timedelta
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from sqlalchemy import func from sqlalchemy import func
from uuid import UUID from uuid import UUID
from typing import Dict from typing import Dict, Optional, Any
from app.models.end_user_model import EndUser from app.models.end_user_model import EndUser
from app.models.user_model import User from app.models.user_model import User
@@ -190,4 +190,63 @@ class HomePageRepository:
user_count_dict = {workspace_id: count for workspace_id, count in user_counts} user_count_dict = {workspace_id: count for workspace_id, count in user_counts}
return workspaces, app_count_dict, user_count_dict return workspaces, app_count_dict, user_count_dict
@staticmethod
def get_version_introduction(db: Session, version: str) -> Optional[Dict[str, Any]]:
"""
从数据库获取版本说明(优先读取已发布的版本)
使用反射方式读取表结构,不依赖 premium 模型类
Args:
db: 数据库会话
version: 版本号,如 "v0.2.7"
Returns:
版本说明字典,格式与 version_info.json 一致
如果数据库中没有该版本,返回 None
"""
try:
from sqlalchemy import Table, MetaData
metadata = MetaData()
version_notes = Table('version_notes', metadata, autoload_with=db.engine)
version_note_items = Table('version_note_items', metadata, autoload_with=db.engine)
note = db.query(version_notes).filter(
version_notes.c.version == version,
version_notes.c.is_published == True
).first()
if not note:
return None
items = db.query(version_note_items).filter(
version_note_items.c.note_id == note.id
).order_by(version_note_items.c.sort_order).all()
core_upgrades = []
for item in items:
title = item.title
content = item.content
if content:
core_upgrades.append(f"{title}<br>{content}")
else:
core_upgrades.append(title)
return {
"introduction": {
"codeName": "",
"releaseDate": note.release_date.isoformat() if note.release_date else "",
"upgradePosition": "",
"coreUpgrades": core_upgrades
},
"introduction_en": {
"codeName": "",
"releaseDate": note.release_date.isoformat() if note.release_date else "",
"upgradePosition": "",
"coreUpgrades": core_upgrades
}
}
except Exception:
return None

View File

@@ -94,29 +94,38 @@ class HomePageService:
@staticmethod @staticmethod
def load_version_introduction(version: str) -> Dict[str, Any]: def load_version_introduction(version: str) -> Dict[str, Any]:
""" """
从 JSON 文件加载对应版本的介绍 加载对应版本的介绍优先从数据库读取fallback 到 JSON 文件)
:param version: 系统版本号(如 "0.2.0" :param version: 系统版本号(如 "0.2.0"
:return: 对应版本的详细介绍 :return: 对应版本的详细介绍
""" """
# 2. 定义 JSON 文件路径(简化路径处理,保留绝对路径调试特性) from copy import deepcopy
from app.db import SessionLocal
from app.repositories.home_page_repository import HomePageRepository
result = deepcopy(HomePageService.DEFAULT_RETURN_DATA)
try:
db = SessionLocal()
try:
db_result = HomePageRepository.get_version_introduction(db, version)
if db_result:
return db_result
finally:
db.close()
except Exception as e:
pass
json_abs_path = Path(__file__).parent.parent / "version_info.json" json_abs_path = Path(__file__).parent.parent / "version_info.json"
json_abs_path = json_abs_path.resolve() json_abs_path = json_abs_path.resolve()
# 3. 初始化返回结果(深拷贝默认模板,避免修改原常量)
from copy import deepcopy
result = deepcopy(HomePageService.DEFAULT_RETURN_DATA)
try: try:
# 4. 简化文件存在性判断(合并逻辑,减少分支)
if not json_abs_path.exists(): if not json_abs_path.exists():
result["message"] = f"版本介绍文件不存在:{json_abs_path}" result["message"] = f"版本介绍文件不存在:{json_abs_path}"
return result return result
# 5. 读取并解析 JSON 文件(简化文件操作流程)
with open(json_abs_path, "r", encoding="utf-8") as f: with open(json_abs_path, "r", encoding="utf-8") as f:
changelogs = json.load(f) changelogs = json.load(f)
# 6. 简化版本匹配逻辑,直接返回结果或更新提示信息
if version in changelogs: if version in changelogs:
return changelogs[version] return changelogs[version]
result["message"] = f"暂未查询到 {version} 版本的详细介绍" result["message"] = f"暂未查询到 {version} 版本的详细介绍"