fix: 修复支付可能存在的缺陷

This commit is contained in:
archer
2023-03-22 12:20:27 +08:00
parent 984baf60f0
commit 5ec303610c
12 changed files with 266 additions and 93 deletions

View File

@@ -15,14 +15,14 @@ import {
import { getPayCode, checkPayResult } from '@/api/user';
import { useToast } from '@/hooks/useToast';
import { useQuery } from '@tanstack/react-query';
import { useUserStore } from '@/store/user';
import { useRouter } from 'next/router';
const PayModal = ({ onClose }: { onClose: () => void }) => {
const router = useRouter();
const { toast } = useToast();
const { initUserInfo } = useUserStore();
const [inputVal, setInputVal] = useState<number | ''>('');
const [loading, setLoading] = useState(false);
const [orderId, setOrderId] = useState('');
const [payId, setPayId] = useState('');
const handleClickPay = useCallback(async () => {
if (!inputVal || inputVal <= 0 || isNaN(+inputVal)) return;
@@ -38,7 +38,7 @@ const PayModal = ({ onClose }: { onClose: () => void }) => {
colorLight: '#ffffff',
correctLevel: QRCode.CorrectLevel.H
});
setOrderId(res.orderId);
setPayId(res.payId);
} catch (error) {
toast({
title: '出现了一些意外...',
@@ -50,21 +50,20 @@ const PayModal = ({ onClose }: { onClose: () => void }) => {
}, [inputVal, toast]);
useQuery(
[orderId],
[payId],
() => {
if (!orderId) return null;
return checkPayResult(orderId);
if (!payId) return null;
return checkPayResult(payId);
},
{
refetchInterval: 2000,
onSuccess(res) {
if (!res) return;
onClose();
initUserInfo();
toast({
title: '充值成功',
status: 'success'
});
router.reload();
}
}
);
@@ -74,17 +73,17 @@ const PayModal = ({ onClose }: { onClose: () => void }) => {
<Modal
isOpen={true}
onClose={() => {
if (orderId) return;
if (payId) return;
onClose();
}}
>
<ModalOverlay />
<ModalContent>
<ModalHeader></ModalHeader>
{!orderId && <ModalCloseButton />}
{!payId && <ModalCloseButton />}
<ModalBody>
{!orderId && (
{!payId && (
<>
<Grid gridTemplateColumns={'repeat(4,1fr)'} gridGap={5} mb={4}>
{[5, 10, 20, 50].map((item) => (
@@ -112,18 +111,23 @@ const PayModal = ({ onClose }: { onClose: () => void }) => {
)}
{/* 付费二维码 */}
<Box textAlign={'center'}>
{orderId && <Box mb={3}>: {inputVal}</Box>}
{payId && <Box mb={3}>: {inputVal}</Box>}
<Box id={'payQRCode'} display={'inline-block'}></Box>
</Box>
</ModalBody>
<ModalFooter>
{!orderId && (
{!payId && (
<>
<Button colorScheme={'gray'} onClick={onClose}>
</Button>
<Button ml={3} isLoading={loading} onClick={handleClickPay}>
<Button
ml={3}
isLoading={loading}
isDisabled={!inputVal || inputVal === 0}
onClick={handleClickPay}
>
</Button>
</>