修改密码 #10

Merged
hnu202326010318 merged 6 commits from liguolin_branch into develop 3 months ago

@ -0,0 +1,90 @@
package com.mathgenerator.controller;
import com.mathgenerator.model.User;
import com.mathgenerator.service.UserService;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.stage.Stage;
import java.io.IOException;
public class ChangePasswordController {
private final UserService userService = new UserService();
private User currentUser;
@FXML private PasswordField oldPasswordField;
@FXML private PasswordField newPasswordField;
@FXML private PasswordField confirmNewPasswordField;
@FXML private Button confirmButton;
@FXML private Button backButton;
@FXML private Label statusLabel;
/**
*
*/
public void initData(User user) {
this.currentUser = user;
}
@FXML
private void handleConfirmAction(ActionEvent event) {
// 1. 获取输入
String oldPassword = oldPasswordField.getText();
String newPassword = newPasswordField.getText();
String confirmNewPassword = confirmNewPasswordField.getText();
// 2. 输入校验
if (oldPassword.isEmpty() || newPassword.isEmpty() || confirmNewPassword.isEmpty()) {
statusLabel.setText("所有密码字段都不能为空!");
return;
}
if (!newPassword.equals(confirmNewPassword)) {
statusLabel.setText("两次输入的新密码不匹配!");
return;
}
if (!UserService.isPasswordValid(newPassword)) {
statusLabel.setText("新密码格式错误必须为6-10位且包含大小写字母和数字。");
return;
}
// 3. 调用后端服务修改密码
boolean success = userService.changePassword(
currentUser.username(),
oldPassword,
newPassword
);
// 4. 更新UI反馈
if (success) {
statusLabel.setText("密码修改成功!请返回主菜单。");
confirmButton.setDisable(true); // 防止重复点击
} else {
statusLabel.setText("修改失败:当前密码错误。");
}
}
/**
*
*/
@FXML
private void handleBackAction(ActionEvent event) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/mathgenerator/view/MainMenuView.fxml"));
Parent root = loader.load();
MainMenuController controller = loader.getController();
controller.initData(currentUser); // 将用户信息传回主菜单
Stage stage = (Stage) backButton.getScene().getWindow();
stage.setScene(new Scene(root));
stage.setTitle("主菜单");
} catch (IOException e) {
e.printStackTrace();
}
}
}

@ -15,7 +15,7 @@ import javafx.stage.Stage;
import java.io.IOException;
import java.util.Optional;
import com.mathgenerator.util.ValidationUtils;
public class LoginController {
// 依赖注入后端服务
@ -46,7 +46,13 @@ public class LoginController {
String username = usernameField.getText();
String password = passwordField.getText();
if (username.isEmpty() || password.isEmpty()) {
// --- 2. 使用工具类进行校验 ---
if (!ValidationUtils.isUsernameValid(username)) {
statusLabel.setText("登录失败:用户名不能为空且不能包含空格。");
return;
}
if (password.isEmpty()) {
statusLabel.setText("用户名和密码不能为空!");
return;
}

@ -48,11 +48,26 @@ public class MainMenuController {
@FXML
private void handleChangePasswordAction(ActionEvent event) {
// TODO: 跳转到修改密码界面
statusLabel.setText("修改密码功能待实现。");
}
try {
// 1\. 加载 FXML 文件
FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/mathgenerator/view/ChangePasswordView.fxml"));
Parent root = loader.load();
// 2\. 获取新界面的控制器
ChangePasswordController controller = loader.getController();
@FXML
// 3\. 调用控制器的方法,传递当前用户信息
controller.initData(currentUser);
// 4\. 显示新场景
Stage stage = (Stage) logoutButton.getScene().getWindow();
stage.setScene(new Scene(root));
stage.setTitle("修改密码");
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
private void handleLogoutAction(ActionEvent event) {
// 跳转回登录界面
loadScene("/com/mathgenerator/view/LoginView.fxml");

@ -12,7 +12,7 @@ import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.io.IOException;
import com.mathgenerator.util.ValidationUtils;
public class RegisterController {
private final UserService userService = new UserService();
@ -51,20 +51,15 @@ public class RegisterController {
@FXML
private void handleRegisterAction(ActionEvent event) {
// 1. 字段校验
if (usernameField.getText().isEmpty() || emailField.getText().isEmpty() ||
verificationCodeField.getText().isEmpty() || passwordField.getText().isEmpty()) {
statusLabel.setText("所有字段都不能为空!");
return;
}
if (!passwordField.getText().equals(confirmPasswordField.getText())) {
statusLabel.setText("两次输入的密码不匹配!");
return;
}
if (this.sentCode == null || !this.sentCode.equals(verificationCodeField.getText())) {
statusLabel.setText("验证码错误!");
String username = usernameField.getText();
// --- 2. 使用工具类进行校验 ---
if (!ValidationUtils.isUsernameValid(username)) {
statusLabel.setText("注册失败:用户名不能为空且不能包含空格!");
return;
}
if (!UserService.isPasswordValid(passwordField.getText())) {
// ...
if (!ValidationUtils.isPasswordValid(passwordField.getText())) { // 同样可以替换密码校验
statusLabel.setText("密码格式错误必须为6-10位且包含大小写字母和数字。");
return;
}

@ -172,25 +172,6 @@ public class PrimarySchoolGenerator implements QuestionGenerator {
parts.add(startIndex, "(");
}
// 在 PrimarySchoolGenerator.java 中添加这个方法
/**
*
* 便
* @return
*/
public String generateBasicQuestionText() {
ThreadLocalRandom random = ThreadLocalRandom.current();
int operandCount = random.nextInt(2, 5);
List<String> parts = new ArrayList<>();
parts.add(String.valueOf(getOperand()));
for (int i = 1; i < operandCount; i++) {
parts.add(getRandomOperator());
parts.add(String.valueOf(getOperand()));
}
if (operandCount > 2 && random.nextBoolean()) {
addParentheses(parts);
}
return String.join(" ", parts);
}
}

@ -0,0 +1,52 @@
package com.mathgenerator.util;
import java.util.regex.Pattern;
/**
*
*
*/
public final class ValidationUtils {
// 密码策略: 6-10位, 必须包含大小写字母和数字
private static final Pattern PASSWORD_PATTERN =
Pattern.compile("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{6,10}$");
// 用户名策略: 不包含任何空白字符
private static final Pattern USERNAME_NO_WHITESPACE_PATTERN =
Pattern.compile("^\\S+$");
// 私有构造函数,防止这个工具类被实例化
private ValidationUtils() {}
/**
*
*
* @param username
* @return true, false
*/
public static boolean isUsernameValid(String username) {
if (username == null || username.isEmpty()) {
return false;
}
return USERNAME_NO_WHITESPACE_PATTERN.matcher(username).matches();
}
/**
*
* @param password
* @return true
*/
public static boolean isPasswordValid(String password) {
return password != null && PASSWORD_PATTERN.matcher(password).matches();
}
/**
* ()
* @param email
* @return true
*/
public static boolean isEmailValid(String email) {
return email != null && !email.isEmpty() && email.contains("@");
}
}

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="450.0" spacing="15.0" style="-fx-background-color: #f4f4f4;" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mathgenerator.controller.ChangePasswordController">
<children>
<Label text="修改密码">
<font>
<Font name="System Bold" size="24.0" />
</font>
</Label>
<PasswordField fx:id="oldPasswordField" maxWidth="300.0" promptText="当前密码" />
<PasswordField fx:id="newPasswordField" maxWidth="300.0" promptText="新密码 (6-10位, 含大小写字母和数字)" />
<PasswordField fx:id="confirmNewPasswordField" maxWidth="300.0" promptText="确认新密码" />
<Button fx:id="confirmButton" mnemonicParsing="false" onAction="#handleConfirmAction" prefWidth="120.0" text="确认修改" />
<Button fx:id="backButton" mnemonicParsing="false" onAction="#handleBackAction" style="-fx-background-color: #6c757d;" text="返回主菜单" textFill="WHITE" />
<Label fx:id="statusLabel" textFill="RED" wrapText="true">
<VBox.margin>
<Insets top="10.0" />
</VBox.margin>
</Label>
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
</VBox>
Loading…
Cancel
Save