- 复制来源: /home/yueli/track @ 192c8ee (feature/ai-audit-update) - 组织隔离目标: LICA - 端口规划: 前端 8030 / 后端 8031 / 数据库 8032 - 已排除 deploy.sh、deploy_full.sh、docker-compose.prod.yml(IRIS 生产发布脚本) - 已排除工作区未提交改动,取干净的 192c8ee 状态
88 lines
2.6 KiB
Python
88 lines
2.6 KiB
Python
"""App 版本更新 API — OTA 热更新检测"""
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy import select, desc
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.database import get_db
|
|
from app.models.app_version import AppVersion
|
|
from app.schemas.app_version import AppVersionResponse
|
|
|
|
router = APIRouter(prefix="/app", tags=["App版本"])
|
|
|
|
|
|
@router.get("/check-update", response_model=AppVersionResponse)
|
|
async def check_update(
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
"""
|
|
热更新检测接口(无需传参)。
|
|
查询 app_versions 表中 is_active=true 的最新记录,
|
|
返回最新版本号、版本代码、WGT下载地址、更新说明。
|
|
App 端自行对比本地版本号决定是否升级。
|
|
"""
|
|
result = await db.execute(
|
|
select(AppVersion)
|
|
.where(AppVersion.is_active.is_(True))
|
|
.order_by(desc(AppVersion.version_code))
|
|
.limit(1)
|
|
)
|
|
latest = result.scalar_one_or_none()
|
|
|
|
if not latest:
|
|
return AppVersionResponse(
|
|
version="0",
|
|
version_code=0,
|
|
has_update=False,
|
|
)
|
|
|
|
return AppVersionResponse(
|
|
version=latest.version,
|
|
version_code=latest.version_code,
|
|
has_update=bool(latest.wgt_url), # 只有配置了 WGT 下载地址才算有效更新
|
|
wgt_url=latest.wgt_url,
|
|
description=latest.description,
|
|
force_update=False,
|
|
)
|
|
|
|
|
|
@router.get("/version", response_model=AppVersionResponse)
|
|
async def check_version(
|
|
current: str = Query(..., description="当前 App 版本号,如 T1.0.0"),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
"""检测是否有新版本可用(服务端对比)"""
|
|
result = await db.execute(
|
|
select(AppVersion)
|
|
.where(AppVersion.is_active.is_(True))
|
|
.order_by(desc(AppVersion.version_code))
|
|
.limit(1)
|
|
)
|
|
latest = result.scalar_one_or_none()
|
|
|
|
if not latest:
|
|
return AppVersionResponse(
|
|
version=current,
|
|
version_code=0,
|
|
has_update=False,
|
|
)
|
|
|
|
has_update = latest.version_code > _parse_version_code(current)
|
|
|
|
return AppVersionResponse(
|
|
version=latest.version,
|
|
version_code=latest.version_code,
|
|
has_update=has_update,
|
|
wgt_url=latest.wgt_url if has_update else None,
|
|
description=latest.description if has_update else None,
|
|
force_update=False,
|
|
)
|
|
|
|
|
|
def _parse_version_code(version_str: str) -> int:
|
|
"""从版本字符串提取数字版本号"""
|
|
import re
|
|
nums = re.findall(r"\d+", version_str)
|
|
if nums:
|
|
return int("".join(nums[-3:]).ljust(3, "0")[:3])
|
|
return 0
|