/* 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 { 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'; 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 (
{expired ? (
{t('订单已过期,请重新下单')}
) : ( <> {/* 二维码 */}
{qrCodeUrl ? ( {t('支付二维码')} ) : (
)}
{/* 支付信息 */} {orderData && (
{t('订单号:')} {orderData.trade_no}
{t('支付金额:')} ¥{orderData.amount?.toFixed(2)}
{t('获得额度:')} {renderQuota(orderData.quota)}
)} {/* 提示文字 */} {polling ? `🔄 ${t('正在等待支付...')}` : t('请使用微信扫码支付')} {/* 操作按钮 */}
)}
); }; export default WechatQRModal;