Harden weapp for review: silent audit book, no flashy module defaults, safe-area PX, subpackages/lazyCodeLoading, and CDN nav icons. Document pre3 and move next work to pre4. Co-authored-by: Cursor <cursoragent@cursor.com>
454 lines
16 KiB
TypeScript
454 lines
16 KiB
TypeScript
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<UserRow[]>([])
|
||
const [q, setQ] = useState('')
|
||
const [tab, setTab] = useState<Tab>('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<string | null>(null)
|
||
|
||
const refresh = useCallback(
|
||
async (search = q) => {
|
||
setLoading(true)
|
||
setErr('')
|
||
try {
|
||
const qs = search.trim() ? `?q=${encodeURIComponent(search.trim())}` : ''
|
||
setUsers(await api<UserRow[]>(`/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<void>, 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 (
|
||
<div className="wx-page">
|
||
<WxHead
|
||
eyebrow="读者运营"
|
||
title="读者"
|
||
desc={`全量读者与小册解锁。VIP 会员身份建议在「VIP用户」管理。共 ${counts.total} 人 · VIP ${counts.vip}。`}
|
||
>
|
||
<Link className="wx-btn wx-btn-ghost" to="/wechat/vip/users">
|
||
VIP用户
|
||
</Link>
|
||
<Link className="wx-btn wx-btn-ghost" to="/wechat/orders">
|
||
订单
|
||
</Link>
|
||
<button type="button" className="wx-btn" onClick={() => void refresh()} disabled={loading}>
|
||
{loading ? '刷新中…' : '刷新'}
|
||
</button>
|
||
</WxHead>
|
||
|
||
{err ? (
|
||
<WxToast kind="err" onClose={() => setErr('')}>
|
||
{err}
|
||
</WxToast>
|
||
) : null}
|
||
{msg ? (
|
||
<WxToast kind="ok" onClose={() => setMsg('')}>
|
||
{msg}
|
||
</WxToast>
|
||
) : null}
|
||
|
||
<div className="wx-stats" style={{ gridTemplateColumns: 'repeat(5, minmax(0, 1fr))' }}>
|
||
<div className="wx-stat">
|
||
<div className="wx-stat-label">读者</div>
|
||
<div className="wx-stat-val">{counts.total}</div>
|
||
</div>
|
||
<div className="wx-stat">
|
||
<div className="wx-stat-label">VIP</div>
|
||
<div className="wx-stat-val">{counts.vip}</div>
|
||
</div>
|
||
<div className="wx-stat">
|
||
<div className="wx-stat-label">小册已解锁</div>
|
||
<div className="wx-stat-val">{counts.unlocked}</div>
|
||
</div>
|
||
<div className="wx-stat wx-stat--warn">
|
||
<div className="wx-stat-label">小册未解锁</div>
|
||
<div className="wx-stat-val">{counts.locked}</div>
|
||
</div>
|
||
<div className="wx-stat">
|
||
<div className="wx-stat-label">拉黑</div>
|
||
<div className="wx-stat-val">{counts.blocked}</div>
|
||
</div>
|
||
</div>
|
||
|
||
<section className="wx-panel">
|
||
<div className="wx-toolbar" style={{ marginBottom: 0 }}>
|
||
<input
|
||
type="search"
|
||
value={q}
|
||
placeholder="搜索 openid / 备注 / 昵称,回车搜索"
|
||
onChange={(e) => setQ(e.target.value)}
|
||
onKeyDown={(e) => {
|
||
if (e.key === 'Enter') void refresh()
|
||
}}
|
||
/>
|
||
<button type="button" className="wx-btn wx-btn-ghost" onClick={() => void refresh()}>
|
||
搜索
|
||
</button>
|
||
<input
|
||
type="text"
|
||
value={manualOpenid}
|
||
placeholder="补录 openid"
|
||
onChange={(e) => setManualOpenid(e.target.value)}
|
||
/>
|
||
<button
|
||
type="button"
|
||
className="wx-btn"
|
||
disabled={!manualOpenid.trim() || busy === 'manual'}
|
||
onClick={() =>
|
||
void run(
|
||
'manual',
|
||
async () => {
|
||
await api('/api/admin/unlock', {
|
||
method: 'POST',
|
||
body: JSON.stringify({ openid: manualOpenid.trim(), book_id: 'default' }),
|
||
})
|
||
setManualOpenid('')
|
||
},
|
||
`已解锁小册 ${manualOpenid.trim()}`,
|
||
)
|
||
}
|
||
>
|
||
补发小册
|
||
</button>
|
||
<button
|
||
type="button"
|
||
className="wx-btn wx-btn-ghost"
|
||
disabled={!manualOpenid.trim() || busy === 'manual-vip'}
|
||
onClick={() =>
|
||
void run(
|
||
'manual-vip',
|
||
async () => {
|
||
await api('/api/admin/unlock', {
|
||
method: 'POST',
|
||
body: JSON.stringify({ openid: manualOpenid.trim(), book_id: VIP_BOOK_ID }),
|
||
})
|
||
setManualOpenid('')
|
||
},
|
||
`已开通 VIP ${manualOpenid.trim()}`,
|
||
)
|
||
}
|
||
>
|
||
补发 VIP
|
||
</button>
|
||
</div>
|
||
<p className="wx-hint" style={{ marginTop: 12, display: 'block' }}>
|
||
小册 = book_id default;VIP = book_id __vip__(支付发货写入)。详见{' '}
|
||
<Link className="wx-link" to="/wechat/checklist">
|
||
部署清单
|
||
</Link>
|
||
。
|
||
</p>
|
||
</section>
|
||
|
||
<WxTabs
|
||
value={tab}
|
||
onChange={setTab}
|
||
items={[
|
||
{ key: 'all', label: '全部', count: counts.total },
|
||
{ key: 'vip', label: 'VIP', count: counts.vip },
|
||
{ key: 'unlocked', label: '小册已解锁', count: counts.unlocked },
|
||
{ key: 'locked', label: '小册未解锁', count: counts.locked },
|
||
{ key: 'blocked', label: '拉黑', count: counts.blocked },
|
||
]}
|
||
/>
|
||
|
||
<div className="wx-table-wrap">
|
||
{filtered.length ? (
|
||
<table className="wx-table">
|
||
<thead>
|
||
<tr>
|
||
<th>读者</th>
|
||
<th>状态</th>
|
||
<th>小册</th>
|
||
<th>VIP</th>
|
||
<th>备注</th>
|
||
<th>活跃</th>
|
||
<th>操作</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{filtered.map((u) => {
|
||
const blocked = (u.status || 'active') === 'blocked'
|
||
const open = openId === u.openid
|
||
return (
|
||
<tr key={u.openid} className={`${blocked ? 'is-dim ' : ''}${open ? 'is-open' : ''}`}>
|
||
<td>
|
||
<button
|
||
type="button"
|
||
className="wx-btn-sm"
|
||
title={u.openid}
|
||
onClick={() =>
|
||
void copyText(u.openid).then((ok) => setMsg(ok ? '已复制 OpenID' : '复制失败'))
|
||
}
|
||
>
|
||
{shortId(u.openid, 12)}
|
||
</button>
|
||
{u.nickname ? <div style={{ marginTop: 4 }}>{u.nickname}</div> : null}
|
||
{open ? (
|
||
<div className="wx-detail">
|
||
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(' · ')
|
||
: '无'}
|
||
</div>
|
||
) : null}
|
||
</td>
|
||
<td>
|
||
<WxBadge tone={blocked ? 'bad' : 'ok'}>{blocked ? '拉黑' : '正常'}</WxBadge>
|
||
</td>
|
||
<td>
|
||
<WxBadge tone={u.unlocked_default ? 'ok' : 'muted'}>
|
||
{u.unlocked_default ? '全书' : '未解锁'}
|
||
</WxBadge>
|
||
</td>
|
||
<td>
|
||
<WxBadge tone={u.unlocked_vip ? 'ok' : 'muted'}>
|
||
{u.unlocked_vip ? 'VIP' : '非会员'}
|
||
</WxBadge>
|
||
</td>
|
||
<td style={{ minWidth: 160 }}>
|
||
<input
|
||
defaultValue={u.notes || ''}
|
||
key={`${u.openid}-notes-${u.updated_at}`}
|
||
placeholder="备注,失焦保存"
|
||
onBlur={(e) => {
|
||
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 }),
|
||
})
|
||
}, '备注已保存')
|
||
}}
|
||
/>
|
||
</td>
|
||
<td>{fmtTime(u.updated_at)}</td>
|
||
<td>
|
||
<div className="wx-row-actions">
|
||
<button
|
||
type="button"
|
||
className="wx-btn-sm"
|
||
onClick={() => setOpenId(open ? null : u.openid)}
|
||
>
|
||
{open ? '收起' : '详情'}
|
||
</button>
|
||
<Link
|
||
className="wx-btn-sm"
|
||
to={`/wechat/orders?openid=${encodeURIComponent(u.openid)}`}
|
||
>
|
||
订单
|
||
</Link>
|
||
<Link
|
||
className="wx-btn-sm"
|
||
to={`/wechat/vip/users?openid=${encodeURIComponent(u.openid)}`}
|
||
>
|
||
VIP页
|
||
</Link>
|
||
<button
|
||
type="button"
|
||
className="wx-btn-sm primary"
|
||
disabled={u.unlocked_default || busy === `unlock-${u.openid}`}
|
||
onClick={() =>
|
||
void run(
|
||
`unlock-${u.openid}`,
|
||
async () => {
|
||
await api('/api/admin/unlock', {
|
||
method: 'POST',
|
||
body: JSON.stringify({ openid: u.openid, book_id: 'default' }),
|
||
})
|
||
},
|
||
'已补发小册',
|
||
)
|
||
}
|
||
>
|
||
小册
|
||
</button>
|
||
<button
|
||
type="button"
|
||
className="wx-btn-sm"
|
||
disabled={!u.unlocked_default || busy === `revoke-${u.openid}`}
|
||
onClick={() => {
|
||
if (!confirm(`收回 ${u.openid} 的小册解锁?`)) return
|
||
void run(
|
||
`revoke-${u.openid}`,
|
||
async () => {
|
||
await api('/api/admin/revoke-unlock', {
|
||
method: 'POST',
|
||
body: JSON.stringify({ openid: u.openid, book_id: 'default' }),
|
||
})
|
||
},
|
||
'已收回小册',
|
||
)
|
||
}}
|
||
>
|
||
收小册
|
||
</button>
|
||
<button
|
||
type="button"
|
||
className="wx-btn-sm primary"
|
||
disabled={!!u.unlocked_vip || busy === `vip-${u.openid}`}
|
||
onClick={() =>
|
||
void run(
|
||
`vip-${u.openid}`,
|
||
async () => {
|
||
await api('/api/admin/unlock', {
|
||
method: 'POST',
|
||
body: JSON.stringify({ openid: u.openid, book_id: VIP_BOOK_ID }),
|
||
})
|
||
},
|
||
'已开通 VIP',
|
||
)
|
||
}
|
||
>
|
||
VIP
|
||
</button>
|
||
<button
|
||
type="button"
|
||
className="wx-btn-sm"
|
||
disabled={!u.unlocked_vip || busy === `rvip-${u.openid}`}
|
||
onClick={() => {
|
||
if (!confirm(`收回 ${u.openid} 的 VIP?`)) return
|
||
void run(
|
||
`rvip-${u.openid}`,
|
||
async () => {
|
||
await api('/api/admin/revoke-unlock', {
|
||
method: 'POST',
|
||
body: JSON.stringify({ openid: u.openid, book_id: VIP_BOOK_ID }),
|
||
})
|
||
},
|
||
'已收回 VIP',
|
||
)
|
||
}}
|
||
>
|
||
收VIP
|
||
</button>
|
||
<button
|
||
type="button"
|
||
className={`wx-btn-sm${blocked ? '' : ' danger'}`}
|
||
disabled={busy === `block-${u.openid}`}
|
||
onClick={() => {
|
||
if (!blocked && !confirm(`拉黑 ${u.openid}?`)) return
|
||
void run(
|
||
`block-${u.openid}`,
|
||
async () => {
|
||
await api(
|
||
`/api/admin/users/${encodeURIComponent(u.openid)}/${
|
||
blocked ? 'unblock' : 'block'
|
||
}`,
|
||
{ method: 'POST' },
|
||
)
|
||
},
|
||
blocked ? '已恢复' : '已拉黑',
|
||
)
|
||
}}
|
||
>
|
||
{blocked ? '解除' : '拉黑'}
|
||
</button>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
)
|
||
})}
|
||
</tbody>
|
||
</table>
|
||
) : (
|
||
<WxEmpty
|
||
title={loading ? '加载中…' : '暂无读者'}
|
||
hint="用户打开小程序登录后会出现;也可上方补录 openid"
|
||
/>
|
||
)}
|
||
</div>
|
||
</div>
|
||
)
|
||
}
|