import React, { useEffect, useState } from 'react'; import { Typography, Tag, Button, Banner, Skeleton, Spin, Tooltip, Input, InputNumber, } from '@douyinfe/semi-ui'; import { SiAlipay, SiWechat, SiStripe } from 'react-icons/si'; import { CreditCard, WalletCards, Activity, ChartColumn, Receipt, Gift, DollarSign, Ticket, ChevronRight, } from 'lucide-react'; 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 [activeTab, setActiveTab] = useState('topup'); const [customAmount, setCustomAmount] = useState(10); const showAmountSkeleton = useMinimumLoadingTime(amountLoading); const shouldShowSubscription = !subscriptionLoading && subscriptionPlans.length > 0; const statusStr = localStorage.getItem('status'); let usdRate = 7; try { if (statusStr) { const s = JSON.parse(statusStr); usdRate = s?.usd_exchange_rate || 7; } } catch (e) {} const { symbol, rate, type: currencyType } = getCurrencyConfig(); const calcRmb = (usdAmount) => { return usdAmount * (priceRatio || 1); }; const getDiscountForAmount = (val) => { const preset = presetAmounts.find((p) => p.value === val); if (preset?.discount) return preset.discount; if (topupInfo?.discount?.[val]) return topupInfo.discount[val]; return 1.0; }; const handleCustomAmountChange = (val) => { const num = Number(val); if (!num || num <= 0) return; setCustomAmount(num); setTopUpCount(num); setSelectedPreset(null); getAmount(num); }; const handlePresetClick = (preset) => { const discount = getDiscountForAmount(preset.value); const num = preset.value * discount; setCustomAmount(preset.value); if (onPresetCardClick) { onPresetCardClick(preset); } else { selectPresetAmount(preset); } }; useEffect(() => { if (subscriptionLoading) return; if (shouldShowSubscription) { setActiveTab('subscription'); } }, [shouldShowSubscription, subscriptionLoading]); const isAnyPayEnabled = enableOnlineTopUp || enableStripeTopUp || enableCreemTopUp || enableAlipayTopUp || enableWechatPayTopUp || enableWaffoTopUp; const tabs = [ { key: 'topup', label: t('充值'), desc: t('灵活付费') }, { key: 'subscription', label: t('订阅'), desc: t('享更低单价') }, ]; // 统计卡片配置 const statCards = [ { key: 'balance', icon: WalletCards, label: t('当前余额'), value: userState?.user?.quota, sublabel: t('剩余配额'), highlight: true, }, { key: 'usage', icon: ChartColumn, label: t('总用量'), value: userState?.user?.used_quota, sublabel: t('总消耗额度'), }, { key: 'requests', icon: Activity, label: t('API 请求'), value: userState?.user?.request_count || 0, sublabel: t('总请求数'), raw: true, }, ]; const topupContent = (
{/* 左侧:充值卡片 */}
{/* 卡片头部 */}
{t('账户充值')}
{t('选择金额和支付方式完成充值')}
{isAnyPayEnabled ? ( <> {/* 预设额度 */}
{t('充值金额')} USD
{presetAmounts.map((preset, index) => { const discount = getDiscountForAmount(preset.value); const rmbAmount = calcRmb(preset.value * discount); const isSelected = selectedPreset === preset.value; return ( ); })}
{/* 自定义金额 */}
{t('自定义金额')}
$
{t('应付')} ¥{calcRmb(customAmount * getDiscountForAmount(customAmount)).toFixed(0)}
{/* 支付方式 */}
{t('支付方式')}
{payMethods .filter((m) => m.type !== 'waffo') .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 getIcon = () => { if (isAlipay) return ; if (isWechat) return ; if (isStripe) return ; return ; }; const btn = ( ); if (disabled && minTopupVal > Number(topUpCount || 0)) { return ( {btn} ); } return {btn}; })} {enableWaffoTopUp && waffoPayMethods && waffoPayMethods.length > 0 && waffoPayMethods.map((method, index) => ( ))} {enableCreemTopUp && creemProducts.length > 0 && creemProducts.map((product, index) => ( ))}
) : ( )}
{/* 右侧:兑换码 + 账单入口 */}
{t('兑换码')}
{t('输入兑换码获取额度')}
{topUpLink && ( )}
{/* 账单入口 */}
); return (
{/* 统计卡片 */}
{statCards.map((card, i) => (
{card.label}
{statusLoading ? ( ) : card.raw ? ( card.value ) : ( renderQuota(card.value) )}
{card.sublabel}
))}
{/* 标签切换 */} {shouldShowSubscription && (
{tabs.map((tab) => ( ))}
)} {/* 主体内容 */} {statusLoading ? (
) : shouldShowSubscription && activeTab === 'subscription' ? ( ) : ( topupContent )}
); }; export default RechargeCard;