164 lines
5.8 KiB
Python
164 lines
5.8 KiB
Python
"""微信后台「开关」— admin.nomadro.cn/wechat/switches。"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Literal
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from pydantic import BaseModel, Field
|
|
|
|
from ebook_svc.core.security import require_admin
|
|
from ebook_svc.services.store import store
|
|
from ebook_svc.services.weapp_switches import read_weapp_switches, write_weapp_switches
|
|
|
|
router = APIRouter(prefix="/api/admin/weapp/switches", tags=["weapp-switches"])
|
|
|
|
|
|
class ModuleIn(BaseModel):
|
|
enabled: bool | None = None
|
|
title: str | None = Field(default=None, max_length=120)
|
|
tabLabel: str | None = Field(default=None, max_length=60)
|
|
note: str | None = Field(default=None, max_length=1000)
|
|
|
|
|
|
class SocialLiveIn(BaseModel):
|
|
"""游民社区实时 UX 超级兜底 — 不限于下列键,可继续扩展。"""
|
|
presenceEnabled: bool | None = None
|
|
presenceStrip: bool | None = None
|
|
presenceCityScope: bool | None = None
|
|
discoveryAutoRefresh: bool | None = None
|
|
discoveryRefreshSec: int | None = Field(default=None, ge=15, le=300)
|
|
configHotReloadToast: bool | None = None
|
|
offlineFallbackShell: bool | None = None
|
|
hideNoFaceAvatar: bool | None = None
|
|
livenessGate: bool | None = None
|
|
trackLiveEvents: bool | None = None
|
|
chatPresencePill: bool | None = None
|
|
mapLivePins: bool | None = None
|
|
joinPriceLive: bool | None = None
|
|
onlineCountBadge: bool | None = None
|
|
softStatusSync: bool | None = None
|
|
configVersionChip: bool | None = None
|
|
trialLive: bool | None = None
|
|
presenceHeartbeatSec: int | None = Field(default=None, ge=10, le=120)
|
|
|
|
|
|
class FlagsIn(BaseModel):
|
|
payEnabled: bool | None = None
|
|
socialFollow: bool | None = None
|
|
offlineBadge: bool | None = None
|
|
joinGroupAction: Literal["popup", "vip"] | None = None
|
|
memoTrialBrowse: bool | None = None
|
|
joinGroupWechat: bool | None = None
|
|
socialTrialEnabled: bool | None = None
|
|
socialAvatarFaceCheck: bool | None = None
|
|
socialLive: SocialLiveIn | None = None
|
|
adsenseEnabled: bool | None = None
|
|
adsenseClientId: str | None = Field(default=None, max_length=64, pattern=r"^(ca-pub-[0-9]+)?$")
|
|
adsensePublisherId: str | None = Field(default=None, max_length=64, pattern=r"^(pub-[0-9]+)?$")
|
|
adsenseSlotBlogMid: str | None = Field(default=None, max_length=32, pattern=r"^[0-9]*$")
|
|
adsenseSlotBlogEnd: str | None = Field(default=None, max_length=32, pattern=r"^[0-9]*$")
|
|
|
|
|
|
class SocialTrialIn(BaseModel):
|
|
enabled: bool | None = None
|
|
chatMessages: int | None = Field(default=None, ge=0)
|
|
dateViews: int | None = Field(default=None, ge=0)
|
|
coffeeBookings: int | None = Field(default=None, ge=0)
|
|
mapOpens: int | None = Field(default=None, ge=0)
|
|
|
|
|
|
class CopyIn(BaseModel):
|
|
homeBrand: str | None = None
|
|
homeTagline: str | None = None
|
|
homeLead: str | None = None
|
|
vipCta: str | None = None
|
|
vipPriceFen: int | None = Field(default=None, ge=0, le=100000000)
|
|
vipProductId: str | None = None
|
|
wechatOaName: str | None = None
|
|
memoUnlockCta: str | None = None
|
|
xHandle: str | None = None
|
|
socialTrial: SocialTrialIn | None = None
|
|
|
|
|
|
class BookAuditIn(BaseModel):
|
|
reviewMode: bool | None = None
|
|
bookId: str | None = None
|
|
|
|
|
|
class VipHeroIn(BaseModel):
|
|
heroEyebrow: str | None = None
|
|
heroTitleAccent: str | None = None
|
|
heroTitleRest: str | None = None
|
|
heroLead: str | None = None
|
|
|
|
|
|
class SwitchesBody(BaseModel):
|
|
expectedVersion: int | None = Field(default=None, ge=0)
|
|
modules: dict[str, ModuleIn] | None = None
|
|
flags: FlagsIn | None = None
|
|
copy: CopyIn | None = Field(default=None, alias="copy")
|
|
vipEntries: dict[str, bool] | None = None
|
|
vipHero: VipHeroIn | None = None
|
|
bookAudit: BookAuditIn | None = None
|
|
|
|
model_config = {"populate_by_name": True}
|
|
|
|
|
|
@router.get("")
|
|
async def get_switches(_: str = Depends(require_admin)) -> dict[str, Any]:
|
|
data = read_weapp_switches()
|
|
books = []
|
|
try:
|
|
for b in store.list_books():
|
|
books.append(
|
|
{
|
|
"id": b.get("id"),
|
|
"title": b.get("title"),
|
|
"is_free": b.get("is_free"),
|
|
"chapter_count": b.get("chapter_count"),
|
|
}
|
|
)
|
|
except Exception:
|
|
books = []
|
|
data["books"] = books
|
|
return data
|
|
|
|
|
|
@router.put("")
|
|
async def put_switches(body: SwitchesBody, _: str = Depends(require_admin)) -> dict[str, Any]:
|
|
if body.expectedVersion is not None:
|
|
current = read_weapp_switches()
|
|
if current.get('version') != body.expectedVersion:
|
|
raise HTTPException(409, '配置已更新,请加载最新配置后重试;当前修改尚未保存')
|
|
patch: dict[str, Any] = {}
|
|
if body.modules is not None:
|
|
patch["modules"] = {
|
|
k: v.model_dump(exclude_none=True) for k, v in body.modules.items()
|
|
}
|
|
if body.flags is not None:
|
|
patch["flags"] = body.flags.model_dump(exclude_none=True)
|
|
if body.copy is not None:
|
|
patch["copy"] = body.copy.model_dump(exclude_none=True)
|
|
if body.vipEntries is not None:
|
|
patch["vipEntries"] = body.vipEntries
|
|
if body.vipHero is not None:
|
|
patch["vipHero"] = body.vipHero.model_dump(exclude_none=True)
|
|
if body.bookAudit is not None:
|
|
patch["bookAudit"] = body.bookAudit.model_dump(exclude_none=True)
|
|
saved = write_weapp_switches(patch)
|
|
books = []
|
|
try:
|
|
for b in store.list_books():
|
|
books.append(
|
|
{
|
|
"id": b.get("id"),
|
|
"title": b.get("title"),
|
|
"is_free": b.get("is_free"),
|
|
"chapter_count": b.get("chapter_count"),
|
|
}
|
|
)
|
|
except Exception:
|
|
books = []
|
|
saved["books"] = books
|
|
return saved
|