import { Typography, Stack, OutlinedInput, InputAdornment, Button, InputLabel, FormControl } from '@mui/material'; import { IconWallet } from '@tabler/icons-react'; import { useTheme } from '@mui/material/styles'; import SubCard from 'ui-component/cards/SubCard'; import UserCard from 'ui-component/cards/UserCard'; import { API } from 'utils/api'; import React, { useEffect, useState } from 'react'; import { showError, showInfo, showSuccess, renderQuota } from 'utils/common'; const TopupCard = () => { const theme = useTheme(); const [redemptionCode, setRedemptionCode] = useState(''); const [topUpLink, setTopUpLink] = useState(''); const [userQuota, setUserQuota] = useState(0); const [isSubmitting, setIsSubmitting] = useState(false); 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 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('siteInfo'); if (status) { status = JSON.parse(status); if (status.top_up_link) { setTopUpLink(status.top_up_link); } } getUserQuota().then(); }, []); return ( 当前额度: {renderQuota(userQuota)} 兑换码 { setRedemptionCode(e.target.value); }} name="key" placeholder="请输入兑换码" endAdornment={ } aria-describedby="helper-text-channel-quota-label" /> 还没有兑换码? 点击获取兑换码: ); }; export default TopupCard;