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.
pairedProject/src/main/java/com/wsf/mathapp/view/RegisterView.java

230 lines
7.0 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.wsf.mathapp.view;
import com.wsf.mathapp.controller.SceneManager;
import com.ybw.mathapp.util.EmailService;
import com.ybw.mathapp.util.Register;
import java.util.regex.Pattern;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import com.ybw.mathapp.util.LoginFileUtils;
public class RegisterView {
private Scene scene;
private final SceneManager sceneManager;
public RegisterView(SceneManager sceneManager) {
this.sceneManager = sceneManager;
createScene();
}
private void createScene() {
VBox root = new VBox(15);
root.setPadding(new Insets(30));
root.setAlignment(Pos.CENTER);
Label titleLabel = new Label("用户注册");
titleLabel.setFont(Font.font(20));
// 添加用户名输入框
TextField usernameField = new TextField();
usernameField.setPromptText("请输入用户名(3-20位字母、数字、下划线)");
usernameField.setMaxWidth(300);
usernameField.setPrefHeight(35);
TextField emailField = new TextField();
emailField.setPromptText("请输入邮箱");
emailField.setMaxWidth(300);
emailField.setPrefHeight(35);
Button sendCodeButton = new Button("发送验证码");
sendCodeButton.setStyle("-fx-background-color: #FF9800; -fx-text-fill: white;");
sendCodeButton.setPrefSize(120, 35);
TextField codeField = new TextField();
codeField.setPromptText("请输入验证码");
codeField.setMaxWidth(300);
codeField.setPrefHeight(35);
PasswordField passwordField = new PasswordField();
passwordField.setPromptText("请输入密码(6-10位含大小写字母和数字)");
passwordField.setMaxWidth(300);
passwordField.setPrefHeight(35);
PasswordField confirmPasswordField = new PasswordField();
confirmPasswordField.setPromptText("请再次输入密码");
confirmPasswordField.setMaxWidth(300);
confirmPasswordField.setPrefHeight(35);
Button registerButton = new Button("注册");
registerButton.setStyle("-fx-background-color: #4CAF50; -fx-text-fill: white; -fx-font-size: 14px;");
registerButton.setPrefSize(300, 40);
Button backButton = new Button("返回登录");
backButton.setStyle("-fx-background-color: #757575; -fx-text-fill: white;");
backButton.setPrefSize(300, 35);
Label statusLabel = new Label();
sendCodeButton.setOnAction(e -> {
String username = usernameField.getText().trim();
String email = emailField.getText().trim();
// 验证用户名
if (!isValidUsername(username)) {
showError(statusLabel, "用户名只包含英文字母和数字)");
return;
}
// 检查用户名是否已存在
if (LoginFileUtils.isNameRegistered(username)) {
showError(statusLabel, "用户名已存在,请选择其他用户名!");
return;
}
if (!EmailService.isValidEmail(email)) {
showError(statusLabel, "请输入有效的邮箱地址!");
return;
}
if (LoginFileUtils.isEmailRegistered(email)){
showError(statusLabel, "该邮箱已注册,请直接登录!");
return;
}
// 生成并发送验证码
boolean sent = EmailService.sendCode(email);
if (sent) {
showSuccess(statusLabel, "验证码已发送到您的邮箱!");
// 禁用发送按钮60秒
sendCodeButton.setDisable(true);
startCountdown(sendCodeButton);
} else {
showError(statusLabel, "发送验证码失败,请稍后重试!");
}
});
registerButton.setOnAction(e -> {
String username = usernameField.getText().trim();
String email = emailField.getText().trim();
String code = codeField.getText().trim();
String password = passwordField.getText();
String confirmPassword = confirmPasswordField.getText();
// 验证用户名
if (!isValidUsername(username)) {
showError(statusLabel, "用户名只包含英文字母和数字");
return;
}
// 检查用户名是否已存在
if (LoginFileUtils.isNameRegistered((username))){
showError(statusLabel, "用户名已存在,请选择其他用户名!");
return;
}
if (!EmailService.isValidEmail(email)) {
showError(statusLabel, "请输入有效的邮箱地址!");
return;
}
if (LoginFileUtils.isEmailRegistered(email)){
showError(statusLabel, "该邮箱已注册,请直接登录!");
return;
}
if (username.isEmpty() || email.isEmpty() || code.isEmpty() || password.isEmpty()) {
showError(statusLabel, "请填写所有字段!");
return;
}
// 验证验证码
if (!EmailService.verifyCode(email, code)) {
showError(statusLabel, "验证码错误或已过期!");
return;
}
if (!Register.isVaildPassword(password)) {
showError(statusLabel, "密码要6-10位包含大小写字母和数字");
return;
}
if (!Register.isEqualPassword(password,confirmPassword)){
showError(statusLabel, "两次密码不一致");
return;
}
if (Register.register(username, email, password)) {
showSuccess(statusLabel, "注册成功!用户名: " + username);
sceneManager.showLoginView();
} else {
showError(statusLabel, "注册失败,请检查信息!");
}
});
backButton.setOnAction(e -> {
sceneManager.showLoginView();
});
root.getChildren().addAll(
titleLabel,
usernameField, // 添加用户名输入框
emailField,
sendCodeButton,
codeField,
passwordField,
confirmPasswordField,
registerButton,
backButton,
statusLabel
);
scene = new Scene(root, 400, 550); // 增加高度以适应新字段
}
private void startCountdown(Button button) {
new Thread(() -> {
try {
for (int i = 60; i > 0; i--) {
int finalI = i;
javafx.application.Platform.runLater(() -> {
button.setText(finalI + "秒后重发");
});
Thread.sleep(1000);
}
javafx.application.Platform.runLater(() -> {
button.setText("发送验证码");
button.setDisable(false);
});
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}).start();
}
private void showError(Label label, String message) {
label.setText(message);
label.setStyle("-fx-text-fill: red;");
}
private void showSuccess(Label label, String message) {
label.setText(message);
label.setStyle("-fx-text-fill: green;");
}
public Scene getScene() {
return scene;
}
public static boolean isValidUsername(String username) {
if (username == null || username.trim().isEmpty()) {
return false;
}
// 用户名规则:只能包含英文字母和数字
String usernameRegex = "^[a-zA-Z0-9]{3,100}$";
return Pattern.matches(usernameRegex, username.trim());
}
}