ADD file via upload

lizihan_branch
hnu202326010302 7 months ago
parent 576bf8c29e
commit 51a467b08d

@ -0,0 +1,83 @@
package com.example.myapp.controller;
import com.example.myapp.Main;
import com.example.myapp.service.UserService;
import com.example.myapp.util.PasswordUtil;
import javafx.fxml.FXML;
import javafx.scene.control.*;
public class SetPasswordController {
@FXML private Label emailLabel;
@FXML private TextField usernameField;
@FXML private PasswordField pwdField;
@FXML private PasswordField pwdConfirmField;
@FXML private Button setBtn;
@FXML private Label statusLabel;
private String email;
private final UserService userService = UserService.getInstance();
public void setEmail(String email){
this.email = email;
emailLabel.setText("设置账号信息 - " + email);
}
@FXML
public void onSetPassword(){
String username = usernameField.getText().trim();
String p1 = pwdField.getText();
String p2 = pwdConfirmField.getText();
// 验证用户名
if (username.isEmpty()) {
statusLabel.setText("请输入用户名");
statusLabel.setStyle("-fx-text-fill: red;");
return;
}
if (username.length() < 2 || username.length() > 20) {
statusLabel.setText("用户名长度应为2-20个字符");
statusLabel.setStyle("-fx-text-fill: red;");
return;
}
// 检查用户名是否已存在
if (userService.usernameExists(username)) {
statusLabel.setText("用户名已存在,请选择其他用户名");
statusLabel.setStyle("-fx-text-fill: red;");
return;
}
// 验证密码
if(!p1.equals(p2)){
statusLabel.setText("两次密码不一致");
statusLabel.setStyle("-fx-text-fill: red;");
return;
}
if(!PasswordUtil.validatePasswordRules(p1)){
statusLabel.setText("密码不符合规则6-10位须含大写、小写和数字");
statusLabel.setStyle("-fx-text-fill: red;");
return;
}
// 设置用户名和密码
boolean usernameSet = userService.setUsername(email, username);
if (!usernameSet) {
statusLabel.setText("设置用户名失败");
statusLabel.setStyle("-fx-text-fill: red;");
return;
}
String hash = PasswordUtil.hash(p1);
userService.setPassword(email, hash);
statusLabel.setText("注册成功!即将进入用户中心");
statusLabel.setStyle("-fx-text-fill: green;");
try {
Main.showDashboard(email);
} catch(Exception e){
e.printStackTrace();
}
}
}
Loading…
Cancel
Save