411 lines
13 KiB
React
411 lines
13 KiB
React
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import { useEffectEvent } from '../../hooks/common/useEffectEvent';
|
|
import React, { useState, useEffect, useContext } from 'react';
|
|
import {
|
|
Card,
|
|
Button,
|
|
Radio,
|
|
Tag,
|
|
Toast,
|
|
Spin,
|
|
Row,
|
|
Col,
|
|
Space,
|
|
Typography,
|
|
} from '@/components/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);
|
|
|
|
// 加载支付配置
|
|
const onMount = useEffectEvent(() => {
|
|
loadPaymentConfig();
|
|
});
|
|
useEffect(() => onMount(), [onMount]);
|
|
|
|
// 当选择档位时自动计算
|
|
const onScopeChange2 = useEffectEvent(() => {
|
|
if (selectedAmountOption) {
|
|
calculateAmount();
|
|
}
|
|
});
|
|
useEffect(() => onScopeChange2(), [onScopeChange2, 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(--ui-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(--ui-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(--ui-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;
|