fix: OTA检测闭环 — 新增GET /check-update + App端uni.getSystemInfoSync版本对比
This commit is contained in:
@ -10,13 +10,47 @@ from app.schemas.app_version import AppVersionResponse
|
||||
router = APIRouter(prefix="/app", tags=["App版本"])
|
||||
|
||||
|
||||
@router.get("/version", response_model=AppVersionResponse)
|
||||
async def check_version(
|
||||
current: str = Query(..., description="当前 App 版本号,如 T1.0.0"),
|
||||
@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))
|
||||
@ -26,14 +60,12 @@ async def check_version(
|
||||
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(
|
||||
@ -47,7 +79,7 @@ async def check_version(
|
||||
|
||||
|
||||
def _parse_version_code(version_str: str) -> int:
|
||||
"""从版本字符串提取数字版本号(兜底用最后3位)"""
|
||||
"""从版本字符串提取数字版本号"""
|
||||
import re
|
||||
nums = re.findall(r"\d+", version_str)
|
||||
if nums:
|
||||
|
||||
Reference in New Issue
Block a user