nomadro/apps/api/services/ebook_svc/routers/douyin_admin.py
eric 0e85d71386 Add Douyin BMI audit homepage with remote A/B switch.
Ship a health-management BMI home for review, keep the nomad hub as hub, and let admin switch platforms.tt.homeVariant live with track/ntfy; fix tt config fetch to api.nomadro.cn/api/config.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-03 02:32:26 -05:00

135 lines
4.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""抖音小程序管理 API — 与微信读者/订单/支付完全隔离。"""
from __future__ import annotations
from typing import Any
from fastapi import APIRouter, Depends
from pydantic import BaseModel, Field
from ebook_svc.core.security import require_admin
from ebook_svc.services.track import ingest_events
from ebook_svc.services.tt_platform_config import read_tt_platform, write_tt_platform
router = APIRouter(prefix="/api/admin/douyin", tags=["douyin-admin"])
class ModuleToggle(BaseModel):
enabled: bool = False
title: str = ""
tabLabel: str | None = None
note: str | None = None
class BookChapter(BaseModel):
id: str
title: str
emoji: str | None = "📖"
content: str = ""
hook: str | None = ""
shareQuote: str | None = ""
class BookContent(BaseModel):
title: str = "数字游民"
subtitle: str | None = ""
author: str | None = ""
description: str | None = ""
tagline: str | None = ""
coverEmoji: str | None = "📗"
chapters: list[BookChapter] = Field(default_factory=list)
updatedAt: str | None = None
class TtPlatformBody(BaseModel):
homeVariant: str | None = None
modules: dict[str, ModuleToggle] | None = None
bookContent: BookContent | None = None
@router.get("/config")
async def get_douyin_config(_: str = Depends(require_admin)) -> dict[str, Any]:
return {"ok": True, "config": read_tt_platform()}
@router.put("/config")
async def put_douyin_config(body: TtPlatformBody, _: str = Depends(require_admin)) -> dict[str, Any]:
before = read_tt_platform()
patch: dict[str, Any] = {}
if body.homeVariant is not None:
if body.homeVariant not in ("bmi", "hub"):
return {"ok": False, "error": "homeVariant must be bmi|hub"}
patch["homeVariant"] = body.homeVariant
if body.modules is not None:
patch["modules"] = {k: v.model_dump() for k, v in body.modules.items()}
if body.bookContent is not None:
patch["bookContent"] = body.bookContent.model_dump()
saved = write_tt_platform(patch)
# 埋点 + ntfy(首页切换 / 入口开关)
old_hv = before.get("homeVariant") or "bmi"
new_hv = saved.get("homeVariant") or "bmi"
changed: list[str] = []
if body.homeVariant is not None and old_hv != new_hv:
changed.append(f"homeVariant {old_hv}→{new_hv}")
if body.modules is not None:
old_mods = before.get("modules") or {}
new_mods = saved.get("modules") or {}
for key in ("nav", "book", "community"):
o = bool((old_mods.get(key) or {}).get("enabled"))
n = bool((new_mods.get(key) or {}).get("enabled"))
if o != n:
changed.append(f"{key}:{'开' if n else '关'}")
if body.bookContent is not None:
changed.append("bookContent")
if changed:
label = " · ".join(changed)[:120]
await ingest_events(
[
{
"name": "config_change",
"page": "admin/douyin",
"label": label,
"props": {
"homeVariant": new_hv,
"from": old_hv,
"changes": ",".join(changed)[:200],
},
}
],
openid=None,
client="admin",
)
return {"ok": True, "config": saved}
@router.get("/meta")
async def douyin_meta(_: str = Depends(require_admin)) -> dict[str, Any]:
"""抖音开发文档 / 包体约定(定制面板用,非微信业务)。"""
return {
"ok": True,
"docs": [
{
"title": "小程序使用指南",
"url": "https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/introduction/usage-guide",
},
{
"title": "使用 AI 开发小程序",
"url": "https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/introduction/ai-mini-app-dev",
},
{
"title": "隐私协议配置",
"url": "https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/open-capacity/basic-capacities/privacy-agreement",
},
],
"limits": {
"mainPackageMB": 4,
"subPackageMB": 2,
"totalMB": 16,
"note": "本仓抖音 packageBook 需 ≤2MB;构建 validate:size",
},
"appId": "tt995defe007255b2701",
"importDir": "apps/miniprogram/tt",
}