277 lines
8.0 KiB
TypeScript
277 lines
8.0 KiB
TypeScript
import { useMemo, useState } from 'react'
|
|
import { keepPreviousData, useQuery } from '@tanstack/react-query'
|
|
import { Alert, Button, Input, Select, Space, Tag, Typography, type TableColumnsType } from 'antd'
|
|
import { ReloadOutlined } from '@ant-design/icons'
|
|
|
|
import { fetchAdminPayments } from '@/features/admin/api'
|
|
import { AdminPageHeader } from '@/features/admin/components/AdminPageHeader'
|
|
import { DataTable } from '@/features/admin/components/DataTable'
|
|
import { FilterBar, FilterField } from '@/features/admin/components/FilterBar'
|
|
import {
|
|
paymentBizTypeMap,
|
|
paymentMethodLabel,
|
|
paymentProviderLabel,
|
|
paymentProviderMap,
|
|
} from '@/features/admin/enumLabels'
|
|
import type { AdminPaymentQuery, AdminPaymentRow } from '@/features/admin/types'
|
|
import { useAdminSessionStore } from '@/stores/adminSession'
|
|
import { formatDateTime, formatMoney, formatText } from '@/shared/utils/format'
|
|
|
|
type Filters = Pick<AdminPaymentQuery, 'order_no' | 'biz_type' | 'status' | 'provider'>
|
|
|
|
const emptyFilters: Filters = {
|
|
order_no: '',
|
|
biz_type: undefined,
|
|
status: undefined,
|
|
provider: undefined,
|
|
}
|
|
|
|
const statusOptions = [
|
|
{ value: 'pending', label: '待支付' },
|
|
{ value: 'paid', label: '已支付' },
|
|
{ value: 'failed', label: '支付失败' },
|
|
{ value: 'closed', label: '已关闭' },
|
|
]
|
|
|
|
const bizTypeOptions = Object.entries(paymentBizTypeMap).map(([value, label]) => ({ value, label }))
|
|
|
|
const providerOptions = Object.entries(paymentProviderMap).map(([value, label]) => ({
|
|
value,
|
|
label,
|
|
}))
|
|
|
|
const statusMeta: Record<string, { label: string; color: string }> = {
|
|
pending: { label: '待支付', color: 'gold' },
|
|
paid: { label: '已支付', color: 'green' },
|
|
failed: { label: '支付失败', color: 'red' },
|
|
closed: { label: '已关闭', color: 'default' },
|
|
}
|
|
|
|
function statusTag(value?: string | null) {
|
|
if (!value) return <Tag>未知</Tag>
|
|
const meta = statusMeta[value] ?? { label: value, color: 'default' }
|
|
return (
|
|
<Tag bordered={false} color={meta.color}>
|
|
{meta.label}
|
|
</Tag>
|
|
)
|
|
}
|
|
|
|
function errorText(error: unknown) {
|
|
const response = (error as { response?: { data?: { message?: string } } })?.response
|
|
if (response?.data?.message) return response.data.message
|
|
return error instanceof Error ? error.message : '请求失败,请稍后重试'
|
|
}
|
|
|
|
const columns: TableColumnsType<AdminPaymentRow> = [
|
|
{
|
|
title: '支付单号',
|
|
key: 'payment',
|
|
width: 240,
|
|
fixed: 'left',
|
|
render: (_, row) => (
|
|
<div className="admin-stack">
|
|
<strong>{row.payment_no}</strong>
|
|
<span className="admin-mono">{formatText(row.order_no)}</span>
|
|
</div>
|
|
),
|
|
},
|
|
{
|
|
title: '用户',
|
|
key: 'user',
|
|
width: 170,
|
|
render: (_, row) => (
|
|
<div className="admin-stack">
|
|
<span>{formatText(row.user_phone)}</span>
|
|
<span className="admin-mono">UID {row.user_id}</span>
|
|
</div>
|
|
),
|
|
},
|
|
{
|
|
title: '渠道 / 支付方式',
|
|
key: 'channel',
|
|
width: 170,
|
|
render: (_, row) => (
|
|
<div className="admin-stack">
|
|
<span>{paymentProviderLabel(row.provider)}</span>
|
|
<span className="admin-mono">{paymentMethodLabel(row.pay_way)}</span>
|
|
</div>
|
|
),
|
|
},
|
|
{
|
|
title: '金额',
|
|
dataIndex: 'amount_cent',
|
|
width: 130,
|
|
align: 'right',
|
|
render: value => <strong>{formatMoney(value)}</strong>,
|
|
},
|
|
{
|
|
title: '状态',
|
|
dataIndex: 'status',
|
|
width: 110,
|
|
render: value => statusTag(value),
|
|
},
|
|
{
|
|
title: '支付时间',
|
|
dataIndex: 'paid_at',
|
|
width: 170,
|
|
render: value => (value ? formatDateTime(value) : '—'),
|
|
},
|
|
{
|
|
title: '失败原因',
|
|
key: 'error',
|
|
render: (_, row) =>
|
|
row.error_message ? (
|
|
<Typography.Text type="danger">
|
|
{row.error_code ? `${row.error_code} · ` : ''}
|
|
{row.error_message}
|
|
</Typography.Text>
|
|
) : (
|
|
<Typography.Text type="secondary">—</Typography.Text>
|
|
),
|
|
},
|
|
{
|
|
title: '创建时间',
|
|
dataIndex: 'created_at',
|
|
width: 170,
|
|
render: value => formatDateTime(value),
|
|
},
|
|
]
|
|
|
|
export function AdminPaymentsPage() {
|
|
const [filters, setFilters] = useState<Filters>(emptyFilters)
|
|
const [applied, setApplied] = useState<Filters>(emptyFilters)
|
|
const [page, setPage] = useState(1)
|
|
const [pageSize, setPageSize] = useState(20)
|
|
const permissions = useAdminSessionStore(state => state.admin?.permissions)
|
|
const canView = Boolean(permissions?.includes('*') || permissions?.includes('wallet:view'))
|
|
|
|
const query = useMemo(
|
|
() => ({
|
|
order_no: applied.order_no?.trim() || undefined,
|
|
biz_type: applied.biz_type,
|
|
status: applied.status,
|
|
provider: applied.provider,
|
|
page,
|
|
page_size: pageSize,
|
|
}),
|
|
[applied, page, pageSize]
|
|
)
|
|
|
|
const paymentsQuery = useQuery({
|
|
queryKey: ['admin-payments', query],
|
|
queryFn: () => fetchAdminPayments(query),
|
|
enabled: canView,
|
|
placeholderData: keepPreviousData,
|
|
})
|
|
|
|
if (!canView) {
|
|
return (
|
|
<div className="admin-page">
|
|
<AdminPageHeader title="支付流水" eyebrow="JIEYOU OPS · 财务" />
|
|
<Alert type="warning" showIcon message="当前账号没有资金查看权限" />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<div className="admin-page">
|
|
<AdminPageHeader
|
|
eyebrow="JIEYOU OPS · 财务"
|
|
title="支付流水"
|
|
description="查看订单支付、充值等渠道流水,核对金额与回调状态。"
|
|
meta={
|
|
<Space size={8} wrap>
|
|
<Tag color="geekblue">共 {paymentsQuery.data?.total ?? 0} 条</Tag>
|
|
</Space>
|
|
}
|
|
extra={
|
|
<Button
|
|
icon={<ReloadOutlined />}
|
|
loading={paymentsQuery.isFetching}
|
|
onClick={() => void paymentsQuery.refetch()}
|
|
>
|
|
刷新
|
|
</Button>
|
|
}
|
|
/>
|
|
|
|
<FilterBar
|
|
loading={paymentsQuery.isFetching}
|
|
onSearch={() => {
|
|
setPage(1)
|
|
setApplied(filters)
|
|
}}
|
|
onReset={() => {
|
|
setFilters(emptyFilters)
|
|
setApplied(emptyFilters)
|
|
setPage(1)
|
|
}}
|
|
>
|
|
<FilterField label="订单号">
|
|
<Input
|
|
allowClear
|
|
value={filters.order_no}
|
|
placeholder="支持完整订单号"
|
|
onChange={event =>
|
|
setFilters(current => ({ ...current, order_no: event.target.value }))
|
|
}
|
|
/>
|
|
</FilterField>
|
|
<FilterField label="业务类型">
|
|
<Select
|
|
allowClear
|
|
value={filters.biz_type}
|
|
options={bizTypeOptions}
|
|
placeholder="全部业务"
|
|
onChange={value => setFilters(current => ({ ...current, biz_type: value }))}
|
|
/>
|
|
</FilterField>
|
|
<FilterField label="状态">
|
|
<Select
|
|
allowClear
|
|
value={filters.status}
|
|
options={statusOptions}
|
|
placeholder="全部状态"
|
|
onChange={value => setFilters(current => ({ ...current, status: value }))}
|
|
/>
|
|
</FilterField>
|
|
<FilterField label="渠道">
|
|
<Select
|
|
allowClear
|
|
value={filters.provider}
|
|
options={providerOptions}
|
|
placeholder="全部渠道"
|
|
onChange={value => setFilters(current => ({ ...current, provider: value }))}
|
|
/>
|
|
</FilterField>
|
|
</FilterBar>
|
|
|
|
{paymentsQuery.isError ? (
|
|
<Alert
|
|
type="error"
|
|
showIcon
|
|
className="admin-table-alert"
|
|
message="支付流水加载失败"
|
|
description={errorText(paymentsQuery.error)}
|
|
/>
|
|
) : null}
|
|
|
|
<DataTable<AdminPaymentRow>
|
|
columns={columns}
|
|
dataSource={paymentsQuery.data?.items ?? []}
|
|
rowKey="id"
|
|
loading={paymentsQuery.isFetching}
|
|
total={paymentsQuery.data?.total ?? 0}
|
|
page={paymentsQuery.data?.page ?? page}
|
|
pageSize={paymentsQuery.data?.page_size ?? pageSize}
|
|
onPageChange={(nextPage, nextSize) => {
|
|
setPage(nextPage)
|
|
setPageSize(nextSize)
|
|
}}
|
|
emptyDescription="还没有支付流水"
|
|
/>
|
|
</div>
|
|
)
|
|
}
|