22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 1 | |
22301009 | 1e2aea7 | 2025-06-08 16:35:54 +0800 | [diff] [blame] | 2 | // // 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; |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 133 | import React, { useState } from 'react'; |
| 134 | import axios from 'axios'; |
| 135 | import { useUser } from '../../context/UserContext'; |
22301009 | 1e2aea7 | 2025-06-08 16:35:54 +0800 | [diff] [blame] | 136 | import toast from 'react-hot-toast'; |
| 137 | import { confirmAlert } from 'react-confirm-alert'; |
| 138 | import 'react-confirm-alert/src/react-confirm-alert.css'; |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 139 | |
22301009 | 4952a0f | 2025-06-07 18:58:16 +0800 | [diff] [blame] | 140 | const UserRecharge = () => { |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 141 | const [amount, setAmount] = useState(''); |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 142 | const [payProcessing, setPayProcessing] = useState(false); |
| 143 | |
| 144 | const { user } = useUser(); |
| 145 | const userId = user?.userId; |
| 146 | |
22301009 | 1e2aea7 | 2025-06-08 16:35:54 +0800 | [diff] [blame] | 147 | // 点击提交充值 |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 148 | const handleSubmit = () => { |
| 149 | if (!userId) { |
22301009 | 1e2aea7 | 2025-06-08 16:35:54 +0800 | [diff] [blame] | 150 | toast.error('未登录,无法充值'); |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | if (!amount || isNaN(amount) || Number(amount) <= 0) { |
22301009 | 1e2aea7 | 2025-06-08 16:35:54 +0800 | [diff] [blame] | 154 | toast.error('请输入有效的充值金额'); |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 155 | return; |
| 156 | } |
22301009 | 1e2aea7 | 2025-06-08 16:35:54 +0800 | [diff] [blame] | 157 | |
| 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 | }); |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 172 | }; |
| 173 | |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 174 | const handlePayConfirm = async () => { |
| 175 | setPayProcessing(true); |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 176 | |
22301009 | 1e2aea7 | 2025-06-08 16:35:54 +0800 | [diff] [blame] | 177 | toast.loading('正在处理支付...', { id: 'recharge' }); |
| 178 | |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 179 | setTimeout(async () => { |
| 180 | try { |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 181 | const response = await axios.post('/echo/user/recharge', null, { |
| 182 | params: { |
| 183 | userId, |
| 184 | amount: Number(amount), |
| 185 | }, |
| 186 | }); |
| 187 | |
22301009 | 1e2aea7 | 2025-06-08 16:35:54 +0800 | [diff] [blame] | 188 | toast.success(response.data.message || '充值成功!', { id: 'recharge' }); |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 189 | setAmount(''); |
| 190 | } catch (error) { |
22301009 | 1e2aea7 | 2025-06-08 16:35:54 +0800 | [diff] [blame] | 191 | toast.error(error.response?.data?.message || '充值失败,请重试', { id: 'recharge' }); |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 192 | } finally { |
| 193 | setPayProcessing(false); |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 194 | } |
| 195 | }, 2000); |
| 196 | }; |
| 197 | |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 198 | 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)} |
22301009 | 1e2aea7 | 2025-06-08 16:35:54 +0800 | [diff] [blame] | 209 | disabled={payProcessing} |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 210 | style={{ marginLeft: 8, width: 120 }} |
| 211 | /> |
| 212 | </label> |
| 213 | </div> |
22301009 | 1e2aea7 | 2025-06-08 16:35:54 +0800 | [diff] [blame] | 214 | <button onClick={handleSubmit} disabled={payProcessing}> |
| 215 | {payProcessing ? '处理中...' : '提交充值'} |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 216 | </button> |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 217 | </div> |
| 218 | ); |
| 219 | }; |
| 220 | |
22301009 | 4952a0f | 2025-06-07 18:58:16 +0800 | [diff] [blame] | 221 | export default UserRecharge; |