"""抖音小程序管理 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 TtJoinBody(BaseModel): """抖音加群弹窗 — 仅 platforms.tt.join,与微信 VIP/join-group 隔离。""" joinGroupWechat: bool | None = None joinGroupDouyin: bool | None = None socialFollowAuto: bool | None = None wechatOaName: str | None = None joinGroupDouyinHint: str | None = None joinGroupDouyinQr: str | None = None class TtPlatformBody(BaseModel): homeVariant: str | None = None modules: dict[str, ModuleToggle] | None = None bookContent: BookContent | None = None join: TtJoinBody | 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() if body.join is not None: patch["join"] = body.join.model_dump(exclude_none=True) 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 body.join is not None: oj = before.get("join") or {} nj = saved.get("join") or {} if bool(oj.get("joinGroupWechat")) != bool(nj.get("joinGroupWechat")) or bool( oj.get("joinGroupDouyin") ) != bool(nj.get("joinGroupDouyin")): ch = "公众号" if nj.get("joinGroupWechat") and not nj.get("joinGroupDouyin") else "抖音群" changed.append(f"join→{ch}") 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", }