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([]) const [loading, setLoading] = useState(true) const [err, setErr] = useState('') const [msg, setMsg] = useState('') const [q, setQ] = useState(deepOpenid) const [tab, setTab] = useState('all') const [kind, setKind] = useState( deepKind === 'vip' || deepKind === 'ebook' || deepKind === 'other' ? deepKind : 'all', ) const [openId, setOpenId] = useState(null) const load = useCallback(() => { setLoading(true) setErr('') api('/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 (
VIP用户 {err ? ( setErr('')}> {err} ) : null} {msg ? ( setMsg('')}> {msg} ) : null} {deepOpenid ? ( 已按用户筛选:{shortId(deepOpenid, 16)} {deepKind === 'vip' ? ' · 仅 VIP 订单' : ''} ) : null}
全部订单
{stats.total}
已支付成交
{fmtYuan(stats.amount)}
{stats.paid} 笔已付
待支付
{stats.pending}
已发货
{stats.delivered}
setQ(e.target.value)} onKeyDown={(e) => { if (e.key === 'Escape') setQ('') }} />
{filtered.length ? ( {filtered.map((o) => { const open = openId === o.out_trade_no const isVip = o.book_id === VIP_BOOK_ID return ( ) })}
业务单号 金额 状态 发货 用户 时间 操作
{o.out_trade_no}
{o.wx_order_id || '无平台单号'}
{kindLabel(o.book_id || '')}
{open ? (
book_id: {o.book_id || '—'} {'\n'}openid: {o.openid} {'\n'}wx_order_id: {o.wx_order_id || '—'} {'\n'}raw status: {o.status}
) : null}
{fmtYuan(o.amount_fen)} {statusLabel(o.status)} {o.delivered ? '已发货' : '未发货'}
VIP用户
{fmtTime(o.created_at)}
用户
) : ( )}
) }