parent
5d2fcf0b35
commit
ac0941b13f
@ -1,149 +1,149 @@
|
||||
package com.example.mathsystemtogether;
|
||||
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.stage.Stage;
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.List;
|
||||
|
||||
public class ChangePasswordController {
|
||||
|
||||
@FXML
|
||||
private TextField usernameField;
|
||||
|
||||
@FXML
|
||||
private PasswordField oldPasswordField;
|
||||
|
||||
@FXML
|
||||
private PasswordField newPasswordField;
|
||||
|
||||
@FXML
|
||||
private PasswordField confirmNewPasswordField;
|
||||
|
||||
@FXML
|
||||
private Button changePasswordButton;
|
||||
|
||||
@FXML
|
||||
private Button cancelButton;
|
||||
|
||||
@FXML
|
||||
private Label statusLabel;
|
||||
|
||||
private String currentUser;
|
||||
private ExamController examController;
|
||||
private static final String USER_DATA_FILE = "user_data.txt";
|
||||
|
||||
public void setCurrentUser(String username) {
|
||||
this.currentUser = username;
|
||||
usernameField.setText(username);
|
||||
}
|
||||
|
||||
public void setExamController(ExamController examController) {
|
||||
this.examController = examController;
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void handleChangePassword() {
|
||||
String oldPassword = oldPasswordField.getText();
|
||||
String newPassword = newPasswordField.getText();
|
||||
String confirmNewPassword = confirmNewPasswordField.getText();
|
||||
|
||||
if (oldPassword.isEmpty() || newPassword.isEmpty() || confirmNewPassword.isEmpty()) {
|
||||
showStatus("请填写所有字段", true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!examController.checkUserPassword(currentUser, oldPassword)) {
|
||||
showStatus("原密码错误", true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (newPassword.length() < 6) {
|
||||
showStatus("新密码至少需要6个字符", true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!newPassword.equals(confirmNewPassword)) {
|
||||
showStatus("两次输入的新密码不一致", true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (oldPassword.equals(newPassword)) {
|
||||
showStatus("新密码不能与原密码相同", true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (updatePassword(currentUser, newPassword)) {
|
||||
showStatus("密码修改成功!", false);
|
||||
|
||||
// 延迟关闭窗口
|
||||
new java.util.Timer().schedule(
|
||||
new java.util.TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
javafx.application.Platform.runLater(() -> {
|
||||
handleClose();
|
||||
});
|
||||
}
|
||||
},
|
||||
1500
|
||||
);
|
||||
} else {
|
||||
showStatus("密码修改失败,请重试", true);
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void handleCancel() {
|
||||
handleClose();
|
||||
}
|
||||
|
||||
private void handleClose() {
|
||||
Stage stage = (Stage) cancelButton.getScene().getWindow();
|
||||
stage.close();
|
||||
}
|
||||
|
||||
private boolean updatePassword(String username, String newPassword) {
|
||||
try {
|
||||
// 读取所有用户数据
|
||||
List<String> lines = Files.readAllLines(Paths.get(USER_DATA_FILE));
|
||||
boolean userFound = false;
|
||||
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
String[] parts = lines.get(i).split("\\|");
|
||||
if (parts.length >= 2 && parts[0].equals(username)) {
|
||||
// 更新密码
|
||||
parts[1] = newPassword;
|
||||
lines.set(i, String.join("|", parts));
|
||||
userFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (userFound) {
|
||||
// 写回文件
|
||||
Files.write(Paths.get(USER_DATA_FILE), lines);
|
||||
// 更新内存中的用户数据
|
||||
examController.updateUserPassword(username, newPassword);
|
||||
return true;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void showStatus(String message, boolean isError) {
|
||||
statusLabel.setText(message);
|
||||
if (isError) {
|
||||
statusLabel.setStyle("-fx-text-fill: #DC143C; -fx-font-weight: bold; -fx-font-size: 14;");
|
||||
} else {
|
||||
statusLabel.setStyle("-fx-text-fill: #228B22; -fx-font-weight: bold; -fx-font-size: 14;");
|
||||
}
|
||||
}
|
||||
}
|
||||
package com.example.mathsystemtogether;
|
||||
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.stage.Stage;
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.List;
|
||||
|
||||
public class ChangePasswordController {
|
||||
|
||||
@FXML
|
||||
private TextField usernameField;
|
||||
|
||||
@FXML
|
||||
private PasswordField oldPasswordField;
|
||||
|
||||
@FXML
|
||||
private PasswordField newPasswordField;
|
||||
|
||||
@FXML
|
||||
private PasswordField confirmNewPasswordField;
|
||||
|
||||
@FXML
|
||||
private Button changePasswordButton;
|
||||
|
||||
@FXML
|
||||
private Button cancelButton;
|
||||
|
||||
@FXML
|
||||
private Label statusLabel;
|
||||
|
||||
private String currentUser;
|
||||
private ExamController examController;
|
||||
private static final String USER_DATA_FILE = "user_data.txt";
|
||||
|
||||
public void setCurrentUser(String username) {
|
||||
this.currentUser = username;
|
||||
usernameField.setText(username);
|
||||
}
|
||||
|
||||
public void setExamController(ExamController examController) {
|
||||
this.examController = examController;
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void handleChangePassword() {
|
||||
String oldPassword = oldPasswordField.getText();
|
||||
String newPassword = newPasswordField.getText();
|
||||
String confirmNewPassword = confirmNewPasswordField.getText();
|
||||
|
||||
if (oldPassword.isEmpty() || newPassword.isEmpty() || confirmNewPassword.isEmpty()) {
|
||||
showStatus("请填写所有字段", true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!examController.checkUserPassword(currentUser, oldPassword)) {
|
||||
showStatus("原密码错误", true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (newPassword.length() < 6) {
|
||||
showStatus("新密码至少需要6个字符", true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!newPassword.equals(confirmNewPassword)) {
|
||||
showStatus("两次输入的新密码不一致", true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (oldPassword.equals(newPassword)) {
|
||||
showStatus("新密码不能与原密码相同", true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (updatePassword(currentUser, newPassword)) {
|
||||
showStatus("密码修改成功!", false);
|
||||
|
||||
// 延迟关闭窗口
|
||||
new java.util.Timer().schedule(
|
||||
new java.util.TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
javafx.application.Platform.runLater(() -> {
|
||||
handleClose();
|
||||
});
|
||||
}
|
||||
},
|
||||
1500
|
||||
);
|
||||
} else {
|
||||
showStatus("密码修改失败,请重试", true);
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void handleCancel() {
|
||||
handleClose();
|
||||
}
|
||||
|
||||
private void handleClose() {
|
||||
Stage stage = (Stage) cancelButton.getScene().getWindow();
|
||||
stage.close();
|
||||
}
|
||||
|
||||
private boolean updatePassword(String username, String newPassword) {
|
||||
try {
|
||||
// 读取所有用户数据
|
||||
List<String> lines = Files.readAllLines(Paths.get(USER_DATA_FILE));
|
||||
boolean userFound = false;
|
||||
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
String[] parts = lines.get(i).split("\\|");
|
||||
if (parts.length >= 2 && parts[0].equals(username)) {
|
||||
// 更新密码
|
||||
parts[1] = newPassword;
|
||||
lines.set(i, String.join("|", parts));
|
||||
userFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (userFound) {
|
||||
// 写回文件
|
||||
Files.write(Paths.get(USER_DATA_FILE), lines);
|
||||
// 更新内存中的用户数据
|
||||
examController.updateUserPassword(username, newPassword);
|
||||
return true;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void showStatus(String message, boolean isError) {
|
||||
statusLabel.setText(message);
|
||||
if (isError) {
|
||||
statusLabel.setStyle("-fx-text-fill: #DC143C; -fx-font-weight: bold; -fx-font-size: 14;");
|
||||
} else {
|
||||
statusLabel.setStyle("-fx-text-fill: #228B22; -fx-font-weight: bold; -fx-font-size: 14;");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
wgll|123456|ymhlovesLQX@163.com|小学|1760162416490
|
||||
wgll|Ymh123456|ymhlovesLQX@163.com|小学|1760162416490
|
||||
666|123456789|252436951@qq.com|小学|1760166999971
|
||||
|
||||
Loading…
Reference in new issue