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.
PAIR/src/main/java/com/ui/RegisterPanel.java

80 lines
2.6 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.mathquiz.ui;
import com.mathquiz.util.EmailUtil;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
/**
* 注册面板
*/
public class RegisterPanel extends VBox {
private final TextField emailField = new TextField();
private final TextField codeField = new TextField();
private final PasswordField pwd1Field = new PasswordField();
private final PasswordField pwd2Field = new PasswordField();
public RegisterPanel(MainWindow mainWindow) {
setPadding(new Insets(20));
setSpacing(10);
getChildren().addAll(
new Label("注册"),
new Label("邮箱:"),
emailField,
new Button("发送注册码") {{
setOnAction(e -> sendCodeAction(mainWindow));
}},
new Label("注册码:"),
codeField,
new Label("密码6-10位含大小写+数字):"),
pwd1Field,
new Label("确认密码:"),
pwd2Field,
new Button("完成注册") {{
setOnAction(e -> registerAction(mainWindow));
}},
new Hyperlink("已有账号?去登录") {{
setOnAction(e -> mainWindow.showLoginPanel());
}}
);
}
private void sendCodeAction(MainWindow mainWindow) {
String email = emailField.getText().trim();
if (email.isEmpty() || !EmailUtil.isValidEmail(email)) {
showAlert("请输入有效的邮箱地址");
return;
}
// 调用服务层
boolean sent = mainWindow.getUserService().sendRegistrationCode(email);
if (sent) {
showAlert("注册码已发送(模拟)");
}
}
private void registerAction(MainWindow mainWindow) {
String email = emailField.getText().trim();
String code = codeField.getText().trim();
String pwd1 = pwd1Field.getText();
String pwd2 = pwd2Field.getText();
if (!mainWindow.getUserService().verifyCode(email, code)) {
showAlert("注册码错误");
return;
}
boolean success = mainWindow.getUserService().setPassword(email, pwd1, pwd2);
if (success) {
showAlert("注册成功!");
mainWindow.showGradeSelectPanel();
} else {
showAlert("密码不符合要求6-10位含大小写字母和数字");
}
}
private void showAlert(String message) {
new Alert(Alert.AlertType.INFORMATION, message).showAndWait();
}
}