| 22301110 | e361be5 | 2025-06-08 15:24:14 +0800 | [diff] [blame^] | 1 | import { useState, useEffect } from "react"; |
| 2 | import "./style.css"; |
| 3 | import { useNavigate } from "react-router-dom"; // 👈 加在顶部 import 区域 |
| 4 | import React from 'react'; |
| 5 | import md5 from 'md5'; |
| 6 | import bcrypt from "bcryptjs"; |
| 7 | export default function RegisterPage() { |
| 8 | const navigate = useNavigate(); // 👈 初始化导航器 |
| 9 | |
| 10 | const [email, setEmail] = useState(""); |
| 11 | const [password, setPassword] = useState(""); |
| 12 | const [code, setCode] = useState(""); |
| 13 | const [sent, setSent] = useState(false); |
| 14 | const [countdown, setCountdown] = useState(0); |
| 15 | const [confirmPassword, setConfirmPassword] = useState(""); |
| 16 | |
| 17 | const hashPassword = async (password) => { |
| 18 | try { |
| 19 | // 使用 10 作为盐的工作因子(越大越慢,更安全) |
| 20 | const encryptedPassword = await bcrypt.hash(password, 10); |
| 21 | console.log('Encrypted password with bcryptjs:', encryptedPassword); |
| 22 | return encryptedPassword; |
| 23 | } catch (error) { |
| 24 | console.error('Error encrypting password:', error); |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | |
| 29 | useEffect(() => { |
| 30 | let timer; |
| 31 | if (countdown > 0) { |
| 32 | timer = setInterval(() => { |
| 33 | setCountdown((prev) => prev - 1); |
| 34 | }, 1000); |
| 35 | } else if (countdown === 0 && sent) { |
| 36 | setSent(false); |
| 37 | } |
| 38 | |
| 39 | return () => clearInterval(timer); |
| 40 | }, [countdown, sent]); |
| 41 | |
| 42 | const handleSendCode = async () => { |
| 43 | if (!email) { |
| 44 | alert("请输入邮箱"); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | try { |
| 49 | const res = await fetch("http://localhost:6001/send-code", { |
| 50 | method: "POST", |
| 51 | headers: { "Content-Type": "application/json" }, |
| 52 | body: JSON.stringify({ email }), |
| 53 | }); |
| 54 | |
| 55 | const data = await res.json(); |
| 56 | if (data.success) { |
| 57 | alert("验证码已发送到邮箱"); |
| 58 | setSent(true); |
| 59 | setCountdown(60); |
| 60 | } else { |
| 61 | alert("发送失败:" + data.message); |
| 62 | } |
| 63 | } catch (error) { |
| 64 | console.error(error); |
| 65 | alert("网络错误"); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | const handleGoToPayment = () => { |
| 70 | navigate("/payment"); |
| 71 | }; |
| 72 | |
| 73 | const handleSubmit = async (e) => { |
| 74 | e.preventDefault(); |
| 75 | |
| 76 | if (!email || !password || !confirmPassword || !code) { |
| 77 | alert("请填写完整信息"); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | if (password !== confirmPassword) { |
| 82 | alert("两次输入的密码不一致"); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | const hash_Password = await bcrypt.hash(password, 10); |
| 87 | |
| 88 | try { |
| 89 | // 1. 校验是否能注册 |
| 90 | const res = await fetch("http://localhost:6001/register", { |
| 91 | method: "POST", |
| 92 | headers: { "Content-Type": "application/json" }, |
| 93 | body: JSON.stringify({ email, password, code }), |
| 94 | }); |
| 95 | |
| 96 | const data = await res.json(); |
| 97 | |
| 98 | if (!res.ok || !data.success) { |
| 99 | alert("注册失败:" + (data.message || "未知错误")); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | // 2. 获取动态 notifyUrl |
| 104 | const ngrokRes = await fetch("http://localhost:6001/ngrok-url"); |
| 105 | const ngrokData = await ngrokRes.json(); |
| 106 | const notifyUrl = (ngrokData.backUrl ? ngrokData.backUrl.replace(/\/$/, "") : "http://localhost:6001") + "/paySuccess"; |
| 107 | console.log("11111111",hash_Password); |
| 108 | // 3. 下单 |
| 109 | const payId = getPayId(); |
| 110 | const orderResponse = await fetch("http://localhost:6001/user/createOrder", { |
| 111 | method: "POST", |
| 112 | headers: { "Content-Type": "application/json" }, |
| 113 | body: JSON.stringify({ |
| 114 | orderid: payId, |
| 115 | paytype: parseInt(payType), |
| 116 | money: price, |
| 117 | notifyUrl: notifyUrl, |
| 118 | returnUrl: "http://localhost:8000/user/login", |
| 119 | user_name: email, |
| 120 | password: hash_Password, |
| 121 | }), |
| 122 | }); |
| 123 | |
| 124 | const result = await orderResponse.json(); |
| 125 | |
| 126 | if (result.code === 0 && result.data) { |
| 127 | window.open(result.data, "_blank"); |
| 128 | } else { |
| 129 | alert("创建订单失败:" + (result.msg || "未知错误")); |
| 130 | } |
| 131 | } catch (error) { |
| 132 | console.error("提交失败:", error); |
| 133 | alert("网络错误或服务器异常"); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | const [payType, setPayType] = useState('1'); |
| 138 | const [price, setPrice] = useState('0.01'); |
| 139 | const username = localStorage.getItem('username') || 'yky'; |
| 140 | |
| 141 | const getCurrentTimeAs14DigitInteger = () => { |
| 142 | const now = new Date(); |
| 143 | return now.toISOString().replace(/[-T:.Z]/g, '').slice(0, 14); |
| 144 | }; |
| 145 | |
| 146 | const getPayId = () => { |
| 147 | return getCurrentTimeAs14DigitInteger() + username; |
| 148 | }; |
| 149 | |
| 150 | return ( |
| 151 | <div className="container"> |
| 152 | <div className="form-box"> |
| 153 | <h1 className="title">注册 PTStation</h1> |
| 154 | <p className="promo"> |
| 155 | 限时活动:<span className="highlight">0.01元开通会员,终身享资源免费!</span> |
| 156 | </p> |
| 157 | <form onSubmit={handleSubmit}> |
| 158 | {/* 表单内容 */} |
| 159 | <label>邮箱</label> |
| 160 | <input |
| 161 | type="email" |
| 162 | value={email} |
| 163 | onChange={(e) => setEmail(e.target.value)} |
| 164 | required |
| 165 | /> |
| 166 | |
| 167 | <label>验证码</label> |
| 168 | <div className="code-box"> |
| 169 | <input |
| 170 | type="text" |
| 171 | value={code} |
| 172 | onChange={(e) => setCode(e.target.value)} |
| 173 | required |
| 174 | /> |
| 175 | <button type="button" onClick={handleSendCode} disabled={sent}> |
| 176 | {sent ? `重新发送(${countdown}s)` : "发送验证码"} |
| 177 | </button> |
| 178 | </div> |
| 179 | |
| 180 | <label>密码</label> |
| 181 | <input |
| 182 | type="password" |
| 183 | value={password} |
| 184 | onChange={(e) => setPassword(e.target.value)} |
| 185 | required |
| 186 | /> |
| 187 | |
| 188 | <label>重复密码</label> |
| 189 | <input |
| 190 | type="password" |
| 191 | value={confirmPassword} |
| 192 | onChange={(e) => setConfirmPassword(e.target.value)} |
| 193 | required |
| 194 | /> |
| 195 | |
| 196 | <button type="submit" className="submit-btn"> |
| 197 | 立即支付0.01,注册并开通会员 |
| 198 | </button> |
| 199 | </form> |
| 200 | |
| 201 | </div> |
| 202 | </div> |
| 203 | ); |
| 204 | } |