nomadro/apps/admin/src/pages/Orders.tsx
Eric d0262eb58d Ship WeChat audit mode, package-size fixes, and live VIP switches.
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>
2026-10-11 15:12:06 +08:00

366 lines
12 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useCallback, useEffect, useMemo, useState } from 'react'
import { Link, useSearchParams } from 'react-router-dom'
import { api } from '../api'
import {
WxBadge,
WxEmpty,
WxHead,
WxTabs,
WxToast,
copyText,
fmtTime,
fmtYuan,
shortId,
} from '../components/wxUi'
type Order = {
out_trade_no: string
wx_order_id: string | null
openid: string
book_id: string
amount_fen: number
status: string
delivered: number
created_at: number
}
type StatusTab = 'all' | 'paid' | 'pending' | 'other'
type KindTab = 'all' | 'vip' | 'ebook' | 'other'
const VIP_BOOK_ID = '__vip__'
function statusTone(status: string): 'ok' | 'warn' | 'bad' | 'muted' {
const s = (status || '').toLowerCase()
if (s.includes('paid') || s.includes('success') || s === 'ok') return 'ok'
if (s.includes('pend') || s.includes('creat') || s.includes('wait')) return 'warn'
if (s.includes('fail') || s.includes('cancel') || s.includes('refund')) return 'bad'
return 'muted'
}
function statusLabel(status: string) {
const s = (status || '').toLowerCase()
if (s.includes('paid') || s.includes('success')) return '已支付'
if (s.includes('pend') || s.includes('creat') || s.includes('wait')) return '待支付'
if (s.includes('cancel')) return '已取消'
if (s.includes('fail')) return '失败'
if (s.includes('refund')) return '退款'
return status || '未知'
}
function orderKind(bookId: string): KindTab {
if (bookId === VIP_BOOK_ID) return 'vip'
if (!bookId || bookId === 'default') return 'ebook'
return 'other'
}
function kindLabel(bookId: string) {
if (bookId === VIP_BOOK_ID) return 'VIP 订单'
if (!bookId || bookId === 'default') return '小册'
return bookId
}
export default function Orders() {
const [searchParams, setSearchParams] = useSearchParams()
const deepOpenid = (searchParams.get('openid') || '').trim()
const deepKind = (searchParams.get('kind') || '').trim() as KindTab | ''
const [orders, setOrders] = useState<Order[]>([])
const [loading, setLoading] = useState(true)
const [err, setErr] = useState('')
const [msg, setMsg] = useState('')
const [q, setQ] = useState(deepOpenid)
const [tab, setTab] = useState<StatusTab>('all')
const [kind, setKind] = useState<KindTab>(
deepKind === 'vip' || deepKind === 'ebook' || deepKind === 'other' ? deepKind : 'all',
)
const [openId, setOpenId] = useState<string | null>(null)
const load = useCallback(() => {
setLoading(true)
setErr('')
api<Order[]>('/api/admin/orders')
.then(setOrders)
.catch((e: Error) => setErr(e.message || '加载失败'))
.finally(() => setLoading(false))
}, [])
useEffect(() => {
load()
}, [load])
useEffect(() => {
if (deepOpenid) setQ(deepOpenid)
if (deepKind === 'vip' || deepKind === 'ebook' || deepKind === 'other') setKind(deepKind)
}, [deepOpenid, deepKind])
const stats = useMemo(() => {
let paid = 0
let pending = 0
let amount = 0
let delivered = 0
let vip = 0
let ebook = 0
let otherKind = 0
for (const o of orders) {
const s = (o.status || '').toLowerCase()
const isPaid = s.includes('paid') || s.includes('success')
const isPend = s.includes('pend') || s.includes('creat') || s.includes('wait')
if (isPaid) {
paid += 1
amount += o.amount_fen || 0
} else if (isPend) pending += 1
if (o.delivered) delivered += 1
const k = orderKind(o.book_id || '')
if (k === 'vip') vip += 1
else if (k === 'ebook') ebook += 1
else otherKind += 1
}
return { paid, pending, amount, delivered, total: orders.length, vip, ebook, otherKind }
}, [orders])
const filtered = useMemo(() => {
const needle = q.trim().toLowerCase()
return orders.filter((o) => {
const s = (o.status || '').toLowerCase()
const isPaid = s.includes('paid') || s.includes('success')
const isPend = s.includes('pend') || s.includes('creat') || s.includes('wait')
if (tab === 'paid' && !isPaid) return false
if (tab === 'pending' && !isPend) return false
if (tab === 'other' && (isPaid || isPend)) return false
const k = orderKind(o.book_id || '')
if (kind !== 'all' && k !== kind) return false
if (!needle) return true
return (
o.out_trade_no.toLowerCase().includes(needle) ||
(o.wx_order_id || '').toLowerCase().includes(needle) ||
o.openid.toLowerCase().includes(needle) ||
(o.book_id || '').toLowerCase().includes(needle) ||
(o.status || '').toLowerCase().includes(needle)
)
})
}, [orders, q, tab, kind])
async function onCopy(text: string, label: string) {
const ok = await copyText(text)
setMsg(ok ? `已复制 ${label}` : '复制失败')
}
function clearDeepLink() {
if (searchParams.has('openid') || searchParams.has('kind')) {
const next = new URLSearchParams(searchParams)
next.delete('openid')
next.delete('kind')
setSearchParams(next, { replace: true })
}
}
return (
<div className="wx-page">
<WxHead
eyebrow="虚拟支付"
title="订单"
desc="支付流水与发货状态。会员身份请在 VIP用户 管理;可用类型筛选与 OpenID 跳转关联。"
>
<Link className="wx-btn wx-btn-ghost" to="/wechat/vip/users">
VIP用户
</Link>
<button type="button" className="wx-btn wx-btn-ghost" onClick={load} disabled={loading}>
{loading ? '刷新中…' : '刷新'}
</button>
</WxHead>
{err ? (
<WxToast kind="err" onClose={() => setErr('')}>
{err}
</WxToast>
) : null}
{msg ? (
<WxToast kind="ok" onClose={() => setMsg('')}>
{msg}
</WxToast>
) : null}
{deepOpenid ? (
<WxToast kind="ok" onClose={clearDeepLink}>
已按用户筛选:{shortId(deepOpenid, 16)}
{deepKind === 'vip' ? ' · 仅 VIP 订单' : ''}
</WxToast>
) : null}
<div className="wx-stats" style={{ gridTemplateColumns: 'repeat(4, minmax(0, 1fr))' }}>
<div className="wx-stat">
<div className="wx-stat-label">全部订单</div>
<div className="wx-stat-val">{stats.total}</div>
</div>
<div className="wx-stat wx-stat--money">
<div className="wx-stat-label">已支付成交</div>
<div className="wx-stat-val">{fmtYuan(stats.amount)}</div>
<div className="wx-stat-label">{stats.paid} 笔已付</div>
</div>
<div className="wx-stat wx-stat--warn">
<div className="wx-stat-label">待支付</div>
<div className="wx-stat-val">{stats.pending}</div>
</div>
<div className="wx-stat">
<div className="wx-stat-label">已发货</div>
<div className="wx-stat-val">{stats.delivered}</div>
</div>
</div>
<WxTabs
value={kind}
onChange={setKind}
items={[
{ key: 'all', label: '全部类型', count: stats.total },
{ key: 'vip', label: 'VIP', count: stats.vip },
{ key: 'ebook', label: '小册', count: stats.ebook },
{ key: 'other', label: '其它', count: stats.otherKind },
]}
/>
<WxTabs
value={tab}
onChange={setTab}
items={[
{ key: 'all', label: '全部状态', count: stats.total },
{ key: 'paid', label: '已支付', count: stats.paid },
{ key: 'pending', label: '待支付', count: stats.pending },
{ key: 'other', label: '其他', count: Math.max(0, stats.total - stats.paid - stats.pending) },
]}
/>
<div className="wx-toolbar">
<input
type="search"
value={q}
placeholder="搜索业务单号 / 平台单号 / OpenID / 书籍"
onChange={(e) => setQ(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Escape') setQ('')
}}
/>
<button
type="button"
className="wx-btn wx-btn-ghost"
onClick={() => {
setQ('')
clearDeepLink()
}}
>
清空
</button>
</div>
<div className="wx-table-wrap">
{filtered.length ? (
<table className="wx-table">
<thead>
<tr>
<th>业务单号</th>
<th>金额</th>
<th>状态</th>
<th>发货</th>
<th>用户</th>
<th>时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{filtered.map((o) => {
const open = openId === o.out_trade_no
const isVip = o.book_id === VIP_BOOK_ID
return (
<tr key={o.out_trade_no} className={open ? 'is-open' : undefined}>
<td>
<div className="wx-mono">{o.out_trade_no}</div>
<div className="wx-mono" style={{ opacity: 0.65, marginTop: 4 }}>
{o.wx_order_id || '无平台单号'}
</div>
<div style={{ marginTop: 4 }}>
<WxBadge tone={isVip ? 'ok' : 'muted'}>{kindLabel(o.book_id || '')}</WxBadge>
</div>
{open ? (
<div className="wx-detail">
book_id: {o.book_id || '—'}
{'\n'}openid: {o.openid}
{'\n'}wx_order_id: {o.wx_order_id || '—'}
{'\n'}raw status: {o.status}
</div>
) : null}
</td>
<td>
<strong>{fmtYuan(o.amount_fen)}</strong>
</td>
<td>
<WxBadge tone={statusTone(o.status)}>{statusLabel(o.status)}</WxBadge>
</td>
<td>
<WxBadge tone={o.delivered ? 'ok' : 'muted'}>
{o.delivered ? '已发货' : '未发货'}
</WxBadge>
</td>
<td>
<button
type="button"
className="wx-btn-sm"
title={o.openid}
onClick={() => onCopy(o.openid, 'OpenID')}
>
{shortId(o.openid, 10)}
</button>
<div style={{ marginTop: 6 }}>
<Link
className="wx-link"
to={`/wechat/vip/users?openid=${encodeURIComponent(o.openid)}`}
>
VIP用户
</Link>
</div>
</td>
<td>{fmtTime(o.created_at)}</td>
<td>
<div className="wx-row-actions">
<button
type="button"
className="wx-btn-sm"
onClick={() => setOpenId(open ? null : o.out_trade_no)}
>
{open ? '收起' : '详情'}
</button>
<Link
className="wx-btn-sm"
to={`/wechat/vip/users?openid=${encodeURIComponent(o.openid)}`}
>
用户
</Link>
<button
type="button"
className="wx-btn-sm"
onClick={() => onCopy(o.out_trade_no, '业务单号')}
>
复制单号
</button>
</div>
</td>
</tr>
)
})}
</tbody>
</table>
) : (
<WxEmpty
title={
loading
? '加载中…'
: q || tab !== 'all' || kind !== 'all'
? '无匹配订单'
: '暂无订单'
}
hint={loading ? undefined : '用户完成虚拟支付后会出现在此列表'}
/>
)}
</div>
</div>
)
}