"""受众分轨 remote-config.audience — .com 国际优先,中文用户特殊对待。""" from __future__ import annotations import json from copy import deepcopy from typing import Any from fastapi import HTTPException from ebook_svc.services.tt_platform_config import resolve_config_file DEFAULT_CN: dict[str, Any] = { "payEnabled": True, "socialFollow": True, "followChannel": "wechat", "vipCta": "立即加入 · ¥88", "homeLead": "", "homeVariant": "portal", "note": "中文用户特殊线:微信内置 / is-china-user;支付走 zpay/xpay;首页=/portal", } DEFAULT_INTL: dict[str, Any] = { "payEnabled": True, "socialFollow": True, "followChannel": "x", "vipCta": "Join VIP", "homeLead": "", "homeVariant": "social", "note": ".com 默认国际线(英语为主);Creem / NOWPayments;首页=/social", } DEFAULT_AUDIENCE: dict[str, Any] = { "defaultLane": "intl", "cn": deepcopy(DEFAULT_CN), "intl": deepcopy(DEFAULT_INTL), } def _read_raw() -> dict[str, Any]: path = resolve_config_file() if not path.is_file(): return {} try: raw = json.loads(path.read_text(encoding="utf-8")) return raw if isinstance(raw, dict) else {} except Exception: return {} def _write_raw(out: dict[str, Any]) -> None: from ebook_svc.services.weapp_switches import _write_raw as write_config write_config(out) def _norm_lane(raw: Any, fallback: dict[str, Any]) -> dict[str, Any]: m = raw if isinstance(raw, dict) else {} out = deepcopy(fallback) if "payEnabled" in m: out["payEnabled"] = bool(m["payEnabled"]) if "socialFollow" in m: out["socialFollow"] = bool(m["socialFollow"]) ch = m.get("followChannel") if ch in ("wechat", "x", "none"): out["followChannel"] = ch if "vipCta" in m and m["vipCta"] is not None: out["vipCta"] = str(m["vipCta"]) if "homeLead" in m and m["homeLead"] is not None: out["homeLead"] = str(m["homeLead"]) hv = m.get("homeVariant") if hv in ("portal", "social", ""): out["homeVariant"] = hv if "note" in m and m["note"] is not None: out["note"] = str(m["note"]) return out def read_audience_config() -> dict[str, Any]: raw = _read_raw() a = raw.get("audience") if isinstance(raw.get("audience"), dict) else {} lane = a.get("defaultLane") default_lane = lane if lane in ("cn", "intl") else "intl" return { "ok": True, "defaultLane": default_lane, "cn": _norm_lane(a.get("cn"), DEFAULT_CN), "intl": _norm_lane(a.get("intl"), DEFAULT_INTL), "detection": { "lib": "is-china-user", "wechatUa": "micromessenger", "repo": "https://github.com/yArna/isChinaUser", "note": ".com 国际优先;微信内置或 is-china-user 命中 → cn 特殊线", }, "version": raw.get("version"), "updatedAt": raw.get("updatedAt"), "configPath": str(resolve_config_file()), } def write_audience_config(patch: dict[str, Any]) -> dict[str, Any]: raw = _read_raw() if patch.get("expectedVersion") is not None and patch["expectedVersion"] != raw.get("version"): raise HTTPException(409, "配置已更新,请加载最新配置后重试;当前草稿尚未保存") out: dict[str, Any] = dict(raw) if raw else {"version": 2} cur = out.get("audience") if isinstance(out.get("audience"), dict) else {} audience = { "defaultLane": cur.get("defaultLane") or "intl", "cn": _norm_lane(cur.get("cn"), DEFAULT_CN), "intl": _norm_lane(cur.get("intl"), DEFAULT_INTL), } if patch.get("defaultLane") in ("cn", "intl"): audience["defaultLane"] = patch["defaultLane"] if isinstance(patch.get("cn"), dict): audience["cn"] = _norm_lane(patch["cn"], audience["cn"]) if isinstance(patch.get("intl"), dict): audience["intl"] = _norm_lane(patch["intl"], audience["intl"]) out["audience"] = audience _write_raw(out) return read_audience_config()