nomadro/lib/remote-config.ts
eric f688c03720 Preserve WeChat top-level remote-config on Douyin admin deploy.
Backup live modules before git reset; only seed platforms.tt for Douyin.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-30 20:45:55 +08:00

262 lines
7.0 KiB
TypeScript
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.

/**
* Nomadro remote UI config — backend is source of truth;
* clients ship DEFAULT_REMOTE_CONFIG as offline seed.
*/
import ttDefaultBook from "./tt-default-book.json";
export type ModuleToggle = {
enabled: boolean;
title: string;
tabLabel?: string;
note?: string;
};
/** 抖音小程序远程小册章节(markdown 正文) */
export type TtBookChapter = {
id: string;
title: string;
emoji?: string;
content: string;
hook?: string;
shareQuote?: string;
};
export type TtBookContent = {
title: string;
subtitle?: string;
author?: string;
description?: string;
tagline?: string;
coverEmoji?: string;
chapters: TtBookChapter[];
updatedAt?: string;
};
/** 抖音小程序独立配置(不污染门户 modules) */
export type TtPlatformConfig = {
modules: {
nav: ModuleToggle;
book: ModuleToggle;
community: ModuleToggle;
};
bookContent: TtBookContent;
};
export type NomadroRemoteConfig = {
version: number;
updatedAt: string;
modules: {
nav: ModuleToggle;
book: ModuleToggle;
community: ModuleToggle;
vip: ModuleToggle;
meetups: ModuleToggle;
destinations: ModuleToggle;
home: ModuleToggle;
};
flags: {
payEnabled: boolean;
socialFollow: boolean;
offlineBadge: boolean;
};
copy: {
homeBrand: string;
homeTagline: string;
homeLead: string;
vipPriceFen: number;
vipProductId: string;
vipCta: string;
};
/** 分端配置;仅抖音等使用。门户 / 微信仍只读顶层 modules,互不影响 */
platforms?: {
tt?: TtPlatformConfig;
};
};
export const DEFAULT_TT_BOOK_CONTENT: TtBookContent = {
title: String(ttDefaultBook.title || "数字游民"),
subtitle: String(ttDefaultBook.subtitle || ""),
author: String(ttDefaultBook.author || ""),
description: String(ttDefaultBook.description || ""),
tagline: String(ttDefaultBook.tagline || ""),
coverEmoji: String(ttDefaultBook.coverEmoji || "📗"),
chapters: (ttDefaultBook.chapters || []).map((c) => ({
id: String(c.id),
title: String(c.title),
emoji: c.emoji ? String(c.emoji) : "📖",
content: String(c.content || ""),
hook: c.hook ? String(c.hook) : "",
shareQuote: c.shareQuote ? String(c.shareQuote) : "",
})),
};
export const DEFAULT_TT_PLATFORM: TtPlatformConfig = {
modules: {
nav: {
enabled: false,
title: "出海导航",
tabLabel: "导航",
note: "抖音默认隐藏,后台可开",
},
book: {
enabled: true,
title: "数字游民小册",
tabLabel: "小册",
note: "默认开启 · 正文可远程改",
},
community: {
enabled: false,
title: "旅居社群",
tabLabel: "社群",
note: "抖音默认隐藏,后台可开",
},
},
bookContent: DEFAULT_TT_BOOK_CONTENT,
};
export const DEFAULT_REMOTE_CONFIG: NomadroRemoteConfig = {
version: 2,
updatedAt: "2026-03-30T00:00:00.000Z",
modules: {
home: { enabled: true, title: "首页", tabLabel: "首页" },
nav: {
enabled: true,
title: "出海导航",
tabLabel: "导航",
note: "apps/navmini links",
},
book: {
enabled: true,
title: "数字游民小册",
tabLabel: "小册",
note: "ebook reader + pay",
},
community: {
enabled: true,
title: "旅居社群",
tabLabel: "社群",
note: "nomadweb",
},
destinations: { enabled: true, title: "目的地" },
meetups: { enabled: true, title: "活动" },
vip: { enabled: true, title: "会员中心", note: "deep link only" },
},
flags: {
payEnabled: true,
socialFollow: true,
offlineBadge: false,
},
copy: {
homeBrand: "nomadro",
homeTagline: "",
homeLead: "",
vipPriceFen: 8800,
vipProductId: "vip_membership",
vipCta: "立即加入",
},
platforms: {
tt: structuredClone(DEFAULT_TT_PLATFORM),
},
};
function mergeModuleToggle(
base: ModuleToggle,
patch?: Partial<ModuleToggle> | null,
): ModuleToggle {
if (!patch) return { ...base };
return {
...base,
...patch,
enabled: typeof patch.enabled === "boolean" ? patch.enabled : base.enabled,
title: patch.title ?? base.title,
};
}
function mergeTtBookContent(
base: TtBookContent,
patch?: Partial<TtBookContent> | null,
): TtBookContent {
if (!patch) return structuredClone(base);
const chapters =
Array.isArray(patch.chapters) && patch.chapters.length > 0
? patch.chapters.map((c, i) => ({
id: String(c?.id || `ch-${i}`),
title: String(c?.title || `第 ${i + 1} 章`),
emoji: c?.emoji ? String(c.emoji) : "📖",
content: String(c?.content || ""),
hook: c?.hook ? String(c.hook) : "",
shareQuote: c?.shareQuote ? String(c.shareQuote) : "",
}))
: structuredClone(base.chapters);
return {
title: patch.title ?? base.title,
subtitle: patch.subtitle ?? base.subtitle,
author: patch.author ?? base.author,
description: patch.description ?? base.description,
tagline: patch.tagline ?? base.tagline,
coverEmoji: patch.coverEmoji ?? base.coverEmoji,
chapters,
updatedAt: patch.updatedAt || base.updatedAt,
};
}
export function mergeTtPlatform(
partial?: Partial<TtPlatformConfig> | null,
): TtPlatformConfig {
const base = structuredClone(DEFAULT_TT_PLATFORM);
if (!partial) return base;
return {
modules: {
nav: mergeModuleToggle(base.modules.nav, partial.modules?.nav),
book: mergeModuleToggle(base.modules.book, partial.modules?.book),
community: mergeModuleToggle(
base.modules.community,
partial.modules?.community,
),
},
bookContent: mergeTtBookContent(base.bookContent, partial.bookContent),
};
}
export function mergeRemoteConfig(
partial: Partial<NomadroRemoteConfig> | null | undefined,
): NomadroRemoteConfig {
if (!partial || typeof partial !== "object") {
return structuredClone(DEFAULT_REMOTE_CONFIG);
}
const base = structuredClone(DEFAULT_REMOTE_CONFIG);
return {
...base,
...partial,
version: Number(partial.version) || base.version,
updatedAt: partial.updatedAt || base.updatedAt,
modules: { ...base.modules, ...(partial.modules || {}) },
flags: { ...base.flags, ...(partial.flags || {}) },
copy: { ...base.copy, ...(partial.copy || {}) },
platforms: {
tt: mergeTtPlatform(partial.platforms?.tt || base.platforms?.tt),
},
};
}
/** 抖音端模块开关:优先 platforms.tt.modules */
export function resolveTtModuleEnabled(
config: NomadroRemoteConfig,
key: "nav" | "book" | "community",
): boolean {
const tt = config.platforms?.tt?.modules?.[key];
if (tt && typeof tt.enabled === "boolean") return tt.enabled;
return Boolean(config.modules[key]?.enabled);
}
export function resolveTtBookContent(
config: NomadroRemoteConfig,
): TtBookContent {
const remote = config.platforms?.tt?.bookContent;
if (remote?.chapters?.length) {
return mergeTtBookContent(DEFAULT_TT_BOOK_CONTENT, remote);
}
return structuredClone(DEFAULT_TT_BOOK_CONTENT);
}