22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 1 | |
| 2 | // export default Recharge; |
| 3 | import React, { useState } from 'react'; |
| 4 | import axios from 'axios'; |
| 5 | import { useUser } from '../../context/UserContext'; |
| 6 | |
22301009 | 4952a0f | 2025-06-07 18:58:16 +0800 | [diff] [blame] | 7 | const UserRecharge = () => { |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 8 | const [amount, setAmount] = useState(''); |
| 9 | const [loading, setLoading] = useState(false); |
| 10 | const [message, setMessage] = useState(''); |
| 11 | const [showPayDialog, setShowPayDialog] = useState(false); |
| 12 | const [payProcessing, setPayProcessing] = useState(false); |
| 13 | |
| 14 | const { user } = useUser(); |
| 15 | const userId = user?.userId; |
| 16 | |
| 17 | // 点击提交充值,打开模拟支付弹窗 |
| 18 | const handleSubmit = () => { |
| 19 | if (!userId) { |
| 20 | setMessage('未登录,无法充值'); |
| 21 | return; |
| 22 | } |
| 23 | if (!amount || isNaN(amount) || Number(amount) <= 0) { |
| 24 | setMessage('请输入有效的充值金额'); |
| 25 | return; |
| 26 | } |
| 27 | setMessage(''); |
| 28 | setShowPayDialog(true); |
| 29 | }; |
| 30 | |
| 31 | // 模拟支付弹窗点击“确认支付” |
| 32 | const handlePayConfirm = async () => { |
| 33 | setPayProcessing(true); |
| 34 | setMessage(''); |
| 35 | |
| 36 | // 模拟支付等待 2 秒 |
| 37 | setTimeout(async () => { |
| 38 | try { |
| 39 | // 支付成功后调用后端充值接口 |
| 40 | const response = await axios.post('/echo/user/recharge', null, { |
| 41 | params: { |
| 42 | userId, |
| 43 | amount: Number(amount), |
| 44 | }, |
| 45 | }); |
| 46 | |
| 47 | setMessage(response.data.message || '充值成功!'); |
| 48 | setAmount(''); |
| 49 | } catch (error) { |
| 50 | setMessage(error.response?.data?.message || '充值失败,请重试'); |
| 51 | } finally { |
| 52 | setPayProcessing(false); |
| 53 | setShowPayDialog(false); |
| 54 | } |
| 55 | }, 2000); |
| 56 | }; |
| 57 | |
| 58 | // 取消支付弹窗 |
| 59 | const handlePayCancel = () => { |
| 60 | setShowPayDialog(false); |
| 61 | setMessage('支付已取消'); |
| 62 | }; |
| 63 | |
| 64 | return ( |
| 65 | <div className="recharge-page" style={{ maxWidth: 400, margin: 'auto', padding: 20 }}> |
| 66 | <h2>充值服务</h2> |
| 67 | <div style={{ marginBottom: 12 }}> |
| 68 | <label> |
| 69 | 充值金额: |
| 70 | <input |
| 71 | type="number" |
| 72 | min="1" |
| 73 | value={amount} |
| 74 | onChange={(e) => setAmount(e.target.value)} |
| 75 | disabled={loading || payProcessing} |
| 76 | style={{ marginLeft: 8, width: 120 }} |
| 77 | /> |
| 78 | </label> |
| 79 | </div> |
| 80 | <button onClick={handleSubmit} disabled={loading || payProcessing}> |
| 81 | {loading || payProcessing ? '处理中...' : '提交充值'} |
| 82 | </button> |
| 83 | |
| 84 | {message && <p style={{ marginTop: 12 }}>{message}</p>} |
| 85 | |
| 86 | {/* 模拟支付弹窗 */} |
| 87 | {showPayDialog && ( |
| 88 | <div |
| 89 | style={{ |
| 90 | position: 'fixed', |
| 91 | top: 0, left: 0, right: 0, bottom: 0, |
| 92 | backgroundColor: 'rgba(0,0,0,0.5)', |
| 93 | display: 'flex', |
| 94 | justifyContent: 'center', |
| 95 | alignItems: 'center', |
| 96 | zIndex: 1000, |
| 97 | }} |
| 98 | > |
| 99 | <div |
| 100 | style={{ |
| 101 | backgroundColor: '#fff', |
| 102 | padding: 20, |
| 103 | borderRadius: 8, |
| 104 | width: 300, |
| 105 | textAlign: 'center', |
| 106 | boxShadow: '0 0 10px rgba(0,0,0,0.25)', |
| 107 | }} |
| 108 | > |
| 109 | <h3>模拟支付</h3> |
| 110 | <p>支付金额:{amount} 元</p> |
| 111 | |
| 112 | {payProcessing ? ( |
| 113 | <p>支付处理中,请稍候...</p> |
| 114 | ) : ( |
| 115 | <> |
| 116 | <button onClick={handlePayConfirm} style={{ marginRight: 10 }}> |
| 117 | 确认支付 |
| 118 | </button> |
| 119 | <button onClick={handlePayCancel}>取消</button> |
| 120 | </> |
| 121 | )} |
| 122 | </div> |
| 123 | </div> |
| 124 | )} |
| 125 | </div> |
| 126 | ); |
| 127 | }; |
| 128 | |
22301009 | 4952a0f | 2025-06-07 18:58:16 +0800 | [diff] [blame] | 129 | export default UserRecharge; |