diff --git a/app/admin/admin-shell.tsx b/app/admin/admin-shell.tsx index eb69054..b0d7622 100644 --- a/app/admin/admin-shell.tsx +++ b/app/admin/admin-shell.tsx @@ -9,6 +9,7 @@ import styles from "./admin.module.css"; const NAV = [ { href: "/admin", label: "总览", emoji: "📡" }, { href: "/admin/config", label: "远程配置", emoji: "⚙️" }, + { href: "/admin/tt", label: "抖音小程序", emoji: "🎵" }, { href: "/admin/users", label: "用户", emoji: "👥" }, { href: "/admin/orders", label: "订单/支付", emoji: "💳" }, { href: "/admin/verticals", label: "垂直业务", emoji: "🧩" }, diff --git a/app/admin/tt/page.tsx b/app/admin/tt/page.tsx new file mode 100644 index 0000000..2e9e8be --- /dev/null +++ b/app/admin/tt/page.tsx @@ -0,0 +1,378 @@ +"use client"; + +import { useCallback, useEffect, useState } from "react"; +import { + DEFAULT_TT_BOOK_CONTENT, + DEFAULT_TT_PLATFORM, + type NomadroRemoteConfig, + type TtBookChapter, + type TtPlatformConfig, +} from "~/lib/remote-config"; +import styles from "../admin.module.css"; + +type TtKey = keyof TtPlatformConfig["modules"]; + +const MODULE_LABELS: Record = { + nav: "出海导航", + book: "数字游民小册", + community: "旅居社群", +}; + +function ensureTt(config: NomadroRemoteConfig): TtPlatformConfig { + return { + modules: { + nav: { ...DEFAULT_TT_PLATFORM.modules.nav, ...config.platforms?.tt?.modules?.nav }, + book: { ...DEFAULT_TT_PLATFORM.modules.book, ...config.platforms?.tt?.modules?.book }, + community: { + ...DEFAULT_TT_PLATFORM.modules.community, + ...config.platforms?.tt?.modules?.community, + }, + }, + bookContent: { + ...DEFAULT_TT_BOOK_CONTENT, + ...config.platforms?.tt?.bookContent, + chapters: + config.platforms?.tt?.bookContent?.chapters?.length + ? config.platforms.tt.bookContent.chapters + : DEFAULT_TT_BOOK_CONTENT.chapters, + }, + }; +} + +export default function AdminTtPage() { + const [config, setConfig] = useState(null); + const [tt, setTt] = useState(null); + const [activeIdx, setActiveIdx] = useState(0); + const [error, setError] = useState(""); + const [saved, setSaved] = useState(""); + const [busy, setBusy] = useState(false); + + const load = useCallback(async () => { + setError(""); + const res = await fetch("/api/admin/config", { cache: "no-store" }); + const data = await res.json().catch(() => ({})); + if (!res.ok) { + setError(data.error || "加载失败"); + return; + } + const cfg = data.config as NomadroRemoteConfig; + setConfig(cfg); + setTt(ensureTt(cfg)); + }, []); + + useEffect(() => { + void load(); + }, [load]); + + function patchTt(next: TtPlatformConfig) { + setTt(next); + } + + function setModuleEnabled(key: TtKey, enabled: boolean) { + if (!tt) return; + patchTt({ + ...tt, + modules: { + ...tt.modules, + [key]: { ...tt.modules[key], enabled }, + }, + }); + } + + function setBookMeta(field: keyof TtPlatformConfig["bookContent"], value: string) { + if (!tt) return; + patchTt({ + ...tt, + bookContent: { ...tt.bookContent, [field]: value }, + }); + } + + function setChapter(idx: number, patch: Partial) { + if (!tt) return; + const chapters = tt.bookContent.chapters.map((c, i) => + i === idx ? { ...c, ...patch } : c, + ); + patchTt({ + ...tt, + bookContent: { ...tt.bookContent, chapters }, + }); + } + + function addChapter() { + if (!tt) return; + const n = tt.bookContent.chapters.length + 1; + patchTt({ + ...tt, + bookContent: { + ...tt.bookContent, + chapters: [ + ...tt.bookContent.chapters, + { + id: `ch-${Date.now()}`, + title: `第 ${n} 章`, + emoji: "📖", + content: `# 第 ${n} 章\n\n在此编辑 Markdown…\n`, + }, + ], + }, + }); + setActiveIdx(n - 1); + } + + function removeChapter(idx: number) { + if (!tt || tt.bookContent.chapters.length <= 1) return; + const chapters = tt.bookContent.chapters.filter((_, i) => i !== idx); + patchTt({ + ...tt, + bookContent: { ...tt.bookContent, chapters }, + }); + setActiveIdx(Math.max(0, Math.min(activeIdx, chapters.length - 1))); + } + + function resetBook() { + if (!tt) return; + if (!window.confirm("恢复为默认 7 章「数字游民」正文?当前远程正文会被覆盖。")) return; + patchTt({ + ...tt, + bookContent: structuredClone(DEFAULT_TT_BOOK_CONTENT), + }); + setActiveIdx(0); + } + + async function save() { + if (!config || !tt) return; + setBusy(true); + setSaved(""); + setError(""); + try { + const next: NomadroRemoteConfig = { + ...config, + platforms: { + ...config.platforms, + tt: { + ...tt, + bookContent: { + ...tt.bookContent, + updatedAt: new Date().toISOString(), + }, + }, + }, + }; + const res = await fetch("/api/admin/config", { + method: "PUT", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ config: next }), + }); + const data = await res.json().catch(() => ({})); + if (!res.ok) { + setError(data.error || "保存失败"); + return; + } + setConfig(data.config); + setTt(ensureTt(data.config)); + setSaved(`已保存 · ${data.config.updatedAt}`); + } finally { + setBusy(false); + } + } + + if (!tt) { + return ( + <> +

🎵 抖音小程序

+

{error || "加载中…"}

+ + ); + } + + const chapter = tt.bookContent.chapters[activeIdx]; + + return ( + <> +

🎵 抖音小程序

+

+ 控制抖音端首页入口显隐,以及小册 Markdown 正文。配置写入{" "} + platforms.tt,客户端无网用包内 7 章「数字游民」兜底。 +

+ {error ?

{error}

: null} + {saved ?

{saved}

: null} + +
+

入口显示权限

+

+ 默认:导航关 · 小册开 · 社群关。勾选即在抖音首页显示对应入口。 +

+
+ {(Object.keys(MODULE_LABELS) as TtKey[]).map((key) => { + const m = tt.modules[key]; + return ( + + ); + })} +
+
+ +
+

小册元信息

+
+ {( + [ + ["title", "书名"], + ["subtitle", "副标题"], + ["author", "作者"], + ["coverEmoji", "封面 emoji"], + ] as const + ).map(([field, label]) => ( + + ))} +
+