blob: b1dff240b8387a6d5618be3bdef8d6c152959d83 [file] [log] [blame]
2230100901d3ff92025-06-07 16:16:26 +08001
223010091e2aea72025-06-08 16:35:54 +08002// // export default Recharge;
3// import React, { useState } from 'react';
4// import axios from 'axios';
5// import { useUser } from '../../context/UserContext';
6// import toast from 'react-hot-toast';
7// import { confirmAlert } from 'react-confirm-alert';
8// import 'react-confirm-alert/src/react-confirm-alert.css';
9
10// const UserRecharge = () => {
11// const [amount, setAmount] = useState('');
12// const [loading, setLoading] = useState(false);
13// const [message, setMessage] = useState('');
14// const [showPayDialog, setShowPayDialog] = useState(false);
15// const [payProcessing, setPayProcessing] = useState(false);
16
17// const { user } = useUser();
18// const userId = user?.userId;
19
20// // 点击提交充值,打开模拟支付弹窗
21// const handleSubmit = () => {
22// if (!userId) {
23// setMessage('未登录,无法充值');
24// return;
25// }
26// if (!amount || isNaN(amount) || Number(amount) <= 0) {
27// setMessage('请输入有效的充值金额');
28// return;
29// }
30// setMessage('');
31// setShowPayDialog(true);
32// };
33
34// // 模拟支付弹窗点击“确认支付”
35// const handlePayConfirm = async () => {
36// setPayProcessing(true);
37// setMessage('');
38
39// // 模拟支付等待 2 秒
40// setTimeout(async () => {
41// try {
42// // 支付成功后调用后端充值接口
43// const response = await axios.post('/echo/user/recharge', null, {
44// params: {
45// userId,
46// amount: Number(amount),
47// },
48// });
49
50// setMessage(response.data.message || '充值成功!');
51// setAmount('');
52// } catch (error) {
53// setMessage(error.response?.data?.message || '充值失败,请重试');
54// } finally {
55// setPayProcessing(false);
56// setShowPayDialog(false);
57// }
58// }, 2000);
59// };
60
61// // 取消支付弹窗
62// const handlePayCancel = () => {
63// setShowPayDialog(false);
64// setMessage('支付已取消');
65// };
66
67// return (
68// <div className="recharge-page" style={{ maxWidth: 400, margin: 'auto', padding: 20 }}>
69// <h2>充值服务</h2>
70// <div style={{ marginBottom: 12 }}>
71// <label>
72// 充值金额:
73// <input
74// type="number"
75// min="1"
76// value={amount}
77// onChange={(e) => setAmount(e.target.value)}
78// disabled={loading || payProcessing}
79// style={{ marginLeft: 8, width: 120 }}
80// />
81// </label>
82// </div>
83// <button onClick={handleSubmit} disabled={loading || payProcessing}>
84// {loading || payProcessing ? '处理中...' : '提交充值'}
85// </button>
86
87// {message && <p style={{ marginTop: 12 }}>{message}</p>}
88
89// {/* 模拟支付弹窗 */}
90// {showPayDialog && (
91// <div
92// style={{
93// position: 'fixed',
94// top: 0, left: 0, right: 0, bottom: 0,
95// backgroundColor: 'rgba(0,0,0,0.5)',
96// display: 'flex',
97// justifyContent: 'center',
98// alignItems: 'center',
99// zIndex: 1000,
100// }}
101// >
102// <div
103// style={{
104// backgroundColor: '#fff',
105// padding: 20,
106// borderRadius: 8,
107// width: 300,
108// textAlign: 'center',
109// boxShadow: '0 0 10px rgba(0,0,0,0.25)',
110// }}
111// >
112// <h3>模拟支付</h3>
113// <p>支付金额:{amount} 元</p>
114
115// {payProcessing ? (
116// <p>支付处理中,请稍候...</p>
117// ) : (
118// <>
119// <button onClick={handlePayConfirm} style={{ marginRight: 10 }}>
120// 确认支付
121// </button>
122// <button onClick={handlePayCancel}>取消</button>
123// </>
124// )}
125// </div>
126// </div>
127// )}
128// </div>
129// );
130// };
131
132// export default UserRecharge;
2230100901d3ff92025-06-07 16:16:26 +0800133import React, { useState } from 'react';
134import axios from 'axios';
135import { useUser } from '../../context/UserContext';
223010091e2aea72025-06-08 16:35:54 +0800136import toast from 'react-hot-toast';
137import { confirmAlert } from 'react-confirm-alert';
138import 'react-confirm-alert/src/react-confirm-alert.css';
2230100901d3ff92025-06-07 16:16:26 +0800139
223010094952a0f2025-06-07 18:58:16 +0800140const UserRecharge = () => {
2230100901d3ff92025-06-07 16:16:26 +0800141 const [amount, setAmount] = useState('');
2230100901d3ff92025-06-07 16:16:26 +0800142 const [payProcessing, setPayProcessing] = useState(false);
143
144 const { user } = useUser();
145 const userId = user?.userId;
146
223010091e2aea72025-06-08 16:35:54 +0800147 // 点击提交充值
2230100901d3ff92025-06-07 16:16:26 +0800148 const handleSubmit = () => {
149 if (!userId) {
223010091e2aea72025-06-08 16:35:54 +0800150 toast.error('未登录,无法充值');
2230100901d3ff92025-06-07 16:16:26 +0800151 return;
152 }
153 if (!amount || isNaN(amount) || Number(amount) <= 0) {
223010091e2aea72025-06-08 16:35:54 +0800154 toast.error('请输入有效的充值金额');
2230100901d3ff92025-06-07 16:16:26 +0800155 return;
156 }
223010091e2aea72025-06-08 16:35:54 +0800157
158 confirmAlert({
159 title: '确认支付',
160 message: `确认充值 ${amount} 元吗?`,
161 buttons: [
162 {
163 label: '确认支付',
164 onClick: () => handlePayConfirm(),
165 },
166 {
167 label: '取消',
168 onClick: () => toast('已取消充值'),
169 },
170 ],
171 });
2230100901d3ff92025-06-07 16:16:26 +0800172 };
173
2230100901d3ff92025-06-07 16:16:26 +0800174 const handlePayConfirm = async () => {
175 setPayProcessing(true);
2230100901d3ff92025-06-07 16:16:26 +0800176
223010091e2aea72025-06-08 16:35:54 +0800177 toast.loading('正在处理支付...', { id: 'recharge' });
178
2230100901d3ff92025-06-07 16:16:26 +0800179 setTimeout(async () => {
180 try {
2230100901d3ff92025-06-07 16:16:26 +0800181 const response = await axios.post('/echo/user/recharge', null, {
182 params: {
183 userId,
184 amount: Number(amount),
185 },
186 });
187
223010091e2aea72025-06-08 16:35:54 +0800188 toast.success(response.data.message || '充值成功!', { id: 'recharge' });
2230100901d3ff92025-06-07 16:16:26 +0800189 setAmount('');
190 } catch (error) {
223010091e2aea72025-06-08 16:35:54 +0800191 toast.error(error.response?.data?.message || '充值失败,请重试', { id: 'recharge' });
2230100901d3ff92025-06-07 16:16:26 +0800192 } finally {
193 setPayProcessing(false);
2230100901d3ff92025-06-07 16:16:26 +0800194 }
195 }, 2000);
196 };
197
2230100901d3ff92025-06-07 16:16:26 +0800198 return (
199 <div className="recharge-page" style={{ maxWidth: 400, margin: 'auto', padding: 20 }}>
200 <h2>充值服务</h2>
201 <div style={{ marginBottom: 12 }}>
202 <label>
203 充值金额:
204 <input
205 type="number"
206 min="1"
207 value={amount}
208 onChange={(e) => setAmount(e.target.value)}
223010091e2aea72025-06-08 16:35:54 +0800209 disabled={payProcessing}
2230100901d3ff92025-06-07 16:16:26 +0800210 style={{ marginLeft: 8, width: 120 }}
211 />
212 </label>
213 </div>
223010091e2aea72025-06-08 16:35:54 +0800214 <button onClick={handleSubmit} disabled={payProcessing}>
215 {payProcessing ? '处理中...' : '提交充值'}
2230100901d3ff92025-06-07 16:16:26 +0800216 </button>
2230100901d3ff92025-06-07 16:16:26 +0800217 </div>
218 );
219};
220
223010094952a0f2025-06-07 18:58:16 +0800221export default UserRecharge;