/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import React, { useState, useEffect, useMemo } from 'react';
import { Empty as AntEmpty, Tag as AntTag } from 'antd';
import {
Typography,
Input,
Table,
Button,
Modal,
Toast,
Pagination,
} from '@/components/ui';
import { Search } from 'lucide-react';
import { API, timestamp2string } from '../../helpers';
import { isAdmin } from '../../helpers/utils';
const { Text } = Typography;
// 状态映射
const STATUS_CONFIG = {
success: { color: 'success', key: '成功' },
pending: { color: 'warning', key: '待支付' },
expired: { color: undefined, key: '已过期' },
};
const HistoryPanel = ({ t, refreshTrigger }) => {
const [loading, setLoading] = useState(false);
const [topups, setTopups] = useState([]);
const [total, setTotal] = useState(0);
const [page, setPage] = useState(1);
const [pageSize, setPageSize] = useState(10);
const [keyword, setKeyword] = useState('');
const userIsAdmin = useMemo(() => isAdmin(), []);
// 加载充值记录
const loadTopups = async (currentPage, currentPageSize) => {
setLoading(true);
try {
const base = userIsAdmin ? '/api/user/topup' : '/api/user/topup/self';
const qs =
`p=${currentPage}&page_size=${currentPageSize}` +
(keyword ? `&keyword=${encodeURIComponent(keyword)}` : '');
const endpoint = `${base}?${qs}`;
const res = await API.get(endpoint);
const { success, message, data } = res.data;
if (success) {
setTopups(data.items || []);
setTotal(data.total || 0);
} else {
Toast.error({ content: message || t('加载失败') });
}
} catch (error) {
console.error('Load topups error:', error);
Toast.error({ content: t('加载账单失败') });
} finally {
setLoading(false);
}
};
useEffect(() => {
loadTopups(page, pageSize);
}, [page, pageSize, keyword, refreshTrigger]);
// 管理员补单
const handleAdminComplete = async (tradeNo) => {
try {
const res = await API.post('/api/user/topup/complete', {
trade_no: tradeNo,
});
const { success, message } = res.data;
if (success) {
Toast.success({ content: t('补单成功') });
await loadTopups(page, pageSize);
} else {
Toast.error({ content: message || t('补单失败') });
}
} catch (e) {
Toast.error({ content: t('补单失败') });
}
};
const confirmAdminComplete = (tradeNo) => {
Modal.confirm({
title: t('确认补单'),
content: t('是否将该订单标记为成功并为用户入账?'),
onOk: () => handleAdminComplete(tradeNo),
});
};
// 渲染状态标签
const renderStatusBadge = (status) => {
const config = STATUS_CONFIG[status] || {
color: 'processing',
key: status,
};
return (
{t(config.key)}
);
};
// 表格列定义
const columns = useMemo(() => {
const baseColumns = [
{
title: t('订单号'),
dataIndex: 'trade_no',
key: 'trade_no',
width: 240,
render: (text) => (
{text || '-'}
),
},
{
title: t('支付金额'),
dataIndex: 'money',
key: 'money',
width: 160,
render: (money) => (
¥
{Number(money || 0).toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})}
),
},
{
title: t('状态'),
dataIndex: 'status',
key: 'status',
width: 80,
render: renderStatusBadge,
},
{
title: t('时间'),
dataIndex: 'create_time',
key: 'create_time',
width: 100,
render: (time) => timestamp2string(time).slice(0, 16),
},
];
// 管理员显示操作列
if (userIsAdmin) {
baseColumns.push({
title: t('操作'),
key: 'action',
width: 60,
render: (_, record) => {
if (record.status !== 'pending') return null;
return (
);
},
});
}
return baseColumns;
}, [t, userIsAdmin]);
return (
{
setKeyword(value);
setPage(1);
}}
showClear
/>
{/* 重置等同清空订单号;搜索按当前订单号重新加载当前页 */}
}
onClick={() => loadTopups(page, pageSize)}
>
{t('搜索')}
}
/>
{total > 0 && (
setPage(p)}
onPageSizeChange={(size) => {
setPageSize(size);
setPage(1);
}}
size='small'
showTotal={(count) => t('共 {{total}} 条', { total: count })}
/>
)}
);
};
export default HistoryPanel;