/*
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 .
For commercial licensing, please contact support@quantumnous.com
*/
import React, { useMemo, useState } from 'react';
import {
Badge,
Button,
Card,
Divider,
Select,
Skeleton,
Space,
Tag,
Tooltip,
Typography,
} from '@douyinfe/semi-ui';
import { API, showError, showSuccess, renderQuota } from '../../helpers';
import { getCurrencyConfig } from '../../helpers/render';
import { RefreshCw, Sparkles } from 'lucide-react';
import SubscriptionPurchaseModal from './modals/SubscriptionPurchaseModal';
import {
formatSubscriptionDuration,
formatSubscriptionResetPeriod,
} from '../../helpers/subscriptionFormat';
const { Text } = Typography;
// 过滤易支付方式
function getEpayMethods(payMethods = []) {
return (payMethods || []).filter(
(m) => m?.type && m.type !== 'stripe' && m.type !== 'creem',
);
}
// 提交易支付表单
function submitEpayForm({ url, params }) {
const form = document.createElement('form');
form.action = url;
form.method = 'POST';
const isSafari =
navigator.userAgent.indexOf('Safari') > -1 &&
navigator.userAgent.indexOf('Chrome') < 1;
if (!isSafari) form.target = '_blank';
Object.keys(params || {}).forEach((key) => {
const input = document.createElement('input');
input.type = 'hidden';
input.name = key;
input.value = params[key];
form.appendChild(input);
});
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
}
const SubscriptionPlansCard = ({
t,
loading = false,
plans = [],
payMethods = [],
enableOnlineTopUp = false,
enableStripeTopUp = false,
enableCreemTopUp = false,
billingPreference,
onChangeBillingPreference,
activeSubscriptions = [],
allSubscriptions = [],
reloadSubscriptionSelf,
withCard = true,
}) => {
const [open, setOpen] = useState(false);
const [selectedPlan, setSelectedPlan] = useState(null);
const [paying, setPaying] = useState(false);
const [selectedEpayMethod, setSelectedEpayMethod] = useState('');
const [refreshing, setRefreshing] = useState(false);
const epayMethods = useMemo(() => getEpayMethods(payMethods), [payMethods]);
const openBuy = (p) => {
setSelectedPlan(p);
setSelectedEpayMethod(epayMethods?.[0]?.type || '');
setOpen(true);
};
const closeBuy = () => {
setOpen(false);
setSelectedPlan(null);
setPaying(false);
};
const handleRefresh = async () => {
setRefreshing(true);
try {
await reloadSubscriptionSelf?.();
} finally {
setRefreshing(false);
}
};
const payStripe = async () => {
if (!selectedPlan?.plan?.stripe_price_id) {
showError(t('该套餐未配置 Stripe'));
return;
}
setPaying(true);
try {
const res = await API.post('/api/subscription/stripe/pay', {
plan_id: selectedPlan.plan.id,
});
if (res.data?.message === 'success') {
window.open(res.data.data?.pay_link, '_blank');
showSuccess(t('已打开支付页面'));
closeBuy();
} else {
const errorMsg =
typeof res.data?.data === 'string'
? res.data.data
: res.data?.message || t('支付失败');
showError(errorMsg);
}
} catch (e) {
showError(t('支付请求失败'));
} finally {
setPaying(false);
}
};
const payCreem = async () => {
if (!selectedPlan?.plan?.creem_product_id) {
showError(t('该套餐未配置 Creem'));
return;
}
setPaying(true);
try {
const res = await API.post('/api/subscription/creem/pay', {
plan_id: selectedPlan.plan.id,
});
if (res.data?.message === 'success') {
window.open(res.data.data?.checkout_url, '_blank');
showSuccess(t('已打开支付页面'));
closeBuy();
} else {
const errorMsg =
typeof res.data?.data === 'string'
? res.data.data
: res.data?.message || t('支付失败');
showError(errorMsg);
}
} catch (e) {
showError(t('支付请求失败'));
} finally {
setPaying(false);
}
};
const payEpay = async () => {
if (!selectedEpayMethod) {
showError(t('请选择支付方式'));
return;
}
setPaying(true);
try {
const res = await API.post('/api/subscription/epay/pay', {
plan_id: selectedPlan.plan.id,
payment_method: selectedEpayMethod,
});
if (res.data?.message === 'success') {
submitEpayForm({ url: res.data.url, params: res.data.data });
showSuccess(t('已发起支付'));
closeBuy();
} else {
const errorMsg =
typeof res.data?.data === 'string'
? res.data.data
: res.data?.message || t('支付失败');
showError(errorMsg);
}
} catch (e) {
showError(t('支付请求失败'));
} finally {
setPaying(false);
}
};
// 当前订阅信息 - 支持多个订阅
const hasActiveSubscription = activeSubscriptions.length > 0;
const hasAnySubscription = allSubscriptions.length > 0;
const planPurchaseCountMap = useMemo(() => {
const map = new Map();
(allSubscriptions || []).forEach((sub) => {
const planId = sub?.subscription?.plan_id;
if (!planId) return;
map.set(planId, (map.get(planId) || 0) + 1);
});
return map;
}, [allSubscriptions]);
const planTitleMap = useMemo(() => {
const map = new Map();
(plans || []).forEach((p) => {
const plan = p?.plan;
if (!plan?.id) return;
map.set(plan.id, plan.title || '');
});
return map;
}, [plans]);
const getPlanPurchaseCount = (planId) =>
planPurchaseCountMap.get(planId) || 0;
// 计算单个订阅的剩余天数
const getRemainingDays = (sub) => {
if (!sub?.subscription?.end_time) return 0;
const now = Date.now() / 1000;
const remaining = sub.subscription.end_time - now;
return Math.max(0, Math.ceil(remaining / 86400));
};
// 计算单个订阅的使用进度
const getUsagePercent = (sub) => {
const total = Number(sub?.subscription?.amount_total || 0);
const used = Number(sub?.subscription?.amount_used || 0);
if (total <= 0) return 0;
return Math.round((used / total) * 100);
};
const cardContent = (
<>
{/* 卡片头部 */}
{loading ? (
{/* 我的订阅骨架屏 */}
{/* 套餐列表骨架屏 */}
{[1, 2, 3].map((i) => (
))}
) : (
{/* 当前订阅状态 */}
{t('我的订阅')}
{hasActiveSubscription ? (
}
>
{activeSubscriptions.length} {t('个生效中')}
) : (
{t('无生效')}
)}
{allSubscriptions.length > activeSubscriptions.length && (
{allSubscriptions.length - activeSubscriptions.length}{' '}
{t('个已过期')}
)}
}
onClick={handleRefresh}
loading={refreshing}
/>
{hasAnySubscription ? (
<>
{allSubscriptions.map((sub, subIndex) => {
const isLast = subIndex === allSubscriptions.length - 1;
const subscription = sub.subscription;
const totalAmount = Number(subscription?.amount_total || 0);
const usedAmount = Number(subscription?.amount_used || 0);
const remainAmount =
totalAmount > 0
? Math.max(0, totalAmount - usedAmount)
: 0;
const planTitle =
planTitleMap.get(subscription?.plan_id) || '';
const remainDays = getRemainingDays(sub);
const usagePercent = getUsagePercent(sub);
const now = Date.now() / 1000;
const isExpired = (subscription?.end_time || 0) < now;
const isActive =
subscription?.status === 'active' && !isExpired;
return (
{/* 订阅概要 */}
{planTitle
? `${planTitle} · ${t('订阅')} #${subscription?.id}`
: `${t('订阅')} #${subscription?.id}`}
{isActive ? (
}
>
{t('生效')}
) : (
{t('已过期')}
)}
{isActive && (
{t('剩余')} {remainDays} {t('天')}
)}
{isActive ? t('至') : t('过期于')}{' '}
{new Date(
(subscription?.end_time || 0) * 1000,
).toLocaleString()}
{t('总额度')}:{' '}
{totalAmount > 0 ? (
{renderQuota(usedAmount)}/
{renderQuota(totalAmount)} · {t('剩余')}{' '}
{renderQuota(remainAmount)}
) : (
t('不限')
)}
{totalAmount > 0 && (
{t('已用')} {usagePercent}%
)}
{!isLast &&
}
);
})}
>
) : (
{t('购买套餐后即可享受模型权益')}
)}
{/* 可购买套餐 - 标准定价卡片 */}
{plans.length > 0 ? (
{plans.map((p, index) => {
const plan = p?.plan;
const totalAmount = Number(plan?.total_amount || 0);
const { symbol, rate } = getCurrencyConfig();
const price = Number(plan?.price_amount || 0);
const convertedPrice = price * rate;
const displayPrice = convertedPrice.toFixed(
Number.isInteger(convertedPrice) ? 0 : 2,
);
const isPopular = index === 0 && plans.length > 1;
const limit = Number(plan?.max_purchase_per_user || 0);
const limitLabel = limit > 0 ? `${t('限购')} ${limit}` : null;
const totalLabel =
totalAmount > 0
? `${t('总额度')}: ${renderQuota(totalAmount)}`
: `${t('总额度')}: ${t('不限')}`;
const upgradeLabel = plan?.upgrade_group
? `${t('升级分组')}: ${plan.upgrade_group}`
: null;
const resetLabel =
formatSubscriptionResetPeriod(plan, t) === t('不重置')
? null
: `${t('额度重置')}: ${formatSubscriptionResetPeriod(plan, t)}`;
const planBenefits = [
{
label: `${t('有效期')}: ${formatSubscriptionDuration(plan, t)}`,
},
resetLabel ? { label: resetLabel } : null,
totalAmount > 0
? {
label: totalLabel,
tooltip: `${t('原生额度')}:${totalAmount}`,
}
: { label: totalLabel },
limitLabel ? { label: limitLabel } : null,
upgradeLabel ? { label: upgradeLabel } : null,
].filter(Boolean);
return (
{/* 推荐标签 */}
{isPopular && (
{t('推荐')}
)}
{/* 套餐名称 */}
{plan?.title || t('订阅套餐')}
{plan?.subtitle && (
{plan.subtitle}
)}
{/* 价格区域 */}
{/* 套餐权益描述 */}
{planBenefits.map((item) => {
const content = (
{item.label}
);
if (!item.tooltip) {
return (
{content}
);
}
return (
{content}
);
})}
{/* 购买按钮 */}
{(() => {
const count = getPlanPurchaseCount(p?.plan?.id);
const reached = limit > 0 && count >= limit;
const tip = reached
? t('已达到购买上限') + ` (${count}/${limit})`
: '';
const buttonEl = (
);
return reached ? (
{buttonEl}
) : (
buttonEl
);
})()}
);
})}
) : (
{t('暂无可购买套餐')}
)}
)}
>
);
return (
<>
{withCard ? (
{cardContent}
) : (
{cardContent}
)}
{/* 购买确认弹窗 */}
>
);
};
export default SubscriptionPlansCard;