Restructure /wechat nav (VIP submenu for ebooks/course/shop/memo), rebuild ops pages with shared wx UI, and wire vip-library remote content end-to-end with README paths. Co-authored-by: Cursor <cursoragent@cursor.com>
268 lines
9.0 KiB
TypeScript
268 lines
9.0 KiB
TypeScript
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<Order[]>([])
|
|
const [loading, setLoading] = useState(true)
|
|
const [err, setErr] = useState('')
|
|
const [msg, setMsg] = useState('')
|
|
const [q, setQ] = useState('')
|
|
const [tab, setTab] = useState<StatusTab>('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])
|
|
|
|
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 (
|
|
<div className="wx-page">
|
|
<WxHead
|
|
eyebrow="虚拟支付"
|
|
title="订单"
|
|
desc="虚拟支付道具直购订单、金额与发货状态。支持搜索、状态筛选与一键复制单号。"
|
|
>
|
|
<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}
|
|
|
|
<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={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('')}>
|
|
清空
|
|
</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
|
|
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>
|
|
{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>
|
|
</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>
|
|
<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' ? '无匹配订单' : '暂无订单'}
|
|
hint={loading ? undefined : '用户完成虚拟支付后会出现在此列表'}
|
|
/>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|