Files
monkey-ai-web/src/components/topup/modals/TopupHistoryModal.jsx
T

352 lines
10 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 {
Modal,
Table,
Typography,
Toast,
Empty,
Button,
Input,
} from '@/components/ui';
import {
IllustrationNoResult,
IllustrationNoResultDark,
} from '../../common/illustrations';
import {
CheckCircle2,
Clock3,
Coins,
Copy,
ReceiptText,
XCircle,
} from 'lucide-react';
import { IconSearch } from '@/components/ui/icons';
import { API, timestamp2string } from '../../../helpers';
import { isAdmin } from '../../../helpers/utils';
import { useIsMobile } from '../../../hooks/common/useIsMobile';
const { Text } = Typography;
// 状态映射配置
const STATUS_CONFIG = {
success: {
type: 'success',
key: '成功',
icon: CheckCircle2,
className:
'border-emerald-200 bg-emerald-50 text-emerald-700 dark:border-emerald-700/40 dark:bg-emerald-900/20 dark:text-emerald-300',
},
pending: {
type: 'warning',
key: '待支付',
icon: Clock3,
className:
'border-amber-200 bg-amber-50 text-amber-700 dark:border-amber-700/40 dark:bg-amber-900/20 dark:text-amber-300',
},
expired: {
type: 'danger',
key: '已过期',
icon: XCircle,
className:
'border-rose-200 bg-rose-50 text-rose-700 dark:border-rose-700/40 dark:bg-rose-900/20 dark:text-rose-300',
},
};
// 支付方式映射
const PAYMENT_METHOD_MAP = {
stripe: 'Stripe',
paypal: 'PayPal',
alipay: '支付宝',
wxpay: '微信',
};
const TopupHistoryModal = ({ visible, onCancel, t }) => {
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 isMobile = useIsMobile();
const loadTopups = async (currentPage, currentPageSize) => {
setLoading(true);
try {
const base = isAdmin() ? '/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 onVisibilityChange = useEffectEvent(() => {
if (visible) {
loadTopups(page, pageSize);
}
});
useEffect(
() => onVisibilityChange(),
[onVisibilityChange, visible, page, pageSize, keyword],
);
const handlePageChange = (currentPage) => {
setPage(currentPage);
};
const handlePageSizeChange = (currentPageSize) => {
setPageSize(currentPageSize);
setPage(1);
};
const handleKeywordChange = (value) => {
setKeyword(value);
setPage(1);
};
// 管理员补单
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] || {
type: 'primary',
key: status,
icon: Clock3,
className:
'border-slate-200 bg-slate-50 text-slate-600 dark:border-slate-700 dark:bg-slate-800/60 dark:text-slate-300',
};
const Icon = config.icon;
return (
<span
className={`inline-flex items-center gap-1.5 rounded-full border px-2.5 py-1 text-xs font-semibold ${config.className}`}
>
<Icon size={14} />
<span>{t(config.key)}</span>
</span>
);
};
// 渲染支付方式
const renderPaymentMethod = (pm) => {
const displayName = PAYMENT_METHOD_MAP[pm];
return <Text>{displayName ? t(displayName) : pm || '-'}</Text>;
};
// 检查是否为管理员
const userIsAdmin = useMemo(() => isAdmin(), []);
const columns = (() => {
const baseColumns = [
{
title: t('订单号'),
dataIndex: 'trade_no',
key: 'trade_no',
render: (text) => (
<div className='group flex min-w-[260px] items-center gap-2'>
<span className='inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-xl bg-blue-50 text-blue-600 dark:bg-blue-900/25 dark:text-blue-300'>
<ReceiptText size={15} />
</span>
<Text
copyable={{ icon: <Copy size={14} /> }}
ellipsis={{ showTooltip: true }}
className='font-mono text-sm text-gray-900 dark:text-gray-100'
>
{text}
</Text>
</div>
),
},
{
title: t('支付方式'),
dataIndex: 'payment_method',
key: 'payment_method',
render: renderPaymentMethod,
},
{
title: t('充值额度'),
dataIndex: 'amount',
key: 'amount',
render: (amount) => (
<span className='inline-flex items-center gap-1.5 rounded-full bg-sky-50 px-2.5 py-1 text-sm font-semibold text-sky-700 dark:bg-sky-900/25 dark:text-sky-300'>
<Coins size={15} />
{amount}
</span>
),
},
{
title: t('支付金额'),
dataIndex: 'money',
key: 'money',
render: (money) => (
<span className='text-base font-semibold text-rose-500'>
¥{Number(money || 0).toFixed(2)}
</span>
),
},
{
title: t('状态'),
dataIndex: 'status',
key: 'status',
render: renderStatusBadge,
},
];
// 管理员才显示操作列
if (userIsAdmin) {
baseColumns.push({
title: t('操作'),
key: 'action',
render: (_, record) => {
if (record.status !== 'pending') return null;
return (
<Button
size='small'
type='primary'
theme='solid'
onClick={() => confirmAdminComplete(record.trade_no)}
className='!rounded-full !bg-gradient-to-r !from-blue-500 !to-cyan-500 !border-0 !px-4 !shadow-md !shadow-blue-500/20'
>
{t('补单')}
</Button>
);
},
});
}
baseColumns.push({
title: t('创建时间'),
dataIndex: 'create_time',
key: 'create_time',
render: (time) => timestamp2string(time),
});
return baseColumns;
})();
return (
<Modal
title={
<div className='flex items-center gap-2 text-lg font-semibold'>
<div className='p-1.5 bg-blue-50 dark:bg-blue-900/30 rounded-lg text-blue-600 dark:text-blue-400'>
<Coins size={20} />
</div>
{t('充值账单')}
</div>
}
visible={visible}
onCancel={onCancel}
footer={null}
size={isMobile ? 'full-width' : 'large'}
className='!rounded-2xl'
bodyStyle={{ padding: isMobile ? '16px' : '24px' }}
>
<div className='mb-4 rounded-2xl border border-white/70 bg-white/[0.55] p-3 shadow-sm backdrop-blur-xl dark:border-white/10 dark:bg-white/[0.045]'>
<Input
prefix={<IconSearch className='text-gray-400' />}
placeholder={t('搜索订单号...')}
value={keyword}
onChange={handleKeywordChange}
showClear
className='topup-history-search !rounded-xl'
/>
</div>
<div className='topup-history-table overflow-hidden rounded-2xl border border-white/70 bg-white/[0.55] shadow-[0_18px_50px_rgba(15,23,42,0.08)] backdrop-blur-xl dark:border-white/10 dark:bg-white/[0.045]'>
<Table
columns={columns}
dataSource={topups}
loading={loading}
rowKey='id'
pagination={{
currentPage: page,
pageSize: pageSize,
total: total,
showSizeChanger: true,
pageSizeOpts: [10, 20, 50, 100],
onPageChange: handlePageChange,
onPageSizeChange: handlePageSizeChange,
className: 'p-4',
}}
size='small'
className='modern-table'
rowClassName='hover:bg-gray-50 dark:hover:bg-gray-800/50 transition-colors'
empty={
<Empty
image={
<IllustrationNoResult style={{ width: 150, height: 150 }} />
}
darkModeImage={
<IllustrationNoResultDark style={{ width: 150, height: 150 }} />
}
description={
<span className='text-gray-500 dark:text-gray-400 font-medium'>
{t('暂无充值记录')}
</span>
}
style={{ padding: 40 }}
/>
}
/>
</div>
</Modal>
);
};
export default TopupHistoryModal;