diff --git a/src/main/java/com/example/myapp/controller/ChangePasswordController.java b/src/main/java/com/example/myapp/controller/ChangePasswordController.java new file mode 100644 index 0000000..8e5de44 --- /dev/null +++ b/src/main/java/com/example/myapp/controller/ChangePasswordController.java @@ -0,0 +1,82 @@ +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 ChangePasswordController { + @FXML private Label userLabel; + @FXML private PasswordField oldPwd; + @FXML private PasswordField newPwd; + @FXML private PasswordField newPwdConfirm; + @FXML private Button changeBtn; + @FXML private Button backBtn; + @FXML private Button clearBtn; + @FXML private Label statusLabel; + + private String email; + private final UserService userService = UserService.getInstance(); + + public void initUser(String email) { + this.email = email; + userLabel.setText("修改密码 - " + email); + clearFields(); + } + + @FXML + public void onChangePassword() { + String oldp = oldPwd.getText(); + String np = newPwd.getText(); + String np2 = newPwdConfirm.getText(); + + if (oldp.isEmpty() || np.isEmpty() || np2.isEmpty()) { + statusLabel.setText("请填写所有密码字段"); + statusLabel.setStyle("-fx-text-fill: red;"); + return; + } + + if (!userService.checkPassword(email, oldp)) { + statusLabel.setText("原密码不正确"); + statusLabel.setStyle("-fx-text-fill: red;"); + return; + } + if (!np.equals(np2)) { + statusLabel.setText("两次新密码不一致"); + statusLabel.setStyle("-fx-text-fill: red;"); + return; + } + if (!PasswordUtil.validatePasswordRules(np)) { + statusLabel.setText("新密码不符合规则(6-10位,含大小写和数字)"); + statusLabel.setStyle("-fx-text-fill: red;"); + return; + } + + userService.setPassword(email, PasswordUtil.hash(np)); + statusLabel.setText("密码修改成功!"); + statusLabel.setStyle("-fx-text-fill: green;"); + clearFields(); + } + + @FXML + public void onBack() { + try { + Main.showDashboard(email); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @FXML + public void onClear() { + clearFields(); + statusLabel.setText(""); + } + + private void clearFields() { + oldPwd.clear(); + newPwd.clear(); + newPwdConfirm.clear(); + } +} \ No newline at end of file