/** 小册列表 · 创建 / 进入编辑 / 删除 */ 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 = { 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([]) 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('/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 (
{err ? ( setErr('')}> {err} ) : null} {msg ? ( setMsg('')}> {msg} ) : null}

新建小册

id 用作路由 `/wechat/books/:id`

setNewId(e.target.value)} /> setNewTitle(e.target.value)} />
{items.length ? ( {items.map((b) => ( ))}
书籍 试读 / 价格 章节 更新 操作
{b.cover_emoji || '📗'} {displayTitle(b)}
{b.id} {b.title && b.title !== displayTitle(b) ? ` · db:${b.title}` : ''}
{b.subtitle ? (
{b.subtitle}
) : null}
{payLabel(b)} {b.chapter_count ?? 0} 章 · {b.word_count ?? 0} 字 {b.updated_at ? fmtTime(b.updated_at) : '—'}
{b.id === 'nomad' || b.id === 'cloudphone' || b.id === 'default' ? '正文+收费' : '编辑'} {b.id !== 'default' && b.id !== '__vip__' ? ( ) : null}
) : ( )}
) }