import React, {useEffect, useState} from 'react'; import {Button, Card, Confirm, Form, Grid, Header, Segment, Statistic} from 'semantic-ui-react'; import {API, isMobile, showError, showInfo, showSuccess} from '../../helpers'; import {renderNumber, renderQuota} from '../../helpers/render'; import {Col, Layout, Row, Typography} from "@douyinfe/semi-ui"; const TopUp = () => { const [redemptionCode, setRedemptionCode] = useState(''); const [topUpCode, setTopUpCode] = useState(''); const [topUpCount, setTopUpCount] = useState(10); const [amount, setAmount] = useState(0); const [topUpLink, setTopUpLink] = useState(''); const [userQuota, setUserQuota] = useState(0); const [isSubmitting, setIsSubmitting] = useState(false); const [open, setOpen] = useState(false); const [payWay, setPayWay] = useState(''); const topUp = async () => { if (redemptionCode === '') { showInfo('请输入充值码!') return; } setIsSubmitting(true); try { const res = await API.post('/api/user/topup', { key: redemptionCode }); const {success, message, data} = res.data; if (success) { showSuccess('充值成功!'); setUserQuota((quota) => { return quota + data; }); setRedemptionCode(''); } else { showError(message); } } catch (err) { showError('请求失败'); } finally { setIsSubmitting(false); } }; const openTopUpLink = () => { if (!topUpLink) { showError('超级管理员未设置充值链接!'); return; } window.open(topUpLink, '_blank'); }; const preTopUp = async (payment) => { if (amount === 0) { await getAmount(); } setPayWay(payment) setOpen(true); } const onlineTopUp = async () => { if (amount === 0) { await getAmount(); } setOpen(false); try { const res = await API.post('/api/user/pay', { amount: parseInt(topUpCount), top_up_code: topUpCode, PaymentMethod: payWay }); if (res !== undefined) { const {message, data} = res.data; // showInfo(message); if (message === 'success') { let params = data let url = res.data.url let form = document.createElement('form') form.action = url form.method = 'POST' form.target = '_blank' for (let key in params) { let input = document.createElement('input') input.type = 'hidden' input.name = key input.value = params[key] form.appendChild(input) } document.body.appendChild(form) form.submit() document.body.removeChild(form) } else { showError(data); // setTopUpCount(parseInt(res.data.count)); // setAmount(parseInt(data)); } } else { showError(res); } } catch (err) { console.log(err); } finally { } } const getUserQuota = async () => { let res = await API.get(`/api/user/self`); const {success, message, data} = res.data; if (success) { setUserQuota(data.quota); } else { showError(message); } } useEffect(() => { let status = localStorage.getItem('status'); if (status) { status = JSON.parse(status); if (status.top_up_link) { setTopUpLink(status.top_up_link); } } getUserQuota().then(); }, []); const renderAmount = () => { console.log(amount); return amount + '元'; } const getAmount = async (value) => { if (value === undefined) { value = topUpCount; } try { const res = await API.post('/api/user/amount', { amount: parseFloat(value), top_up_code: topUpCode }); if (res !== undefined) { const {message, data} = res.data; // showInfo(message); if (message === 'success') { setAmount(parseInt(data)); } else { showError(data); // setTopUpCount(parseInt(res.data.count)); // setAmount(parseInt(data)); } } else { showError(res); } } catch (err) { console.log(err); } finally { } } const handleCancel = () => { setOpen(false); } return (

我的钱包

兑换

{ setRedemptionCode(e.target.value); }} />
{renderQuota(userQuota)} 剩余额度
在线充值,最低1
{ setTopUpCount(e.target.value); await getAmount(e.target.value); }} />
{renderAmount()} 支付金额
); }; export default TopUp;