parent
bd77d69c6a
commit
37047295fa
@ -0,0 +1,142 @@
|
||||
package com.example.myapp.controller;
|
||||
|
||||
import com.example.myapp.Main;
|
||||
import com.example.myapp.service.EmailService;
|
||||
import com.example.myapp.service.UserService;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.*;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class RegisterController {
|
||||
@FXML private TextField emailField;
|
||||
@FXML private Button sendCodeBtn;
|
||||
@FXML private TextField codeField;
|
||||
@FXML private Button verifyBtn;
|
||||
@FXML private Label statusLabel;
|
||||
|
||||
private final UserService userService = UserService.getInstance();
|
||||
private EmailService emailService;
|
||||
|
||||
public RegisterController(){
|
||||
try {
|
||||
emailService = new EmailService();
|
||||
} catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void onSendCode(){
|
||||
String email = emailField.getText().trim();
|
||||
|
||||
// 验证邮箱格式
|
||||
if(email.isEmpty()){
|
||||
statusLabel.setText("请输入邮箱地址");
|
||||
statusLabel.setStyle("-fx-text-fill: red;");
|
||||
return;
|
||||
}
|
||||
|
||||
if(!isValidEmail(email)){
|
||||
statusLabel.setText("请输入有效的邮箱地址");
|
||||
statusLabel.setStyle("-fx-text-fill: red;");
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查邮箱是否已被注册
|
||||
if(userService.emailExists(email)){
|
||||
// 检查用户是否已完成注册(设置了密码)
|
||||
if(userService.isUserRegistered(email)){
|
||||
statusLabel.setText("该邮箱已被注册,请直接登录");
|
||||
statusLabel.setStyle("-fx-text-fill: red;");
|
||||
return;
|
||||
} else {
|
||||
// 用户已存在但未完成注册,可以重新发送验证码
|
||||
statusLabel.setText("该邮箱正在注册流程中,重新发送验证码");
|
||||
statusLabel.setStyle("-fx-text-fill: orange;");
|
||||
}
|
||||
}
|
||||
|
||||
String code = generateCode();
|
||||
userService.createPendingUser(email, code);
|
||||
try{
|
||||
emailService.sendRegistrationCode(email, code);
|
||||
statusLabel.setText("注册码已发送到您的邮箱,请查收");
|
||||
statusLabel.setStyle("-fx-text-fill: green;");
|
||||
|
||||
// 禁用发送按钮一段时间,防止重复发送
|
||||
disableSendButtonTemporarily();
|
||||
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
statusLabel.setText("发送失败: " + e.getMessage());
|
||||
statusLabel.setStyle("-fx-text-fill: red;");
|
||||
}
|
||||
}
|
||||
|
||||
private String generateCode(){
|
||||
Random r = new Random();
|
||||
int v = 100000 + r.nextInt(900000);
|
||||
return String.valueOf(v);
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void onVerify(){
|
||||
String email = emailField.getText().trim();
|
||||
String code = codeField.getText().trim();
|
||||
|
||||
if(email.isEmpty() || code.isEmpty()){
|
||||
statusLabel.setText("请输入邮箱和验证码");
|
||||
statusLabel.setStyle("-fx-text-fill: red;");
|
||||
return;
|
||||
}
|
||||
|
||||
if(userService.verifyCode(email, code)){
|
||||
statusLabel.setText("验证通过,请设置密码");
|
||||
statusLabel.setStyle("-fx-text-fill: green;");
|
||||
try {
|
||||
Main.showSetPassword(email);
|
||||
} catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
statusLabel.setText("注册码错误或已过期");
|
||||
statusLabel.setStyle("-fx-text-fill: red;");
|
||||
}
|
||||
}
|
||||
@FXML
|
||||
public void onClear() {
|
||||
emailField.clear();
|
||||
codeField.clear();
|
||||
statusLabel.setText("");
|
||||
sendCodeBtn.setDisable(false);
|
||||
}
|
||||
@FXML
|
||||
public void onBackToLogin() {
|
||||
try {
|
||||
Main.showLogin();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValidEmail(String email) {
|
||||
String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
|
||||
return email.matches(emailRegex);
|
||||
}
|
||||
|
||||
private void disableSendButtonTemporarily() {
|
||||
sendCodeBtn.setDisable(true);
|
||||
new java.util.Timer().schedule(
|
||||
new java.util.TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
javafx.application.Platform.runLater(() -> {
|
||||
sendCodeBtn.setDisable(false);
|
||||
});
|
||||
}
|
||||
},
|
||||
60000 // 60秒后才能重新发送
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue