import React, { useEffect, useState, useCallback } from 'react'; import { Typography, Tag, Button, Banner, Skeleton, Spin, Tooltip, Input, InputNumber, Progress, } from '@douyinfe/semi-ui'; import { SiAlipay, SiWechat, SiStripe } from 'react-icons/si'; import { CreditCard, WalletCards, Activity, ChartColumn, Receipt, Gift, DollarSign, Ticket, ChevronRight, Gauge, TrendingUp, } from 'lucide-react'; import { API } from '../../helpers'; 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 [rpmTpmData, setRpmTpmData] = useState(null); 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 fetchRpmTpm = useCallback(async () => { try { const now = Math.floor(Date.now() / 1000); const res = await API.get( `/api/log/self/stat?start_timestamp=${now - 3600}&end_timestamp=${now}`, ); if (res?.data?.success) { setRpmTpmData(res.data.data); } } catch { // 静默失败 } }, []); useEffect(() => { fetchRpmTpm(); const interval = setInterval(fetchRpmTpm, 30000); return () => clearInterval(interval); }, [fetchRpmTpm]); const isAnyPayEnabled = enableOnlineTopUp || enableStripeTopUp || enableCreemTopUp || enableAlipayTopUp || enableWechatPayTopUp || enableWaffoTopUp; const tabs = [ { key: 'topup', label: t('充值'), desc: t('灵活付费') }, { key: 'subscription', label: t('订阅'), desc: t('享更低单价') }, ]; const totalQuota = (userState?.user?.used_quota || 0) + (userState?.user?.quota || 0); const usagePercent = totalQuota > 0 ? Math.round(((userState?.user?.used_quota || 0) / totalQuota) * 100) : 0; // 统计卡片配置 const statCards = [ { key: 'balance', icon: WalletCards, label: t('当前余额'), value: userState?.user?.quota, sublabel: `${t('已用')} ${usagePercent}%`, 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, }, { key: 'rpm', icon: Gauge, label: t('每分钟请求'), value: rpmTpmData?.rpm, sublabel: t('最近1分钟'), raw: true, live: true, }, { key: 'tpm', icon: TrendingUp, label: t('每分钟Token'), value: rpmTpmData?.tpm, sublabel: t('最近1分钟'), raw: true, live: true, }, ]; const topupContent = (