import MyModal from '@fastgpt/web/components/common/MyModal'; import React, { useCallback, useEffect, useRef, useState } from 'react'; import { useTranslation } from 'next-i18next'; import { Box, ModalBody } from '@chakra-ui/react'; import { checkBalancePayResult } from '@/web/support/wallet/bill/api'; import { useToast } from '@fastgpt/web/hooks/useToast'; import LightTip from '@fastgpt/web/components/common/LightTip'; import QRCode from 'qrcode'; import { useRequest2 } from '@fastgpt/web/hooks/useRequest'; export type QRPayProps = { readPrice: number; codeUrl: string; billId: string; }; const qrCodeSize = 168; const QRCodePayModal = ({ tip, readPrice, codeUrl, billId, onSuccess }: QRPayProps & { tip?: string; onSuccess?: () => any }) => { const { t } = useTranslation(); const { toast } = useToast(); const canvasRef = useRef(null); const drawCode = useCallback(() => { const canvas = document.createElement('canvas'); QRCode.toCanvas(canvas, codeUrl, { width: qrCodeSize, margin: 0, color: { dark: '#000000', light: '#ffffff' } }) .then(() => { if (canvasRef.current) { canvasRef.current.innerHTML = ''; canvasRef.current.appendChild(canvas); } else { drawCode(); } }) .catch((err) => { console.error('QRCode generation error:', err); }); }, [codeUrl]); useEffect(() => { drawCode(); }, [drawCode]); useRequest2(() => checkBalancePayResult(billId), { manual: false, pollingInterval: 2000, onSuccess: (res) => { if (res) { onSuccess?.(); } }, errorToast: '' }); return ( {tip && } {t('common:pay.wechat', { price: readPrice })} ); }; export default React.memo(QRCodePayModal);