import { useState, useEffect, Card, Row, Col, Button, Tag, Typography, Space, Radio, Select, Spin, message, Steps, Divider, Alert, ConfigProvider, theme, SaveOutlined, CopyOutlined, DatabaseOutlined, DownloadOutlined, DollarOutlined, CheckCircleOutlined, CreditCardOutlined, AimOutlined, PieChartOutlined, Title, Text, Paragraph, FC, } from '@/imports'; import { instanceDataSource, InstanceType, InstanceConfig } from '@/services/instanceDataSource'; import { useUser } from '@/contexts/UserContext'; const InstancePurchase: FC = () => { const { currentUser } = useUser(); const [currentStep, setCurrentStep] = useState(0); const [instanceTypes, setInstanceTypes] = useState([]); const [selectedInstanceType, setSelectedInstanceType] = useState('medium'); const [instanceConfig, setInstanceConfig] = useState({ cpu: 4, memory: 8, storage: 200, bandwidth: 50 }); const [billingCycle, setBillingCycle] = useState<'monthly' | 'yearly'>('monthly'); const [price, setPrice] = useState({ monthly: 0, yearly: 0, discount: 0 }); const [paymentMethod, setPaymentMethod] = useState('creditCard'); const [loading, setLoading] = useState(false); const [configOptions, setConfigOptions] = useState({ cpuOptions: [] as number[], memoryOptions: [] as number[], storageOptions: [] as number[], bandwidthOptions: [] as number[] }); // 检查用户是否为 ADMIN const isAdmin = currentUser?.role === 'ADMIN'; useEffect(() => { loadInstanceTypes(); loadConfigOptions(); }, []); useEffect(() => { if (selectedInstanceType) { calculatePrice(); } }, [selectedInstanceType, instanceConfig, billingCycle]); const loadInstanceTypes = async () => { setLoading(true); try { const types = await instanceDataSource.getInstanceTypes(); setInstanceTypes(types); if (types.length > 0) { const defaultType = types.find(t => t.recommended) || types[0]; setSelectedInstanceType(defaultType.id); setInstanceConfig(defaultType.defaultConfig); } } catch (error) { message.error('Failed to load instance types'); } finally { setLoading(false); } }; const loadConfigOptions = async () => { try { const options = await instanceDataSource.getInstanceConfigOptions(); setConfigOptions(options); } catch (error) { message.error('Failed to load config options'); } }; const calculatePrice = async () => { try { const priceData = await instanceDataSource.calculatePrice( selectedInstanceType, instanceConfig, billingCycle ); setPrice(priceData); } catch (error) { message.error('Failed to calculate price'); } }; const handleInstanceTypeChange = (typeId: string) => { setSelectedInstanceType(typeId); const instanceType = instanceTypes.find(t => t.id === typeId); if (instanceType) { setInstanceConfig(instanceType.defaultConfig); } }; const handleConfigChange = (key: keyof InstanceConfig, value: number) => { setInstanceConfig(prev => ({ ...prev, [key]: value })); }; const handleNext = () => { if (currentStep < 3) { setCurrentStep(currentStep + 1); } }; const handlePrevious = () => { if (currentStep > 0) { setCurrentStep(currentStep - 1); } }; const handleSubmit = async () => { setLoading(true); try { const order = await instanceDataSource.createOrder( selectedInstanceType, instanceConfig, billingCycle, paymentMethod ); message.success('Order created successfully!'); setCurrentStep(4); } catch (error) { message.error('Failed to create order'); } finally { setLoading(false); } }; const renderInstanceTypeSelection = () => ( {instanceTypes.map(type => ( handleInstanceTypeChange(type.id)} >
{type.icon}
{type.name} {type.cupSize} {type.recommended && ( 推荐 )} {type.description} {isAdmin && ( <>
CPU {type.defaultConfig.cpu} 核
内存 {type.defaultConfig.memory} GB
存储 {type.defaultConfig.storage} GB
带宽 {type.defaultConfig.bandwidth} Mbps
)}
))}
); const renderInstanceConfig = () => ( CPU核心数}> handleConfigChange('memory', value)} options={configOptions.memoryOptions.map(option => ({ value: option, label: `${option} GB` }))} /> 存储}> handleConfigChange('bandwidth', value)} options={configOptions.bandwidthOptions.map(option => ({ value: option, label: `${option} Mbps` }))} /> setBillingCycle(e.target.value)} buttonStyle="solid" size="large" > 月付 ¥{price.monthly} 年付 ¥{price.yearly} (省{price.discount}%) ); const renderPaymentMethod = () => ( setPaymentMethod(e.target.value)} style={{ width: '100%' }} > setPaymentMethod('creditCard')} > 信用卡 支持Visa、Mastercard、American Express等 setPaymentMethod('alipay')} > 支付宝 使用支付宝扫码支付 setPaymentMethod('wechat')} > 微信支付 使用微信扫码支付 ); const renderOrderSummary = () => { const instanceType = instanceTypes.find(t => t.id === selectedInstanceType); return (
实例类型 {instanceType?.name}
CPU {instanceConfig.cpu} 核
内存 {instanceConfig.memory} GB
存储 {instanceConfig.storage} GB
带宽 {instanceConfig.bandwidth} Mbps
计费周期 {billingCycle === 'monthly' ? '月付' : '年付'}
支付方式 {paymentMethod === 'creditCard' ? '信用卡' : paymentMethod === 'alipay' ? '支付宝' : '微信支付'}
总计 ¥{billingCycle === 'monthly' ? price.monthly : price.yearly}
); }; const renderSuccess = () => (
订单创建成功! 您的实例将在支付完成后立即激活
); // 根据用户角色生成步骤 const steps = isAdmin ? [ { title: '选择实例类型' }, { title: '配置实例' }, { title: '选择支付方式' }, { title: '确认订单' }, { title: '购买成功' } ] : [ { title: '选择套餐' }, { title: '选择支付方式' }, { title: '确认订单' }, { title: '购买成功' } ]; // 处理下一步逻辑 const handleNext = () => { if (isAdmin) { if (currentStep < 3) { setCurrentStep(currentStep + 1); } } else { if (currentStep < 2) { setCurrentStep(currentStep + 1); } } }; // 处理上一步逻辑 const handlePrevious = () => { if (currentStep > 0) { setCurrentStep(currentStep - 1); } }; return (
<ServerOutlined style={{ marginRight: '8px' }} /> 实例购买 {isAdmin ? '选择适合您业务需求的实例配置' : '选择适合您业务需求的套餐'}
{currentStep === 0 && renderInstanceTypeSelection()} {isAdmin && currentStep === 1 && renderInstanceConfig()} {currentStep === (isAdmin ? 2 : 1) && renderPaymentMethod()} {currentStep === (isAdmin ? 3 : 2) && renderOrderSummary()} {currentStep === (isAdmin ? 4 : 3) && renderSuccess()}
{currentStep > 0 && currentStep < (isAdmin ? 4 : 3) && ( )} {currentStep < (isAdmin ? 3 : 2) && ( )} {currentStep === (isAdmin ? 3 : 2) && ( )}
); }; export default InstancePurchase;