blob: 0df1837497c589154d6456081ed8ad878cb1bf54 [file] [log] [blame]
2230100901d3ff92025-06-07 16:16:26 +08001// import React, { useState } from 'react';
2// import axios from 'axios';
3// import { useUser } from '../../context/UserContext';
4
5// const Recharge = () => {
6// const [amount, setAmount] = useState('');
7// const [loading, setLoading] = useState(false);
8// const [message, setMessage] = useState('');
9
10// // 从上下文获取当前登录用户
11// const { user } = useUser();
12// const userId = user?.userId;
13
14// const handleRecharge = async () => {
15// if (!userId) {
16// setMessage('未登录,无法充值');
17// return;
18// }
19// if (!amount || isNaN(amount) || Number(amount) <= 0) {
20// setMessage('请输入有效的充值金额');
21// return;
22// }
23// setLoading(true);
24// setMessage('');
25// try {
26// // 充值接口调用,注意后端用的是请求参数形式传递 userId 和 amount
27// const response = await axios.post('/echo/user/recharge', null, {
28// params: {
29// userId,
30// amount: Number(amount),
31// },
32// });
33// setMessage(response.data.message || '充值成功!');
34// setAmount('');
35// } catch (error) {
36// setMessage(error.response?.data?.message || '充值失败,请重试');
37// } finally {
38// setLoading(false);
39// }
40// };
41
42// return (
43// <div className="recharge-page">
44// <h2>充值服务</h2>
45// <div>
46// <label>
47// 充值金额:
48// <input
49// type="number"
50// min="1"
51// value={amount}
52// onChange={(e) => setAmount(e.target.value)}
53// disabled={loading}
54// />
55// </label>
56// </div>
57// <button onClick={handleRecharge} disabled={loading}>
58// {loading ? '处理中...' : '提交充值'}
59// </button>
60// {message && <p>{message}</p>}
61// </div>
62// );
63// };
64
65// export default Recharge;
66import React, { useState } from 'react';
67import axios from 'axios';
68import { useUser } from '../../context/UserContext';
69
70const Recharge = () => {
71 const [amount, setAmount] = useState('');
72 const [loading, setLoading] = useState(false);
73 const [message, setMessage] = useState('');
74 const [showPayDialog, setShowPayDialog] = useState(false);
75 const [payProcessing, setPayProcessing] = useState(false);
76
77 const { user } = useUser();
78 const userId = user?.userId;
79
80 // 点击提交充值,打开模拟支付弹窗
81 const handleSubmit = () => {
82 if (!userId) {
83 setMessage('未登录,无法充值');
84 return;
85 }
86 if (!amount || isNaN(amount) || Number(amount) <= 0) {
87 setMessage('请输入有效的充值金额');
88 return;
89 }
90 setMessage('');
91 setShowPayDialog(true);
92 };
93
94 // 模拟支付弹窗点击“确认支付”
95 const handlePayConfirm = async () => {
96 setPayProcessing(true);
97 setMessage('');
98
99 // 模拟支付等待 2 秒
100 setTimeout(async () => {
101 try {
102 // 支付成功后调用后端充值接口
103 const response = await axios.post('/echo/user/recharge', null, {
104 params: {
105 userId,
106 amount: Number(amount),
107 },
108 });
109
110 setMessage(response.data.message || '充值成功!');
111 setAmount('');
112 } catch (error) {
113 setMessage(error.response?.data?.message || '充值失败,请重试');
114 } finally {
115 setPayProcessing(false);
116 setShowPayDialog(false);
117 }
118 }, 2000);
119 };
120
121 // 取消支付弹窗
122 const handlePayCancel = () => {
123 setShowPayDialog(false);
124 setMessage('支付已取消');
125 };
126
127 return (
128 <div className="recharge-page" style={{ maxWidth: 400, margin: 'auto', padding: 20 }}>
129 <h2>充值服务</h2>
130 <div style={{ marginBottom: 12 }}>
131 <label>
132 充值金额:
133 <input
134 type="number"
135 min="1"
136 value={amount}
137 onChange={(e) => setAmount(e.target.value)}
138 disabled={loading || payProcessing}
139 style={{ marginLeft: 8, width: 120 }}
140 />
141 </label>
142 </div>
143 <button onClick={handleSubmit} disabled={loading || payProcessing}>
144 {loading || payProcessing ? '处理中...' : '提交充值'}
145 </button>
146
147 {message && <p style={{ marginTop: 12 }}>{message}</p>}
148
149 {/* 模拟支付弹窗 */}
150 {showPayDialog && (
151 <div
152 style={{
153 position: 'fixed',
154 top: 0, left: 0, right: 0, bottom: 0,
155 backgroundColor: 'rgba(0,0,0,0.5)',
156 display: 'flex',
157 justifyContent: 'center',
158 alignItems: 'center',
159 zIndex: 1000,
160 }}
161 >
162 <div
163 style={{
164 backgroundColor: '#fff',
165 padding: 20,
166 borderRadius: 8,
167 width: 300,
168 textAlign: 'center',
169 boxShadow: '0 0 10px rgba(0,0,0,0.25)',
170 }}
171 >
172 <h3>模拟支付</h3>
173 <p>支付金额:{amount} 元</p>
174
175 {payProcessing ? (
176 <p>支付处理中,请稍候...</p>
177 ) : (
178 <>
179 <button onClick={handlePayConfirm} style={{ marginRight: 10 }}>
180 确认支付
181 </button>
182 <button onClick={handlePayCancel}>取消</button>
183 </>
184 )}
185 </div>
186 </div>
187 )}
188 </div>
189 );
190};
191
192export default Recharge;