新增注册时用户名,并且可用用户名登陆

yangshuailu_branch
YangShuaiLu 4 months ago
parent 9e9899f1f7
commit c34c088dea

@ -12,7 +12,14 @@ public class Main extends Application {
private Stage primaryStage;
private MailSender mailSender = new MailSender("3417398995@qq.com", "zhwytlhmucfxcibe");
private String generatedCode;
private Map<String, String> userDatabase = new HashMap<>(); // 存储邮箱 -> 密码
// 存储用户信息:邮箱 -> 密码
private Map<String, String> userDatabase = new HashMap<>();
// 存储用户名 -> 邮箱 映射
private Map<String, String> usernameToEmail = new HashMap<>();
// 存储邮箱 -> 用户名 映射
private Map<String, String> emailToUsername = new HashMap<>();
private Map<String, String> currentUser = new HashMap<>(); // 存储当前登录用户信息
private List<Question> currentExam = new ArrayList<>();
private int currentIndex = 0;
@ -39,26 +46,29 @@ public class Main extends Application {
Label titleLabel = new Label("数学学习软件 - 登录");
titleLabel.setStyle("-fx-font-size: 16; -fx-font-weight: bold;");
TextField emailField = new TextField();
emailField.setPromptText("请输入邮箱");
TextField loginField = new TextField();
loginField.setPromptText("请输入邮箱或用户名");
PasswordField passwordField = new PasswordField();
passwordField.setPromptText("请输入密码");
Button loginBtn = new Button("登录");
Button registerBtn = new Button("前往注册");
Label infoLabel = new Label();
Label tipLabel = new Label("提示:可以使用邮箱或用户名登录");
tipLabel.setStyle("-fx-font-size: 10; -fx-text-fill: gray;");
// 登录按钮事件
loginBtn.setOnAction(e -> {
String email = emailField.getText().trim();
String loginInput = loginField.getText().trim();
String password = passwordField.getText();
if (email.isEmpty() || password.isEmpty()) {
infoLabel.setText("请输入邮箱和密码");
if (loginInput.isEmpty() || password.isEmpty()) {
infoLabel.setText("请输入登录信息和密码");
return;
}
if (!userDatabase.containsKey(email)) {
String email = getEmailFromLoginInput(loginInput);
if (email == null) {
infoLabel.setText("用户不存在");
return;
}
@ -70,7 +80,8 @@ public class Main extends Application {
// 登录成功
currentUser.put("email", email);
infoLabel.setText("登录成功!");
currentUser.put("username", emailToUsername.get(email));
infoLabel.setText("登录成功!欢迎 " + emailToUsername.get(email));
primaryStage.setScene(buildLevelSelectionScene());
});
@ -79,10 +90,21 @@ public class Main extends Application {
primaryStage.setScene(buildRegisterScene());
});
root.getChildren().addAll(titleLabel, emailField, passwordField, loginBtn, registerBtn, infoLabel);
root.getChildren().addAll(titleLabel, loginField, passwordField, tipLabel, loginBtn, registerBtn, infoLabel);
return new Scene(root, 400, 300);
}
// 根据登录输入获取邮箱
private String getEmailFromLoginInput(String loginInput) {
// 如果输入包含@,认为是邮箱
if (loginInput.contains("@")) {
return userDatabase.containsKey(loginInput) ? loginInput : null;
} else {
// 否则认为是用户名
return usernameToEmail.get(loginInput);
}
}
// 注册界面
private Scene buildRegisterScene() {
VBox root = new VBox(10);
@ -90,6 +112,8 @@ public class Main extends Application {
Label titleLabel = new Label("用户注册");
titleLabel.setStyle("-fx-font-size: 16; -fx-font-weight: bold;");
TextField usernameField = new TextField();
usernameField.setPromptText("设置用户名 (4-10位字符)");
TextField emailField = new TextField();
emailField.setPromptText("请输入邮箱");
Button sendCodeBtn = new Button("发送验证码");
@ -104,7 +128,20 @@ public class Main extends Application {
Label infoLabel = new Label();
sendCodeBtn.setOnAction(e -> {
String username = usernameField.getText().trim();
String email = emailField.getText().trim();
// 验证用户名
if (!isValidUsername(username)) {
infoLabel.setText("用户名必须4-10位只能包含字母、数字和下划线");
return;
}
if (usernameToEmail.containsKey(username)) {
infoLabel.setText("用户名已存在");
return;
}
if (!email.contains("@")) {
infoLabel.setText("邮箱格式不正确");
return;
@ -130,11 +167,22 @@ public class Main extends Application {
});
registerBtn.setOnAction(e -> {
String username = usernameField.getText().trim();
String email = emailField.getText().trim();
String inputCode = codeField.getText().trim();
String pw = passwordField.getText();
String pwConfirm = confirmPasswordField.getText();
if (!isValidUsername(username)) {
infoLabel.setText("用户名必须4-10位只能包含字母、数字和下划线");
return;
}
if (usernameToEmail.containsKey(username)) {
infoLabel.setText("用户名已存在");
return;
}
if (!inputCode.equals(generatedCode)) {
infoLabel.setText("验证码错误");
} else if (!pw.equals(pwConfirm)) {
@ -144,10 +192,14 @@ public class Main extends Application {
} else if (userDatabase.containsKey(email)) {
infoLabel.setText("邮箱已注册");
} else {
// 注册用户
userDatabase.put(email, pw);
infoLabel.setText("注册成功!");
usernameToEmail.put(username, email);
emailToUsername.put(email, username);
infoLabel.setText("注册成功!用户名: " + username);
// 注册成功后自动登录
currentUser.put("email", email);
currentUser.put("username", username);
primaryStage.setScene(buildLevelSelectionScene());
}
});
@ -156,9 +208,18 @@ public class Main extends Application {
primaryStage.setScene(buildLoginScene());
});
root.getChildren().addAll(titleLabel, emailField, sendCodeBtn, codeField,
root.getChildren().addAll(titleLabel, usernameField, emailField, sendCodeBtn, codeField,
passwordField, confirmPasswordField, registerBtn, backBtn, infoLabel);
return new Scene(root, 400, 400);
return new Scene(root, 400, 450);
}
// 用户名验证方法
private boolean isValidUsername(String username) {
if (username.length() < 4 || username.length() > 10) {
return false;
}
// 只能包含字母、数字、下划线
return username.matches("^[a-zA-Z0-9_]+$");
}
// 密码验证方法
@ -183,6 +244,12 @@ public class Main extends Application {
private Scene buildLevelSelectionScene() {
VBox root = new VBox(15);
root.setStyle("-fx-padding: 20;");
// 显示欢迎信息
String username = currentUser.get("username");
Label welcomeLabel = new Label("欢迎, " + username + "!");
welcomeLabel.setStyle("-fx-font-size: 16; -fx-font-weight: bold; -fx-text-fill: #2E8B57;");
Label label = new Label("请选择难度");
label.setStyle("-fx-font-size: 14; -fx-font-weight: bold;");
@ -214,7 +281,7 @@ public class Main extends Application {
HBox bottomBox = new HBox(10, changePwdBtn, logoutBtn);
bottomBox.setStyle("-fx-alignment: center;");
root.getChildren().addAll(label, buttonBox, bottomBox);
root.getChildren().addAll(welcomeLabel, label, buttonBox, bottomBox);
Scene scene = new Scene(root, 400, 300);
primaryStage.setScene(scene);
return scene;
@ -227,6 +294,9 @@ public class Main extends Application {
Label titleLabel = new Label("修改密码");
titleLabel.setStyle("-fx-font-size: 16; -fx-font-weight: bold;");
Label userLabel = new Label("用户: " + currentUser.get("username"));
userLabel.setStyle("-fx-font-size: 12; -fx-text-fill: gray;");
PasswordField oldPwdField = new PasswordField();
oldPwdField.setPromptText("请输入原密码");
PasswordField newPwdField = new PasswordField();
@ -260,9 +330,9 @@ public class Main extends Application {
primaryStage.setScene(buildLevelSelectionScene());
});
root.getChildren().addAll(titleLabel, oldPwdField, newPwdField, confirmPwdField,
root.getChildren().addAll(titleLabel, userLabel, oldPwdField, newPwdField, confirmPwdField,
confirmBtn, backBtn, infoLabel);
return new Scene(root, 400, 300);
return new Scene(root, 400, 350);
}
// 输入题目数量界面
@ -370,7 +440,6 @@ public class Main extends Application {
}
// 显示分数
// 显示分数(详细版)
private Scene buildScoreScene() {
VBox root = new VBox(15);
root.setStyle("-fx-padding: 30; -fx-alignment: center; -fx-background-color: #f5f5f5;");

Loading…
Cancel
Save