import React, { useState, useCallback } from 'react'; import { Modal, ModalOverlay, ModalContent, ModalHeader, ModalFooter, ModalBody, ModalCloseButton, Button, Input, Box, Grid } from '@chakra-ui/react'; import { getPayCode, checkPayResult } from '@/api/user'; import { useToast } from '@/hooks/useToast'; import { useQuery } from '@tanstack/react-query'; import { useRouter } from 'next/router'; const PayModal = ({ onClose }: { onClose: () => void }) => { const router = useRouter(); const { toast } = useToast(); const [inputVal, setInputVal] = useState(''); const [loading, setLoading] = useState(false); const [payId, setPayId] = useState(''); const handleClickPay = useCallback(async () => { if (!inputVal || inputVal <= 0 || isNaN(+inputVal)) return; setLoading(true); try { // 获取支付二维码 const res = await getPayCode(inputVal); new QRCode(document.getElementById('payQRCode'), { text: res.codeUrl, width: 128, height: 128, colorDark: '#000000', colorLight: '#ffffff', correctLevel: QRCode.CorrectLevel.H }); setPayId(res.payId); } catch (error) { toast({ title: '出现了一些意外...', status: 'error' }); console.log(error); } setLoading(false); }, [inputVal, toast]); useQuery( [payId], () => { if (!payId) return null; return checkPayResult(payId); }, { refetchInterval: 2000, onSuccess(res) { if (!res) return; toast({ title: '充值成功', status: 'success' }); router.reload(); } } ); return ( <> { if (payId) return; onClose(); }} > 充值 {!payId && } {!payId && ( <> {/* 价格表 */} {/* {modelList.map((item, i) => ( ))}
模型类型 价格(元/1K tokens,包含所有上下文)
{item.name} {formatPrice(item.price, 1000)}
*/} {[5, 10, 20, 50].map((item) => ( ))} { setInputVal(Math.floor(+e.target.value)); }} > )} {/* 付费二维码 */} {payId && 请微信扫码支付: {inputVal}元,请勿关闭页面}
{!payId && ( <> )}
); }; export default PayModal;