Refresh audience/admin UX, ntfy/track, social paths, and replace ebook chapters with the expanded catalog; keep local SQLite and one-off probe scripts out of tree. Co-authored-by: Cursor <cursoragent@cursor.com>
217 lines
6.4 KiB
TypeScript
217 lines
6.4 KiB
TypeScript
/** 小册列表 · 创建 / 进入编辑 / 删除 */
|
||
import { useCallback, useEffect, useState } from 'react'
|
||
import { Link } from 'react-router-dom'
|
||
import { api } from '../api'
|
||
import { WxEmpty, WxHead, WxToast, fmtTime } from '../components/wxUi'
|
||
|
||
type Book = {
|
||
id: string
|
||
title: string
|
||
subtitle?: string
|
||
cover_emoji?: string
|
||
price_fen?: number
|
||
product_id?: string
|
||
unlock_percent?: number
|
||
chapter_count?: number
|
||
word_count?: number
|
||
updated_at?: number
|
||
}
|
||
|
||
const BOOK_DISPLAY: Record<string, { label: string; editTo: string }> = {
|
||
nomad: { label: '数字游民', editTo: '/wechat/vip/ebooks/nomad' },
|
||
cloudphone: { label: '云手机', editTo: '/wechat/vip/ebooks/cloudphone' },
|
||
default: { label: 'Youtube运营笔记', editTo: '/wechat/books/default' },
|
||
audit: { label: '数字游民 · 审核版', editTo: '/wechat/books/audit' },
|
||
}
|
||
|
||
function displayTitle(b: Book) {
|
||
return BOOK_DISPLAY[b.id]?.label || b.title || b.id
|
||
}
|
||
|
||
function editPath(b: Book) {
|
||
return BOOK_DISPLAY[b.id]?.editTo || `/wechat/books/${b.id}`
|
||
}
|
||
|
||
function payLabel(b: Book) {
|
||
const unlock = Number(b.unlock_percent ?? 0)
|
||
const fen = Number(b.price_fen ?? 0)
|
||
if (unlock >= 100 || !b.product_id) return '免费全书'
|
||
return `试读 ${unlock}% · ¥${(fen / 100).toFixed(0)} · ${b.product_id}`
|
||
}
|
||
|
||
export default function Books() {
|
||
const [items, setItems] = useState<Book[]>([])
|
||
const [loading, setLoading] = useState(true)
|
||
const [busy, setBusy] = useState(false)
|
||
const [err, setErr] = useState('')
|
||
const [msg, setMsg] = useState('')
|
||
const [newId, setNewId] = useState('')
|
||
const [newTitle, setNewTitle] = useState('')
|
||
|
||
const load = useCallback(async () => {
|
||
setLoading(true)
|
||
setErr('')
|
||
try {
|
||
const data = await api<Book[]>('/api/admin/books')
|
||
setItems(Array.isArray(data) ? data : [])
|
||
} catch (e: any) {
|
||
setErr(e.message || '加载失败')
|
||
} finally {
|
||
setLoading(false)
|
||
}
|
||
}, [])
|
||
|
||
useEffect(() => {
|
||
void load()
|
||
}, [load])
|
||
|
||
async function create() {
|
||
const id = newId.trim()
|
||
if (!id) {
|
||
setErr('需要书籍 id')
|
||
return
|
||
}
|
||
setBusy(true)
|
||
setErr('')
|
||
try {
|
||
await api('/api/admin/books', {
|
||
method: 'POST',
|
||
body: JSON.stringify({ id, title: newTitle.trim() || id, free: true }),
|
||
})
|
||
setMsg('已创建')
|
||
setNewId('')
|
||
setNewTitle('')
|
||
await load()
|
||
} catch (e: any) {
|
||
setErr(e.message || '创建失败')
|
||
} finally {
|
||
setBusy(false)
|
||
}
|
||
}
|
||
|
||
async function remove(id: string) {
|
||
if (!confirm(`删除小册 ${id}?`)) return
|
||
setBusy(true)
|
||
try {
|
||
await api(`/api/admin/books/${encodeURIComponent(id)}`, { method: 'DELETE' })
|
||
setMsg('已删除')
|
||
await load()
|
||
} catch (e: any) {
|
||
setErr(e.message || '删除失败')
|
||
} finally {
|
||
setBusy(false)
|
||
}
|
||
}
|
||
|
||
return (
|
||
<div className="wx-page">
|
||
<WxHead
|
||
eyebrow="内容"
|
||
title="小册列表"
|
||
desc="SQLite books 表:创建壳、进入章节编辑、删除(default / __vip__ 不可删)。"
|
||
>
|
||
<button type="button" className="wx-btn" onClick={() => void load()} disabled={loading}>
|
||
刷新
|
||
</button>
|
||
</WxHead>
|
||
|
||
{err ? (
|
||
<WxToast kind="err" onClose={() => setErr('')}>
|
||
{err}
|
||
</WxToast>
|
||
) : null}
|
||
{msg ? (
|
||
<WxToast kind="ok" onClose={() => setMsg('')}>
|
||
{msg}
|
||
</WxToast>
|
||
) : null}
|
||
|
||
<div className="wx-panel" style={{ marginBottom: 16 }}>
|
||
<div className="wx-panel-head">
|
||
<div>
|
||
<h2>新建小册</h2>
|
||
<p>id 用作路由 `/wechat/books/:id`</p>
|
||
</div>
|
||
</div>
|
||
<div className="wx-toolbar">
|
||
<input
|
||
placeholder="id,如 audit / youtube-notes"
|
||
value={newId}
|
||
onChange={(e) => setNewId(e.target.value)}
|
||
/>
|
||
<input
|
||
placeholder="标题(可选)"
|
||
value={newTitle}
|
||
onChange={(e) => setNewTitle(e.target.value)}
|
||
/>
|
||
<button type="button" className="wx-btn" disabled={busy} onClick={() => void create()}>
|
||
创建
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="wx-table-wrap">
|
||
{items.length ? (
|
||
<table className="wx-table">
|
||
<thead>
|
||
<tr>
|
||
<th>书籍</th>
|
||
<th>试读 / 价格</th>
|
||
<th>章节</th>
|
||
<th>更新</th>
|
||
<th>操作</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{items.map((b) => (
|
||
<tr key={b.id}>
|
||
<td>
|
||
<strong>
|
||
{b.cover_emoji || '📗'} {displayTitle(b)}
|
||
</strong>
|
||
<div className="wx-mono" style={{ opacity: 0.65, fontSize: 12 }}>
|
||
{b.id}
|
||
{b.title && b.title !== displayTitle(b) ? ` · db:${b.title}` : ''}
|
||
</div>
|
||
{b.subtitle ? (
|
||
<div className="wx-muted" style={{ fontSize: 12 }}>
|
||
{b.subtitle}
|
||
</div>
|
||
) : null}
|
||
</td>
|
||
<td>{payLabel(b)}</td>
|
||
<td>
|
||
{b.chapter_count ?? 0} 章 · {b.word_count ?? 0} 字
|
||
</td>
|
||
<td>{b.updated_at ? fmtTime(b.updated_at) : '—'}</td>
|
||
<td>
|
||
<div className="wx-row-actions">
|
||
<Link className="wx-btn-sm primary" to={editPath(b)}>
|
||
{b.id === 'nomad' || b.id === 'cloudphone' || b.id === 'default'
|
||
? '正文+收费'
|
||
: '编辑'}
|
||
</Link>
|
||
{b.id !== 'default' && b.id !== '__vip__' ? (
|
||
<button
|
||
type="button"
|
||
className="wx-btn-sm danger"
|
||
disabled={busy}
|
||
onClick={() => void remove(b.id)}
|
||
>
|
||
删除
|
||
</button>
|
||
) : null}
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
) : (
|
||
<WxEmpty title={loading ? '加载中…' : '暂无小册'} />
|
||
)}
|
||
</div>
|
||
</div>
|
||
)
|
||
}
|