nomadro/src/lib/remote-config.ts
eric 534400f554 Merge stash WIP onto BMI A/B: join-group, live config, channel tracking.
Keep Douyin homeVariant BMI router intact while restoring admin join-group controls, TLS-safe config fetch, and ntfy channel labels.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-03 15:42:09 +08:00

327 lines
9.4 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;
};
/** 抖音首页变体:bmi=过审健康工具;hub=正式数字游民门户 */
export type TtHomeVariant = "bmi" | "hub";
/** 抖音小程序独立配置(不污染门户 modules) */
export type TtPlatformConfig = {
/** 首页 A/B:默认 bmi 过审;后台可切 hub */
homeVariant: TtHomeVariant;
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;
/** 关注引导总开关:false 时不自动弹公众号/X(含「稍后再说」) */
socialFollow: boolean;
/**
* 首页延迟自动弹出关注弹窗(公众号或 X)。
* 须 socialFollow !== false 且本项为 true 才弹出;默认 false,不影响现网行为。
*/
socialFollowAuto: boolean;
/** 加群 → 微信公众号(后台开关,用户无感知) */
joinGroupWechat: boolean;
/** 加群 → 抖音群(后台开关,用户无感知) */
joinGroupDouyin: boolean;
offlineBadge: boolean;
};
copy: {
homeBrand: string;
homeTagline: string;
homeLead: string;
vipPriceFen: number;
vipProductId: string;
vipCta: string;
wechatOaName?: string;
joinGroupDouyinHint?: string;
joinGroupDouyinQr?: 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 = {
/** 过审默认 BMI;审核通过后后台切 hub */
homeVariant: "bmi",
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: "modules/nav 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,
socialFollowAuto: false,
joinGroupWechat: true,
joinGroupDouyin: false,
offlineBadge: false,
},
copy: {
homeBrand: "nomadro",
homeTagline: "",
homeLead: "",
vipPriceFen: 29800,
vipProductId: "vip_membership",
vipCta: "立即加入",
wechatOaName: "异度世界",
joinGroupDouyinHint: "打开抖音搜群「数字游民」或扫码进群",
joinGroupDouyinQr: "",
},
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,
};
}
function resolveTtHomeVariant(
v: unknown,
fallback: TtHomeVariant,
): TtHomeVariant {
return v === "hub" || v === "bmi" ? v : fallback;
}
export function mergeTtPlatform(
partial?: Partial<TtPlatformConfig> | null,
): TtPlatformConfig {
const base = structuredClone(DEFAULT_TT_PLATFORM);
if (!partial) return base;
return {
homeVariant: resolveTtHomeVariant(partial.homeVariant, base.homeVariant),
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 resolveTtHomeVariantFromConfig(
config: NomadroRemoteConfig,
): TtHomeVariant {
return resolveTtHomeVariant(
config.platforms?.tt?.homeVariant,
DEFAULT_TT_PLATFORM.homeVariant,
);
}
function asBool(v: unknown, fallback: boolean): boolean {
return typeof v === "boolean" ? v : fallback;
}
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);
const modKeys = Object.keys(base.modules) as (keyof typeof base.modules)[];
const modules = { ...base.modules };
for (const key of modKeys) {
modules[key] = mergeModuleToggle(
base.modules[key],
partial.modules?.[key] as Partial<ModuleToggle> | undefined,
);
}
const pf = (partial.flags || {}) as Partial<NomadroRemoteConfig["flags"]>;
return {
...base,
...partial,
version: Number(partial.version) || base.version,
updatedAt: partial.updatedAt || base.updatedAt,
modules,
flags: {
payEnabled: asBool(pf.payEnabled, base.flags.payEnabled),
socialFollow: asBool(pf.socialFollow, base.flags.socialFollow),
socialFollowAuto: asBool(pf.socialFollowAuto, base.flags.socialFollowAuto),
joinGroupWechat: asBool(pf.joinGroupWechat, base.flags.joinGroupWechat),
joinGroupDouyin: asBool(pf.joinGroupDouyin, base.flags.joinGroupDouyin),
offlineBadge: asBool(pf.offlineBadge, base.flags.offlineBadge),
},
copy: { ...base.copy, ...(partial.copy || {}) },
platforms: {
// 未传 platforms.tt 时保留已有/默认,避免后台保存门户配置时冲掉抖音小册
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);
}