nomadro/apps/admin/src/pages/Vip.tsx
Eric b90e524b25 Add portal/admin PWA icons and freeze the game home entry.
Ship brand favicons and install-to-home manifests for nomadro.cn and admin, improve admin mobile drawer UX, and keep the game module forced off.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-06 23:19:59 +08:00

312 lines
9.9 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.

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<VipCfg | null>(null)
const [draft, setDraft] = useState<VipCfg>(empty)
const [offers, setOffers] = useState<ProductOffer[]>([])
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<VipCfg>('/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<VipCfg>, okHint: string) {
setBusy(true)
setMsg('')
setErr('')
const next = { ...draft, ...patch }
setDraft(next)
try {
const data = await api<VipCfg>('/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<VipCfg>('/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 <div className="page">加载 VIP 配置…</div>
}
const vipJumpOn = draft.joinGroupAction === 'vip'
return (
<div className="page">
<header className="page-head">
<div>
<h1>VIP</h1>
<p>
默认全关。付费方案与小册相同(免费 / 已发布道具);改完写远程配置并升 version,小程序约 3
秒热更新。
</p>
</div>
</header>
{err ? (
<p className="badge bad" style={{ display: 'inline-block', marginBottom: 12 }}>
{err}
</p>
) : null}
{msg ? (
<p className="badge ok" style={{ display: 'inline-block', marginBottom: 12 }}>
{msg}
</p>
) : null}
<section className="panel monetize-panel" style={{ marginBottom: 16 }}>
<div className="monetize-head">
<div>
<h2 className="monetize-title">立即加入 · 付费控制</h2>
<p className="hint">
一键切换免费 / 已发布道具。切换后小程序「立即加入」按钮文案与价格立即跟随(已打开会员页约
3s 热更新;否则重新进入即可)。
</p>
</div>
<div className="live-preview">
<span className="live-preview-label">小程序按钮预览</span>
<div className={`live-btn ${isFree ? 'free' : ''}`}>{draft.vipCta || '立即加入'}</div>
<span className="live-preview-sub">
{isFree
? '免费模式 · 不拉起虚拟支付'
: `道具 ${draft.vipProductId || '-'} · ¥${(draft.vipPriceFen / 100).toFixed(0)}`}
</span>
</div>
</div>
<div className="offer-grid offer-grid-6">
{offers.map((o) => {
const active = o.key === activeOfferKey
const img = o.image ? `/${o.image.replace(/^\/+/, '')}` : ''
return (
<button
key={o.key}
type="button"
className={`offer-card ${active ? 'active' : ''} ${o.free ? 'is-free' : ''}`}
disabled={busy}
onClick={() => void applyOffer(o.key)}
>
{o.free ? (
<div className="offer-free-badge">FREE</div>
) : img ? (
<img className="offer-img" src={img} alt={o.label} />
) : (
<div className="offer-free-badge">{o.product_id.slice(0, 6)}</div>
)}
<strong>{o.label}</strong>
<span className="offer-id">{o.free ? '免费加入' : o.product_id}</span>
<em>{o.price_yuan}</em>
{o.remark && !o.remark.includes('unlock_percent') ? (
<i className="offer-remark">{o.remark}</i>
) : o.free ? (
<i className="offer-remark">不拉起支付 · 内容全开</i>
) : null}
{active ? <b className="offer-on">使用中</b> : null}
</button>
)
})}
</div>
{!isFree ? (
<div className="unlock-hero monetize-unlock">
<div className="price-box mono-box" style={{ width: '100%' }}>
<div>
<label>道具 ID</label>
<code>{draft.vipProductId || '—'}</code>
</div>
<div>
<label>价格</label>
<code>
¥{(draft.vipPriceFen / 100).toFixed(0)}({draft.vipPriceFen} 分)
</code>
</div>
<div>
<label>CTA</label>
<code>{draft.vipCta}</code>
</div>
</div>
</div>
) : (
<div className="free-banner">当前为免费会员:小程序不拉起支付,「立即加入」显示「免费加入」。</div>
)}
</section>
<section className="panel" style={{ marginBottom: 16 }}>
<h2>开关(默认关闭)</h2>
<div className="vip-row">
<div>
<strong>VIP 会员入口</strong>
<p className="vip-hint">关闭后会员页闸门拦截</p>
</div>
<button
type="button"
className={draft.vipEnabled ? 'primary-link' : 'ghost-btn'}
disabled={busy}
onClick={() =>
void save({ vipEnabled: !draft.vipEnabled }, draft.vipEnabled ? 'VIP 已关闭' : 'VIP 已开启')
}
>
{draft.vipEnabled ? '已开启' : '已关闭'}
</button>
</div>
<div className="vip-row">
<div>
<strong>加群 → 跳转 VIP</strong>
<p className="vip-hint">关闭=社群「加群」弹窗引导;开启=跳转会员页</p>
</div>
<button
type="button"
className={vipJumpOn ? 'primary-link' : 'ghost-btn'}
disabled={busy}
onClick={() =>
void save(
{ joinGroupAction: vipJumpOn ? 'popup' : 'vip' },
vipJumpOn ? '加群跳 VIP 已关闭(弹窗)' : '加群跳 VIP 已开启',
)
}
>
{vipJumpOn ? '已开启' : '已关闭'}
</button>
</div>
<div className="vip-row">
<div>
<strong>数字游民 · 游戏入口</strong>
<p className="vip-hint">已冻结隐藏,首页不展示;请勿开启</p>
</div>
<button type="button" className="ghost-btn" disabled>
已冻结
</button>
</div>
{cfg?.updatedAt ? (
<p className="vip-hint" style={{ marginTop: 16 }}>
最近更新 · v{cfg.version ?? '—'} · {cfg.updatedAt}
</p>
) : null}
</section>
</div>
)
}