267 lines
8.1 KiB
React
267 lines
8.1 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, useRef } from 'react';
|
|
import { Modal, Button, Spin, Typography, Space, Toast } from '@/components/ui';
|
|
import { IconRefresh, IconClose } from '@/components/ui/icons';
|
|
import { useTranslation } from 'react-i18next';
|
|
import QRCode from 'qrcode';
|
|
import { API, renderQuota } from '../../../helpers';
|
|
import { formatCurrency } from '../../../helpers/safeDisplay';
|
|
|
|
const { Text } = Typography;
|
|
|
|
// pollFn: 可选,自定义轮询函数,签名为 async (tradeNo: string) => { status: string } | null
|
|
// 默认走个人充值接口 /api/user/topup/self
|
|
const WechatQRModal = ({ visible, onClose, orderData, pollFn }) => {
|
|
const { t } = useTranslation();
|
|
const [qrCodeUrl, setQrCodeUrl] = useState('');
|
|
const [_loading, _setLoading] = useState(false);
|
|
const [polling, setPolling] = useState(false);
|
|
const [expired, setExpired] = useState(false);
|
|
const pollingTimerRef = useRef(null);
|
|
const expireTimerRef = useRef(null);
|
|
const errorCountRef = useRef(0); // 错误计数
|
|
|
|
// 生成二维码
|
|
const onVisibilityChange = useEffectEvent(() => {
|
|
if (visible && orderData && orderData.code_url) {
|
|
// 【Bug#5修复】重置过期状态,避免上一个过期订单影响新订单的展示
|
|
setExpired(false);
|
|
setQrCodeUrl(''); // 清空旧二维码,避免短暂显示上一个订单的二维码
|
|
generateQRCode(orderData.code_url);
|
|
startPolling();
|
|
startExpireTimer();
|
|
}
|
|
|
|
return () => {
|
|
stopPolling();
|
|
stopExpireTimer();
|
|
};
|
|
});
|
|
useEffect(
|
|
() => onVisibilityChange(),
|
|
[onVisibilityChange, visible, orderData],
|
|
);
|
|
|
|
const generateQRCode = async (url) => {
|
|
try {
|
|
const qrUrl = await QRCode.toDataURL(url, {
|
|
width: 300,
|
|
margin: 2,
|
|
});
|
|
setQrCodeUrl(qrUrl);
|
|
} catch (error) {
|
|
Toast.error(t('生成二维码失败'));
|
|
}
|
|
};
|
|
|
|
// 开始轮询订单状态
|
|
const startPolling = () => {
|
|
if (!orderData || !orderData.trade_no) return;
|
|
|
|
setPolling(true);
|
|
errorCountRef.current = 0; // 重置错误计数
|
|
|
|
const defaultPollFn = async (tradeNo) => {
|
|
const res = await API.get(`/api/user/topup/self?keyword=${tradeNo}`);
|
|
const items = res.data.data?.items || res.data.data || [];
|
|
return items.length > 0 ? items[0] : null;
|
|
};
|
|
|
|
const activePollFn = pollFn || defaultPollFn;
|
|
|
|
pollingTimerRef.current = setInterval(async () => {
|
|
try {
|
|
const order = await activePollFn(orderData.trade_no);
|
|
errorCountRef.current = 0;
|
|
|
|
if (order) {
|
|
if (order.status === 'success') {
|
|
stopPolling();
|
|
Toast.success(t('支付成功!'));
|
|
onClose(true);
|
|
} else if (order.status === 'expired' || order.status === 'closed') {
|
|
stopPolling();
|
|
setExpired(true);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('查询订单状态失败:', error);
|
|
errorCountRef.current++;
|
|
|
|
if (errorCountRef.current >= 3) {
|
|
stopPolling();
|
|
Toast.error(t('网络异常,请刷新页面重试'));
|
|
setExpired(true);
|
|
}
|
|
}
|
|
}, 3000);
|
|
};
|
|
|
|
// 停止轮询
|
|
const stopPolling = () => {
|
|
if (pollingTimerRef.current) {
|
|
clearInterval(pollingTimerRef.current);
|
|
pollingTimerRef.current = null;
|
|
}
|
|
setPolling(false);
|
|
};
|
|
|
|
// 开始过期计时器
|
|
const startExpireTimer = () => {
|
|
if (!orderData || !orderData.expire_at) return;
|
|
|
|
const now = Math.floor(Date.now() / 1000);
|
|
const expireIn = orderData.expire_at - now;
|
|
|
|
if (expireIn > 0) {
|
|
expireTimerRef.current = setTimeout(() => {
|
|
setExpired(true);
|
|
stopPolling();
|
|
Toast.warning(t('订单已过期'));
|
|
}, expireIn * 1000);
|
|
}
|
|
};
|
|
|
|
// 停止过期计时器
|
|
const stopExpireTimer = () => {
|
|
if (expireTimerRef.current) {
|
|
clearTimeout(expireTimerRef.current);
|
|
expireTimerRef.current = null;
|
|
}
|
|
};
|
|
|
|
// 刷新二维码
|
|
const handleRefresh = () => {
|
|
if (orderData && orderData.code_url) {
|
|
generateQRCode(orderData.code_url);
|
|
Toast.success(t('二维码已刷新'));
|
|
}
|
|
};
|
|
|
|
// 关闭弹窗
|
|
const handleClose = () => {
|
|
stopPolling();
|
|
stopExpireTimer();
|
|
onClose(false);
|
|
};
|
|
|
|
return (
|
|
<Modal
|
|
title={t('微信支付')}
|
|
visible={visible}
|
|
onCancel={handleClose}
|
|
footer={null}
|
|
width={450}
|
|
centered
|
|
>
|
|
<div style={{ textAlign: 'center', padding: '20px 0' }}>
|
|
{expired ? (
|
|
<div>
|
|
<Text type='danger' style={{ fontSize: 18 }}>
|
|
{t('订单已过期,请重新下单')}
|
|
</Text>
|
|
<br />
|
|
<Button
|
|
type='primary'
|
|
style={{ marginTop: 20 }}
|
|
onClick={handleClose}
|
|
>
|
|
{t('关闭')}
|
|
</Button>
|
|
</div>
|
|
) : (
|
|
<>
|
|
<Space vertical align='center' spacing='large'>
|
|
{/* 二维码 */}
|
|
<div style={{ position: 'relative' }}>
|
|
{qrCodeUrl ? (
|
|
<img
|
|
src={qrCodeUrl}
|
|
alt={t('支付二维码')}
|
|
style={{
|
|
width: 300,
|
|
height: 300,
|
|
border: '1px solid #e8e8e8',
|
|
}}
|
|
/>
|
|
) : (
|
|
<div
|
|
style={{
|
|
width: 300,
|
|
height: 300,
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
border: '1px solid #e8e8e8',
|
|
}}
|
|
>
|
|
<Spin size='large' />
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{/* 支付信息 */}
|
|
{orderData && (
|
|
<Space vertical align='start' spacing='small'>
|
|
<div>
|
|
<Text type='secondary'>{t('订单号:')}</Text>
|
|
<Text copyable>{orderData.trade_no}</Text>
|
|
</div>
|
|
<div>
|
|
<Text type='secondary'>{t('支付金额:')}</Text>
|
|
<Text strong style={{ fontSize: 20, color: '#f5222d' }}>
|
|
{formatCurrency(orderData.amount)}
|
|
</Text>
|
|
</div>
|
|
<div>
|
|
<Text type='secondary'>{t('获得额度:')}</Text>
|
|
<Text strong>{renderQuota(orderData.quota)}</Text>
|
|
</div>
|
|
</Space>
|
|
)}
|
|
|
|
{/* 提示文字 */}
|
|
<Text type='tertiary' style={{ fontSize: 14 }}>
|
|
{polling
|
|
? `🔄 ${t('正在等待支付...')}`
|
|
: t('请使用微信扫码支付')}
|
|
</Text>
|
|
|
|
{/* 操作按钮 */}
|
|
<Space>
|
|
<Button icon={<IconRefresh />} onClick={handleRefresh}>
|
|
{t('刷新二维码')}
|
|
</Button>
|
|
<Button icon={<IconClose />} onClick={handleClose}>
|
|
{t('取消支付')}
|
|
</Button>
|
|
</Space>
|
|
</Space>
|
|
</>
|
|
)}
|
|
</div>
|
|
</Modal>
|
|
);
|
|
};
|
|
|
|
export default WechatQRModal;
|