531 lines
23 KiB
React
531 lines
23 KiB
React
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import { I18N_TECHNICAL_TEXT } from '../../i18n/technicalText.js';
|
|
import { Receipt as ActionReceipt } from '@/components/common/icons/ActionIcons';
|
|
import React, { useRef } from 'react';
|
|
import {
|
|
Typography,
|
|
Card,
|
|
Button,
|
|
Banner,
|
|
Form,
|
|
Space,
|
|
Spin,
|
|
Tooltip,
|
|
} from '@/components/ui';
|
|
import { CreditCard, Coins, Wallet } from 'lucide-react';
|
|
import {
|
|
AlipayBrandIcon,
|
|
WechatBrandIcon,
|
|
StripeBrandIcon,
|
|
} from '../common/icons/BrandIcons';
|
|
import { IconGift } from '@/components/ui/icons';
|
|
import { useMinimumLoadingTime } from '../../hooks/common/useMinimumLoadingTime';
|
|
import { getCurrencyConfig } from '../../helpers/render';
|
|
import { formatNumber, toFiniteNumber } from '../../helpers/safeDisplay';
|
|
|
|
const { Text } = Typography;
|
|
|
|
const RechargeCard = ({
|
|
t,
|
|
enableOnlineTopUp,
|
|
enableStripeTopUp,
|
|
enableCreemTopUp,
|
|
creemProducts,
|
|
creemPreTopUp,
|
|
presetAmounts,
|
|
selectedPreset,
|
|
selectPresetAmount,
|
|
formatLargeNumber,
|
|
priceRatio,
|
|
topUpCount,
|
|
minTopUp,
|
|
renderQuotaWithAmount,
|
|
getAmount,
|
|
setTopUpCount,
|
|
setSelectedPreset,
|
|
renderAmount,
|
|
amountLoading,
|
|
payMethods,
|
|
preTopUp,
|
|
paymentLoading,
|
|
payWay,
|
|
redemptionCode,
|
|
setRedemptionCode,
|
|
topUp,
|
|
isSubmitting,
|
|
topUpLink,
|
|
openTopUpLink,
|
|
userState,
|
|
renderQuota,
|
|
statusLoading,
|
|
topupInfo,
|
|
onOpenHistory,
|
|
}) => {
|
|
const onlineFormApiRef = useRef(null);
|
|
const redeemFormApiRef = useRef(null);
|
|
const _showAmountSkeleton = useMinimumLoadingTime(amountLoading);
|
|
console.log(
|
|
' enabled screem ?',
|
|
enableCreemTopUp,
|
|
' products ?',
|
|
creemProducts,
|
|
);
|
|
return (
|
|
<Card className='!rounded-2xl shadow-lg border-0 bg-white/50 backdrop-blur-sm dark:bg-zinc-800/50 transition-all duration-300 hover:shadow-xl'>
|
|
{/* 卡片头部 */}
|
|
<div className='flex items-center justify-between mb-6 pb-4 border-b border-gray-100 dark:border-gray-700'>
|
|
<div className='flex items-center gap-4'>
|
|
<div className='p-3 bg-gradient-to-br from-blue-500 to-brand-to rounded-xl shadow-lg shadow-blue-500/30 text-white'>
|
|
<CreditCard size={24} />
|
|
</div>
|
|
<div>
|
|
<Typography.Text className='text-xl font-bold bg-clip-text text-transparent bg-gradient-to-r from-gray-900 to-gray-600 dark:from-white dark:to-gray-300'>
|
|
{t('账户充值')}
|
|
</Typography.Text>
|
|
<div className='text-sm text-gray-500 dark:text-gray-400 mt-1'>
|
|
{t('多种充值方式,安全便捷')}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<Button
|
|
icon={<ActionReceipt size={18} />}
|
|
theme='solid'
|
|
type='secondary'
|
|
className='!rounded-xl !px-4 hover:scale-105 transition-transform'
|
|
onClick={onOpenHistory}
|
|
>
|
|
{t('账单')}
|
|
</Button>
|
|
</div>
|
|
|
|
<Space vertical style={{ width: '100%' }} spacing='loose'>
|
|
{/* 统计数据 */}
|
|
<div className='relative overflow-hidden rounded-2xl bg-gradient-to-r from-blue-600 to-brand-to shadow-xl shadow-blue-500/20'>
|
|
<div className='absolute top-0 right-0 w-64 h-64 bg-white/10 rounded-full -mr-16 -mt-16 blur-3xl'></div>
|
|
<div className='absolute bottom-0 left-0 w-48 h-48 bg-black/10 rounded-full -ml-12 -mb-12 blur-2xl'></div>
|
|
|
|
<div className='relative z-10 p-6'>
|
|
<div className='flex justify-between items-center mb-6'>
|
|
<div className='flex items-center gap-2'>
|
|
<div className='p-1.5 bg-white/20 rounded-lg backdrop-blur-md'>
|
|
<Wallet size={16} className='text-white' />
|
|
</div>
|
|
<span className='text-blue-100 font-medium'>
|
|
{t('账户概览')}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div className='grid grid-cols-3 gap-8'>
|
|
<div className='text-center relative group'>
|
|
<div className='text-3xl font-bold text-white mb-2 tracking-tight group-hover:scale-110 transition-transform duration-300'>
|
|
{renderQuota(userState?.user?.quota)}
|
|
</div>
|
|
<div className='text-blue-100/80 text-sm font-medium'>
|
|
{t('当前余额')}
|
|
</div>
|
|
<div className='absolute right-0 top-1/2 -translate-y-1/2 w-px h-8 bg-gradient-to-b from-transparent via-white/20 to-transparent'></div>
|
|
</div>
|
|
|
|
<div className='text-center relative group'>
|
|
<div className='text-3xl font-bold text-white mb-2 tracking-tight group-hover:scale-110 transition-transform duration-300'>
|
|
{renderQuota(userState?.user?.used_quota)}
|
|
</div>
|
|
<div className='text-blue-100/80 text-sm font-medium'>
|
|
{t('历史消耗')}
|
|
</div>
|
|
<div className='absolute right-0 top-1/2 -translate-y-1/2 w-px h-8 bg-gradient-to-b from-transparent via-white/20 to-transparent'></div>
|
|
</div>
|
|
|
|
<div className='text-center group'>
|
|
<div className='text-3xl font-bold text-white mb-2 tracking-tight group-hover:scale-110 transition-transform duration-300'>
|
|
{userState?.user?.request_count || 0}
|
|
</div>
|
|
<div className='text-blue-100/80 text-sm font-medium'>
|
|
{t('请求次数')}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* 在线充值表单 */}
|
|
{statusLoading ? (
|
|
<div className='py-12 flex justify-center'>
|
|
<Spin size='large' />
|
|
</div>
|
|
) : enableOnlineTopUp ||
|
|
enableStripeTopUp ||
|
|
enableCreemTopUp ||
|
|
(payMethods && payMethods.length > 0) ? (
|
|
<div className='bg-gray-50/50 dark:bg-gray-800/50 rounded-2xl p-6 border border-gray-100 dark:border-gray-700'>
|
|
<Form
|
|
getFormApi={(api) => (onlineFormApiRef.current = api)}
|
|
initValues={{ topUpCount: topUpCount }}
|
|
>
|
|
<div className='space-y-8'>
|
|
{/* 自定义金额输入区域 - 已隐藏,不允许用户自己输入金额 */}
|
|
{(enableOnlineTopUp ||
|
|
enableStripeTopUp ||
|
|
(payMethods && payMethods.length > 0)) && (
|
|
<div className='grid grid-cols-1 lg:grid-cols-12 gap-8'>
|
|
<div className='lg:col-span-12'>
|
|
<Form.Slot
|
|
label={
|
|
<span className='text-base font-semibold text-gray-700 dark:text-gray-200'>
|
|
{t('支付方式')}
|
|
</span>
|
|
}
|
|
>
|
|
{payMethods && payMethods.length > 0 ? (
|
|
<div className='grid grid-cols-2 sm:grid-cols-3 gap-3'>
|
|
{payMethods.map((payMethod) => {
|
|
const minTopupVal =
|
|
Number(payMethod.min_topup) || 0;
|
|
const isStripe = payMethod.type === 'stripe';
|
|
const disabled =
|
|
(!enableOnlineTopUp && !isStripe) ||
|
|
(!enableStripeTopUp && isStripe) ||
|
|
minTopupVal > Number(topUpCount || 0);
|
|
|
|
const buttonContent = (
|
|
<div
|
|
className={`flex flex-col items-center justify-center p-3 h-full gap-2 transition-all duration-200 ${disabled ? 'opacity-50' : 'group-hover:scale-105'}`}
|
|
>
|
|
{payMethod.type === 'alipay' ? (
|
|
<AlipayBrandIcon
|
|
size={28}
|
|
color='#1677FF'
|
|
/>
|
|
) : payMethod.type === 'wxpay' ? (
|
|
<WechatBrandIcon
|
|
size={28}
|
|
color='#07C160'
|
|
/>
|
|
) : payMethod.type === 'stripe' ? (
|
|
<StripeBrandIcon
|
|
size={28}
|
|
color='#635BFF'
|
|
/>
|
|
) : (
|
|
<CreditCard
|
|
size={28}
|
|
color={
|
|
payMethod.color ||
|
|
'var(--ui-color-text-2)'
|
|
}
|
|
/>
|
|
)}
|
|
<span className='font-medium text-sm'>
|
|
{payMethod.name}
|
|
</span>
|
|
</div>
|
|
);
|
|
|
|
const containerClass = `h-24 rounded-xl border-2 cursor-pointer transition-all duration-200 ${
|
|
payWay === payMethod.type
|
|
? 'border-blue-500 bg-blue-50 dark:bg-blue-900/20 shadow-md ring-2 ring-blue-200 dark:ring-blue-800'
|
|
: 'border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 hover:border-blue-300 dark:hover:border-blue-600 hover:shadow-md'
|
|
} ${disabled ? 'cursor-not-allowed grayscale' : 'group'}`;
|
|
|
|
const buttonEl = (
|
|
<div
|
|
key={payMethod.type}
|
|
className={containerClass}
|
|
onClick={() =>
|
|
!disabled && preTopUp(payMethod.type)
|
|
}
|
|
>
|
|
{buttonContent}
|
|
</div>
|
|
);
|
|
|
|
return disabled &&
|
|
minTopupVal > Number(topUpCount || 0) ? (
|
|
<Tooltip
|
|
content={t('最低充值 {{minTopupVal}}', {
|
|
minTopupVal: minTopupVal,
|
|
})}
|
|
key={payMethod.type}
|
|
>
|
|
{buttonEl}
|
|
</Tooltip>
|
|
) : (
|
|
<React.Fragment key={payMethod.type}>
|
|
{buttonEl}
|
|
</React.Fragment>
|
|
);
|
|
})}
|
|
</div>
|
|
) : (
|
|
<div className='text-gray-500 text-sm p-4 bg-gray-100 rounded-xl border border-dashed border-gray-300 text-center'>
|
|
{t('暂无可用的支付方式')}
|
|
</div>
|
|
)}
|
|
</Form.Slot>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{(enableOnlineTopUp ||
|
|
enableStripeTopUp ||
|
|
(payMethods && payMethods.length > 0)) && (
|
|
<Form.Slot
|
|
label={
|
|
<div className='flex items-center gap-2 mb-2'>
|
|
<span className='text-base font-semibold text-gray-700 dark:text-gray-200'>
|
|
{t('快捷选择')}
|
|
</span>
|
|
{(() => {
|
|
const { symbol, rate, type } = getCurrencyConfig();
|
|
if (type === 'USD') return null;
|
|
|
|
return (
|
|
<span className='text-xs font-normal px-2 py-0.5 bg-gray-100 dark:bg-gray-700 rounded-full text-gray-500'>
|
|
{I18N_TECHNICAL_TEXT.USD_EXCHANGE_PREFIX}{' '}
|
|
{formatNumber(rate, 2, '1.00')} {symbol}
|
|
</span>
|
|
);
|
|
})()}
|
|
</div>
|
|
}
|
|
>
|
|
<div className='grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 gap-4'>
|
|
{presetAmounts.map((preset, index) => {
|
|
const presetValue = toFiniteNumber(preset.value) ?? 0;
|
|
const discount =
|
|
toFiniteNumber(
|
|
preset.discount ??
|
|
topupInfo?.discount?.[preset.value],
|
|
) ?? 1.0;
|
|
const originalPrice =
|
|
presetValue * (toFiniteNumber(priceRatio) ?? 0);
|
|
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 = 1;
|
|
try {
|
|
if (statusStr) {
|
|
const s = JSON.parse(statusStr);
|
|
// 注意:如果设置了汇率值(包括1),使用设置值;只有未设置(undefined/null)时才用默认值1
|
|
const parsedRate = toFiniteNumber(
|
|
s?.usd_exchange_rate,
|
|
);
|
|
usdRate =
|
|
parsedRate !== null && parsedRate > 0
|
|
? parsedRate
|
|
: 1;
|
|
}
|
|
} catch (e) {}
|
|
|
|
let displayValue = presetValue;
|
|
let displayActualPay = actualPay;
|
|
let displaySave = save;
|
|
|
|
if (type === 'USD') {
|
|
displayActualPay = actualPay / usdRate;
|
|
displaySave = save / usdRate;
|
|
} else if (type === 'CNY') {
|
|
displayValue = preset.value * usdRate;
|
|
} else if (type === 'CUSTOM') {
|
|
displayValue = preset.value * rate;
|
|
displayActualPay = (actualPay / usdRate) * rate;
|
|
displaySave = (save / usdRate) * rate;
|
|
}
|
|
|
|
const isSelected = selectedPreset === preset.value;
|
|
|
|
return (
|
|
<div
|
|
key={index}
|
|
className={`relative cursor-pointer rounded-xl p-4 transition-all duration-200 border-2 overflow-hidden group ${
|
|
isSelected
|
|
? 'border-blue-500 bg-blue-50 dark:bg-blue-900/20 shadow-md transform scale-105'
|
|
: 'border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 hover:border-blue-300 hover:shadow-lg'
|
|
}`}
|
|
onClick={() => {
|
|
selectPresetAmount(preset);
|
|
onlineFormApiRef.current?.setValue(
|
|
'topUpCount',
|
|
preset.value,
|
|
);
|
|
}}
|
|
>
|
|
{hasDiscount && (
|
|
<div className='absolute -right-8 top-3 rotate-45 bg-red-500 text-white text-xs py-0.5 px-8 font-bold shadow-sm'>
|
|
{t('优惠')}
|
|
</div>
|
|
)}
|
|
<div className='flex flex-col items-center justify-center text-center'>
|
|
<div
|
|
className={`text-xl font-bold mb-1 flex items-center gap-1 ${isSelected ? 'text-blue-600 dark:text-blue-400' : 'text-gray-700 dark:text-gray-200'}`}
|
|
>
|
|
<Coins
|
|
size={18}
|
|
className={
|
|
isSelected
|
|
? 'text-blue-500'
|
|
: 'text-gray-400'
|
|
}
|
|
/>
|
|
{formatLargeNumber(displayValue)}{' '}
|
|
<span className='text-sm font-normal'>
|
|
{symbol}
|
|
</span>
|
|
</div>
|
|
|
|
{hasDiscount && (
|
|
<div className='text-xs font-semibold px-2 py-0.5 bg-green-100 text-green-700 rounded-full mb-2'>
|
|
{t('省')} {symbol}
|
|
{formatNumber(displaySave, 0, '0')}
|
|
</div>
|
|
)}
|
|
|
|
<div className='text-sm text-gray-500'>
|
|
{t('实付')}{' '}
|
|
<span
|
|
className={`font-bold ${isSelected ? 'text-blue-600' : 'text-gray-900 dark:text-gray-100'}`}
|
|
>
|
|
{symbol}
|
|
{formatNumber(displayActualPay, 2, '0.00')}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
</Form.Slot>
|
|
)}
|
|
|
|
{/* Creem 充值区域 */}
|
|
{enableCreemTopUp && creemProducts.length > 0 && (
|
|
<Form.Slot
|
|
label={
|
|
<span className='text-base font-semibold text-gray-700 dark:text-gray-200'>
|
|
{I18N_TECHNICAL_TEXT.CREEM} {t('套餐')}
|
|
</span>
|
|
}
|
|
>
|
|
<div className='grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4'>
|
|
{creemProducts.map((product, index) => (
|
|
<div
|
|
key={index}
|
|
onClick={() => creemPreTopUp(product)}
|
|
className='cursor-pointer rounded-xl border-2 border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 p-5 hover:border-blue-400 hover:shadow-lg transition-all duration-300 group'
|
|
>
|
|
<div className='font-bold text-lg mb-2 text-gray-800 dark:text-gray-200 group-hover:text-blue-600 transition-colors'>
|
|
{product.name}
|
|
</div>
|
|
<div className='flex justify-between items-end'>
|
|
<div className='text-sm text-gray-500'>
|
|
{t('额度')}:{' '}
|
|
<span className='font-medium text-gray-700 dark:text-gray-300'>
|
|
{product.quota}
|
|
</span>
|
|
</div>
|
|
<div className='text-xl font-bold text-blue-600'>
|
|
{product.currency === 'EUR' ? '€' : '¥'}
|
|
{product.price}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</Form.Slot>
|
|
)}
|
|
</div>
|
|
</Form>
|
|
</div>
|
|
) : (
|
|
<Banner
|
|
type='info'
|
|
description={t(
|
|
'管理员未开启在线充值功能,请联系管理员开启或使用兑换码充值。',
|
|
)}
|
|
className='!rounded-xl border-blue-100 bg-blue-50'
|
|
closeIcon={null}
|
|
/>
|
|
)}
|
|
|
|
{/* 兑换码充值 */}
|
|
<div className='bg-white dark:bg-zinc-800 rounded-2xl p-6 border border-dashed border-gray-300 dark:border-gray-600'>
|
|
<div className='flex items-center gap-2 mb-4'>
|
|
<div className='p-2 bg-brand/10 dark:bg-brand-deep/30 rounded-lg text-brand-deep dark:text-brand-from'>
|
|
<IconGift size='large' />
|
|
</div>
|
|
<span className='font-bold text-gray-700 dark:text-gray-200'>
|
|
{t('兑换码充值')}
|
|
</span>
|
|
</div>
|
|
<Form
|
|
getFormApi={(api) => (redeemFormApiRef.current = api)}
|
|
initValues={{ redemptionCode: redemptionCode }}
|
|
className='flex flex-col sm:flex-row gap-4'
|
|
>
|
|
<div className='flex-1'>
|
|
<Form.Input
|
|
field='redemptionCode'
|
|
noLabel={true}
|
|
placeholder={t('请输入您的兑换码')}
|
|
value={redemptionCode}
|
|
onChange={(value) => setRedemptionCode(value)}
|
|
className='w-full'
|
|
size='large'
|
|
showClear
|
|
/>
|
|
</div>
|
|
<Button
|
|
theme='solid'
|
|
type='primary'
|
|
onClick={topUp}
|
|
loading={isSubmitting}
|
|
size='large'
|
|
className='!rounded-lg !px-8 !font-semibold bg-gradient-to-r from-brand-from to-brand-to hover:from-brand-from hover:to-brand-to border-none'
|
|
>
|
|
{t('立即兑换')}
|
|
</Button>
|
|
</Form>
|
|
{topUpLink && (
|
|
<div className='mt-3 text-right'>
|
|
<Text type='tertiary' className='text-sm'>
|
|
{t('没有兑换码?')}
|
|
<span
|
|
className='text-blue-600 hover:text-blue-700 cursor-pointer font-medium ml-1 transition-colors'
|
|
onClick={openTopUpLink}
|
|
>
|
|
{t('去购买')} →
|
|
</span>
|
|
</Text>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</Space>
|
|
</Card>
|
|
);
|
|
};
|
|
|
|
export default RechargeCard;
|