You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
two_project/src/main/java/com/mathgenerator/controller/RegisterController.java

95 lines
3.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.mathgenerator.controller;
import com.mathgenerator.service.UserService;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.io.IOException;
public class RegisterController {
private final UserService userService = new UserService();
private String sentCode; // 用于存储已发送的验证码
@FXML private TextField usernameField;
@FXML private TextField emailField;
@FXML private TextField verificationCodeField;
@FXML private PasswordField passwordField;
@FXML private PasswordField confirmPasswordField;
@FXML private Button sendCodeButton;
@FXML private Button registerButton;
@FXML private Button backToLoginButton;
@FXML private Label statusLabel;
@FXML
private void handleSendCodeAction(ActionEvent event) {
String email = emailField.getText();
if (email.isEmpty() || !email.contains("@")) {
statusLabel.setText("请输入一个有效的邮箱地址!");
return;
}
// 调用后端服务发送验证码(模拟)
this.sentCode = userService.sendVerificationCode(email);
statusLabel.setText("验证码已发送(请查看控制台输出)。");
sendCodeButton.setDisable(true); // 防止重复点击
}
@FXML
private void handleRegisterAction(ActionEvent event) {
// 1. 字段校验
if (usernameField.getText().isEmpty() || emailField.getText().isEmpty() ||
verificationCodeField.getText().isEmpty() || passwordField.getText().isEmpty()) {
statusLabel.setText("所有字段都不能为空!");
return;
}
if (!passwordField.getText().equals(confirmPasswordField.getText())) {
statusLabel.setText("两次输入的密码不匹配!");
return;
}
if (this.sentCode == null || !this.sentCode.equals(verificationCodeField.getText())) {
statusLabel.setText("验证码错误!");
return;
}
if (!UserService.isPasswordValid(passwordField.getText())) {
statusLabel.setText("密码格式错误必须为6-10位且包含大小写字母和数字。");
return;
}
// 2. 调用后端服务进行注册
boolean success = userService.register(
usernameField.getText(),
emailField.getText(),
passwordField.getText()
);
// 3. 根据结果更新UI
if (success) {
statusLabel.setText("注册成功!请返回登录。");
registerButton.setDisable(true);
} else {
statusLabel.setText("注册失败:用户名或邮箱已被占用。");
}
}
@FXML
private void handleBackToLoginAction(ActionEvent event) {
loadScene("/com/mathgenerator/view/LoginView.fxml");
}
private void loadScene(String fxmlPath) {
try {
Parent root = FXMLLoader.load(getClass().getResource(fxmlPath));
Stage stage = (Stage) backToLoginButton.getScene().getWindow();
stage.setScene(new Scene(root));
} catch (IOException e) {
e.printStackTrace();
}
}
}