import { useCallback, useEffect, useMemo, useState } from 'react' import { Link } from 'react-router-dom' import { api } from '../api' import { WxBadge, WxEmpty, WxHead, WxTabs, WxToast, copyText, fmtTime, shortId, } from '../components/wxUi' type UnlockRow = { book_id: string; unlocked_at: number; source: string } type UserRow = { openid: string unionid?: string | null nickname?: string | null status?: string notes?: string | null pb_record_id?: string | null updated_at: number created_at?: number unlocked_default: boolean unlocked_vip?: boolean unlocks: UnlockRow[] } type Tab = 'all' | 'unlocked' | 'vip' | 'locked' | 'blocked' const VIP_BOOK_ID = '__vip__' export default function Users() { const [users, setUsers] = useState([]) const [q, setQ] = useState('') const [tab, setTab] = useState('all') const [msg, setMsg] = useState('') const [err, setErr] = useState('') const [manualOpenid, setManualOpenid] = useState('') const [busy, setBusy] = useState('') const [loading, setLoading] = useState(true) const [openId, setOpenId] = useState(null) const refresh = useCallback( async (search = q) => { setLoading(true) setErr('') try { const qs = search.trim() ? `?q=${encodeURIComponent(search.trim())}` : '' setUsers(await api(`/api/admin/users${qs}`)) } catch (e: any) { setErr(e.message || String(e)) } finally { setLoading(false) } }, [q], ) useEffect(() => { void refresh('') }, []) const counts = useMemo(() => { let unlocked = 0 let vip = 0 let locked = 0 let blocked = 0 for (const u of users) { if ((u.status || 'active') === 'blocked') blocked += 1 if (u.unlocked_vip) vip += 1 if (u.unlocked_default) unlocked += 1 else locked += 1 } return { total: users.length, unlocked, vip, locked, blocked } }, [users]) const filtered = useMemo(() => { return users.filter((u) => { const blocked = (u.status || 'active') === 'blocked' if (tab === 'unlocked' && !u.unlocked_default) return false if (tab === 'vip' && !u.unlocked_vip) return false if (tab === 'locked' && u.unlocked_default) return false if (tab === 'blocked' && !blocked) return false return true }) }, [users, tab]) async function run(key: string, fn: () => Promise, okMsg?: string) { setBusy(key) setMsg('') setErr('') try { await fn() await refresh() if (okMsg) setMsg(okMsg) } catch (e: any) { setErr(e.message || String(e)) } finally { setBusy('') } } return (
VIP用户 订单 {err ? ( setErr('')}> {err} ) : null} {msg ? ( setMsg('')}> {msg} ) : null}
读者
{counts.total}
VIP
{counts.vip}
小册已解锁
{counts.unlocked}
小册未解锁
{counts.locked}
拉黑
{counts.blocked}
setQ(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') void refresh() }} /> setManualOpenid(e.target.value)} />

小册 = book_id default;VIP = book_id __vip__(支付发货写入)。详见{' '} 部署清单 。

{filtered.length ? ( {filtered.map((u) => { const blocked = (u.status || 'active') === 'blocked' const open = openId === u.openid return ( ) })}
读者 状态 小册 VIP 备注 活跃 操作
{u.nickname ?
{u.nickname}
: null} {open ? (
openid: {u.openid} {u.unionid ? `\nunionid: ${u.unionid}` : ''} {u.pb_record_id ? `\nPB: ${u.pb_record_id}` : ''} {'\n'}unlocks:{' '} {u.unlocks?.length ? u.unlocks .map((x) => `${x.book_id}${x.source ? `(${x.source})` : ''}`) .join(' · ') : '无'}
) : null}
{blocked ? '拉黑' : '正常'} {u.unlocked_default ? '全书' : '未解锁'} {u.unlocked_vip ? 'VIP' : '非会员'} { const notes = e.target.value if (notes === (u.notes || '')) return void run(`notes-${u.openid}`, async () => { await api(`/api/admin/users/${encodeURIComponent(u.openid)}`, { method: 'PATCH', body: JSON.stringify({ notes }), }) }, '备注已保存') }} /> {fmtTime(u.updated_at)}
订单 VIP页
) : ( )}
) }