Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
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 React from 'react';
|
||||
import { Typography } from '@douyinfe/semi-ui';
|
||||
import { getCurrencyConfig } from '../../helpers/render';
|
||||
import { renderQuota } from '../../helpers';
|
||||
import { Sparkles, Gift, Check } from 'lucide-react';
|
||||
|
||||
const { Text } = Typography;
|
||||
|
||||
const AmountCard = ({
|
||||
preset,
|
||||
selected,
|
||||
onClick,
|
||||
topupInfo,
|
||||
priceRatio,
|
||||
t,
|
||||
}) => {
|
||||
const discount =
|
||||
preset.discount || topupInfo?.discount?.[preset.value] || 1.0;
|
||||
const hasDiscount = discount < 1.0;
|
||||
|
||||
// 计算实付金额(人民币)
|
||||
// 新版档位(有 id 字段):amount 已经是人民币金额,直接乘以折扣
|
||||
// 旧版档位或自定义金额:需要乘以汇率
|
||||
const isNewVersion = preset.id !== undefined;
|
||||
const discountedPrice = isNewVersion
|
||||
? preset.value * discount // 新版:人民币金额 * 折扣
|
||||
: preset.value * priceRatio * discount; // 旧版:额度 * 汇率 * 折扣
|
||||
|
||||
// 获取货币显示配置(仅用于显示充值额度的货币符号)
|
||||
const { symbol } = getCurrencyConfig();
|
||||
|
||||
// 获取新版属性
|
||||
const badge = preset.badge;
|
||||
const badgeColor = preset.badgeColor || '#10b981';
|
||||
const bonus = preset.bonus;
|
||||
const label = preset.label;
|
||||
|
||||
return (
|
||||
<div
|
||||
onClick={onClick}
|
||||
className={`
|
||||
group relative cursor-pointer rounded-xl p-3 text-center transition-all duration-300
|
||||
${
|
||||
selected
|
||||
? 'bg-gradient-to-br from-blue-500 to-brand-to text-white shadow-xl shadow-blue-500/30 scale-[1.03]'
|
||||
: 'bg-white/80 dark:bg-neutral-900/60 backdrop-blur-md border border-white/70 dark:border-neutral-700/50 hover:border-blue-300 dark:hover:border-blue-600 hover:shadow-lg hover:shadow-blue-500/10 hover:scale-[1.02]'
|
||||
}
|
||||
`}
|
||||
>
|
||||
{/* 选中指示器 */}
|
||||
{selected && (
|
||||
<div className='absolute top-2 right-2 w-5 h-5 bg-white rounded-full flex items-center justify-center shadow'>
|
||||
<Check size={12} className='text-blue-600' />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 角标:优先显示自定义badge,其次显示折扣 */}
|
||||
{badge ? (
|
||||
<div
|
||||
className='absolute -top-2 -right-2 px-2 py-0.5 rounded-full text-xs font-medium text-white shadow-lg flex items-center gap-1'
|
||||
style={{ backgroundColor: badgeColor }}
|
||||
>
|
||||
<Sparkles size={10} />
|
||||
{badge}
|
||||
</div>
|
||||
) : (
|
||||
hasDiscount && (
|
||||
<div className='absolute -top-2 -right-2 px-2 py-0.5 rounded-full text-xs font-medium bg-gradient-to-r from-emerald-500 to-teal-500 text-white shadow-lg'>
|
||||
{t('{{rate}}折', {
|
||||
rate: (discount * 10).toFixed(1),
|
||||
percent: Math.round(100 - discount * 100),
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
)}
|
||||
|
||||
{/* 金额 - 优先使用label,新版档位显示人民币,旧版显示额度 */}
|
||||
<Text
|
||||
strong
|
||||
className={`text-lg block font-bold ${selected ? '!text-white' : 'bg-clip-text text-transparent bg-gradient-to-r from-blue-600 to-brand-to dark:from-blue-400 dark:to-brand-to'}`}
|
||||
>
|
||||
{label ||
|
||||
(isNewVersion ? `¥${preset.value}` : `${symbol}${preset.value}`)}
|
||||
</Text>
|
||||
|
||||
{/* 实付 - 始终显示人民币金额 */}
|
||||
<Text
|
||||
size='small'
|
||||
className={`block mt-1 ${selected ? '!text-white/80' : 'text-gray-500 dark:text-gray-400'}`}
|
||||
>
|
||||
{t('实付')}{' '}
|
||||
<span className='font-semibold'>¥{discountedPrice.toFixed(2)}</span>
|
||||
</Text>
|
||||
|
||||
{/* 赠送额度 */}
|
||||
{bonus > 0 && (
|
||||
<div
|
||||
className={`mt-1.5 pt-1.5 border-t ${selected ? 'border-white/20' : 'border-gray-100 dark:border-neutral-800'}`}
|
||||
>
|
||||
<div
|
||||
className={`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium ${
|
||||
selected
|
||||
? 'bg-white/20 text-white'
|
||||
: 'bg-gradient-to-r from-amber-100 to-orange-100 dark:from-amber-900/30 dark:to-orange-900/30 text-amber-600 dark:text-amber-400'
|
||||
}`}
|
||||
>
|
||||
<Gift size={10} />+{renderQuota(bonus)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 悬停光效 */}
|
||||
{!selected && (
|
||||
<div className='absolute inset-0 rounded-xl bg-gradient-to-br from-blue-500/0 to-brand-to/0 group-hover:from-blue-500/5 group-hover:to-brand-to/5 transition-all pointer-events-none' />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default AmountCard;
|
||||
@@ -0,0 +1,358 @@
|
||||
/*
|
||||
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 React, { useState, useEffect, useMemo } from 'react';
|
||||
import {
|
||||
Card,
|
||||
Typography,
|
||||
Input,
|
||||
Table,
|
||||
Empty,
|
||||
Button,
|
||||
Modal,
|
||||
Toast,
|
||||
Pagination,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import { IconSearch } from '@douyinfe/semi-icons';
|
||||
import {
|
||||
IllustrationNoResult,
|
||||
IllustrationNoResultDark,
|
||||
} from '../../components/common/illustrations';
|
||||
import {
|
||||
Receipt,
|
||||
Sparkles,
|
||||
Clock,
|
||||
CheckCircle,
|
||||
AlertCircle,
|
||||
XCircle,
|
||||
} from 'lucide-react';
|
||||
import { API, timestamp2string } from '../../helpers';
|
||||
import { isAdmin } from '../../helpers/utils';
|
||||
|
||||
const { Text, Title } = Typography;
|
||||
|
||||
// 状态映射
|
||||
const STATUS_CONFIG = {
|
||||
success: {
|
||||
type: 'success',
|
||||
key: '成功',
|
||||
icon: CheckCircle,
|
||||
color: 'text-emerald-500',
|
||||
},
|
||||
pending: {
|
||||
type: 'warning',
|
||||
key: '待支付',
|
||||
icon: Clock,
|
||||
color: 'text-amber-500',
|
||||
},
|
||||
expired: {
|
||||
type: 'danger',
|
||||
key: '已过期',
|
||||
icon: XCircle,
|
||||
color: 'text-red-500',
|
||||
},
|
||||
};
|
||||
|
||||
const HistoryPanel = ({ t, refreshTrigger }) => {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [topups, setTopups] = useState([]);
|
||||
const [total, setTotal] = useState(0);
|
||||
const [page, setPage] = useState(1);
|
||||
const [pageSize, setPageSize] = useState(10);
|
||||
const [keyword, setKeyword] = useState('');
|
||||
|
||||
const userIsAdmin = useMemo(() => isAdmin(), []);
|
||||
|
||||
// 加载充值记录
|
||||
const loadTopups = async (currentPage, currentPageSize) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const base = userIsAdmin ? '/api/user/topup' : '/api/user/topup/self';
|
||||
const qs =
|
||||
`p=${currentPage}&page_size=${currentPageSize}` +
|
||||
(keyword ? `&keyword=${encodeURIComponent(keyword)}` : '');
|
||||
const endpoint = `${base}?${qs}`;
|
||||
const res = await API.get(endpoint);
|
||||
const { success, message, data } = res.data;
|
||||
if (success) {
|
||||
setTopups(data.items || []);
|
||||
setTotal(data.total || 0);
|
||||
} else {
|
||||
Toast.error({ content: message || t('加载失败') });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Load topups error:', error);
|
||||
Toast.error({ content: t('加载账单失败') });
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
loadTopups(page, pageSize);
|
||||
}, [page, pageSize, keyword, refreshTrigger]);
|
||||
|
||||
// 管理员补单
|
||||
const handleAdminComplete = async (tradeNo) => {
|
||||
try {
|
||||
const res = await API.post('/api/user/topup/complete', {
|
||||
trade_no: tradeNo,
|
||||
});
|
||||
const { success, message } = res.data;
|
||||
if (success) {
|
||||
Toast.success({ content: t('补单成功') });
|
||||
await loadTopups(page, pageSize);
|
||||
} else {
|
||||
Toast.error({ content: message || t('补单失败') });
|
||||
}
|
||||
} catch (e) {
|
||||
Toast.error({ content: t('补单失败') });
|
||||
}
|
||||
};
|
||||
|
||||
const confirmAdminComplete = (tradeNo) => {
|
||||
Modal.confirm({
|
||||
title: t('确认补单'),
|
||||
content: t('是否将该订单标记为成功并为用户入账?'),
|
||||
onOk: () => handleAdminComplete(tradeNo),
|
||||
});
|
||||
};
|
||||
|
||||
// 渲染状态徽章
|
||||
const renderStatusBadge = (status) => {
|
||||
const config = STATUS_CONFIG[status] || {
|
||||
type: 'primary',
|
||||
key: status,
|
||||
icon: AlertCircle,
|
||||
color: 'text-gray-500',
|
||||
};
|
||||
const IconComponent = config.icon;
|
||||
return (
|
||||
<span className={`flex items-center gap-1.5 ${config.color}`}>
|
||||
<IconComponent size={14} />
|
||||
<span className='text-xs font-medium'>{t(config.key)}</span>
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
// 表格列定义
|
||||
const columns = useMemo(() => {
|
||||
const baseColumns = [
|
||||
{
|
||||
title: t('订单号'),
|
||||
dataIndex: 'trade_no',
|
||||
key: 'trade_no',
|
||||
width: 240,
|
||||
render: (text) => (
|
||||
<Text
|
||||
copyable={{ content: text }}
|
||||
size='small'
|
||||
className='font-mono'
|
||||
ellipsis={{ showTooltip: true }}
|
||||
style={{ maxWidth: 200 }}
|
||||
>
|
||||
{text || '-'}
|
||||
</Text>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('支付'),
|
||||
dataIndex: 'money',
|
||||
key: 'money',
|
||||
width: 80,
|
||||
render: (money) => (
|
||||
<Text type='danger' size='small'>
|
||||
¥{money?.toFixed(2)}
|
||||
</Text>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('状态'),
|
||||
dataIndex: 'status',
|
||||
key: 'status',
|
||||
width: 80,
|
||||
render: renderStatusBadge,
|
||||
},
|
||||
{
|
||||
title: t('时间'),
|
||||
dataIndex: 'create_time',
|
||||
key: 'create_time',
|
||||
width: 100,
|
||||
render: (time) => (
|
||||
<Text size='small' type='tertiary'>
|
||||
{timestamp2string(time).slice(5, 16)}
|
||||
</Text>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
// 管理员显示操作列
|
||||
if (userIsAdmin) {
|
||||
baseColumns.push({
|
||||
title: t('操作'),
|
||||
key: 'action',
|
||||
width: 60,
|
||||
render: (_, record) => {
|
||||
if (record.status !== 'pending') return null;
|
||||
return (
|
||||
<Button
|
||||
size='small'
|
||||
theme='outline'
|
||||
onClick={() => confirmAdminComplete(record.trade_no)}
|
||||
>
|
||||
{t('补单')}
|
||||
</Button>
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return baseColumns;
|
||||
}, [t, userIsAdmin]);
|
||||
|
||||
return (
|
||||
<div className='relative'>
|
||||
{/* 外层发光效果 - 与 RechargePanel 对齐 */}
|
||||
<div className='absolute -inset-0.5 bg-gradient-to-br from-emerald-400/20 via-teal-400/20 to-cyan-400/20 rounded-[20px] blur-md opacity-60' />
|
||||
|
||||
<Card
|
||||
className='relative !rounded-2xl shadow-xl border border-white/60 dark:border-neutral-800/60 bg-white/70 dark:bg-neutral-900/70 backdrop-blur-xl overflow-hidden'
|
||||
style={{
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
maxHeight: 'calc(100vh - 160px)',
|
||||
}}
|
||||
bodyStyle={{
|
||||
flex: 1,
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
minHeight: 0,
|
||||
overflow: 'hidden',
|
||||
}}
|
||||
>
|
||||
{/* 顶部装饰线 */}
|
||||
<div className='absolute top-0 left-0 right-0 h-1 bg-gradient-to-r from-emerald-500 via-teal-500 to-cyan-500' />
|
||||
|
||||
{/* 背景装饰 */}
|
||||
<div className='absolute top-0 right-0 w-64 h-64 bg-gradient-to-br from-emerald-400/10 to-teal-400/10 rounded-full blur-3xl -mr-32 -mt-32 pointer-events-none' />
|
||||
<div className='absolute bottom-0 left-0 w-48 h-48 bg-gradient-to-br from-cyan-400/10 to-blue-400/10 rounded-full blur-3xl -ml-24 -mb-24 pointer-events-none' />
|
||||
|
||||
{/* 这层必须是 flex 列并参与父级伸缩:Card 的 bodyStyle 已经是
|
||||
flex-column + minHeight:0 + overflow:hidden,但中间隔着这个普通块级 div,
|
||||
下面表格区的 flex-1/min-h-0 与分页的 flex-shrink-0 就都不是 flex 项了——
|
||||
每页选 20/50 条时表格会顶破 maxHeight 被裁掉,分页控件跟着滚出可视区。 */}
|
||||
<div className='relative flex-1 min-h-0 flex flex-col'>
|
||||
{/* 头部 */}
|
||||
<div className='flex items-center justify-between mb-3 flex-shrink-0'>
|
||||
<div className='flex items-center gap-3'>
|
||||
<div className='relative'>
|
||||
<div className='absolute -inset-1 bg-gradient-to-br from-emerald-500/40 to-teal-500/40 rounded-xl blur-md' />
|
||||
<div className='relative w-10 h-10 rounded-xl bg-gradient-to-br from-emerald-500 to-teal-600 flex items-center justify-center shadow-lg shadow-emerald-500/30'>
|
||||
<Receipt size={20} className='text-white' />
|
||||
</div>
|
||||
<Sparkles
|
||||
size={12}
|
||||
className='absolute -top-1 -right-1 text-amber-400'
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Title
|
||||
heading={5}
|
||||
className='!mb-0 bg-clip-text text-transparent bg-gradient-to-r from-emerald-600 to-teal-600 dark:from-emerald-400 dark:to-teal-400'
|
||||
>
|
||||
{t('充值记录')}
|
||||
</Title>
|
||||
<Text type='tertiary' size='small'>
|
||||
{t('查看历史充值订单')}
|
||||
</Text>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 搜索框 */}
|
||||
<div className='mb-3 flex-shrink-0'>
|
||||
<div className='relative group'>
|
||||
<div className='absolute -inset-0.5 bg-gradient-to-r from-emerald-400/20 to-teal-400/20 rounded-xl blur-sm opacity-0 group-hover:opacity-100 transition-opacity' />
|
||||
<Input
|
||||
prefix={<IconSearch className='text-emerald-500' />}
|
||||
placeholder={t('搜索订单号')}
|
||||
value={keyword}
|
||||
onChange={(value) => {
|
||||
setKeyword(value);
|
||||
setPage(1);
|
||||
}}
|
||||
showClear
|
||||
className='!rounded-xl relative'
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 表格区域 - 可滚动 */}
|
||||
<div className='flex-1 overflow-auto min-h-0 image-list-scroll rounded-xl bg-white/40 dark:bg-neutral-800/30 backdrop-blur-sm border border-white/60 dark:border-neutral-700/40'>
|
||||
<Table
|
||||
columns={columns}
|
||||
dataSource={topups}
|
||||
loading={loading}
|
||||
rowKey='id'
|
||||
size='small'
|
||||
pagination={false}
|
||||
className='history-table'
|
||||
empty={
|
||||
<Empty
|
||||
image={
|
||||
<IllustrationNoResult style={{ width: 100, height: 100 }} />
|
||||
}
|
||||
darkModeImage={
|
||||
<IllustrationNoResultDark
|
||||
style={{ width: 100, height: 100 }}
|
||||
/>
|
||||
}
|
||||
description={t('暂无充值记录')}
|
||||
style={{ padding: 20 }}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 分页器 - 固定在底部 */}
|
||||
{total > 0 && (
|
||||
<div className='flex-shrink-0 pt-3 border-t border-gray-100 dark:border-gray-700 mt-3'>
|
||||
<Pagination
|
||||
total={total}
|
||||
currentPage={page}
|
||||
pageSize={pageSize}
|
||||
showSizeChanger
|
||||
pageSizeOpts={[10, 20, 50]}
|
||||
onPageChange={(p) => setPage(p)}
|
||||
onPageSizeChange={(size) => {
|
||||
setPageSize(size);
|
||||
setPage(1);
|
||||
}}
|
||||
showTotal
|
||||
style={{ justifyContent: 'flex-end' }}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default HistoryPanel;
|
||||
@@ -0,0 +1,989 @@
|
||||
/*
|
||||
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 React, { useEffect, useState, useContext } from 'react';
|
||||
import {
|
||||
Typography,
|
||||
Button,
|
||||
Tag,
|
||||
Modal,
|
||||
Spin,
|
||||
Empty,
|
||||
Banner,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import {
|
||||
Crown,
|
||||
Sparkles,
|
||||
Zap,
|
||||
Calendar,
|
||||
Check,
|
||||
Clock,
|
||||
Shield,
|
||||
Star,
|
||||
FileText,
|
||||
} from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { UserContext } from '../../context/User';
|
||||
import { API, showError, showSuccess, renderQuota } from '../../helpers';
|
||||
import MembershipHistoryModal from '../../components/membership/MembershipHistoryModal';
|
||||
|
||||
const { Text } = Typography;
|
||||
|
||||
// 方案类型图标和颜色映射
|
||||
const planTypeConfig = {
|
||||
weekly: {
|
||||
label: '周卡',
|
||||
color: 'var(--brand-gradient-from)',
|
||||
gradient: 'from-brand-from to-brand',
|
||||
icon: Clock,
|
||||
btnGradient:
|
||||
'linear-gradient(135deg, var(--brand-gradient-from), var(--brand-primary))',
|
||||
},
|
||||
monthly: {
|
||||
label: '月卡',
|
||||
color: 'var(--brand-gradient-to)',
|
||||
gradient: 'from-brand-from to-brand-to',
|
||||
icon: Calendar,
|
||||
btnGradient:
|
||||
'linear-gradient(135deg, var(--brand-gradient-to), var(--brand-primary-hover))',
|
||||
},
|
||||
quarterly: {
|
||||
label: '季卡',
|
||||
color: 'var(--brand-primary-hover)',
|
||||
gradient: 'from-brand-deep to-brand',
|
||||
icon: Crown,
|
||||
btnGradient:
|
||||
'linear-gradient(135deg, var(--brand-primary-hover), var(--brand-primary))',
|
||||
},
|
||||
};
|
||||
|
||||
const tierConfig = {
|
||||
lite: {
|
||||
label: '轻度版',
|
||||
color: 'var(--brand-primary)',
|
||||
desc: '适合偶尔使用',
|
||||
},
|
||||
pro: {
|
||||
label: '重度版',
|
||||
color: 'var(--brand-primary-hover)',
|
||||
desc: '高频使用首选',
|
||||
},
|
||||
};
|
||||
|
||||
const MembershipPanel = () => {
|
||||
const { t } = useTranslation();
|
||||
const [, userDispatch] = useContext(UserContext);
|
||||
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [membershipEnabled, setMembershipEnabled] = useState(false);
|
||||
const [plans, setPlans] = useState([]);
|
||||
const [, setTitle] = useState('会员订阅');
|
||||
const [description, setDescription] = useState('');
|
||||
|
||||
// 当前会员信息
|
||||
const [membershipInfo, setMembershipInfo] = useState(null);
|
||||
const [infoLoading, setInfoLoading] = useState(true);
|
||||
|
||||
// 购买相关
|
||||
const [selectedPlan, setSelectedPlan] = useState(null);
|
||||
const [purchaseModalVisible, setPurchaseModalVisible] = useState(false);
|
||||
const [purchasing, setPurchasing] = useState(false);
|
||||
|
||||
// 升级相关
|
||||
const [upgradeModalVisible, setUpgradeModalVisible] = useState(false);
|
||||
const [upgradeInfo, setUpgradeInfo] = useState(null);
|
||||
const [upgradeLoading, setUpgradeLoading] = useState(false);
|
||||
const [upgrading, setUpgrading] = useState(false);
|
||||
|
||||
// 选中的方案类型 tab
|
||||
const [activeType, setActiveType] = useState('monthly');
|
||||
|
||||
// 会员购买历史弹窗
|
||||
const [historyModalVisible, setHistoryModalVisible] = useState(false);
|
||||
|
||||
// 获取方案列表
|
||||
const fetchPlans = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await API.get('/api/user/membership/plans');
|
||||
const { success, data } = res.data;
|
||||
if (success) {
|
||||
setMembershipEnabled(data.membership_enabled);
|
||||
setPlans(data.plans || []);
|
||||
setTitle(data.title || '会员订阅');
|
||||
setDescription(data.description || '');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('获取会员方案失败:', err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 获取当前会员信息
|
||||
const fetchMembershipInfo = async () => {
|
||||
setInfoLoading(true);
|
||||
try {
|
||||
const res = await API.get('/api/user/membership/info');
|
||||
const { success, data } = res.data;
|
||||
if (success) {
|
||||
setMembershipInfo(data);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('获取会员信息失败:', err);
|
||||
} finally {
|
||||
setInfoLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 购买会员
|
||||
const handlePurchase = async () => {
|
||||
if (!selectedPlan) return;
|
||||
setPurchasing(true);
|
||||
try {
|
||||
const res = await API.post('/api/user/membership/purchase', {
|
||||
plan_id: selectedPlan.id,
|
||||
payment_method: 'balance',
|
||||
});
|
||||
const { success, message } = res.data;
|
||||
if (success) {
|
||||
showSuccess(t('购买成功!'));
|
||||
setPurchaseModalVisible(false);
|
||||
setSelectedPlan(null);
|
||||
fetchMembershipInfo();
|
||||
// 刷新用户信息
|
||||
try {
|
||||
const userRes = await API.get('/api/user/self');
|
||||
if (userRes.data.success) {
|
||||
userDispatch({ type: 'login', payload: userRes.data.data });
|
||||
}
|
||||
} catch (e) {}
|
||||
} else {
|
||||
showError(message);
|
||||
}
|
||||
} catch (err) {
|
||||
showError(t('购买失败'));
|
||||
} finally {
|
||||
setPurchasing(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 检查是否为升级场景并获取差价
|
||||
const checkUpgrade = async (plan) => {
|
||||
if (!membershipInfo?.has_membership) return false;
|
||||
if (plan.daily_quota <= membershipInfo.daily_quota) return false;
|
||||
if (plan.plan_type !== membershipInfo.plan_type) return false; // 只能同类型内升级
|
||||
|
||||
setUpgradeLoading(true);
|
||||
try {
|
||||
const res = await API.get(
|
||||
`/api/user/membership/upgrade/cost?plan_id=${plan.id}`,
|
||||
);
|
||||
const { success, data } = res.data;
|
||||
if (success && data.can_upgrade) {
|
||||
setUpgradeInfo(data);
|
||||
return true;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('查询升级信息失败:', err);
|
||||
} finally {
|
||||
setUpgradeLoading(false);
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
// 判断是否为当前正在使用的同款方案(匹配 tier + plan_type,可续费但不可升级)
|
||||
const isCurrentPlanCheck = (plan) => {
|
||||
return (
|
||||
membershipInfo?.has_membership &&
|
||||
membershipInfo?.daily_quota === plan.daily_quota &&
|
||||
membershipInfo?.tier === plan.tier &&
|
||||
membershipInfo?.plan_type === plan.plan_type
|
||||
);
|
||||
};
|
||||
|
||||
// 判断是否可以升级(同一时长类型内,更高档位)
|
||||
const canUpgradeCheck = (plan) => {
|
||||
return (
|
||||
membershipInfo?.has_membership &&
|
||||
plan.daily_quota > (membershipInfo?.daily_quota || 0) &&
|
||||
plan.plan_type === membershipInfo?.plan_type
|
||||
);
|
||||
};
|
||||
|
||||
// 判断是否为降级(同一时长类型内,更低档位)
|
||||
const isDowngradeCheck = (plan) => {
|
||||
return (
|
||||
membershipInfo?.has_membership &&
|
||||
plan.plan_type === membershipInfo?.plan_type &&
|
||||
plan.daily_quota < (membershipInfo?.daily_quota || 0)
|
||||
);
|
||||
};
|
||||
|
||||
// 判断该方案是否已有未来待生效的续费
|
||||
const hasFutureForPlan = (plan) => {
|
||||
return (
|
||||
membershipInfo?.has_future_membership &&
|
||||
membershipInfo?.future_plan_type === plan.plan_type &&
|
||||
membershipInfo?.future_tier === plan.tier
|
||||
);
|
||||
};
|
||||
|
||||
// 点击方案卡片
|
||||
const handlePlanClick = async (plan) => {
|
||||
setSelectedPlan(plan);
|
||||
// 判断是否为升级场景(同一时长类型内,且每日额度更高)
|
||||
if (canUpgradeCheck(plan)) {
|
||||
const isUpgrade = await checkUpgrade(plan);
|
||||
if (isUpgrade) {
|
||||
setUpgradeModalVisible(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 普通购买 / 续期(包括同款方案续费)
|
||||
setPurchaseModalVisible(true);
|
||||
};
|
||||
|
||||
// 执行升级
|
||||
const handleUpgrade = async () => {
|
||||
if (!selectedPlan) return;
|
||||
setUpgrading(true);
|
||||
try {
|
||||
const res = await API.post('/api/user/membership/upgrade', {
|
||||
plan_id: selectedPlan.id,
|
||||
});
|
||||
const { success, message } = res.data;
|
||||
if (success) {
|
||||
showSuccess(message || '升级成功!');
|
||||
setUpgradeModalVisible(false);
|
||||
setSelectedPlan(null);
|
||||
setUpgradeInfo(null);
|
||||
fetchMembershipInfo();
|
||||
// 刷新用户信息
|
||||
try {
|
||||
const userRes = await API.get('/api/user/self');
|
||||
if (userRes.data.success) {
|
||||
userDispatch({ type: 'login', payload: userRes.data.data });
|
||||
}
|
||||
} catch (e) {}
|
||||
} else {
|
||||
showError(message);
|
||||
}
|
||||
} catch (err) {
|
||||
showError(t('升级失败'));
|
||||
} finally {
|
||||
setUpgrading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchPlans();
|
||||
fetchMembershipInfo();
|
||||
}, []);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className='flex items-center justify-center py-20'>
|
||||
<Spin size='large' />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!membershipEnabled) {
|
||||
return (
|
||||
<div className='py-12'>
|
||||
<Empty
|
||||
title={t('会员功能未开启')}
|
||||
description={t('管理员尚未启用会员订阅功能')}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// 按类型分组方案
|
||||
const plansByType = {};
|
||||
plans.forEach((plan) => {
|
||||
if (!plansByType[plan.plan_type]) {
|
||||
plansByType[plan.plan_type] = [];
|
||||
}
|
||||
plansByType[plan.plan_type].push(plan);
|
||||
});
|
||||
|
||||
const typeKeys = Object.keys(plansByType);
|
||||
|
||||
// 格式化日期
|
||||
const formatDate = (timestamp) => {
|
||||
return new Date(timestamp * 1000).toLocaleDateString('zh-CN');
|
||||
};
|
||||
|
||||
return (
|
||||
<div className='space-y-4'>
|
||||
{/* 当前会员状态卡片 */}
|
||||
{!infoLoading && membershipInfo?.has_membership && (
|
||||
<div
|
||||
className='relative overflow-hidden rounded-2xl p-5 text-white shadow-2xl'
|
||||
style={{
|
||||
background:
|
||||
'linear-gradient(135deg, color-mix(in srgb, var(--brand-primary-hover) 35%, #0b1020) 0%, color-mix(in srgb, var(--brand-primary-hover) 55%, #161029) 30%, color-mix(in srgb, var(--brand-gradient-to) 70%, #211038) 60%, var(--brand-primary-hover) 100%)',
|
||||
}}
|
||||
>
|
||||
{/* 背景装饰 */}
|
||||
<div className='absolute top-0 right-0 w-64 h-64 bg-brand/15 rounded-full blur-3xl' />
|
||||
<div className='absolute -bottom-16 -left-16 w-48 h-48 bg-brand-from/10 rounded-full blur-3xl' />
|
||||
<div className='absolute top-1/2 right-1/4 w-24 h-24 bg-brand-from/10 rounded-full blur-2xl' />
|
||||
|
||||
<div className='relative'>
|
||||
<div className='flex items-center justify-between mb-3.5'>
|
||||
<div className='flex items-center gap-2.5'>
|
||||
<div className='w-8 h-8 rounded-lg bg-gradient-to-br from-yellow-400 to-amber-500 flex items-center justify-center shadow-lg shadow-amber-500/30'>
|
||||
<Crown size={18} className='text-white' />
|
||||
</div>
|
||||
<span className='text-white font-semibold text-base tracking-wide'>
|
||||
{t('当前会员')}
|
||||
</span>
|
||||
</div>
|
||||
<div className='flex items-center gap-2'>
|
||||
<span className='inline-flex items-center px-2.5 py-1 rounded-md text-xs font-medium bg-white/15 text-white backdrop-blur-sm border border-white/20'>
|
||||
{membershipInfo.plan_name}
|
||||
</span>
|
||||
<button
|
||||
onClick={() => setHistoryModalVisible(true)}
|
||||
className='inline-flex items-center gap-1.5 px-2.5 py-1 rounded-md text-xs font-medium bg-white/10 text-white/90 backdrop-blur-sm border border-white/15 hover:bg-white/20 transition-all duration-200'
|
||||
>
|
||||
<FileText size={12} />
|
||||
{t('查看记录')}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className='grid grid-cols-2 gap-3 mb-3.5'>
|
||||
<div className='p-3 rounded-xl bg-white/10 backdrop-blur-sm border border-white/10'>
|
||||
<span className='text-white/70 text-xs block mb-1'>
|
||||
{t('今日剩余额度')}
|
||||
</span>
|
||||
<span
|
||||
className='text-white font-bold text-xl block'
|
||||
style={{ textShadow: '0 1px 2px rgba(0,0,0,0.2)' }}
|
||||
>
|
||||
{renderQuota(membershipInfo.membership_quota)}
|
||||
</span>
|
||||
</div>
|
||||
<div className='p-3 rounded-xl bg-white/10 backdrop-blur-sm border border-white/10'>
|
||||
<span className='text-white/70 text-xs block mb-1'>
|
||||
{t('每日额度')}
|
||||
</span>
|
||||
<span
|
||||
className='text-white font-bold text-xl block'
|
||||
style={{ textShadow: '0 1px 2px rgba(0,0,0,0.2)' }}
|
||||
>
|
||||
{renderQuota(membershipInfo.daily_quota)}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 额度使用进度 */}
|
||||
{membershipInfo.daily_quota > 0 && (
|
||||
<div className='mb-3'>
|
||||
<div className='flex justify-between text-xs text-white/60 mb-1.5'>
|
||||
<span>{t('今日用量')}</span>
|
||||
<span>
|
||||
{Math.round(
|
||||
(membershipInfo.membership_quota /
|
||||
membershipInfo.daily_quota) *
|
||||
100,
|
||||
)}
|
||||
%
|
||||
</span>
|
||||
</div>
|
||||
<div className='w-full h-2 bg-white/10 rounded-full overflow-hidden'>
|
||||
<div
|
||||
className='h-full rounded-full transition-all duration-500'
|
||||
style={{
|
||||
width: `${Math.round((membershipInfo.membership_quota / membershipInfo.daily_quota) * 100)}%`,
|
||||
background:
|
||||
'linear-gradient(90deg, var(--brand-gradient-from), var(--brand-gradient-via), var(--brand-gradient-to))',
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className='flex items-center justify-between text-white/70 text-xs'>
|
||||
<span className='flex items-center gap-1.5'>
|
||||
<Calendar size={12} />
|
||||
{t('到期日')}: {formatDate(membershipInfo.end_date)}
|
||||
</span>
|
||||
<span className='flex items-center gap-1.5 px-2 py-0.5 rounded-full bg-white/10'>
|
||||
<Clock size={12} />
|
||||
{t('剩余')} {membershipInfo.remaining_days} {t('天')}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* 已续费提示 */}
|
||||
{membershipInfo.has_future_membership && (
|
||||
<div className='mt-3 pt-2.5 border-t border-white/15'>
|
||||
<div className='flex items-center gap-2 px-3 py-2 rounded-lg bg-emerald-500/15 border border-emerald-400/20'>
|
||||
<Check size={14} className='text-emerald-300 flex-shrink-0' />
|
||||
<span className='text-white text-xs'>
|
||||
{t('已续费')}:{membershipInfo.future_plan_name},
|
||||
{formatDate(membershipInfo.future_start_date)} ~{' '}
|
||||
{formatDate(membershipInfo.future_end_date)} {t('生效')}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 描述提示 */}
|
||||
{description && (
|
||||
<Banner type='info' description={description} closeIcon={null} />
|
||||
)}
|
||||
|
||||
{/* 查看购买历史按钮 - 当没有会员时也显示 */}
|
||||
{!membershipInfo?.has_membership && (
|
||||
<div className='flex justify-end -mt-1'>
|
||||
<Button
|
||||
theme='borderless'
|
||||
icon={<FileText size={16} />}
|
||||
onClick={() => setHistoryModalVisible(true)}
|
||||
className='text-gray-600 dark:text-gray-400 hover:text-brand-deep dark:hover:text-brand-from'
|
||||
>
|
||||
{t('查看购买记录')}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 方案类型切换 */}
|
||||
{typeKeys.length > 1 && (
|
||||
<div className='flex items-center gap-1.5 p-1.5 bg-gray-100/80 dark:bg-neutral-800/80 rounded-2xl backdrop-blur-sm border border-gray-200/50 dark:border-neutral-700/50'>
|
||||
{typeKeys.map((type) => {
|
||||
const config = planTypeConfig[type] || {
|
||||
label: type,
|
||||
gradient: 'from-gray-500 to-gray-600',
|
||||
};
|
||||
const TypeIcon = config.icon || Clock;
|
||||
const isActive = activeType === type;
|
||||
return (
|
||||
<button
|
||||
key={type}
|
||||
onClick={() => setActiveType(type)}
|
||||
className={`
|
||||
flex-1 flex items-center justify-center gap-2 py-2.5 px-4 rounded-xl text-sm font-semibold transition-all duration-300
|
||||
${
|
||||
isActive
|
||||
? `bg-gradient-to-r ${config.gradient} text-white shadow-lg transform scale-[1.02]`
|
||||
: 'text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-300 hover:bg-white/60 dark:hover:bg-neutral-700/60'
|
||||
}
|
||||
`}
|
||||
>
|
||||
<TypeIcon size={16} />
|
||||
{config.label}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 方案卡片列表 */}
|
||||
<div className='grid grid-cols-1 sm:grid-cols-2 gap-4'>
|
||||
{(plansByType[activeType] || []).map((plan) => {
|
||||
const typeConf = planTypeConfig[plan.plan_type] || {};
|
||||
const tierConf = tierConfig[plan.tier] || {};
|
||||
const isSelected = selectedPlan?.id === plan.id;
|
||||
const isRecommend = plan.badge || plan.is_recommend;
|
||||
|
||||
return (
|
||||
<div
|
||||
key={plan.id}
|
||||
onClick={() => handlePlanClick(plan)}
|
||||
className={`
|
||||
group relative cursor-pointer rounded-2xl transition-all duration-300 overflow-hidden
|
||||
${
|
||||
isSelected
|
||||
? 'ring-2 ring-brand shadow-xl shadow-brand/20 scale-[1.02]'
|
||||
: 'hover:shadow-xl hover:shadow-gray-200/50 dark:hover:shadow-neutral-900/50 hover:-translate-y-1'
|
||||
}
|
||||
${
|
||||
isRecommend
|
||||
? 'bg-gradient-to-br from-white to-brand-to/50 dark:from-neutral-900 dark:to-brand-to/30 border-2 border-brand/20 dark:border-brand-deep/50'
|
||||
: 'bg-white dark:bg-neutral-900 border border-gray-200/80 dark:border-neutral-700/80'
|
||||
}
|
||||
`}
|
||||
>
|
||||
{/* 推荐角标 */}
|
||||
{plan.badge && (
|
||||
<div
|
||||
className='absolute top-0 right-0 px-3 py-1.5 rounded-bl-2xl text-xs font-bold text-white shadow-lg flex items-center gap-1'
|
||||
style={{
|
||||
background: plan.badge_color
|
||||
? `linear-gradient(135deg, ${plan.badge_color}, ${plan.badge_color}dd)`
|
||||
: 'linear-gradient(135deg, var(--brand-primary), var(--brand-gradient-to))',
|
||||
}}
|
||||
>
|
||||
<Star size={10} className='fill-current' />
|
||||
{plan.badge}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className='p-4'>
|
||||
{/* 方案头部 */}
|
||||
<div className='flex items-center gap-3 mb-3'>
|
||||
<div
|
||||
className={`w-10 h-10 rounded-xl bg-gradient-to-br ${typeConf.gradient || 'from-gray-500 to-gray-600'} flex items-center justify-center shadow-lg`}
|
||||
>
|
||||
<Zap size={18} className='text-white' />
|
||||
</div>
|
||||
<div>
|
||||
<Text strong className='text-base block leading-tight'>
|
||||
{plan.name}
|
||||
</Text>
|
||||
<Text className='text-xs text-gray-400 mt-0.5 block'>
|
||||
{plan.duration_days}
|
||||
{t('天')} · {tierConf.label}
|
||||
</Text>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 价格 */}
|
||||
<div className='mb-3'>
|
||||
<span className='text-sm font-medium text-gray-400 mr-0.5'>
|
||||
¥
|
||||
</span>
|
||||
<span
|
||||
className='text-4xl font-extrabold tracking-tight'
|
||||
style={{ color: typeConf.color || 'var(--brand-primary)' }}
|
||||
>
|
||||
{plan.price}
|
||||
</span>
|
||||
<span className='text-sm text-gray-400 ml-1 font-medium'>
|
||||
/{typeConf.label}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* 每日额度 - 高亮区域 */}
|
||||
<div className='flex items-center gap-2 mb-3 py-2 px-3 rounded-xl bg-gradient-to-r from-emerald-50 to-teal-50 dark:from-emerald-900/20 dark:to-teal-900/20 border border-emerald-100 dark:border-emerald-800/30'>
|
||||
<Sparkles
|
||||
size={15}
|
||||
className='text-emerald-500 flex-shrink-0'
|
||||
/>
|
||||
<span className='text-sm text-gray-600 dark:text-gray-300'>
|
||||
{t('每日到账')}{' '}
|
||||
<strong className='text-emerald-600 dark:text-emerald-400'>
|
||||
{renderQuota(plan.daily_quota)}
|
||||
</strong>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* 描述 */}
|
||||
{plan.description && (
|
||||
<Text className='text-xs text-gray-400 block mb-2'>
|
||||
{plan.description}
|
||||
</Text>
|
||||
)}
|
||||
|
||||
{/* 功能点 */}
|
||||
<div className='space-y-1.5 mb-3.5'>
|
||||
<div className='flex items-center gap-2.5 text-xs text-gray-500 dark:text-gray-400'>
|
||||
<div className='w-4 h-4 rounded-full bg-green-100 dark:bg-green-900/30 flex items-center justify-center flex-shrink-0'>
|
||||
<Check
|
||||
size={10}
|
||||
className='text-green-600 dark:text-green-400'
|
||||
/>
|
||||
</div>
|
||||
<span>{t('每日自动重置额度')}</span>
|
||||
</div>
|
||||
<div className='flex items-center gap-2.5 text-xs text-gray-500 dark:text-gray-400'>
|
||||
<div className='w-4 h-4 rounded-full bg-green-100 dark:bg-green-900/30 flex items-center justify-center flex-shrink-0'>
|
||||
<Check
|
||||
size={10}
|
||||
className='text-green-600 dark:text-green-400'
|
||||
/>
|
||||
</div>
|
||||
<span>{t('会员额度优先消耗')}</span>
|
||||
</div>
|
||||
<div className='flex items-center gap-2.5 text-xs text-gray-500 dark:text-gray-400'>
|
||||
<div className='w-4 h-4 rounded-full bg-blue-100 dark:bg-blue-900/30 flex items-center justify-center flex-shrink-0'>
|
||||
<Shield
|
||||
size={10}
|
||||
className='text-blue-600 dark:text-blue-400'
|
||||
/>
|
||||
</div>
|
||||
<span>{t('用完自动切换账户余额')}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 立即订阅/升级/续费按钮 */}
|
||||
{(() => {
|
||||
const isCurrentPlan = isCurrentPlanCheck(plan);
|
||||
const canUpgrade = canUpgradeCheck(plan);
|
||||
const isDowngrade = isDowngradeCheck(plan);
|
||||
const hasFuture = hasFutureForPlan(plan);
|
||||
// 按钮文案和样式 - 跟随卡片类型色调
|
||||
let btnText = t('立即订阅');
|
||||
let btnBg =
|
||||
typeConf.btnGradient ||
|
||||
'linear-gradient(135deg, var(--brand-primary), var(--brand-primary-hover))';
|
||||
let subText = '';
|
||||
if (isCurrentPlan) {
|
||||
btnText = hasFuture ? t('再次续费') : t('续费');
|
||||
} else if (canUpgrade) {
|
||||
btnText = t('立即升级');
|
||||
subText = t('立即生效,补差价');
|
||||
} else if (isDowngrade) {
|
||||
subText = t('到期后生效');
|
||||
} else if (membershipInfo?.has_membership) {
|
||||
subText = t('到期后生效');
|
||||
}
|
||||
return (
|
||||
<div>
|
||||
<button
|
||||
className='w-full py-2.5 rounded-xl text-white text-sm font-semibold shadow-lg hover:shadow-xl transition-all duration-300 hover:opacity-90 active:scale-[0.98]'
|
||||
style={{
|
||||
background: btnBg,
|
||||
}}
|
||||
>
|
||||
{btnText}
|
||||
</button>
|
||||
{subText && (
|
||||
<span className='text-xs text-gray-400 text-center block mt-1.5'>
|
||||
{subText}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})()}
|
||||
</div>
|
||||
|
||||
{/* 悬停光效 */}
|
||||
<div className='absolute inset-0 rounded-2xl bg-gradient-to-br from-brand-from/0 to-brand-to/0 group-hover:from-brand-from/[0.03] group-hover:to-brand-to/[0.06] transition-all duration-500 pointer-events-none' />
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
{plans.length === 0 && (
|
||||
<Empty
|
||||
title={t('暂无可用方案')}
|
||||
description={t('管理员尚未配置会员方案')}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 购买确认弹窗 */}
|
||||
<Modal
|
||||
title={
|
||||
<div className='flex items-center gap-2'>
|
||||
<Crown size={20} className='text-amber-500' />
|
||||
{selectedPlan && isCurrentPlanCheck(selectedPlan)
|
||||
? t('确认续费会员')
|
||||
: t('确认购买会员')}
|
||||
</div>
|
||||
}
|
||||
visible={purchaseModalVisible}
|
||||
onOk={handlePurchase}
|
||||
onCancel={() => {
|
||||
setPurchaseModalVisible(false);
|
||||
setSelectedPlan(null);
|
||||
}}
|
||||
confirmLoading={purchasing}
|
||||
okText={
|
||||
selectedPlan && isCurrentPlanCheck(selectedPlan)
|
||||
? t('确认续费')
|
||||
: t('确认购买')
|
||||
}
|
||||
cancelText={t('取消')}
|
||||
centered
|
||||
maskClosable={false}
|
||||
>
|
||||
{selectedPlan && (
|
||||
<div className='space-y-4 py-2'>
|
||||
<div className='p-4 rounded-xl bg-gradient-to-r from-gray-50 to-gray-100 dark:from-neutral-800 dark:to-neutral-900'>
|
||||
<div className='flex justify-between items-center mb-2'>
|
||||
<Text strong className='text-lg'>
|
||||
{selectedPlan.name}
|
||||
</Text>
|
||||
<Tag color='blue' size='small'>
|
||||
{selectedPlan.duration_days}
|
||||
{t('天')}
|
||||
</Tag>
|
||||
</div>
|
||||
<div className='flex justify-between items-center mb-2'>
|
||||
<Text className='text-gray-500'>{t('每日到账额度')}</Text>
|
||||
<Text strong className='text-emerald-600'>
|
||||
{renderQuota(selectedPlan.daily_quota)}
|
||||
</Text>
|
||||
</div>
|
||||
<div className='flex justify-between items-center'>
|
||||
<Text className='text-gray-500'>{t('总计到账')}</Text>
|
||||
<Text strong className='text-blue-600'>
|
||||
{renderQuota(
|
||||
selectedPlan.daily_quota * selectedPlan.duration_days,
|
||||
)}
|
||||
</Text>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className='flex justify-between items-center pt-2 border-t border-gray-200 dark:border-neutral-700'>
|
||||
<Text className='text-gray-500'>{t('支付方式')}</Text>
|
||||
<Text strong>{t('账户余额抵扣')}</Text>
|
||||
</div>
|
||||
|
||||
<div className='flex justify-between items-center'>
|
||||
<Text className='text-gray-500'>{t('需支付')}</Text>
|
||||
<Text strong className='text-2xl text-red-500'>
|
||||
¥{selectedPlan.price}
|
||||
</Text>
|
||||
</div>
|
||||
|
||||
{/* 场景化提示 */}
|
||||
{(() => {
|
||||
if (!membershipInfo?.has_membership) {
|
||||
// 新用户,无会员
|
||||
return (
|
||||
<Banner
|
||||
type='info'
|
||||
description={t(
|
||||
'购买后会员立即生效,每日额度将于每天自动重置到账。',
|
||||
)}
|
||||
closeIcon={null}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const isRenewal = isCurrentPlanCheck(selectedPlan);
|
||||
const isDowngrade = isDowngradeCheck(selectedPlan);
|
||||
|
||||
return (
|
||||
<>
|
||||
{/* 续费同款方案 */}
|
||||
{isRenewal && (
|
||||
<Banner
|
||||
type='success'
|
||||
description={
|
||||
t('续费提示') +
|
||||
':' +
|
||||
t('您将续费当前「') +
|
||||
membershipInfo.plan_name +
|
||||
t('」,新的 ') +
|
||||
selectedPlan.duration_days +
|
||||
t(' 天会员将在当前会员到期后(') +
|
||||
formatDate(membershipInfo.end_date) +
|
||||
t(')自动生效,届时每日额度保持不变。')
|
||||
}
|
||||
closeIcon={null}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 购买不同类型的方案(非升级、非同款) */}
|
||||
{!isRenewal && !isDowngrade && (
|
||||
<Banner
|
||||
type='info'
|
||||
description={
|
||||
t('您当前有「') +
|
||||
membershipInfo.plan_name +
|
||||
t('」会员(到期日:') +
|
||||
formatDate(membershipInfo.end_date) +
|
||||
t(')。新购买的「') +
|
||||
selectedPlan.name +
|
||||
t('」将在当前会员到期后开始生效。')
|
||||
}
|
||||
closeIcon={null}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 降级方案提示 */}
|
||||
{isDowngrade && (
|
||||
<Banner
|
||||
type='warning'
|
||||
description={
|
||||
t('注意:此方案的每日额度(') +
|
||||
renderQuota(selectedPlan.daily_quota) +
|
||||
t(')低于您当前方案(') +
|
||||
renderQuota(membershipInfo.daily_quota) +
|
||||
t(
|
||||
')。新会员将在当前会员到期后生效,届时每日额度将降低。如需立即提升额度,建议使用「升级」功能。',
|
||||
)
|
||||
}
|
||||
closeIcon={null}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 同类型升级提示(用户通过购买而非升级入口来的) */}
|
||||
{!isRenewal &&
|
||||
!isDowngrade &&
|
||||
canUpgradeCheck(selectedPlan) && (
|
||||
<Banner
|
||||
type='info'
|
||||
description={t(
|
||||
'提示:此方案额度高于您当前方案,您也可以选择「升级」,升级后立即生效且只需补差价。',
|
||||
)}
|
||||
closeIcon={null}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 已有未来待生效的续费 */}
|
||||
{membershipInfo.has_future_membership && (
|
||||
<Banner
|
||||
type='warning'
|
||||
description={
|
||||
t('您已有一个待生效的续费会员「') +
|
||||
membershipInfo.future_plan_name +
|
||||
t('」(') +
|
||||
formatDate(membershipInfo.future_start_date) +
|
||||
t(
|
||||
' 生效)。再次购买将在其到期后生效,请确认是否需要继续。',
|
||||
)
|
||||
}
|
||||
closeIcon={null}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
})()}
|
||||
|
||||
<Banner
|
||||
type='warning'
|
||||
description={t(
|
||||
'购买后将从您的账户余额中扣除对应额度,会员每日额度不累加,当日未使用次日重置。',
|
||||
)}
|
||||
closeIcon={null}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</Modal>
|
||||
|
||||
{/* 升级确认弹窗 */}
|
||||
<Modal
|
||||
title={
|
||||
<div className='flex items-center gap-2'>
|
||||
<Zap size={20} className='text-orange-500' />
|
||||
{t('确认升级会员')}
|
||||
</div>
|
||||
}
|
||||
visible={upgradeModalVisible}
|
||||
onOk={handleUpgrade}
|
||||
onCancel={() => {
|
||||
setUpgradeModalVisible(false);
|
||||
setSelectedPlan(null);
|
||||
setUpgradeInfo(null);
|
||||
}}
|
||||
confirmLoading={upgrading}
|
||||
okText={t('确认升级')}
|
||||
cancelText={t('取消')}
|
||||
centered
|
||||
maskClosable={false}
|
||||
>
|
||||
{selectedPlan && upgradeInfo && (
|
||||
<div className='space-y-4 py-2'>
|
||||
{/* 升级对比 */}
|
||||
<div className='p-4 rounded-xl bg-gradient-to-r from-orange-50 to-amber-50 dark:from-orange-900/20 dark:to-amber-900/20'>
|
||||
<div className='flex items-center justify-between mb-3'>
|
||||
<div className='text-center flex-1'>
|
||||
<Text className='text-xs text-gray-400 block mb-1'>
|
||||
{t('当前方案')}
|
||||
</Text>
|
||||
<Text strong className='text-sm block'>
|
||||
{upgradeInfo.current_plan}
|
||||
</Text>
|
||||
<Text className='text-xs text-gray-500 block mt-1'>
|
||||
{t('每日')} {renderQuota(upgradeInfo.current_daily)}
|
||||
</Text>
|
||||
</div>
|
||||
<div className='mx-3 flex flex-col items-center'>
|
||||
<Zap size={20} className='text-orange-500' />
|
||||
<Text className='text-xs text-orange-500 mt-1'>
|
||||
{t('升级')}
|
||||
</Text>
|
||||
</div>
|
||||
<div className='text-center flex-1'>
|
||||
<Text className='text-xs text-gray-400 block mb-1'>
|
||||
{t('升级方案')}
|
||||
</Text>
|
||||
<Text strong className='text-sm block text-orange-600'>
|
||||
{upgradeInfo.new_plan}
|
||||
</Text>
|
||||
<Text className='text-xs text-orange-500 block mt-1'>
|
||||
{t('每日')} {renderQuota(upgradeInfo.new_daily)}
|
||||
</Text>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 差价明细 */}
|
||||
<div className='p-4 rounded-xl bg-gradient-to-r from-gray-50 to-gray-100 dark:from-neutral-800 dark:to-neutral-900'>
|
||||
<div className='flex justify-between items-center mb-2'>
|
||||
<Text className='text-gray-500'>{t('剩余会员天数')}</Text>
|
||||
<Text strong>
|
||||
{upgradeInfo.remaining_days} {t('天')}
|
||||
</Text>
|
||||
</div>
|
||||
<div className='flex justify-between items-center mb-2'>
|
||||
<Text className='text-gray-500'>{t('升级差价')}</Text>
|
||||
<Text strong className='text-orange-600'>
|
||||
¥{upgradeInfo.upgrade_cost}
|
||||
</Text>
|
||||
</div>
|
||||
<div className='flex justify-between items-center'>
|
||||
<Text className='text-gray-500'>{t('需补缴额度')}</Text>
|
||||
<Text strong className='text-blue-600'>
|
||||
{renderQuota(upgradeInfo.quota_needed)}
|
||||
</Text>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className='flex justify-between items-center pt-2 border-t border-gray-200 dark:border-neutral-700'>
|
||||
<Text className='text-gray-500'>{t('支付方式')}</Text>
|
||||
<Text strong>{t('账户余额抵扣')}</Text>
|
||||
</div>
|
||||
|
||||
<div className='flex justify-between items-center'>
|
||||
<Text className='text-gray-500'>{t('需支付')}</Text>
|
||||
<Text strong className='text-2xl text-red-500'>
|
||||
¥{upgradeInfo.upgrade_cost}
|
||||
</Text>
|
||||
</div>
|
||||
|
||||
<Banner
|
||||
type='info'
|
||||
description={t(
|
||||
'升级后立即生效,今日额度将重置为新方案额度。到期日期不变。',
|
||||
)}
|
||||
closeIcon={null}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
{upgradeLoading && (
|
||||
<div className='flex items-center justify-center py-8'>
|
||||
<Spin size='large' />
|
||||
</div>
|
||||
)}
|
||||
</Modal>
|
||||
|
||||
{/* 会员购买历史弹窗 */}
|
||||
<MembershipHistoryModal
|
||||
visible={historyModalVisible}
|
||||
onClose={() => setHistoryModalVisible(false)}
|
||||
t={t}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default MembershipPanel;
|
||||
@@ -0,0 +1,544 @@
|
||||
/*
|
||||
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 React from 'react';
|
||||
import {
|
||||
Card,
|
||||
Typography,
|
||||
Button,
|
||||
Input,
|
||||
Spin,
|
||||
Tooltip,
|
||||
Skeleton,
|
||||
Banner,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import { IconGift } from '@douyinfe/semi-icons';
|
||||
import {
|
||||
CreditCard,
|
||||
Wallet,
|
||||
Coins,
|
||||
Sparkles,
|
||||
TrendingUp,
|
||||
Gift,
|
||||
Zap,
|
||||
ExternalLink,
|
||||
Receipt,
|
||||
Headphones,
|
||||
} from 'lucide-react';
|
||||
import {
|
||||
AlipayBrandIcon,
|
||||
WechatBrandIcon,
|
||||
StripeBrandIcon,
|
||||
PayPalBrandIcon,
|
||||
} from '../../components/common/icons/BrandIcons';
|
||||
import AmountCard from './AmountCard';
|
||||
import { useMinimumLoadingTime } from '../../hooks/common/useMinimumLoadingTime';
|
||||
|
||||
const { Text, Title } = Typography;
|
||||
|
||||
const RechargePanel = ({
|
||||
t,
|
||||
userState,
|
||||
renderQuota,
|
||||
renderQuotaWithAmount,
|
||||
statusLoading,
|
||||
balanceLoading,
|
||||
enableOnlineTopUp,
|
||||
enableWechatTopUp,
|
||||
enableAlipayTopUp,
|
||||
enableStripeTopUp,
|
||||
enablePayPalTopUp,
|
||||
enableCreemTopUp,
|
||||
creemProducts,
|
||||
creemPreTopUp,
|
||||
presetAmounts,
|
||||
selectedPreset,
|
||||
selectPresetAmount,
|
||||
formatLargeNumber,
|
||||
priceRatio,
|
||||
topUpCount,
|
||||
minTopUp,
|
||||
getAmount,
|
||||
setTopUpCount,
|
||||
setSelectedPreset,
|
||||
setSelectedPresetObj,
|
||||
renderAmount,
|
||||
amountLoading,
|
||||
payMethods,
|
||||
preTopUp,
|
||||
paymentLoading,
|
||||
payWay,
|
||||
redemptionCode,
|
||||
setRedemptionCode,
|
||||
topUp,
|
||||
isSubmitting,
|
||||
topUpLink,
|
||||
openTopUpLink,
|
||||
topupInfo,
|
||||
}) => {
|
||||
useMinimumLoadingTime(amountLoading);
|
||||
const showBalanceSkeleton = useMinimumLoadingTime(balanceLoading);
|
||||
|
||||
// Creem 使用独立产品选择;其余渠道共用金额和支付方式区块。
|
||||
const hasStandardOnlinePayment =
|
||||
enableOnlineTopUp ||
|
||||
enableWechatTopUp ||
|
||||
enableAlipayTopUp ||
|
||||
enableStripeTopUp ||
|
||||
enablePayPalTopUp;
|
||||
const hasAnyOnlinePayment = hasStandardOnlinePayment || enableCreemTopUp;
|
||||
|
||||
// 渲染支付方式图标
|
||||
const renderPayIcon = (type, color) => {
|
||||
if (type === 'alipay') {
|
||||
return <AlipayBrandIcon size={18} color='#1677FF' />;
|
||||
} else if (type === 'wxpay') {
|
||||
return <WechatBrandIcon size={18} color='#07C160' />;
|
||||
} else if (type === 'stripe') {
|
||||
return <StripeBrandIcon size={18} color='#635BFF' />;
|
||||
} else if (type === 'paypal') {
|
||||
return <PayPalBrandIcon size={18} />;
|
||||
} else {
|
||||
return (
|
||||
<CreditCard size={18} color={color || 'var(--semi-color-text-2)'} />
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className='relative'>
|
||||
{/* 外层发光效果 */}
|
||||
<div className='absolute -inset-0.5 bg-gradient-to-br from-blue-400/20 via-brand-via/20 to-brand-to/20 rounded-[20px] blur-md opacity-60' />
|
||||
|
||||
<Card className='relative !rounded-2xl shadow-xl border border-white/60 dark:border-neutral-800/60 bg-white/70 dark:bg-neutral-900/70 backdrop-blur-xl h-full overflow-hidden'>
|
||||
{/* 顶部装饰线 */}
|
||||
<div className='absolute top-0 left-0 right-0 h-1 bg-gradient-to-r from-blue-500 via-brand-via to-brand-to' />
|
||||
|
||||
{/* 背景装饰 */}
|
||||
<div className='absolute top-0 right-0 w-64 h-64 bg-gradient-to-br from-blue-400/10 to-brand-to/10 rounded-full blur-3xl -mr-32 -mt-32 pointer-events-none' />
|
||||
<div className='absolute bottom-0 left-0 w-48 h-48 bg-gradient-to-br from-brand-from/10 to-brand-to/10 rounded-full blur-3xl -ml-24 -mb-24 pointer-events-none' />
|
||||
|
||||
<div className='relative'>
|
||||
{/* 头部:余额展示 */}
|
||||
<div className='flex items-center justify-between mb-4'>
|
||||
<div className='flex items-center gap-3'>
|
||||
<div className='relative'>
|
||||
<div className='absolute -inset-1 bg-gradient-to-br from-blue-500/40 to-brand-to/40 rounded-xl blur-md' />
|
||||
<div className='relative w-10 h-10 rounded-xl bg-gradient-to-br from-blue-500 to-brand-to flex items-center justify-center shadow-lg shadow-blue-500/30'>
|
||||
<CreditCard size={20} className='text-white' />
|
||||
</div>
|
||||
<Sparkles
|
||||
size={12}
|
||||
className='absolute -top-1 -right-1 text-amber-400'
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Title
|
||||
heading={5}
|
||||
className='!mb-0 bg-clip-text text-transparent bg-gradient-to-r from-blue-600 to-brand-to dark:from-blue-400 dark:to-brand-to'
|
||||
>
|
||||
{t('在线充值')}
|
||||
</Title>
|
||||
<Text type='tertiary' size='small'>
|
||||
{t('多种支付方式,安全便捷')}
|
||||
</Text>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 余额卡片 */}
|
||||
<div className='relative overflow-hidden bg-gradient-to-r from-blue-500 via-brand-via to-brand-to rounded-2xl p-4 mb-4 shadow-xl shadow-blue-500/20'>
|
||||
{/* 装饰元素 */}
|
||||
<div className='absolute top-0 right-0 w-32 h-32 bg-white/10 rounded-full -mr-8 -mt-8 blur-2xl' />
|
||||
<div className='absolute bottom-0 left-0 w-24 h-24 bg-black/10 rounded-full -ml-8 -mb-8 blur-xl' />
|
||||
|
||||
<div className='relative z-10 flex items-center justify-between gap-4'>
|
||||
{/* 左侧:余额信息 */}
|
||||
<div className='flex items-center gap-3 min-w-0'>
|
||||
<div className='p-2 bg-white/20 rounded-xl backdrop-blur-md flex-shrink-0'>
|
||||
<Wallet size={18} className='text-white' />
|
||||
</div>
|
||||
<div className='min-w-0'>
|
||||
<Text className='!text-white/80 text-xs font-medium'>
|
||||
{t('当前余额')}
|
||||
</Text>
|
||||
<Skeleton
|
||||
loading={showBalanceSkeleton}
|
||||
placeholder={
|
||||
<Skeleton.Title
|
||||
style={{
|
||||
width: 100,
|
||||
height: 28,
|
||||
backgroundColor: 'rgba(255,255,255,0.2)',
|
||||
}}
|
||||
/>
|
||||
}
|
||||
>
|
||||
<Title
|
||||
heading={4}
|
||||
className='!text-white !mb-0 drop-shadow-sm truncate'
|
||||
>
|
||||
{renderQuota(userState?.user?.quota)}
|
||||
</Title>
|
||||
</Skeleton>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 分隔线 */}
|
||||
<div className='w-px h-10 bg-white/20 flex-shrink-0' />
|
||||
|
||||
{/* 右侧:历史消耗 */}
|
||||
<div className='flex items-center gap-3 min-w-0'>
|
||||
<div className='p-2 bg-white/10 rounded-xl backdrop-blur-md flex-shrink-0'>
|
||||
<TrendingUp size={18} className='text-white/80' />
|
||||
</div>
|
||||
<div className='min-w-0'>
|
||||
<Text className='!text-white/60 text-xs'>
|
||||
{t('历史消耗')}
|
||||
</Text>
|
||||
<Text className='!text-white font-semibold text-sm block truncate'>
|
||||
{renderQuota(userState?.user?.used_quota)}
|
||||
</Text>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 开票服务公告 */}
|
||||
<div className='relative mb-4 overflow-hidden rounded-xl border border-amber-200/80 dark:border-amber-700/40 bg-amber-50/80 dark:bg-amber-950/20 backdrop-blur-sm'>
|
||||
{/* 左侧强调色条 */}
|
||||
<div className='absolute left-0 top-0 bottom-0 w-1 bg-gradient-to-b from-amber-400 to-orange-400' />
|
||||
<div className='flex items-center gap-3 pl-4 pr-4 py-3'>
|
||||
{/* 图标区 */}
|
||||
<div className='flex-shrink-0 w-9 h-9 rounded-xl bg-gradient-to-br from-amber-400 to-orange-500 flex items-center justify-center shadow-md shadow-amber-400/30'>
|
||||
<Receipt size={16} className='text-white' />
|
||||
</div>
|
||||
{/* 文字区 */}
|
||||
<div className='flex-1 min-w-0'>
|
||||
<div className='flex items-center gap-2 mb-0.5'>
|
||||
<span className='text-xs font-bold text-amber-700 dark:text-amber-400 tracking-wide uppercase'>
|
||||
{t('发票服务')}
|
||||
</span>
|
||||
<span className='text-[10px] px-1.5 py-0.5 rounded-full bg-amber-100 dark:bg-amber-900/50 text-amber-600 dark:text-amber-400 border border-amber-200 dark:border-amber-700/60 font-medium'>
|
||||
{t('温馨提示')}
|
||||
</span>
|
||||
</div>
|
||||
<Text
|
||||
size='small'
|
||||
className='!text-gray-600 dark:!text-gray-300 leading-relaxed'
|
||||
>
|
||||
{t('需要开发票的用户,请联系')}
|
||||
<span className='inline-flex items-center gap-1 font-semibold text-amber-700 dark:text-amber-400'>
|
||||
<Headphones size={12} />
|
||||
{t('在线客服')}
|
||||
</span>
|
||||
{t('进行申请处理')}
|
||||
</Text>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{statusLoading ? (
|
||||
<div className='py-12 flex flex-col items-center justify-center'>
|
||||
<Spin size='large' />
|
||||
<Text type='tertiary' className='mt-4'>
|
||||
{t('加载中...')}
|
||||
</Text>
|
||||
</div>
|
||||
) : hasAnyOnlinePayment ? (
|
||||
<div className='space-y-3.5'>
|
||||
{/* 充值金额选择 */}
|
||||
{hasStandardOnlinePayment && (
|
||||
<div className='relative group'>
|
||||
<div className='absolute -inset-0.5 bg-gradient-to-r from-emerald-400/20 to-teal-400/20 rounded-2xl blur-sm opacity-0 group-hover:opacity-100 transition-opacity' />
|
||||
<div className='relative bg-white/50 dark:bg-neutral-800/40 backdrop-blur-md rounded-xl p-3.5 border border-white/70 dark:border-neutral-700/50 shadow-sm'>
|
||||
<div className='flex items-center gap-2 mb-3'>
|
||||
<div className='p-1.5 bg-gradient-to-br from-emerald-500 to-teal-500 rounded-lg shadow-md shadow-emerald-500/20'>
|
||||
<Coins size={14} className='text-white' />
|
||||
</div>
|
||||
<Text strong className='text-gray-800 dark:text-gray-200'>
|
||||
{t('选择充值金额')}
|
||||
</Text>
|
||||
</div>
|
||||
<div className='grid grid-cols-2 sm:grid-cols-4 gap-2.5'>
|
||||
{presetAmounts.slice(0, 8).map((preset, index) => (
|
||||
<AmountCard
|
||||
key={index}
|
||||
preset={preset}
|
||||
selected={selectedPreset === preset.value}
|
||||
onClick={() => selectPresetAmount(preset)}
|
||||
topupInfo={topupInfo}
|
||||
priceRatio={priceRatio}
|
||||
t={t}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 支付方式 */}
|
||||
{hasStandardOnlinePayment && (
|
||||
<div className='relative group'>
|
||||
<div className='absolute -inset-0.5 bg-gradient-to-r from-brand-from/20 to-brand-to/20 rounded-2xl blur-sm opacity-0 group-hover:opacity-100 transition-opacity' />
|
||||
<div className='relative bg-white/50 dark:bg-neutral-800/40 backdrop-blur-md rounded-xl p-3.5 border border-white/70 dark:border-neutral-700/50 shadow-sm'>
|
||||
<div className='flex items-center gap-2 mb-3'>
|
||||
<div className='p-1.5 bg-gradient-to-br from-brand-from to-brand-to rounded-lg shadow-md shadow-brand/20'>
|
||||
<CreditCard size={14} className='text-white' />
|
||||
</div>
|
||||
<Text strong className='text-gray-800 dark:text-gray-200'>
|
||||
{t('支付方式')}
|
||||
</Text>
|
||||
</div>
|
||||
{payMethods && payMethods.length > 0 ? (
|
||||
<div className='flex flex-wrap gap-3'>
|
||||
{payMethods.map((method) => {
|
||||
const minTopupVal = Number(method.min_topup) || 0;
|
||||
const isStripe = method.type === 'stripe';
|
||||
const isPayPal = method.type === 'paypal';
|
||||
const isWechat = method.type === 'wxpay';
|
||||
const isAlipay = method.type === 'alipay';
|
||||
|
||||
const disabled =
|
||||
(!enableOnlineTopUp &&
|
||||
!isStripe &&
|
||||
!isPayPal &&
|
||||
!isWechat &&
|
||||
!isAlipay) ||
|
||||
(!enableStripeTopUp && isStripe) ||
|
||||
(!enablePayPalTopUp && isPayPal) ||
|
||||
(!enableWechatTopUp && isWechat) ||
|
||||
(!enableAlipayTopUp && isAlipay) ||
|
||||
minTopupVal > Number(topUpCount || 0);
|
||||
|
||||
const button = (
|
||||
<Button
|
||||
key={method.type}
|
||||
theme={payWay === method.type ? 'solid' : 'light'}
|
||||
type={
|
||||
payWay === method.type ? 'primary' : 'tertiary'
|
||||
}
|
||||
onClick={() => preTopUp(method.type)}
|
||||
disabled={disabled}
|
||||
loading={paymentLoading && payWay === method.type}
|
||||
icon={renderPayIcon(method.type, method.color)}
|
||||
className={`!rounded-xl !px-5 !py-2.5 !h-auto transition-all ${
|
||||
payWay === method.type
|
||||
? '!bg-gradient-to-r !from-brand-from !to-brand-to !shadow-lg !shadow-brand/30'
|
||||
: 'hover:!bg-brand/5 dark:hover:!bg-brand-deep/20 hover:!border-brand-from'
|
||||
}`}
|
||||
>
|
||||
{method.name}
|
||||
</Button>
|
||||
);
|
||||
|
||||
if (
|
||||
disabled &&
|
||||
minTopupVal > Number(topUpCount || 0)
|
||||
) {
|
||||
return (
|
||||
<Tooltip
|
||||
key={method.type}
|
||||
content={
|
||||
t('此支付方式最低充值金额为') +
|
||||
' ' +
|
||||
minTopupVal
|
||||
}
|
||||
>
|
||||
{button}
|
||||
</Tooltip>
|
||||
);
|
||||
}
|
||||
return button;
|
||||
})}
|
||||
</div>
|
||||
) : (
|
||||
<div className='text-gray-500 text-sm p-4 bg-gray-100/50 dark:bg-gray-800/50 rounded-xl border border-dashed border-gray-300 dark:border-gray-600 flex items-center gap-2'>
|
||||
<Zap size={16} className='text-gray-400' />
|
||||
{t('暂无可用的支付方式,请联系管理员配置')}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Creem 充值 */}
|
||||
{enableCreemTopUp && creemProducts.length > 0 && (
|
||||
<div className='relative group'>
|
||||
<div className='absolute -inset-0.5 bg-gradient-to-r from-cyan-400/20 to-blue-400/20 rounded-2xl blur-sm opacity-0 group-hover:opacity-100 transition-opacity' />
|
||||
<div className='relative bg-white/50 dark:bg-neutral-800/40 backdrop-blur-md rounded-xl p-3.5 border border-white/70 dark:border-neutral-700/50 shadow-sm'>
|
||||
<div className='flex items-center gap-2 mb-3'>
|
||||
<div className='p-1.5 bg-gradient-to-br from-cyan-500 to-blue-500 rounded-lg shadow-md shadow-cyan-500/20'>
|
||||
<Zap size={14} className='text-white' />
|
||||
</div>
|
||||
<Text strong className='text-gray-800 dark:text-gray-200'>
|
||||
{t('Creem 充值')}
|
||||
</Text>
|
||||
</div>
|
||||
<div className='grid grid-cols-2 sm:grid-cols-3 gap-3'>
|
||||
{creemProducts.map((product, index) => (
|
||||
<div
|
||||
key={index}
|
||||
onClick={() => creemPreTopUp(product)}
|
||||
className='cursor-pointer rounded-xl p-4 bg-white dark:bg-neutral-900 border border-gray-200 dark:border-neutral-700 transition-all hover:shadow-lg hover:border-cyan-300 dark:hover:border-cyan-700 hover:scale-[1.02] text-center'
|
||||
>
|
||||
<Text
|
||||
strong
|
||||
className='block text-gray-800 dark:text-gray-200'
|
||||
>
|
||||
{product.name}
|
||||
</Text>
|
||||
<div className='text-xs text-gray-500 my-2'>
|
||||
{t('额度')}: {product.quota}
|
||||
</div>
|
||||
<Text className='text-lg font-bold bg-clip-text text-transparent bg-gradient-to-r from-cyan-600 to-blue-600'>
|
||||
{product.currency === 'EUR' ? '€' : '¥'}
|
||||
{product.price}
|
||||
</Text>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 分隔线 */}
|
||||
<div className='h-px bg-gradient-to-r from-transparent via-gray-200 dark:via-gray-700 to-transparent' />
|
||||
|
||||
{/* 兑换码充值 */}
|
||||
<div className='relative group'>
|
||||
<div className='absolute -inset-0.5 bg-gradient-to-r from-amber-400/20 to-orange-400/20 rounded-2xl blur-sm opacity-0 group-hover:opacity-100 transition-opacity' />
|
||||
<div className='relative bg-white/50 dark:bg-neutral-800/40 backdrop-blur-md rounded-xl p-3.5 border border-white/70 dark:border-neutral-700/50 shadow-sm'>
|
||||
<div className='flex items-center gap-2 mb-3'>
|
||||
<div className='p-1.5 bg-gradient-to-br from-amber-500 to-orange-500 rounded-lg shadow-md shadow-amber-500/20'>
|
||||
<Gift size={14} className='text-white' />
|
||||
</div>
|
||||
<Text strong className='text-gray-800 dark:text-gray-200'>
|
||||
{t('兑换码充值')}
|
||||
</Text>
|
||||
</div>
|
||||
<div className='flex gap-3'>
|
||||
<Input
|
||||
prefix={<IconGift className='text-amber-500' />}
|
||||
placeholder={t('请输入兑换码')}
|
||||
value={redemptionCode}
|
||||
onChange={(value) => setRedemptionCode(value)}
|
||||
showClear
|
||||
className='flex-1 !rounded-xl'
|
||||
style={{ flex: 1 }}
|
||||
/>
|
||||
<Button
|
||||
theme='solid'
|
||||
onClick={topUp}
|
||||
loading={isSubmitting}
|
||||
className='!bg-gradient-to-r !from-amber-500 !to-orange-500 !rounded-xl !px-6 !shadow-lg !shadow-amber-500/20 hover:!shadow-amber-500/30 transition-all'
|
||||
>
|
||||
{t('兑换')}
|
||||
</Button>
|
||||
</div>
|
||||
{topUpLink && (
|
||||
<div className='mt-2 flex items-center gap-1'>
|
||||
<Text type='tertiary' size='small'>
|
||||
{t('在找兑换码?')}
|
||||
</Text>
|
||||
<Text
|
||||
link
|
||||
onClick={openTopUpLink}
|
||||
className='cursor-pointer flex items-center gap-1 text-amber-600 hover:text-amber-700'
|
||||
>
|
||||
{t('购买兑换码')}
|
||||
<ExternalLink size={12} />
|
||||
</Text>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className='space-y-3.5'>
|
||||
{/* 提示信息 */}
|
||||
<Banner
|
||||
type='info'
|
||||
description={t(
|
||||
'管理员未开启在线充值功能,请联系管理员开启或使用兑换码充值。',
|
||||
)}
|
||||
className='!rounded-xl'
|
||||
closeIcon={null}
|
||||
/>
|
||||
|
||||
{/* 分隔线 */}
|
||||
<div className='h-px bg-gradient-to-r from-transparent via-gray-200 dark:via-gray-700 to-transparent' />
|
||||
|
||||
{/* 兑换码充值 */}
|
||||
<div className='relative group'>
|
||||
<div className='absolute -inset-0.5 bg-gradient-to-r from-amber-400/20 to-orange-400/20 rounded-2xl blur-sm opacity-0 group-hover:opacity-100 transition-opacity' />
|
||||
<div className='relative bg-white/50 dark:bg-neutral-800/40 backdrop-blur-md rounded-xl p-3.5 border border-white/70 dark:border-neutral-700/50 shadow-sm'>
|
||||
<div className='flex items-center gap-2 mb-3'>
|
||||
<div className='p-1.5 bg-gradient-to-br from-amber-500 to-orange-500 rounded-lg'>
|
||||
<Gift size={14} className='text-white' />
|
||||
</div>
|
||||
<Text strong className='text-gray-800 dark:text-gray-200'>
|
||||
{t('兑换码充值')}
|
||||
</Text>
|
||||
</div>
|
||||
<div className='flex gap-3'>
|
||||
<Input
|
||||
prefix={<IconGift className='text-amber-500' />}
|
||||
placeholder={t('请输入兑换码')}
|
||||
value={redemptionCode}
|
||||
onChange={(value) => setRedemptionCode(value)}
|
||||
showClear
|
||||
className='flex-1 !rounded-xl'
|
||||
style={{ flex: 1 }}
|
||||
/>
|
||||
<Button
|
||||
theme='solid'
|
||||
onClick={topUp}
|
||||
loading={isSubmitting}
|
||||
className='!bg-gradient-to-r !from-amber-500 !to-orange-500 !rounded-xl !px-6 !shadow-lg !shadow-amber-500/20 hover:!shadow-amber-500/30 transition-all'
|
||||
>
|
||||
{t('兑换')}
|
||||
</Button>
|
||||
</div>
|
||||
{topUpLink && (
|
||||
<div className='mt-2 flex items-center gap-1'>
|
||||
<Text type='tertiary' size='small'>
|
||||
{t('在找兑换码?')}
|
||||
</Text>
|
||||
<Text
|
||||
link
|
||||
onClick={openTopUpLink}
|
||||
className='cursor-pointer flex items-center gap-1 text-amber-600 hover:text-amber-700'
|
||||
>
|
||||
{t('购买兑换码')}
|
||||
<ExternalLink size={12} />
|
||||
</Text>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default RechargePanel;
|
||||
@@ -0,0 +1,63 @@
|
||||
.recharge-v2-page {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.amount-section {
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
.amount-card {
|
||||
position: relative;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s;
|
||||
border: 2px solid transparent;
|
||||
}
|
||||
|
||||
.amount-card:hover {
|
||||
transform: translateY(-5px);
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
||||
}
|
||||
|
||||
.amount-card.selected {
|
||||
border-color: var(--semi-color-primary);
|
||||
background: rgba(var(--semi-blue-0), 0.05);
|
||||
}
|
||||
|
||||
.amount-card .badge {
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
right: 10px;
|
||||
}
|
||||
|
||||
.amount-card .recommend-badge {
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
left: 10px;
|
||||
}
|
||||
|
||||
.amount-card .hot-badge {
|
||||
position: absolute;
|
||||
top: 40px;
|
||||
left: 10px;
|
||||
}
|
||||
|
||||
.payment-method-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.payment-channels {
|
||||
padding: 15px;
|
||||
background: #f5f5f5;
|
||||
border-radius: 8px;
|
||||
margin-top: 15px;
|
||||
}
|
||||
|
||||
.pay-button-wrapper {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.pay-button-wrapper button {
|
||||
min-width: 300px;
|
||||
}
|
||||
@@ -0,0 +1,407 @@
|
||||
/*
|
||||
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 React, { useState, useEffect, useContext } from 'react';
|
||||
import {
|
||||
Card,
|
||||
Button,
|
||||
Radio,
|
||||
Tag,
|
||||
Toast,
|
||||
Spin,
|
||||
Row,
|
||||
Col,
|
||||
Space,
|
||||
Typography,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { UserContext } from '../../context/User';
|
||||
import { API, showError, showSuccess, renderQuota } from '../../helpers';
|
||||
import WechatQRModal from '../../components/topup/modals/WechatQRModal';
|
||||
import './RechargeV2.css';
|
||||
|
||||
const { Title, Text } = Typography;
|
||||
|
||||
const RechargeV2 = () => {
|
||||
const { t } = useTranslation();
|
||||
const [userState, userDispatch] = useContext(UserContext);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [config, setConfig] = useState(null);
|
||||
const [selectedAmountOption, setSelectedAmountOption] = useState(null);
|
||||
const [selectedPaymentMethod, setSelectedPaymentMethod] = useState(null);
|
||||
const [selectedChannel, setSelectedChannel] = useState(null);
|
||||
const [payInfo, setPayInfo] = useState(null);
|
||||
const [calculating, setCalculating] = useState(false);
|
||||
const [paying, setPaying] = useState(false);
|
||||
|
||||
// 微信支付二维码弹窗
|
||||
const [wechatQRVisible, setWechatQRVisible] = useState(false);
|
||||
const [wechatOrderData, setWechatOrderData] = useState(null);
|
||||
|
||||
// 加载支付配置
|
||||
useEffect(() => {
|
||||
loadPaymentConfig();
|
||||
}, []);
|
||||
|
||||
// 当选择档位时自动计算
|
||||
useEffect(() => {
|
||||
if (selectedAmountOption) {
|
||||
calculateAmount();
|
||||
}
|
||||
}, [selectedAmountOption]);
|
||||
|
||||
const loadPaymentConfig = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const res = await API.get('/api/user/payment/config');
|
||||
if (res.data.success) {
|
||||
setConfig(res.data.data);
|
||||
|
||||
// 设置默认选项
|
||||
if (
|
||||
res.data.data.amount_options &&
|
||||
res.data.data.amount_options.length > 0
|
||||
) {
|
||||
const recommended = res.data.data.amount_options.find(
|
||||
(opt) => opt.is_recommend,
|
||||
);
|
||||
setSelectedAmountOption(
|
||||
recommended || res.data.data.amount_options[0],
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
res.data.data.payment_methods &&
|
||||
res.data.data.payment_methods.length > 0
|
||||
) {
|
||||
const defaultMethod = res.data.data.payment_methods[0];
|
||||
setSelectedPaymentMethod(defaultMethod);
|
||||
if (defaultMethod.channels && defaultMethod.channels.length > 0) {
|
||||
setSelectedChannel(defaultMethod.channels[0].value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
showError(t('加载支付配置失败'));
|
||||
}
|
||||
} catch (error) {
|
||||
showError(t('加载支付配置失败'));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 计算支付金额
|
||||
const calculateAmount = async () => {
|
||||
if (!selectedAmountOption) return;
|
||||
|
||||
try {
|
||||
setCalculating(true);
|
||||
const res = await API.post('/api/user/payment/calculate', {
|
||||
amount_option_id: selectedAmountOption.id,
|
||||
});
|
||||
if (res.data.success) {
|
||||
setPayInfo(res.data.data);
|
||||
} else {
|
||||
showError(res.data.message);
|
||||
}
|
||||
} catch (error) {
|
||||
showError(t('计算失败'));
|
||||
} finally {
|
||||
setCalculating(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 选择充值档位
|
||||
const handleSelectAmount = (option) => {
|
||||
setSelectedAmountOption(option);
|
||||
};
|
||||
|
||||
// 选择支付方式
|
||||
const handleSelectPaymentMethod = (method) => {
|
||||
setSelectedPaymentMethod(method);
|
||||
if (method.channels && method.channels.length > 0) {
|
||||
setSelectedChannel(method.channels[0].value);
|
||||
}
|
||||
};
|
||||
|
||||
// 发起支付
|
||||
const handlePay = async () => {
|
||||
if (!selectedAmountOption || !selectedChannel) {
|
||||
Toast.warning(t('请选择充值金额和支付方式'));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setPaying(true);
|
||||
|
||||
const endpoint = {
|
||||
wechat_native: '/api/user/wechat/native',
|
||||
wechat_h5: '/api/user/wechat/h5',
|
||||
alipay_pc: '/api/user/alipay/pc',
|
||||
alipay_wap: '/api/user/alipay/wap',
|
||||
alipay_qr: '/api/user/alipay/qr',
|
||||
}[selectedChannel];
|
||||
|
||||
if (!endpoint) {
|
||||
Toast.error(t('不支持的支付方式'));
|
||||
return;
|
||||
}
|
||||
|
||||
const res = await API.post(endpoint, {
|
||||
amount_option_id: selectedAmountOption.id,
|
||||
});
|
||||
|
||||
if (res.data.success) {
|
||||
const data = res.data.data;
|
||||
|
||||
if (selectedChannel === 'wechat_native') {
|
||||
// 显示微信支付二维码
|
||||
setWechatOrderData(data);
|
||||
setWechatQRVisible(true);
|
||||
} else if (selectedChannel.startsWith('alipay')) {
|
||||
// 跳转到支付宝支付页面
|
||||
window.location.href = data.pay_url;
|
||||
} else if (selectedChannel === 'wechat_h5') {
|
||||
// 跳转到微信H5支付
|
||||
window.location.href = data.h5_url;
|
||||
}
|
||||
} else {
|
||||
showError(res.data.message);
|
||||
}
|
||||
} catch (error) {
|
||||
showError(t('创建支付订单失败'));
|
||||
} finally {
|
||||
setPaying(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 微信支付完成回调
|
||||
const handleWechatPayComplete = (success) => {
|
||||
setWechatQRVisible(false);
|
||||
if (success) {
|
||||
// 刷新用户额度
|
||||
if (userState.user) {
|
||||
const updatedUser = {
|
||||
...userState.user,
|
||||
quota: userState.user.quota + payInfo.quota,
|
||||
};
|
||||
userDispatch({ type: 'login', payload: updatedUser });
|
||||
}
|
||||
showSuccess(t('充值成功!'));
|
||||
}
|
||||
};
|
||||
|
||||
if (loading && !config) {
|
||||
return (
|
||||
<div style={{ textAlign: 'center', padding: '50px' }}>
|
||||
<Spin size='large' />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='recharge-v2-page'>
|
||||
<Card title={t('充值中心')} style={{ maxWidth: 1200, margin: '0 auto' }}>
|
||||
{/* 充值档位选择 */}
|
||||
<div className='amount-section'>
|
||||
<Title heading={4}>{t('选择充值金额')}</Title>
|
||||
<Row gutter={[16, 16]}>
|
||||
{config?.amount_options?.map((option) => (
|
||||
<Col span={8} key={option.id}>
|
||||
<Card
|
||||
className={`amount-card ${selectedAmountOption?.id === option.id ? 'selected' : ''}`}
|
||||
bodyStyle={{ padding: 20 }}
|
||||
onClick={() => handleSelectAmount(option)}
|
||||
hoverable
|
||||
>
|
||||
{option.badge && (
|
||||
<Tag color={option.badge_color || 'blue'} className='badge'>
|
||||
{option.badge}
|
||||
</Tag>
|
||||
)}
|
||||
{option.is_recommend && (
|
||||
<Tag color='blue' className='recommend-badge'>
|
||||
{t('推荐')}
|
||||
</Tag>
|
||||
)}
|
||||
{option.is_hot && (
|
||||
<Tag color='red' className='hot-badge'>
|
||||
{t('热门')}
|
||||
</Tag>
|
||||
)}
|
||||
<div
|
||||
className='amount-label'
|
||||
style={{ fontSize: 24, fontWeight: 'bold', marginTop: 10 }}
|
||||
>
|
||||
¥{option.amount}
|
||||
</div>
|
||||
{option.discount < 1.0 && (
|
||||
<div
|
||||
className='discount-info'
|
||||
style={{
|
||||
color: 'var(--semi-color-success)',
|
||||
marginTop: 5,
|
||||
}}
|
||||
>
|
||||
{t('{{rate}}折优惠', {
|
||||
rate: (option.discount * 10).toFixed(1),
|
||||
percent: Math.round(100 - option.discount * 100),
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
{option.bonus > 0 && (
|
||||
<div
|
||||
className='bonus-info'
|
||||
style={{ color: '#1890ff', marginTop: 5 }}
|
||||
>
|
||||
{t('赠送')} {renderQuota(option.bonus)}
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
</Col>
|
||||
))}
|
||||
</Row>
|
||||
</div>
|
||||
|
||||
{/* 支付信息预览 */}
|
||||
{payInfo && (
|
||||
<Card style={{ marginTop: 20, background: '#f8f9fa' }}>
|
||||
<Title heading={5}>{t('支付信息')}</Title>
|
||||
<Space vertical align='start' spacing='medium'>
|
||||
<div>
|
||||
<Text type='secondary'>{t('原价:')}</Text>
|
||||
<Text delete>¥{payInfo.original_money.toFixed(2)}</Text>
|
||||
</div>
|
||||
<div>
|
||||
<Text type='secondary'>{t('折扣:')}</Text>
|
||||
<Text strong>
|
||||
{t('{{rate}}折', {
|
||||
rate: (payInfo.discount_rate * 10).toFixed(1),
|
||||
percent: Math.round(100 - payInfo.discount_rate * 100),
|
||||
})}
|
||||
</Text>
|
||||
</div>
|
||||
<div>
|
||||
<Text type='secondary'>{t('实付:')}</Text>
|
||||
<Text
|
||||
style={{ fontSize: 24, color: 'var(--semi-color-danger)' }}
|
||||
strong
|
||||
>
|
||||
¥{payInfo.pay_money.toFixed(2)}
|
||||
</Text>
|
||||
</div>
|
||||
<div>
|
||||
<Text type='secondary'>{t('获得额度:')}</Text>
|
||||
<Text strong>{renderQuota(payInfo.quota)}</Text>
|
||||
</div>
|
||||
{payInfo.save_money > 0 && (
|
||||
<div>
|
||||
<Text type='secondary'>{t('节省:')}</Text>
|
||||
<Text style={{ color: 'var(--semi-color-success)' }}>
|
||||
¥{payInfo.save_money.toFixed(2)}
|
||||
</Text>
|
||||
</div>
|
||||
)}
|
||||
</Space>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* 支付方式选择 */}
|
||||
<div className='payment-methods' style={{ marginTop: 20 }}>
|
||||
<Title heading={4}>{t('选择支付方式')}</Title>
|
||||
<Radio.Group
|
||||
value={selectedPaymentMethod?.type}
|
||||
onChange={(e) => {
|
||||
const method = config.payment_methods.find(
|
||||
(m) => m.type === e.target.value,
|
||||
);
|
||||
handleSelectPaymentMethod(method);
|
||||
}}
|
||||
>
|
||||
<Space wrap>
|
||||
{config?.payment_methods?.map((method) => (
|
||||
<Radio key={method.type} value={method.type}>
|
||||
<div className='payment-method-item'>
|
||||
{method.logo && (
|
||||
<img
|
||||
src={method.logo}
|
||||
alt={method.name}
|
||||
style={{ width: 24, height: 24, marginRight: 8 }}
|
||||
/>
|
||||
)}
|
||||
<span>{method.name}</span>
|
||||
</div>
|
||||
</Radio>
|
||||
))}
|
||||
</Space>
|
||||
</Radio.Group>
|
||||
|
||||
{/* 支付渠道选择 */}
|
||||
{selectedPaymentMethod &&
|
||||
selectedPaymentMethod.channels &&
|
||||
selectedPaymentMethod.channels.length > 1 && (
|
||||
<div className='payment-channels' style={{ marginTop: 15 }}>
|
||||
<Text type='secondary'>{t('支付渠道:')}</Text>
|
||||
<Radio.Group
|
||||
value={selectedChannel}
|
||||
onChange={(e) => setSelectedChannel(e.target.value)}
|
||||
style={{ marginLeft: 10 }}
|
||||
>
|
||||
<Space wrap>
|
||||
{selectedPaymentMethod.channels.map((channel) => (
|
||||
<Radio key={channel.value} value={channel.value}>
|
||||
{channel.icon} {channel.name}
|
||||
</Radio>
|
||||
))}
|
||||
</Space>
|
||||
</Radio.Group>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 支付按钮 */}
|
||||
<div className='pay-button-wrapper' style={{ marginTop: 30 }}>
|
||||
<Button
|
||||
type='primary'
|
||||
size='large'
|
||||
block
|
||||
loading={paying}
|
||||
onClick={handlePay}
|
||||
disabled={!selectedAmountOption || !selectedChannel || calculating}
|
||||
>
|
||||
{paying
|
||||
? t('处理中...')
|
||||
: calculating
|
||||
? t('计算中...')
|
||||
: `${t('支付')} ¥${payInfo?.pay_money?.toFixed(2) || '0.00'}`}
|
||||
</Button>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
{/* 微信支付二维码弹窗 */}
|
||||
<WechatQRModal
|
||||
visible={wechatQRVisible}
|
||||
onClose={handleWechatPayComplete}
|
||||
orderData={wechatOrderData}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default RechargeV2;
|
||||
@@ -0,0 +1,923 @@
|
||||
/*
|
||||
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 React, { useEffect, useState, useContext } from 'react';
|
||||
import { Modal, Toast } from '@douyinfe/semi-ui';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { UserContext } from '../../context/User';
|
||||
import { StatusContext } from '../../context/Status';
|
||||
import {
|
||||
API,
|
||||
showError,
|
||||
showInfo,
|
||||
showSuccess,
|
||||
renderQuota,
|
||||
renderQuotaWithAmount,
|
||||
} from '../../helpers';
|
||||
|
||||
import PageBackground from '../../components/common/PageBackground';
|
||||
import RechargePanel from './RechargePanel';
|
||||
import MembershipPanel from './MembershipPanel';
|
||||
import HistoryPanel from './HistoryPanel';
|
||||
import PaymentConfirmModal from '../../components/topup/modals/PaymentConfirmModal';
|
||||
import WechatQRModal from '../../components/topup/modals/WechatQRModal';
|
||||
import {
|
||||
formatPayPalUSD,
|
||||
getPayPalReturnState,
|
||||
isPayPalAvailable,
|
||||
parsePayPalQuote,
|
||||
requestPayPalOrder,
|
||||
removePayPalReturnState,
|
||||
} from '../../helpers/paypalPayment';
|
||||
|
||||
const Recharge = ({ embedded = false }) => {
|
||||
const { t } = useTranslation();
|
||||
const [userState, userDispatch] = useContext(UserContext);
|
||||
const [statusState] = useContext(StatusContext);
|
||||
|
||||
// 充值相关状态
|
||||
const [redemptionCode, setRedemptionCode] = useState('');
|
||||
const [amount, setAmount] = useState(0.0);
|
||||
const [minTopUp, setMinTopUp] = useState(statusState?.status?.min_topup || 1);
|
||||
const [topUpCount, setTopUpCount] = useState(
|
||||
statusState?.status?.min_topup || 1,
|
||||
);
|
||||
const [priceRatio, setPriceRatio] = useState(statusState?.status?.price || 1);
|
||||
const [topUpLink, setTopUpLink] = useState(
|
||||
statusState?.status?.top_up_link || '',
|
||||
);
|
||||
|
||||
// 支付相关状态
|
||||
const [enableOnlineTopUp, setEnableOnlineTopUp] = useState(false);
|
||||
const [enableAlipayTopUp, setEnableAlipayTopUp] = useState(false);
|
||||
const [enableWechatTopUp, setEnableWechatTopUp] = useState(false);
|
||||
const [enableStripeTopUp, setEnableStripeTopUp] = useState(false);
|
||||
const [enablePayPalTopUp, setEnablePayPalTopUp] = useState(false);
|
||||
const [payPalMinTopUp, setPayPalMinTopUp] = useState(1);
|
||||
const [payPalQuote, setPayPalQuote] = useState(null);
|
||||
const [enableCreemTopUp, setEnableCreemTopUp] = useState(false);
|
||||
const [creemProducts, setCreemProducts] = useState([]);
|
||||
const [selectedCreemProduct, setSelectedCreemProduct] = useState(null);
|
||||
const [creemOpen, setCreemOpen] = useState(false);
|
||||
|
||||
// 微信支付二维码弹窗
|
||||
const [wechatQRVisible, setWechatQRVisible] = useState(false);
|
||||
const [wechatOrderData, setWechatOrderData] = useState(null);
|
||||
|
||||
const [statusLoading, setStatusLoading] = useState(true);
|
||||
const [balanceLoading, setBalanceLoading] = useState(true);
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [payWay, setPayWay] = useState('');
|
||||
const [amountLoading, setAmountLoading] = useState(false);
|
||||
const [paymentLoading, setPaymentLoading] = useState(false);
|
||||
const [confirmLoading, setConfirmLoading] = useState(false);
|
||||
const [payMethods, setPayMethods] = useState([]);
|
||||
|
||||
// 预设充值额度
|
||||
const [presetAmounts, setPresetAmounts] = useState([]);
|
||||
const [selectedPreset, setSelectedPreset] = useState(null);
|
||||
const [selectedPresetObj, setSelectedPresetObj] = useState(null); // 存储完整的预设对象
|
||||
|
||||
// 充值配置信息
|
||||
const [topupInfo, setTopupInfo] = useState({
|
||||
amount_options: [],
|
||||
discount: {},
|
||||
});
|
||||
|
||||
// 刷新账单列表的回调
|
||||
const [refreshHistory, setRefreshHistory] = useState(0);
|
||||
|
||||
// Tab 切换:充值 / 会员
|
||||
const [activeTab, setActiveTab] = useState('recharge');
|
||||
|
||||
// 兑换码充值
|
||||
const topUp = async () => {
|
||||
if (redemptionCode === '') {
|
||||
showInfo(t('请输入兑换码!'));
|
||||
return;
|
||||
}
|
||||
setIsSubmitting(true);
|
||||
try {
|
||||
const res = await API.post('/api/user/topup', {
|
||||
key: redemptionCode,
|
||||
});
|
||||
const { success, message, data } = res.data;
|
||||
if (success) {
|
||||
showSuccess(t('兑换成功!'));
|
||||
Modal.success({
|
||||
title: t('兑换成功!'),
|
||||
content: t('成功兑换额度:') + renderQuota(data),
|
||||
centered: true,
|
||||
});
|
||||
if (userState.user) {
|
||||
const updatedUser = {
|
||||
...userState.user,
|
||||
quota: userState.user.quota + data,
|
||||
};
|
||||
userDispatch({ type: 'login', payload: updatedUser });
|
||||
}
|
||||
setRedemptionCode('');
|
||||
setRefreshHistory((prev) => prev + 1);
|
||||
} else {
|
||||
showError(message);
|
||||
}
|
||||
} catch (err) {
|
||||
showError(t('请求失败'));
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 打开充值链接
|
||||
const openTopUpLink = () => {
|
||||
if (!topUpLink) {
|
||||
showError(t('超级管理员未设置充值链接!'));
|
||||
return;
|
||||
}
|
||||
window.open(topUpLink, '_blank');
|
||||
};
|
||||
|
||||
// 预充值
|
||||
const preTopUp = async (payment) => {
|
||||
// 必须先选择充值档位
|
||||
if (selectedPreset === null) {
|
||||
showError(t('请先选择充值金额'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (payment === 'stripe') {
|
||||
if (!enableStripeTopUp) {
|
||||
showError(t('管理员未开启Stripe充值!'));
|
||||
return;
|
||||
}
|
||||
} else if (payment === 'paypal') {
|
||||
if (!isPayPalAvailable(enablePayPalTopUp, payMethods)) {
|
||||
showError(t('管理员未开启 PayPal 充值!'));
|
||||
return;
|
||||
}
|
||||
} else if (payment === 'alipay') {
|
||||
if (!enableAlipayTopUp) {
|
||||
showError(t('管理员未开启支付宝充值!'));
|
||||
return;
|
||||
}
|
||||
} else if (payment === 'wxpay') {
|
||||
if (!enableWechatTopUp) {
|
||||
showError(t('管理员未开启微信充值!'));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (!enableOnlineTopUp) {
|
||||
showError(t('管理员未开启在线充值!'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
setPayWay(payment);
|
||||
setPaymentLoading(true);
|
||||
try {
|
||||
// 如果选择了预设档位,已经在 selectPresetAmount 中计算好了金额(包含折扣和汇率),不需要重新获取
|
||||
// 只有自定义金额时才需要从后端获取
|
||||
if (payment === 'paypal') {
|
||||
await getPayPalAmount();
|
||||
} else if (!selectedPresetObj) {
|
||||
if (payment === 'stripe') {
|
||||
await getStripeAmount();
|
||||
} else {
|
||||
await getAmount();
|
||||
}
|
||||
}
|
||||
|
||||
const selectedMinimum = payment === 'paypal' ? payPalMinTopUp : minTopUp;
|
||||
if (topUpCount < selectedMinimum) {
|
||||
showError(t('充值数量不能小于') + selectedMinimum);
|
||||
return;
|
||||
}
|
||||
setOpen(true);
|
||||
} catch (error) {
|
||||
showError(t('获取金额失败'));
|
||||
} finally {
|
||||
setPaymentLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 在线充值
|
||||
const onlineTopUp = async () => {
|
||||
let keepConfirmOpen = false;
|
||||
// 【Bug#7修复】仅在非预设档位(自定义金额)且金额为0时才从后端获取金额
|
||||
// 避免覆盖已计算好的档位折扣价
|
||||
if (!selectedPresetObj && payWay !== 'paypal') {
|
||||
if (payWay === 'stripe') {
|
||||
if (amount === 0) {
|
||||
await getStripeAmount();
|
||||
}
|
||||
} else {
|
||||
if (amount === 0) {
|
||||
await getAmount();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const selectedMinimum = payWay === 'paypal' ? payPalMinTopUp : minTopUp;
|
||||
if (topUpCount < selectedMinimum) {
|
||||
showError('充值数量不能小于' + selectedMinimum);
|
||||
return;
|
||||
}
|
||||
setConfirmLoading(true);
|
||||
try {
|
||||
let res;
|
||||
if (payWay === 'stripe') {
|
||||
res = await API.post('/api/user/stripe/pay', {
|
||||
amount: parseInt(topUpCount),
|
||||
payment_method: 'stripe',
|
||||
});
|
||||
} else if (payWay === 'paypal' && enablePayPalTopUp) {
|
||||
const requestData = selectedPresetObj?.id
|
||||
? {
|
||||
amount_option_id: selectedPresetObj.id,
|
||||
quote_token: payPalQuote?.quoteToken,
|
||||
}
|
||||
: {
|
||||
amount: parseInt(topUpCount),
|
||||
quote_token: payPalQuote?.quoteToken,
|
||||
};
|
||||
res = await requestPayPalOrder(
|
||||
API,
|
||||
'/api/user/paypal/pay',
|
||||
requestData,
|
||||
{ skipErrorHandler: true },
|
||||
);
|
||||
} else if (payWay === 'alipay' && enableAlipayTopUp) {
|
||||
// 使用独立支付宝接口
|
||||
// 如果选择了预设档位,发送 amount_option_id 以应用折扣
|
||||
const requestData = selectedPresetObj?.id
|
||||
? { amount_option_id: selectedPresetObj.id }
|
||||
: { amount: parseInt(topUpCount) };
|
||||
res = await API.post('/api/user/alipay/pc', requestData);
|
||||
} else if (payWay === 'wxpay' && enableWechatTopUp) {
|
||||
// 使用独立微信接口
|
||||
// 如果选择了预设档位,发送 amount_option_id 以应用折扣
|
||||
const requestData = selectedPresetObj?.id
|
||||
? { amount_option_id: selectedPresetObj.id }
|
||||
: { amount: parseInt(topUpCount) };
|
||||
res = await API.post('/api/user/wechat/native', requestData);
|
||||
} else {
|
||||
// 使用易支付接口
|
||||
res = await API.post('/api/user/pay', {
|
||||
amount: parseInt(topUpCount),
|
||||
payment_method: payWay,
|
||||
});
|
||||
}
|
||||
|
||||
if (res !== undefined) {
|
||||
const { success, message, data } = res.data;
|
||||
// 支持新版和旧版响应格式
|
||||
if (success || message === 'success') {
|
||||
if (payWay === 'stripe') {
|
||||
window.open(data.pay_link, '_blank');
|
||||
} else if (payWay === 'paypal' && data.approval_url) {
|
||||
setPayPalQuote(parsePayPalQuote(data));
|
||||
window.location.href = data.approval_url;
|
||||
return;
|
||||
} else if (
|
||||
payWay === 'paypal' &&
|
||||
res.status === 202 &&
|
||||
message === 'PAYPAL_ORDER_CREATING'
|
||||
) {
|
||||
showInfo(t('PayPal 订单创建中,请稍后重试'));
|
||||
return;
|
||||
} else if (payWay === 'alipay' && enableAlipayTopUp && data.pay_url) {
|
||||
// 支付宝独立接口 - 打开支付页面
|
||||
// Safari 会拦截 await 之后的 window.open(用户手势已过期),改为同标签跳转
|
||||
let isSafari =
|
||||
navigator.userAgent.indexOf('Safari') > -1 &&
|
||||
navigator.userAgent.indexOf('Chrome') < 1;
|
||||
if (isSafari) {
|
||||
// 同标签跳转支付宝,页面即将卸载:立即 return,
|
||||
// 避免下方 setRefreshHistory 再发起注定被中断的账单请求而误报网络错误
|
||||
window.location.href = data.pay_url;
|
||||
return;
|
||||
} else {
|
||||
window.open(data.pay_url, '_blank');
|
||||
}
|
||||
} else if (payWay === 'wxpay' && enableWechatTopUp && data.code_url) {
|
||||
// 微信独立接口 - 显示二维码弹窗
|
||||
setWechatOrderData(data);
|
||||
setWechatQRVisible(true);
|
||||
} else {
|
||||
// 易支付 - 表单提交
|
||||
let params = data;
|
||||
let url = res.data.url;
|
||||
let form = document.createElement('form');
|
||||
form.action = url;
|
||||
form.method = 'POST';
|
||||
let isSafari =
|
||||
navigator.userAgent.indexOf('Safari') > -1 &&
|
||||
navigator.userAgent.indexOf('Chrome') < 1;
|
||||
if (!isSafari) {
|
||||
form.target = '_blank';
|
||||
}
|
||||
for (let key in params) {
|
||||
let 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);
|
||||
}
|
||||
setRefreshHistory((prev) => prev + 1);
|
||||
} else {
|
||||
showError(data || message);
|
||||
}
|
||||
} else {
|
||||
showError(res);
|
||||
}
|
||||
} catch (err) {
|
||||
const code = err?.response?.data?.message;
|
||||
if (payWay === 'paypal' && code?.startsWith('PAYPAL_QUOTE_')) {
|
||||
try {
|
||||
await getPayPalAmount();
|
||||
keepConfirmOpen = true;
|
||||
showInfo(t('PayPal 报价已更新,请重新确认'));
|
||||
} catch {
|
||||
showError(t('获取金额失败'));
|
||||
}
|
||||
} else {
|
||||
console.log(err);
|
||||
showError(err?.response?.data?.message || t('支付请求失败'));
|
||||
}
|
||||
} finally {
|
||||
if (!keepConfirmOpen) setOpen(false);
|
||||
setConfirmLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// Creem 预充值
|
||||
const creemPreTopUp = async (product) => {
|
||||
if (!enableCreemTopUp) {
|
||||
showError(t('管理员未开启 Creem 充值!'));
|
||||
return;
|
||||
}
|
||||
setSelectedCreemProduct(product);
|
||||
setCreemOpen(true);
|
||||
};
|
||||
|
||||
// Creem 在线充值
|
||||
const onlineCreemTopUp = async () => {
|
||||
if (!selectedCreemProduct) {
|
||||
showError(t('请选择产品'));
|
||||
return;
|
||||
}
|
||||
if (!selectedCreemProduct.productId) {
|
||||
showError(t('产品配置错误,请联系管理员'));
|
||||
return;
|
||||
}
|
||||
setConfirmLoading(true);
|
||||
try {
|
||||
const res = await API.post('/api/user/creem/pay', {
|
||||
product_id: selectedCreemProduct.productId,
|
||||
payment_method: 'creem',
|
||||
});
|
||||
if (res !== undefined) {
|
||||
const { message, data } = res.data;
|
||||
if (message === 'success') {
|
||||
window.open(data.checkout_url, '_blank');
|
||||
setRefreshHistory((prev) => prev + 1);
|
||||
} else {
|
||||
showError(data);
|
||||
}
|
||||
} else {
|
||||
showError(res);
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
showError(t('支付请求失败'));
|
||||
} finally {
|
||||
setCreemOpen(false);
|
||||
setConfirmLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 微信支付完成回调
|
||||
const handleWechatPayComplete = (success) => {
|
||||
setWechatQRVisible(false);
|
||||
setWechatOrderData(null);
|
||||
if (success) {
|
||||
showSuccess(t('充值成功!'));
|
||||
getUserQuota();
|
||||
setRefreshHistory((prev) => prev + 1);
|
||||
}
|
||||
};
|
||||
|
||||
// 获取用户配额
|
||||
const getUserQuota = async () => {
|
||||
setBalanceLoading(true);
|
||||
try {
|
||||
let res = await API.get(`/api/user/self`);
|
||||
const { success, message, data } = res.data;
|
||||
if (success) {
|
||||
userDispatch({ type: 'login', payload: data });
|
||||
} else {
|
||||
showError(message);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('获取用户信息失败:', err);
|
||||
} finally {
|
||||
setBalanceLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 获取充值配置信息
|
||||
const getTopupInfo = async () => {
|
||||
try {
|
||||
const res = await API.get('/api/user/topup/info');
|
||||
const { data, success } = res.data;
|
||||
if (success) {
|
||||
setTopupInfo({
|
||||
amount_options: data.amount_options || [],
|
||||
discount: data.discount || {},
|
||||
});
|
||||
|
||||
// 处理支付方式
|
||||
let payMethods = data.pay_methods || [];
|
||||
try {
|
||||
if (typeof payMethods === 'string') {
|
||||
payMethods = JSON.parse(payMethods);
|
||||
}
|
||||
if (payMethods && payMethods.length > 0) {
|
||||
payMethods = payMethods.filter(
|
||||
(method) => method.name && method.type,
|
||||
);
|
||||
payMethods = payMethods.map((method) => {
|
||||
const normalizedMinTopup = Number(method.min_topup);
|
||||
method.min_topup = Number.isFinite(normalizedMinTopup)
|
||||
? normalizedMinTopup
|
||||
: 0;
|
||||
|
||||
if (
|
||||
method.type === 'stripe' &&
|
||||
(!method.min_topup || method.min_topup <= 0)
|
||||
) {
|
||||
const stripeMin = Number(data.stripe_min_topup);
|
||||
if (Number.isFinite(stripeMin)) {
|
||||
method.min_topup = stripeMin;
|
||||
}
|
||||
}
|
||||
|
||||
if (!method.color) {
|
||||
if (method.type === 'alipay') {
|
||||
method.color = 'rgba(var(--semi-blue-5), 1)';
|
||||
} else if (method.type === 'wxpay') {
|
||||
method.color = 'rgba(var(--semi-green-5), 1)';
|
||||
} else if (method.type === 'stripe') {
|
||||
method.color = 'rgba(var(--semi-purple-5), 1)';
|
||||
} else if (method.type === 'paypal') {
|
||||
method.color = '#0070ba';
|
||||
} else {
|
||||
method.color = 'rgba(var(--semi-primary-5), 1)';
|
||||
}
|
||||
}
|
||||
return method;
|
||||
});
|
||||
} else {
|
||||
payMethods = [];
|
||||
}
|
||||
setPayMethods(payMethods);
|
||||
|
||||
const enableOnline = data.enable_online_topup || false;
|
||||
const enableAlipay = data.enable_alipay_topup || false;
|
||||
const enableWechat = data.enable_wechat_topup || false;
|
||||
const enableStripe = data.enable_stripe_topup || false;
|
||||
const enablePayPal = data.enable_paypal_topup || false;
|
||||
const enableCreem = data.enable_creem_topup || false;
|
||||
const minTopUpValue = enableOnline
|
||||
? data.min_topup
|
||||
: enableStripe
|
||||
? data.stripe_min_topup
|
||||
: 1;
|
||||
|
||||
setEnableOnlineTopUp(enableOnline);
|
||||
setEnableAlipayTopUp(enableAlipay);
|
||||
setEnableWechatTopUp(enableWechat);
|
||||
setEnableStripeTopUp(enableStripe);
|
||||
setEnablePayPalTopUp(enablePayPal);
|
||||
setPayPalMinTopUp(Number(data.paypal_min_topup) || 1);
|
||||
setEnableCreemTopUp(enableCreem);
|
||||
setMinTopUp(minTopUpValue);
|
||||
setTopUpCount(minTopUpValue);
|
||||
|
||||
// 设置 Creem 产品
|
||||
try {
|
||||
const products = JSON.parse(data.creem_products || '[]');
|
||||
setCreemProducts(products);
|
||||
} catch (e) {
|
||||
setCreemProducts([]);
|
||||
}
|
||||
|
||||
// 处理充值档位配置
|
||||
// 优先使用新版 amount_options_v2,其次使用旧版 amount_options,最后使用默认生成
|
||||
if (data.amount_options_v2 && data.amount_options_v2.length > 0) {
|
||||
// 使用新版充值档位(按 sort 排序)
|
||||
const sortedOptions = [...data.amount_options_v2].sort(
|
||||
(a, b) => (a.sort || 0) - (b.sort || 0),
|
||||
);
|
||||
const customPresets = sortedOptions.map((opt) => ({
|
||||
id: opt.id,
|
||||
value: opt.amount,
|
||||
discount: opt.discount || 1.0,
|
||||
label: opt.label,
|
||||
badge: opt.badge,
|
||||
badgeColor: opt.badge_color,
|
||||
isRecommend: opt.is_recommend,
|
||||
isHot: opt.is_hot,
|
||||
bonus: opt.bonus,
|
||||
minVipLevel: opt.min_vip_level,
|
||||
}));
|
||||
setPresetAmounts(customPresets);
|
||||
} else if (data.amount_options && data.amount_options.length > 0) {
|
||||
// 使用旧版充值档位
|
||||
const customPresets = data.amount_options.map((amount) => ({
|
||||
value: amount,
|
||||
discount: data.discount[amount] || 1.0,
|
||||
}));
|
||||
setPresetAmounts(customPresets);
|
||||
} else {
|
||||
// 生成默认预设
|
||||
setPresetAmounts(generatePresetAmounts(minTopUpValue));
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('解析支付方式失败:', e);
|
||||
setPayMethods([]);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取充值配置异常:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// 获取支付金额
|
||||
const getAmount = async (value) => {
|
||||
if (value === undefined) {
|
||||
value = topUpCount;
|
||||
}
|
||||
setAmountLoading(true);
|
||||
try {
|
||||
const res = await API.post('/api/user/amount', {
|
||||
amount: parseFloat(value),
|
||||
});
|
||||
if (res !== undefined) {
|
||||
const { message, data } = res.data;
|
||||
if (message === 'success') {
|
||||
setAmount(parseFloat(data));
|
||||
} else {
|
||||
setAmount(0);
|
||||
Toast.error({ content: '错误:' + data, id: 'getAmount' });
|
||||
}
|
||||
} else {
|
||||
showError(res);
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
setAmountLoading(false);
|
||||
};
|
||||
|
||||
// 获取 Stripe 支付金额
|
||||
const getStripeAmount = async (value) => {
|
||||
if (value === undefined) {
|
||||
value = topUpCount;
|
||||
}
|
||||
setAmountLoading(true);
|
||||
try {
|
||||
const res = await API.post('/api/user/stripe/amount', {
|
||||
amount: parseFloat(value),
|
||||
});
|
||||
if (res !== undefined) {
|
||||
const { message, data } = res.data;
|
||||
if (message === 'success') {
|
||||
setAmount(parseFloat(data));
|
||||
} else {
|
||||
setAmount(0);
|
||||
Toast.error({ content: '错误:' + data, id: 'getAmount' });
|
||||
}
|
||||
} else {
|
||||
showError(res);
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
} finally {
|
||||
setAmountLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const getPayPalAmount = async () => {
|
||||
setAmountLoading(true);
|
||||
try {
|
||||
const requestData = selectedPresetObj?.id
|
||||
? { amount_option_id: selectedPresetObj.id }
|
||||
: { amount: parseInt(topUpCount) };
|
||||
const res = await API.post('/api/user/paypal/amount', requestData);
|
||||
if (!res?.data?.success) {
|
||||
throw new Error(res?.data?.message || t('获取金额失败'));
|
||||
}
|
||||
const quote = parsePayPalQuote(res);
|
||||
setPayPalQuote(quote);
|
||||
setAmount(quote.payAmount);
|
||||
return quote;
|
||||
} finally {
|
||||
setAmountLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 选择预设充值额度
|
||||
const selectPresetAmount = (preset) => {
|
||||
setTopUpCount(preset.value);
|
||||
setSelectedPreset(preset.value);
|
||||
setSelectedPresetObj(preset); // 保存完整的预设对象
|
||||
const discount = preset.discount || topupInfo.discount[preset.value] || 1.0;
|
||||
|
||||
// 计算实付金额(人民币)
|
||||
// 新版档位(有 id 字段):amount 已经是人民币金额,直接乘以折扣
|
||||
// 旧版档位或自定义金额:需要乘以汇率
|
||||
const isNewVersion = preset.id !== undefined;
|
||||
const discountedAmount = isNewVersion
|
||||
? preset.value * discount // 新版:人民币金额 * 折扣
|
||||
: preset.value * priceRatio * discount; // 旧版:额度 * 汇率 * 折扣
|
||||
|
||||
setAmount(discountedAmount);
|
||||
};
|
||||
|
||||
// 生成预设充值额度
|
||||
const generatePresetAmounts = (minAmount) => {
|
||||
const multipliers = [1, 5, 10, 30, 50, 100, 300, 500];
|
||||
return multipliers.map((multiplier) => ({
|
||||
value: minAmount * multiplier,
|
||||
}));
|
||||
};
|
||||
|
||||
// 格式化大数字
|
||||
const formatLargeNumber = (num) => {
|
||||
return num.toString();
|
||||
};
|
||||
|
||||
const renderAmount = () => {
|
||||
if (payWay === 'paypal') {
|
||||
return formatPayPalUSD(payPalQuote?.payAmount || amount);
|
||||
}
|
||||
return amount + ' ' + t('元');
|
||||
};
|
||||
|
||||
const handleCancel = () => {
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
const handleCreemCancel = () => {
|
||||
setCreemOpen(false);
|
||||
setSelectedCreemProduct(null);
|
||||
};
|
||||
|
||||
// 页面加载时始终刷新用户余额,确保显示最新数据
|
||||
// (用户可能在其他页面消费或在其他设备充值,localStorage缓存的余额可能过期)
|
||||
useEffect(() => {
|
||||
getUserQuota();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
getTopupInfo();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const result = getPayPalReturnState(window.location.search);
|
||||
if (!result) return;
|
||||
if (result.status === 'success') showSuccess(t('PayPal 充值成功'));
|
||||
else if (result.status === 'pending')
|
||||
showInfo(t('PayPal 支付处理中,请稍后查看充值记录'));
|
||||
else if (result.status === 'cancelled') showInfo(t('已取消 PayPal 支付'));
|
||||
else showError(t('PayPal 支付确认失败,请稍后重试'));
|
||||
getUserQuota();
|
||||
setRefreshHistory((previous) => previous + 1);
|
||||
window.history.replaceState(
|
||||
{},
|
||||
'',
|
||||
removePayPalReturnState(window.location.href),
|
||||
);
|
||||
// The callback query must be consumed exactly once on page entry.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (statusState?.status) {
|
||||
setTopUpLink(statusState.status.top_up_link || '');
|
||||
setPriceRatio(statusState.status.price || 1);
|
||||
setStatusLoading(false);
|
||||
}
|
||||
}, [statusState?.status]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`relative w-full min-h-screen pt-[64px] ${embedded ? '' : 'bg-[var(--surface-bg-base)]'}`}
|
||||
>
|
||||
{/* 统一页面背景层 —— 走 bg-glow-* 主题变量。
|
||||
* embedded(嵌入 UserCenter/topup)时由父 shell 提供背景,此处跳过避免双层叠加;
|
||||
* 独立路由(/recharge、/console/topup)时自渲染 rich 背景 + 网格。 */}
|
||||
{!embedded && <PageBackground variant='rich' grid />}
|
||||
|
||||
{/* 支付确认弹窗 */}
|
||||
<PaymentConfirmModal
|
||||
t={t}
|
||||
open={open}
|
||||
onlineTopUp={onlineTopUp}
|
||||
handleCancel={handleCancel}
|
||||
confirmLoading={confirmLoading}
|
||||
topUpCount={topUpCount}
|
||||
renderQuotaWithAmount={(value) =>
|
||||
payWay === 'paypal' && payPalQuote?.quota
|
||||
? renderQuota(payPalQuote.quota)
|
||||
: renderQuotaWithAmount(value)
|
||||
}
|
||||
amountLoading={amountLoading}
|
||||
renderAmount={renderAmount}
|
||||
payWay={payWay}
|
||||
payMethods={payMethods}
|
||||
amountNumber={payWay === 'paypal' ? 0 : amount}
|
||||
discountRate={
|
||||
payWay === 'paypal'
|
||||
? 1.0
|
||||
: selectedPresetObj?.discount ||
|
||||
topupInfo?.discount?.[topUpCount] ||
|
||||
1.0
|
||||
}
|
||||
payPalQuote={payPalQuote}
|
||||
/>
|
||||
|
||||
{/* Creem 确认弹窗 */}
|
||||
<Modal
|
||||
title={t('确认充值')}
|
||||
visible={creemOpen}
|
||||
onOk={onlineCreemTopUp}
|
||||
onCancel={handleCreemCancel}
|
||||
maskClosable={false}
|
||||
size='small'
|
||||
centered
|
||||
confirmLoading={confirmLoading}
|
||||
>
|
||||
{selectedCreemProduct && (
|
||||
<div className='space-y-2'>
|
||||
<p>
|
||||
{t('产品名称')}:{selectedCreemProduct.name}
|
||||
</p>
|
||||
<p>
|
||||
{t('价格')}:{selectedCreemProduct.currency === 'EUR' ? '€' : '¥'}
|
||||
{selectedCreemProduct.price}
|
||||
</p>
|
||||
<p>
|
||||
{t('充值额度')}:{selectedCreemProduct.quota}
|
||||
</p>
|
||||
<p className='text-orange-500'>{t('是否确认充值?')}</p>
|
||||
</div>
|
||||
)}
|
||||
</Modal>
|
||||
|
||||
{/* 微信支付二维码弹窗 */}
|
||||
<WechatQRModal
|
||||
visible={wechatQRVisible}
|
||||
onClose={handleWechatPayComplete}
|
||||
orderData={wechatOrderData}
|
||||
/>
|
||||
|
||||
{/* 主内容区 */}
|
||||
<div className='relative z-10 max-w-7xl mx-auto px-4 sm:px-6 py-4'>
|
||||
{/* Tab 切换:充值 / 包月 */}
|
||||
<div className='flex items-center gap-1.5 mb-4 p-1 bg-white/60 dark:bg-neutral-800/60 rounded-2xl w-fit backdrop-blur-xl border border-white/50 dark:border-neutral-700/50 shadow-lg shadow-black/[0.03]'>
|
||||
<button
|
||||
onClick={() => setActiveTab('recharge')}
|
||||
className={`
|
||||
flex items-center gap-2 px-6 py-2 rounded-xl text-sm font-semibold transition-all duration-300
|
||||
${
|
||||
activeTab === 'recharge'
|
||||
? 'bg-white/90 dark:bg-neutral-700/90 text-blue-600 dark:text-blue-400 shadow-md backdrop-blur-sm'
|
||||
: 'text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-300 hover:bg-white/40 dark:hover:bg-neutral-700/40'
|
||||
}
|
||||
`}
|
||||
>
|
||||
<svg
|
||||
className='w-4 h-4'
|
||||
fill='none'
|
||||
viewBox='0 0 24 24'
|
||||
stroke='currentColor'
|
||||
>
|
||||
<path
|
||||
strokeLinecap='round'
|
||||
strokeLinejoin='round'
|
||||
strokeWidth={2}
|
||||
d='M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z'
|
||||
/>
|
||||
</svg>
|
||||
{t('充值')}
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setActiveTab('membership')}
|
||||
className={`
|
||||
flex items-center gap-2 px-6 py-2 rounded-xl text-sm font-semibold transition-all duration-300
|
||||
${
|
||||
activeTab === 'membership'
|
||||
? 'bg-gradient-to-r from-brand-from to-brand-to text-white shadow-md shadow-brand/25'
|
||||
: 'text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-300 hover:bg-white/40 dark:hover:bg-neutral-700/40'
|
||||
}
|
||||
`}
|
||||
>
|
||||
<svg
|
||||
className='w-4 h-4'
|
||||
fill='none'
|
||||
viewBox='0 0 24 24'
|
||||
stroke='currentColor'
|
||||
>
|
||||
<path
|
||||
strokeLinecap='round'
|
||||
strokeLinejoin='round'
|
||||
strokeWidth={2}
|
||||
d='M5 3v4M3 5h4M6 17v4m-2-2h4m5-16l2.286 6.857L21 12l-5.714 2.143L13 21l-2.286-6.857L5 12l5.714-2.143L13 3z'
|
||||
/>
|
||||
</svg>
|
||||
{t('包月')}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 左右分栏布局 - 使用 grid 确保等高和顶部对齐 */}
|
||||
<div className='grid grid-cols-1 lg:grid-cols-2 gap-5 lg:items-start'>
|
||||
{/* 左侧内容:根据 Tab 切换 */}
|
||||
{activeTab === 'membership' ? (
|
||||
<MembershipPanel />
|
||||
) : (
|
||||
<RechargePanel
|
||||
t={t}
|
||||
userState={userState}
|
||||
renderQuota={renderQuota}
|
||||
renderQuotaWithAmount={renderQuotaWithAmount}
|
||||
statusLoading={statusLoading}
|
||||
balanceLoading={balanceLoading}
|
||||
enableOnlineTopUp={enableOnlineTopUp}
|
||||
enableWechatTopUp={enableWechatTopUp}
|
||||
enableAlipayTopUp={enableAlipayTopUp}
|
||||
enableStripeTopUp={enableStripeTopUp}
|
||||
enablePayPalTopUp={enablePayPalTopUp}
|
||||
enableCreemTopUp={enableCreemTopUp}
|
||||
creemProducts={creemProducts}
|
||||
creemPreTopUp={creemPreTopUp}
|
||||
presetAmounts={presetAmounts}
|
||||
selectedPreset={selectedPreset}
|
||||
selectPresetAmount={selectPresetAmount}
|
||||
formatLargeNumber={formatLargeNumber}
|
||||
priceRatio={priceRatio}
|
||||
topUpCount={topUpCount}
|
||||
minTopUp={minTopUp}
|
||||
getAmount={getAmount}
|
||||
setTopUpCount={setTopUpCount}
|
||||
setSelectedPreset={setSelectedPreset}
|
||||
setSelectedPresetObj={setSelectedPresetObj}
|
||||
renderAmount={renderAmount}
|
||||
amountLoading={amountLoading}
|
||||
payMethods={payMethods}
|
||||
preTopUp={preTopUp}
|
||||
paymentLoading={paymentLoading}
|
||||
payWay={payWay}
|
||||
redemptionCode={redemptionCode}
|
||||
setRedemptionCode={setRedemptionCode}
|
||||
topUp={topUp}
|
||||
isSubmitting={isSubmitting}
|
||||
topUpLink={topUpLink}
|
||||
openTopUpLink={openTopUpLink}
|
||||
topupInfo={topupInfo}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 右侧:充值记录 */}
|
||||
<div className='lg:sticky lg:top-4'>
|
||||
<HistoryPanel t={t} refreshTrigger={refreshHistory} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Recharge;
|
||||
Reference in New Issue
Block a user