import React, { useEffect, useRef, useState } from 'react'; import { Avatar, Typography, Tag, Card, Button, Banner, Skeleton, Form, Space, Row, Col, Spin, Tooltip, Tabs, TabPane, } from '@douyinfe/semi-ui'; import { SiAlipay, SiWechat, SiStripe } from 'react-icons/si'; import { CreditCard, Coins, Wallet, BarChart2, TrendingUp, Receipt, Sparkles, } from 'lucide-react'; import { IconGift } from '@douyinfe/semi-icons'; import { useMinimumLoadingTime } from '../../hooks/common/useMinimumLoadingTime'; import { getCurrencyConfig } from '../../helpers/render'; import SubscriptionPlansCard from './SubscriptionPlansCard'; import './preset-amount-card.css'; const { Text } = Typography; const RechargeCard = ({ t, enableOnlineTopUp, enableStripeTopUp, enableCreemTopUp, creemProducts, creemPreTopUp, presetAmounts, selectedPreset, selectPresetAmount, onPresetCardClick, formatLargeNumber, priceRatio, topUpCount, minTopUp, renderTopUpCount, getAmount, setTopUpCount, setSelectedPreset, renderAmount, amountLoading, payMethods, preTopUp, paymentLoading, payWay, redemptionCode, setRedemptionCode, topUp, isSubmitting, topUpLink, openTopUpLink, userState, renderQuota, statusLoading, topupInfo, onOpenHistory, enableAlipayTopUp, enableWechatPayTopUp, enableWaffoTopUp, waffoTopUp, waffoPayMethods, subscriptionLoading = false, subscriptionPlans = [], billingPreference, onChangeBillingPreference, activeSubscriptions = [], allSubscriptions = [], reloadSubscriptionSelf, }) => { const onlineFormApiRef = useRef(null); const redeemFormApiRef = useRef(null); const initialTabSetRef = useRef(false); const showAmountSkeleton = useMinimumLoadingTime(amountLoading); const [activeTab, setActiveTab] = useState('topup'); const shouldShowSubscription = !subscriptionLoading && subscriptionPlans.length > 0; useEffect(() => { if (initialTabSetRef.current) return; if (subscriptionLoading) return; setActiveTab(shouldShowSubscription ? 'subscription' : 'topup'); initialTabSetRef.current = true; }, [shouldShowSubscription, subscriptionLoading]); useEffect(() => { if (!shouldShowSubscription && activeTab !== 'topup') { setActiveTab('topup'); } }, [shouldShowSubscription, activeTab]); const topupContent = ( {/* 统计数据 */}
{t('账户统计')}
{/* 统计数据 */}
{/* 当前余额 */}
{renderQuota(userState?.user?.quota)}
{t('当前余额')}
{/* 历史消耗 */}
{renderQuota(userState?.user?.used_quota)}
{t('历史消耗')}
{/* 请求次数 */}
{userState?.user?.request_count || 0}
{t('请求次数')}
} > {/* 在线充值表单 */} {statusLoading ? (
) : enableOnlineTopUp || enableStripeTopUp || enableCreemTopUp || enableAlipayTopUp || enableWechatPayTopUp || enableWaffoTopUp ? (
(onlineFormApiRef.current = api)} initValues={{ topUpCount: topUpCount }} >
{(enableOnlineTopUp || enableStripeTopUp || enableWaffoTopUp || enableAlipayTopUp || enableWechatPayTopUp) && ( { if (value && value >= 1) { setTopUpCount(value); setSelectedPreset(null); await getAmount(value); } }} onBlur={(e) => { const value = parseInt(e.target.value); if (!value || value < 1) { setTopUpCount(1); getAmount(1); } }} formatter={(value) => (value ? `${value}` : '')} parser={(value) => value ? parseInt(value.replace(/[^\d]/g, '')) : 0 } extraText={ } > {t('实付金额:')} {renderAmount()} } style={{ width: '100%' }} /> {payMethods && payMethods.filter((m) => m.type !== 'waffo' && m.type !== 'custom1').length > 0 && ( {payMethods .filter((m) => m.type !== 'waffo' && m.type !== 'custom1') .map((payMethod) => { const minTopupVal = Number(payMethod.min_topup) || 0; const isStripe = payMethod.type === 'stripe'; const isAlipay = payMethod.type === 'alipay'; const isWechat = payMethod.type === 'wxpay'; const disabled = (!enableOnlineTopUp && !isStripe && !isAlipay && !isWechat) || (!enableStripeTopUp && isStripe) || (!enableAlipayTopUp && isAlipay) || (!enableWechatPayTopUp && isWechat) || minTopupVal > Number(topUpCount || 0); const buttonEl = ( ); return disabled && minTopupVal > Number(topUpCount || 0) ? ( {buttonEl} ) : ( {buttonEl} ); })} )} )} {(enableOnlineTopUp || enableStripeTopUp || enableWaffoTopUp || enableAlipayTopUp || enableWechatPayTopUp) && ( {t('选择充值额度')} {(() => { const { symbol, rate, type } = getCurrencyConfig(); if (type === 'USD') return null; return ( (1 USD = {rate.toFixed(2)} {symbol}) ); })()}
} >
{presetAmounts.map((preset, index) => { const discount = preset.discount || topupInfo?.discount?.[preset.value] || 1.0; const originalPrice = preset.value * priceRatio; const discountedPrice = originalPrice * discount; const hasDiscount = discount < 1.0; const actualPay = discountedPrice; const save = originalPrice - discountedPrice; // 根据当前货币类型换算显示金额和数量 const { symbol, rate, type } = getCurrencyConfig(); const statusStr = localStorage.getItem('status'); let usdRate = 7; // 默认CNY汇率 try { if (statusStr) { const s = JSON.parse(statusStr); usdRate = s?.usd_exchange_rate || 7; } } catch (e) {} // 充值数量始终以美元数量展示,不随全局展示货币切换。 let displayValue = preset.value; let displayActualPay = actualPay; let displaySave = save; if (type === 'USD') { // 数量保持USD,价格从CNY转USD displayActualPay = actualPay / usdRate; displaySave = save / usdRate; } else if (type === 'CUSTOM') { // 自定义货币仅影响价格显示,数量仍显示为美元数量 displayActualPay = (actualPay / usdRate) * rate; displaySave = (save / usdRate) * rate; } return (
{ onlineFormApiRef.current?.setValue( 'topUpCount', preset.value, ); if (onPresetCardClick) { onPresetCardClick(preset); } else { selectPresetAmount(preset); } }} >
{formatLargeNumber(displayValue)} {hasDiscount && ( {t('折').includes('off') ? ( (1 - parseFloat(discount)) * 100 ).toFixed(1) : (discount * 10).toFixed(1)} {t('折')} )}
{t('实付')} {symbol} {displayActualPay.toFixed(2)} {hasDiscount && displaySave > 0.005 && ( <> ,{t('节省')} {symbol} {displaySave.toFixed(2)} )}
); })}
)} {/* Waffo 充值区域 */} {enableWaffoTopUp && waffoPayMethods && waffoPayMethods.length > 0 && ( {waffoPayMethods.map((method, index) => ( ))} )} {/* Creem 充值区域 */} {enableCreemTopUp && creemProducts.length > 0 && (
{creemProducts.map((product, index) => ( creemPreTopUp(product)} className='cursor-pointer !rounded-2xl transition-all hover:shadow-md border-gray-200 hover:border-gray-300' bodyStyle={{ textAlign: 'center', padding: '16px' }} >
{product.name}
{t('充值额度')}: {product.quota}
{product.currency === 'EUR' ? '€' : 'USD'} {product.price}
))}
)}
) : ( )}
{/* 兑换码充值 */} {t('兑换码充值')} } >
(redeemFormApiRef.current = api)} initValues={{ redemptionCode: redemptionCode }} > setRedemptionCode(value)} prefix={} suffix={
} showClear style={{ width: '100%' }} extraText={ topUpLink && ( {t('在找兑换码?')} {t('购买兑换码')} ) } />
); return ( {/* 卡片头部 */}
{t('账户充值')}
{t('多种充值方式,安全便捷')}
{shouldShowSubscription ? ( {t('订阅套餐')} } itemKey='subscription' >
{t('额度充值')} } itemKey='topup' >
{topupContent}
) : ( topupContent )}
); }; export default RechargeCard;