From cee288b764ea5e148cceaf1337bd85295b6f65d4 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 22 Nov 2026 11:45:01 +0800 Subject: [PATCH] Give each ebook independent trial percent and paid unlock offers. Co-authored-by: Cursor --- apps/admin/deploy/_deploy_per_book_pay_82.py | 82 ++++++ .../src/components/BookMonetizePanel.tsx | 235 +++++++++++++++ apps/admin/src/pages/BookEdit.tsx | 4 +- apps/admin/src/pages/VipLibrary.tsx | 33 ++- apps/api/services/ebook_svc/routers/admin.py | 17 +- .../nomadmini/backend/app/routers/admin.py | 17 +- .../h5/src/pages/vip-ebook/index.scss | 55 ++++ .../h5/src/pages/vip-ebook/index.tsx | 268 ++++++++++++++---- 8 files changed, 630 insertions(+), 81 deletions(-) create mode 100644 apps/admin/deploy/_deploy_per_book_pay_82.py create mode 100644 apps/admin/src/components/BookMonetizePanel.tsx diff --git a/apps/admin/deploy/_deploy_per_book_pay_82.py b/apps/admin/deploy/_deploy_per_book_pay_82.py new file mode 100644 index 0000000..d8eae66 --- /dev/null +++ b/apps/admin/deploy/_deploy_per_book_pay_82.py @@ -0,0 +1,82 @@ +# -*- coding: utf-8 -*- +"""Deploy admin.py (book shells) + admin SPA for per-book monetize.""" +from __future__ import annotations + +import subprocess +import tarfile +import time +from io import BytesIO +from pathlib import Path + +import paramiko + +REPO = Path(__file__).resolve().parents[3] +ROOT = REPO / "apps" / "admin" +HOST, USER, PASS = "82.157.112.245", "ubuntu", "Xiao4669805" + + +def main() -> int: + print("vite build…", flush=True) + r = subprocess.run( + ["pnpm", "exec", "vite", "build"], + cwd=ROOT, + shell=True, + capture_output=True, + encoding="utf-8", + errors="replace", + ) + if r.returncode != 0: + print(r.stdout) + print(r.stderr) + return r.returncode + + dist = ROOT / "dist" + buf = BytesIO() + with tarfile.open(fileobj=buf, mode="w:gz") as tar: + for p in dist.rglob("*"): + if p.is_file(): + tar.add(p, arcname="admin-dist/" + p.relative_to(dist).as_posix()) + buf.seek(0) + + api = (REPO / "apps/api/services/ebook_svc/routers/admin.py").read_text(encoding="utf-8") + api = api.replace("ebook_svc.", "app.").encode("utf-8") + + c = paramiko.SSHClient() + c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + c.connect(HOST, username=USER, password=PASS, timeout=60, allow_agent=False, look_for_keys=False) + sftp = c.open_sftp() + with sftp.file("/tmp/admin-spa.tar.gz", "wb") as rf: + rf.write(buf.getvalue()) + with sftp.file("/tmp/admin.py", "wb") as rf: + rf.write(api) + sftp.close() + + cmd = r""" +set -e +# keep props +sudo mkdir -p /tmp/props-bak && sudo cp -a /www/wwwroot/admin.nomadro.cn/props/. /tmp/props-bak/ 2>/dev/null || true +cd /tmp +rm -rf admin-unpack && mkdir admin-unpack +tar -xzf admin-spa.tar.gz -C admin-unpack +sudo rsync -a --delete admin-unpack/admin-dist/ /www/wwwroot/admin.nomadro.cn/ +sudo mkdir -p /www/wwwroot/admin.nomadro.cn/props +sudo cp -a /tmp/props-bak/. /www/wwwroot/admin.nomadro.cn/props/ 2>/dev/null || true +sudo chown -R www:www /www/wwwroot/admin.nomadro.cn +sudo cp /tmp/admin.py /opt/nomadmini/backend/app/routers/admin.py +sudo systemctl restart nomadmini-api.service +sleep 2 +systemctl is-active nomadmini-api.service +curl -sk -o /dev/null -w "admin %{http_code}\n" https://admin.nomadro.cn/wechat/vip/ebooks/nomad +curl -sk -o /dev/null -w "props %{http_code}\n" https://admin.nomadro.cn/props/nomad_lite.png +""" + _, stdout, stderr = c.exec_command(cmd, timeout=120) + print(stdout.read().decode()) + err = stderr.read().decode() + if err: + print("STDERR", err[-800:]) + c.close() + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/apps/admin/src/components/BookMonetizePanel.tsx b/apps/admin/src/components/BookMonetizePanel.tsx new file mode 100644 index 0000000..db2d8f8 --- /dev/null +++ b/apps/admin/src/components/BookMonetizePanel.tsx @@ -0,0 +1,235 @@ +/** + * 单本小册独立试读/付费方案(互不影响)。 + * 数据落在 books/{bookId}:product_id / price_fen / unlock_percent。 + */ +import { useCallback, useEffect, useMemo, useState } from 'react' +import { api } from '../api' +import '../pages/BookEdit.css' + +export type ProductOffer = { + key: string + label: string + name: string + product_id: string + price_fen: number + price_yuan: string + free: boolean + image: string + remark?: string +} + +export type BookMonetize = { + id: string + title?: string + product_id: string + price_fen: number + unlock_percent: number + offer_key?: string + offer_name?: string + is_free?: boolean +} + +type Props = { + bookId: string + /** 展示名,如「数字游民」 */ + bookLabel?: string + /** 方案变更后回调(可用来同步 VIP 库 free 标记) */ + onChanged?: (book: BookMonetize) => void +} + +export default function BookMonetizePanel({ bookId, bookLabel, onChanged }: Props) { + const [book, setBook] = useState(null) + const [offers, setOffers] = useState([]) + const [busy, setBusy] = useState(false) + const [msg, setMsg] = useState('') + const [err, setErr] = useState('') + + const load = useCallback(async () => { + const [b, o] = await Promise.all([ + api(`/api/admin/books/${encodeURIComponent(bookId)}`), + api<{ items: ProductOffer[] }>('/api/admin/product-offers'), + ]) + setBook(b) + setOffers(o.items || []) + }, [bookId]) + + useEffect(() => { + setMsg('') + setErr('') + load().catch((e) => setErr(e.message || '加载付费方案失败')) + }, [load]) + + const isFree = !!book && (book.is_free || book.unlock_percent >= 100 || !book.product_id) + const activeOfferKey = useMemo(() => { + if (!book) return '' + if (book.offer_key) return book.offer_key + if (isFree) return 'free' + return ( + offers.find((o) => !o.free && o.product_id === book.product_id)?.key || + '' + ) + }, [book, offers, isFree]) + + async function applyOffer(offerKey: string) { + if (!bookId || busy) return + setBusy(true) + setMsg('') + setErr('') + try { + const updated = await api( + `/api/admin/books/${encodeURIComponent(bookId)}/offer`, + { + method: 'POST', + body: JSON.stringify({ offer_key: offerKey, keep_preview_percent: true }), + }, + ) + setBook(updated) + onChanged?.(updated) + const label = offers.find((o) => o.key === offerKey)?.name || offerKey + setMsg( + updated.unlock_percent >= 100 || !updated.product_id + ? `「${bookLabel || bookId}」已切免费 · 与其它小册互不影响` + : `「${bookLabel || bookId}」已切 ${label} · 试读 ${Math.min(99, updated.unlock_percent)}% · 仅本册`, + ) + } catch (e: any) { + setErr(e.message || '切换方案失败') + await load().catch(() => undefined) + } finally { + setBusy(false) + } + } + + async function savePreview() { + if (!book || busy) return + setBusy(true) + setMsg('') + setErr('') + try { + const updated = await api( + `/api/admin/books/${encodeURIComponent(bookId)}`, + { + method: 'PUT', + body: JSON.stringify({ + unlock_percent: Math.min(99, Math.max(1, Number(book.unlock_percent) || 30)), + }), + }, + ) + setBook(updated) + onChanged?.(updated) + setMsg(`「${bookLabel || bookId}」试读 ${Math.min(99, updated.unlock_percent)}% 已保存 · 仅本册`) + } catch (e: any) { + setErr(e.message || '保存失败') + } finally { + setBusy(false) + } + } + + if (!book && !err) { + return
加载本册付费方案…
+ } + if (!book) { + return ( +
+

+ {err} +

+
+ ) + } + + return ( +
+
+
+

本册 · 试读与付费

+

+ 只改《{bookLabel || book.title || bookId}》的道具与试读比例,不影响其它小册 / VIP + 会员价。后台切换后小程序约 1–2 秒热更新。 +

+
+
+ 本册按钮预览 +
+ {isFree ? '开始阅读' : `解锁全文 · ¥${(book.price_fen / 100).toFixed(0)}`} +
+ + {isFree + ? '免费模式 · 无支付墙' + : `${book.offer_name || book.product_id || '-'} · 试读 ${Math.min(99, book.unlock_percent)}%`} + +
+
+ + {err ?

{err}

: null} + {msg ?

{msg}

: null} + +
+ {offers.map((o) => { + const active = o.key === activeOfferKey + const img = o.image ? `/${o.image.replace(/^\/+/, '')}` : '' + return ( + + ) + })} +
+ + {!isFree ? ( +
+
+ +
+ + setBook({ ...book, unlock_percent: Number(e.target.value) }) + } + /> + {Math.min(99, book.unlock_percent)}% +
+

点「保存试读」写入本册;点免费卡片 = 全书开放。

+ +
+
+
+ + {book.product_id || '—'} +
+
+ + + ¥{(book.price_fen / 100).toFixed(0)}({book.price_fen} 分) + +
+
+
+ ) : ( +
本册当前免费全书;其它小册付费设置不变。
+ )} +
+ ) +} diff --git a/apps/admin/src/pages/BookEdit.tsx b/apps/admin/src/pages/BookEdit.tsx index ff674d6..7944e0b 100644 --- a/apps/admin/src/pages/BookEdit.tsx +++ b/apps/admin/src/pages/BookEdit.tsx @@ -298,9 +298,9 @@ export default function BookEdit() {
-

阅读与付费

+

本册 · 试读与付费

- 随时切换免费 / 付费档。小程序封面与阅读页约 1–2 秒热更新价格与解锁按钮,无需重进。 + 只改本册道具与试读比例,与其它小册互不影响。小程序约 1–2 秒热更新。

diff --git a/apps/admin/src/pages/VipLibrary.tsx b/apps/admin/src/pages/VipLibrary.tsx index 7dadd1e..bd064b0 100644 --- a/apps/admin/src/pages/VipLibrary.tsx +++ b/apps/admin/src/pages/VipLibrary.tsx @@ -1,6 +1,7 @@ import { Link, useLocation, useNavigate, useParams } from 'react-router-dom' import { useCallback, useEffect, useMemo, useState } from 'react' import { api } from '../api' +import BookMonetizePanel, { type BookMonetize } from '../components/BookMonetizePanel' import './Vip.css' type Ebook = { @@ -501,25 +502,23 @@ export default function VipLibrary() { onChange={(e) => setDraftTitle(e.target.value)} />
-
-
- 免费开放 -

打开后无需会员也能读完全书

-
- -
+ {/* 每本独立试读/道具,与 youtube运营笔记(books/default) 同机制、互不影响 */} + { + const free = !!(m.is_free || m.unlock_percent >= 100 || !m.product_id) + setDraftFree(free) + // 同步 VIP 库 free 标记,供落地页卡片展示;付费字段以 books 表为准 + void api(`/api/admin/vip/library/ebooks/${bookId}`, { + method: 'PUT', + body: JSON.stringify({ free }), + }).catch(() => undefined) + }} + /> +