Files
monkey-ai-web/src/pages/Recharge/HistoryPanel.jsx
T

271 lines
7.2 KiB
React

/*
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 <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { useEffectEvent } from '../../hooks/common/useEffectEvent';
import React, { useState, useEffect, useMemo } from 'react';
import { Button as AntButton, 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);
}
};
const onScopeChange = useEffectEvent(() => {
loadTopups(page, pageSize);
});
useEffect(
() => onScopeChange(),
[onScopeChange, 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 (
<AntTag bordered={false} color={config.color}>
{t(config.key)}
</AntTag>
);
};
// 表格列定义
const columns = (() => {
const baseColumns = [
{
title: t('订单号'),
dataIndex: 'trade_no',
key: 'trade_no',
width: 240,
render: (text) => (
<Text
copyable={{ text: text }}
className='rc-history-mono'
ellipsis={{ showTooltip: true }}
>
{text || '-'}
</Text>
),
},
{
title: t('支付金额'),
dataIndex: 'money',
key: 'money',
width: 160,
render: (money) => (
<span className='rc-history-money'>
¥
{Number(money || 0).toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})}
</span>
),
},
{
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 (
<AntButton
size='small'
type='link'
className='rc-history-complete-action'
onClick={() => confirmAdminComplete(record.trade_no)}
>
{t('补单')}
</AntButton>
);
},
});
}
return baseColumns;
})();
return (
<div className='rc-history'>
<div className='rc-history-search'>
<Input
placeholder={t('请输入订单号')}
value={keyword}
onChange={(value) => {
setKeyword(value);
setPage(1);
}}
showClear
/>
{/* 重置等同清空订单号;搜索按当前订单号重新加载当前页 */}
<Button
onClick={() => {
setKeyword('');
setPage(1);
}}
>
{t('重置')}
</Button>
<Button
type='primary'
icon={<Search size={14} />}
onClick={() => loadTopups(page, pageSize)}
>
{t('搜索')}
</Button>
</div>
<Table
columns={columns}
dataSource={topups}
loading={loading}
rowKey='id'
scroll={{ x: 'max-content' }}
size='middle'
pagination={false}
className='rc-history-table'
empty={
<AntEmpty
image={AntEmpty.PRESENTED_IMAGE_SIMPLE}
description={t('暂无充值记录')}
/>
}
/>
{total > 0 && (
<div className='rc-history-pagination'>
<Pagination
total={total}
currentPage={page}
pageSize={pageSize}
showSizeChanger
pageSizeOpts={[10, 20, 50]}
onPageChange={(p) => setPage(p)}
onPageSizeChange={(size) => {
setPageSize(size);
setPage(1);
}}
size='small'
showTotal={(count) => t('共 {{total}} 条', { total: count })}
/>
</div>
)}
</div>
);
};
export default HistoryPanel;