529 lines
16 KiB
React
529 lines
16 KiB
React
import { Trans } from 'react-i18next';
|
||
import {
|
||
Check as ActionCheck,
|
||
Copy as ActionCopy,
|
||
} from '@/components/common/icons/ActionIcons';
|
||
import TableEmpty from '../../components/common/table/TableEmpty';
|
||
import TableToolbar from '../../components/common/table/TableToolbar';
|
||
import {
|
||
ConsoleCard,
|
||
ConsoleSearchBar,
|
||
} from '../../components/console/ConsolePrimitives';
|
||
import React, { useState, useCallback } from 'react';
|
||
import { useFilterSearch } from '../../hooks/common/useFilterSearch';
|
||
import { useTableFilterQuery } from '../../hooks/common/useTableFilterQuery';
|
||
import { useTranslation } from 'react-i18next';
|
||
import { Search } from 'lucide-react';
|
||
import {
|
||
Table,
|
||
Input,
|
||
Tag,
|
||
Select,
|
||
Toast,
|
||
DatePicker,
|
||
InputNumber,
|
||
} from '@/components/ui';
|
||
import { API } from '../../helpers/api';
|
||
import { showError, copy } from '../../helpers';
|
||
import { renderQuota } from '../../helpers/render';
|
||
import { formatCurrency, toFiniteNumber } from '../../helpers/safeDisplay';
|
||
|
||
const paymentMethodMap = {
|
||
wechat: '微信',
|
||
alipay: '支付宝',
|
||
stripe: 'Stripe',
|
||
paypal: 'PayPal',
|
||
creem: 'Creem',
|
||
epay: '易支付',
|
||
};
|
||
|
||
const statusColorMap = {
|
||
pending: 'orange',
|
||
success: 'green',
|
||
expired: 'red',
|
||
};
|
||
|
||
const statusTextMap = {
|
||
pending: '待支付',
|
||
success: '已完成',
|
||
expired: '已过期',
|
||
};
|
||
|
||
const payPalSettlementStateMap = {
|
||
pending: '待回填',
|
||
complete: '已完整',
|
||
unavailable: '不可获取',
|
||
};
|
||
|
||
const formatUSDMinor = (value) => {
|
||
const minor = toFiniteNumber(value);
|
||
return minor === null ? '-' : formatCurrency(minor / 100, '$');
|
||
};
|
||
|
||
import { Descriptions } from 'antd';
|
||
import { ConsoleDrawer } from '../../components/console/ConsolePrimitives';
|
||
import { Button } from '@/components/ui';
|
||
import {
|
||
ConsoleFilterField,
|
||
ConsoleFilterActions,
|
||
} from '../../components/console/ConsoleModuleFilters';
|
||
|
||
const AdminOrder = () => {
|
||
const { t, i18n } = useTranslation();
|
||
const [detailOrder, setDetailOrder] = useState(null);
|
||
const [queryVersion, setQueryVersion] = useState(0);
|
||
const [orders, setOrders] = useState([]);
|
||
const [total, setTotal] = useState(0);
|
||
const [loading, setLoading] = useState(false);
|
||
const [page, setPage] = useState(1);
|
||
const [pageSize, setPageSize] = useState(20);
|
||
const [keyword, setKeyword] = useState('');
|
||
const [searchKeyword, setSearchKeyword] = useState('');
|
||
const [orderId, setOrderId] = useState(undefined);
|
||
const [appliedOrderId, setAppliedOrderId] = useState(undefined);
|
||
const [userId, setUserId] = useState(undefined);
|
||
const [appliedUserId, setAppliedUserId] = useState(undefined);
|
||
const [statusFilter, setStatusFilter] = useState('');
|
||
const [copiedId, setCopiedId] = useState(null);
|
||
const [paymentMethodFilter, setPaymentMethodFilter] = useState('');
|
||
const [dateRange, setDateRange] = useState(null);
|
||
const [appliedDateRange, setAppliedDateRange] = useState(null);
|
||
const [appliedStatus, setAppliedStatus] = useState('');
|
||
const [appliedPaymentMethod, setAppliedPaymentMethod] = useState('');
|
||
const [minMoney, setMinMoney] = useState(undefined);
|
||
const [maxMoney, setMaxMoney] = useState(undefined);
|
||
const [appliedMinMoney, setAppliedMinMoney] = useState(undefined);
|
||
const [appliedMaxMoney, setAppliedMaxMoney] = useState(undefined);
|
||
|
||
const fetchOrders = useCallback(
|
||
async (signal) => {
|
||
setLoading(true);
|
||
try {
|
||
const params = { p: page, page_size: pageSize };
|
||
if (searchKeyword) params.keyword = searchKeyword;
|
||
if (appliedOrderId) params.order_id = appliedOrderId;
|
||
if (appliedUserId) params.user_id = appliedUserId;
|
||
if (appliedStatus) params.status = appliedStatus;
|
||
if (appliedPaymentMethod) params.payment_method = appliedPaymentMethod;
|
||
if (appliedDateRange?.[0])
|
||
params.start_time = Math.floor(appliedDateRange[0].getTime() / 1000);
|
||
if (appliedDateRange?.[1])
|
||
params.end_time = Math.floor(appliedDateRange[1].getTime() / 1000);
|
||
if (appliedMinMoney !== undefined && appliedMinMoney !== null)
|
||
params.min_money = appliedMinMoney;
|
||
if (appliedMaxMoney !== undefined && appliedMaxMoney !== null)
|
||
params.max_money = appliedMaxMoney;
|
||
const res = await API.get('/api/order/search', { params, signal });
|
||
if (signal?.aborted) return;
|
||
if (res.data.success) {
|
||
setOrders(res.data.data?.items || []);
|
||
setTotal(res.data.data?.total || 0);
|
||
} else {
|
||
showError(res.data.message || t('查询失败'));
|
||
}
|
||
} catch (err) {
|
||
if (!signal?.aborted && !err.isAxiosError) showError(err.message);
|
||
} finally {
|
||
if (!signal?.aborted) setLoading(false);
|
||
}
|
||
},
|
||
[
|
||
page,
|
||
pageSize,
|
||
searchKeyword,
|
||
appliedOrderId,
|
||
appliedUserId,
|
||
appliedStatus,
|
||
appliedPaymentMethod,
|
||
appliedDateRange,
|
||
appliedMinMoney,
|
||
appliedMaxMoney,
|
||
t,
|
||
],
|
||
);
|
||
|
||
const { cancelQuery } = useTableFilterQuery(fetchOrders, queryVersion);
|
||
|
||
const applyFilters = () => {
|
||
const trimmedKeyword = keyword.trim();
|
||
const normalizedOrderId =
|
||
orderId && Number(orderId) > 0 ? Math.floor(Number(orderId)) : undefined;
|
||
const normalizedUserId =
|
||
userId && Number(userId) > 0 ? Math.floor(Number(userId)) : undefined;
|
||
const numericKeywordAsOrderId =
|
||
!normalizedOrderId && /^\d+$/.test(trimmedKeyword)
|
||
? Number(trimmedKeyword)
|
||
: undefined;
|
||
const ignoreNumericKeyword =
|
||
normalizedOrderId && /^\d+$/.test(trimmedKeyword);
|
||
setSearchKeyword(
|
||
numericKeywordAsOrderId || ignoreNumericKeyword ? '' : trimmedKeyword,
|
||
);
|
||
setAppliedOrderId(normalizedOrderId || numericKeywordAsOrderId);
|
||
setAppliedUserId(normalizedUserId);
|
||
setAppliedMinMoney(minMoney);
|
||
setAppliedMaxMoney(maxMoney);
|
||
setAppliedStatus(statusFilter || '');
|
||
setAppliedPaymentMethod(paymentMethodFilter || '');
|
||
setAppliedDateRange(dateRange);
|
||
setQueryVersion((version) => version + 1);
|
||
};
|
||
|
||
const { changeFilter, submitSearch, cancelPendingSearch } = useFilterSearch(
|
||
applyFilters,
|
||
cancelQuery,
|
||
);
|
||
|
||
const handleReset = () => {
|
||
cancelPendingSearch();
|
||
setAppliedStatus('');
|
||
setAppliedPaymentMethod('');
|
||
setAppliedDateRange(null);
|
||
setQueryVersion((version) => version + 1);
|
||
setPage(1);
|
||
setKeyword('');
|
||
setSearchKeyword('');
|
||
setOrderId(undefined);
|
||
setAppliedOrderId(undefined);
|
||
setUserId(undefined);
|
||
setAppliedUserId(undefined);
|
||
setStatusFilter('');
|
||
setPaymentMethodFilter('');
|
||
setDateRange(null);
|
||
setMinMoney(undefined);
|
||
setMaxMoney(undefined);
|
||
setAppliedMinMoney(undefined);
|
||
setAppliedMaxMoney(undefined);
|
||
};
|
||
|
||
// 走公共 copy:HTTP 部署下 navigator.clipboard 为 undefined,
|
||
// 直接调用 writeText 会同步抛错,.catch 降级永远走不到。
|
||
const copyText = async (text, id) => {
|
||
if (await copy(text)) {
|
||
setCopiedId(id);
|
||
Toast.success(t('已复制'));
|
||
setTimeout(() => setCopiedId(null), 2000);
|
||
} else {
|
||
showError(t('复制失败'));
|
||
}
|
||
};
|
||
|
||
const formatTime = (timestamp) => {
|
||
if (!timestamp) return '-';
|
||
return new Date(timestamp * 1000).toLocaleString(i18n.language);
|
||
};
|
||
|
||
const columns = [
|
||
{ title: 'ID', dataIndex: 'id', width: 60 },
|
||
{
|
||
title: t('订单号'),
|
||
dataIndex: 'trade_no',
|
||
width: 180,
|
||
ellipsis: true,
|
||
render: (text, record) => (
|
||
<div className='flex items-center gap-1'>
|
||
<span className='truncate text-xs font-mono' title={text}>
|
||
{text}
|
||
</span>
|
||
<button
|
||
className='app-action-button flex-shrink-0 p-0.5 rounded hover:bg-gray-100 dark:hover:bg-neutral-700 cursor-pointer bg-transparent border-none'
|
||
onClick={() => copyText(text, `trade-${record.id}`)}
|
||
title={t('复制订单号')}
|
||
>
|
||
{copiedId === `trade-${record.id}` ? (
|
||
<ActionCheck size={12} className='text-green-500' />
|
||
) : (
|
||
<ActionCopy size={12} className='text-gray-400' />
|
||
)}
|
||
</button>
|
||
</div>
|
||
),
|
||
},
|
||
{
|
||
title: t('用户'),
|
||
width: 140,
|
||
ellipsis: true,
|
||
render: (_, record) => {
|
||
const displayName = record.display_name || '';
|
||
const username = record.username || '';
|
||
if (displayName && username) {
|
||
return (
|
||
<span title={`${displayName} (${username})`}>
|
||
{displayName}{' '}
|
||
<span className='text-xs text-gray-400'>({username})</span>
|
||
</span>
|
||
);
|
||
}
|
||
if (displayName || username) {
|
||
return displayName || username;
|
||
}
|
||
return (
|
||
<span className='text-gray-400'>
|
||
{t('用户 #{{id}}', { id: record.user_id })}
|
||
</span>
|
||
);
|
||
},
|
||
},
|
||
{ title: t('用户ID'), dataIndex: 'user_id', width: 70 },
|
||
{
|
||
title: t('充值额度'),
|
||
dataIndex: 'amount',
|
||
width: 100,
|
||
render: (v) => renderQuota(v, 2),
|
||
},
|
||
{
|
||
title: t('支付金额'),
|
||
dataIndex: 'money',
|
||
width: 90,
|
||
render: (v) => (v ? formatCurrency(v) : '-'),
|
||
},
|
||
{
|
||
title: t('支付方式'),
|
||
dataIndex: 'payment_method',
|
||
width: 80,
|
||
render: (v) => (v ? t(paymentMethodMap[v] || v) : '-'),
|
||
},
|
||
{
|
||
title: t('PayPal 结算'),
|
||
width: 210,
|
||
render: (_, record) => {
|
||
if (record.payment_method !== 'paypal') return '-';
|
||
return (
|
||
<div className='text-xs leading-5'>
|
||
<div>
|
||
{t('基础金额')}:{' '}
|
||
{formatUSDMinor(record.provider_base_amount_minor)}
|
||
</div>
|
||
<div>
|
||
{t('支付服务费')}:{' '}
|
||
{formatUSDMinor(record.payment_service_fee_minor)}
|
||
</div>
|
||
<div>
|
||
{t('PayPal 实际手续费')}:{' '}
|
||
{formatUSDMinor(record.provider_fee_minor)}
|
||
</div>
|
||
<div>
|
||
{t('PayPal 净收金额')}:{' '}
|
||
{formatUSDMinor(record.provider_net_amount_minor)}
|
||
</div>
|
||
<div>
|
||
<Trans
|
||
t={t}
|
||
i18nKey='结算状态: {{label}}'
|
||
values={{
|
||
label: t(
|
||
payPalSettlementStateMap[record.paypal_settlement_state] ||
|
||
record.paypal_settlement_state ||
|
||
'未知',
|
||
),
|
||
}}
|
||
/>
|
||
</div>
|
||
</div>
|
||
);
|
||
},
|
||
},
|
||
{
|
||
title: t('状态'),
|
||
dataIndex: 'status',
|
||
width: 80,
|
||
render: (status) => (
|
||
<Tag color={statusColorMap[status] || 'grey'}>
|
||
{t(statusTextMap[status] || status || '未知')}
|
||
</Tag>
|
||
),
|
||
},
|
||
{
|
||
title: t('创建时间'),
|
||
dataIndex: 'create_time',
|
||
width: 150,
|
||
render: (v) => formatTime(v),
|
||
},
|
||
{
|
||
title: t('完成时间'),
|
||
dataIndex: 'complete_time',
|
||
width: 150,
|
||
render: (v) => formatTime(v),
|
||
},
|
||
{
|
||
title: t('操作'),
|
||
key: 'operate',
|
||
fixed: 'right',
|
||
width: 80,
|
||
render: (_, record) => (
|
||
<Button type='link' size='small' onClick={() => setDetailOrder(record)}>
|
||
{t('详情')}
|
||
</Button>
|
||
),
|
||
},
|
||
];
|
||
|
||
return (
|
||
<div className='console-content'>
|
||
<ConsoleDrawer
|
||
title={t('记录详情')}
|
||
classNames={{ header: 'console-detail-header' }}
|
||
open={Boolean(detailOrder)}
|
||
onClose={() => setDetailOrder(null)}
|
||
placement='right'
|
||
maskClosable
|
||
width={560}
|
||
footer={
|
||
<div className='console-model-footer'>
|
||
<Button onClick={() => setDetailOrder(null)}>{t('关闭')}</Button>
|
||
</div>
|
||
}
|
||
>
|
||
<Descriptions
|
||
bordered
|
||
column={1}
|
||
size='small'
|
||
items={
|
||
detailOrder
|
||
? columns
|
||
.filter((column) => column.key !== 'operate')
|
||
.map((column, key) => ({
|
||
key,
|
||
label: column.title,
|
||
children:
|
||
column.dataIndex === 'trade_no'
|
||
? detailOrder.trade_no
|
||
: column.render
|
||
? column.render(
|
||
detailOrder[column.dataIndex],
|
||
detailOrder,
|
||
)
|
||
: (detailOrder[column.dataIndex] ?? '-'),
|
||
}))
|
||
: []
|
||
}
|
||
/>
|
||
</ConsoleDrawer>
|
||
<ConsoleSearchBar
|
||
formProps={{
|
||
layout: 'vertical',
|
||
onFinish: submitSearch,
|
||
className: 'console-module-filter-form',
|
||
}}
|
||
>
|
||
<div className='console-module-filter-grid'>
|
||
<ConsoleFilterField label={t('搜索昵称/订单号')}>
|
||
<Input
|
||
prefix={<Search size={14} />}
|
||
placeholder={t('搜索昵称/订单号')}
|
||
value={keyword}
|
||
onChange={changeFilter(setKeyword)}
|
||
/>
|
||
</ConsoleFilterField>
|
||
<ConsoleFilterField label={t('订单ID')}>
|
||
<InputNumber
|
||
placeholder={t('订单ID')}
|
||
value={orderId}
|
||
onChange={changeFilter(setOrderId)}
|
||
min={1}
|
||
hideButtons
|
||
/>
|
||
</ConsoleFilterField>
|
||
<ConsoleFilterField label={t('用户ID')}>
|
||
<InputNumber
|
||
placeholder={t('用户ID')}
|
||
value={userId}
|
||
onChange={changeFilter(setUserId)}
|
||
min={1}
|
||
hideButtons
|
||
/>
|
||
</ConsoleFilterField>
|
||
<ConsoleFilterField label={t('状态')}>
|
||
<Select
|
||
placeholder={t('状态')}
|
||
value={statusFilter || undefined}
|
||
onChange={changeFilter(setStatusFilter)}
|
||
allowClear
|
||
optionList={[
|
||
{ label: t('待支付'), value: 'pending' },
|
||
{ label: t('已完成'), value: 'success' },
|
||
{ label: t('已过期'), value: 'expired' },
|
||
]}
|
||
/>
|
||
</ConsoleFilterField>
|
||
<ConsoleFilterField label={t('支付方式')}>
|
||
<Select
|
||
placeholder={t('支付方式')}
|
||
value={paymentMethodFilter || undefined}
|
||
onChange={changeFilter(setPaymentMethodFilter)}
|
||
allowClear
|
||
optionList={Object.entries(paymentMethodMap).map(
|
||
([value, label]) => ({ label, value }),
|
||
)}
|
||
/>
|
||
</ConsoleFilterField>
|
||
<ConsoleFilterField label={t('创建时间')}>
|
||
<DatePicker
|
||
tableFilter
|
||
type='dateTimeRange'
|
||
value={dateRange}
|
||
onChange={changeFilter(setDateRange)}
|
||
placeholder={[t('开始时间'), t('结束时间')]}
|
||
density='compact'
|
||
/>
|
||
</ConsoleFilterField>
|
||
<ConsoleFilterField label={t('支付金额')}>
|
||
<div className='flex items-center gap-1'>
|
||
<InputNumber
|
||
placeholder={t('最小金额')}
|
||
value={minMoney}
|
||
onChange={changeFilter(setMinMoney)}
|
||
min={0}
|
||
precision={2}
|
||
prefix='¥'
|
||
/>
|
||
<span className='text-gray-400'>-</span>
|
||
<InputNumber
|
||
placeholder={t('最大金额')}
|
||
value={maxMoney}
|
||
onChange={changeFilter(setMaxMoney)}
|
||
min={0}
|
||
precision={2}
|
||
prefix='¥'
|
||
/>
|
||
</div>
|
||
</ConsoleFilterField>
|
||
</div>
|
||
<ConsoleFilterActions
|
||
onReset={handleReset}
|
||
onSearch={submitSearch}
|
||
loading={loading}
|
||
/>
|
||
</ConsoleSearchBar>
|
||
|
||
{/* 表格 */}
|
||
<ConsoleCard>
|
||
<TableToolbar title={t('订单列表')} />
|
||
<Table
|
||
columns={columns}
|
||
dataSource={orders}
|
||
loading={loading}
|
||
rowKey='id'
|
||
pagination={{
|
||
preservePage: true,
|
||
currentPage: page,
|
||
total,
|
||
pageSize,
|
||
onPageChange: setPage,
|
||
onPageSizeChange: (size) => {
|
||
setPage(1);
|
||
setPageSize(size);
|
||
},
|
||
showTotal: true,
|
||
}}
|
||
scroll={{ x: 1100 }}
|
||
size='middle'
|
||
empty={<TableEmpty />}
|
||
/>
|
||
</ConsoleCard>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default AdminOrder;
|