import { useCallback, useEffect, useMemo, useState } from 'react' import { api } from '../api' import './BookEdit.css' type JoinAction = 'popup' | 'vip' type VipCfg = { ok?: boolean vipEnabled: boolean gameEnabled: boolean joinGroupAction: JoinAction vipPriceFen: number vipProductId: string vipCta: string vipOfferKey?: string is_free?: boolean updatedAt?: string version?: number } type ProductOffer = { key: string label: string name: string product_id: string price_fen: number price_yuan: string free: boolean image: string remark?: string } const empty: VipCfg = { vipEnabled: false, gameEnabled: false, joinGroupAction: 'popup', vipPriceFen: 29800, vipProductId: 'tiktok', vipCta: '立即加入 · ¥298', vipOfferKey: 'tiktok', is_free: false, } function fromApi(data: VipCfg): VipCfg { return { vipEnabled: !!data.vipEnabled, gameEnabled: !!data.gameEnabled, joinGroupAction: data.joinGroupAction === 'vip' ? 'vip' : 'popup', vipPriceFen: Number(data.vipPriceFen) || 0, vipProductId: String(data.vipProductId || ''), vipCta: String(data.vipCta || '立即加入'), vipOfferKey: String(data.vipOfferKey || ''), is_free: !!data.is_free, updatedAt: data.updatedAt, version: data.version, } } /** 微信小程序 · VIP / 加群行为 / 3D 入口 + 动态付费方案(对齐小册「阅读与付费控制」) */ export default function Vip() { const [cfg, setCfg] = useState(null) const [draft, setDraft] = useState(empty) const [offers, setOffers] = useState([]) const [err, setErr] = useState('') const [msg, setMsg] = useState('') const [busy, setBusy] = useState(false) const load = useCallback(async () => { setErr('') const [data, offerRes] = await Promise.all([ api('/api/admin/vip'), api<{ items: ProductOffer[] }>('/api/admin/product-offers'), ]) const next = fromApi(data) setCfg(next) setDraft(next) setOffers(offerRes.items || []) }, []) useEffect(() => { load().catch((e) => setErr(e.message || '加载失败')) }, [load]) const isFree = !!draft.is_free || (draft.vipPriceFen <= 0 && !draft.vipProductId) const activeOfferKey = useMemo(() => { if (draft.vipOfferKey) return draft.vipOfferKey const hit = offers.find( (o) => (!o.free && o.product_id === draft.vipProductId) || (o.free && isFree), ) return hit?.key || '' }, [draft.vipOfferKey, draft.vipProductId, offers, isFree]) async function save(patch: Partial, okHint: string) { setBusy(true) setMsg('') setErr('') const next = { ...draft, ...patch } setDraft(next) try { const data = await api('/api/admin/vip', { method: 'PUT', body: JSON.stringify({ vipEnabled: next.vipEnabled, gameEnabled: next.gameEnabled, joinGroupAction: next.joinGroupAction, vipPriceFen: next.vipPriceFen, vipProductId: next.vipProductId, vipCta: next.vipCta, vipOfferKey: next.vipOfferKey, }), }) const saved = fromApi(data) setCfg(saved) setDraft(saved) setMsg(okHint) } catch (e: any) { setErr(e.message || '保存失败') await load().catch(() => undefined) } finally { setBusy(false) } } async function applyOffer(offerKey: string) { setBusy(true) setMsg('') setErr('') try { const data = await api('/api/admin/vip/offer', { method: 'POST', body: JSON.stringify({ offer_key: offerKey }), }) const saved = fromApi(data) setCfg(saved) setDraft(saved) setMsg( saved.is_free ? '已切换免费 · 小程序「立即加入」→ 免费加入' : `已切换 ${saved.vipProductId} · CTA「${saved.vipCta}」约 3s 热更新`, ) } catch (e: any) { setErr(e.message || '切换方案失败') await load().catch(() => undefined) } finally { setBusy(false) } } if (!cfg && !err) { return
加载 VIP 配置…
} const vipJumpOn = draft.joinGroupAction === 'vip' return (

VIP

默认全关。付费方案与小册相同(免费 / 已发布道具);改完写远程配置并升 version,小程序约 3 秒热更新。

{err ? (

{err}

) : null} {msg ? (

{msg}

) : null}

立即加入 · 付费控制

一键切换免费 / 已发布道具。切换后小程序「立即加入」按钮文案与价格立即跟随(已打开会员页约 3s 热更新;否则重新进入即可)。

小程序按钮预览
{draft.vipCta || '立即加入'}
{isFree ? '免费模式 · 不拉起虚拟支付' : `道具 ${draft.vipProductId || '-'} · ¥${(draft.vipPriceFen / 100).toFixed(0)}`}
{offers.map((o) => { const active = o.key === activeOfferKey const img = o.image ? `/${o.image.replace(/^\/+/, '')}` : '' return ( ) })}
{!isFree ? (
{draft.vipProductId || '—'}
¥{(draft.vipPriceFen / 100).toFixed(0)}({draft.vipPriceFen} 分)
{draft.vipCta}
) : (
当前为免费会员:小程序不拉起支付,「立即加入」显示「免费加入」。
)}

开关(默认关闭)

VIP 会员入口

关闭后会员页闸门拦截

加群 → 跳转 VIP

关闭=社群「加群」弹窗引导;开启=跳转会员页

数字游民 · 游戏入口

已冻结隐藏,首页不展示;请勿开启

{cfg?.updatedAt ? (

最近更新 · v{cfg.version ?? '—'} · {cfg.updatedAt}

) : null}
) }