import { useCallback, useEffect, useMemo, useState } from 'react' 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' 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 || '未知' } export default function Orders() { const [orders, setOrders] = useState([]) const [loading, setLoading] = useState(true) const [err, setErr] = useState('') const [msg, setMsg] = useState('') const [q, setQ] = useState('') const [tab, setTab] = useState('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]) const stats = useMemo(() => { let paid = 0 let pending = 0 let amount = 0 let delivered = 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 } return { paid, pending, amount, delivered, total: orders.length } }, [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 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]) async function onCopy(text: string, label: string) { const ok = await copyText(text) setMsg(ok ? `已复制 ${label}` : '复制失败') } return (
{err ? ( setErr('')}> {err} ) : null} {msg ? ( setMsg('')}> {msg} ) : 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 return ( ) })}
业务单号 金额 状态 发货 用户 时间 操作
{o.out_trade_no}
{o.wx_order_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 ? '已发货' : '未发货'} {fmtTime(o.created_at)}
) : ( )}
) }