You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
4.5 KiB
149 lines
4.5 KiB
package ui;
|
|
|
|
import model.UserManager;
|
|
import javax.swing.BorderFactory;
|
|
import javax.swing.JButton;
|
|
import javax.swing.JDialog;
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JOptionPane;
|
|
import javax.swing.JPanel;
|
|
import javax.swing.JPasswordField;
|
|
import java.awt.Dimension;
|
|
import java.awt.Font;
|
|
import java.awt.Frame;
|
|
import java.awt.GridBagConstraints;
|
|
import java.awt.GridBagLayout;
|
|
import java.awt.Insets;
|
|
import java.util.regex.Pattern;
|
|
|
|
/**
|
|
* 修改密码对话框
|
|
* 用户在登录状态下可以修改密码
|
|
*/
|
|
public class ChangePasswordDialog extends JDialog {
|
|
private static final long serialVersionUID = 1L;
|
|
private static final Pattern PASSWORD_PATTERN =
|
|
Pattern.compile("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{6,10}$");
|
|
|
|
private final JPasswordField oldPasswordField;
|
|
private final JPasswordField newPassword1Field;
|
|
private final JPasswordField newPassword2Field;
|
|
private final JButton confirmButton;
|
|
private final UserManager userManager;
|
|
private final String email;
|
|
|
|
/**
|
|
* 构造修改密码对话框
|
|
*
|
|
* @param parent 父窗口
|
|
* @param email 用户邮箱
|
|
*/
|
|
public ChangePasswordDialog(Frame parent, String email) {
|
|
super(parent, "修改密码", true);
|
|
this.email = email;
|
|
this.userManager = new UserManager();
|
|
|
|
setSize(400, 300);
|
|
setLocationRelativeTo(parent);
|
|
setResizable(false);
|
|
|
|
JPanel panel = new JPanel(new GridBagLayout());
|
|
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
|
|
GridBagConstraints gbc = new GridBagConstraints();
|
|
gbc.insets = new Insets(5, 5, 5, 5);
|
|
gbc.fill = GridBagConstraints.HORIZONTAL;
|
|
|
|
JLabel infoLabel = new JLabel("<html>新密码要求:<br>" +
|
|
"长度6-10位,必须包含大小写字母和数字</html>");
|
|
infoLabel.setFont(new Font("微软雅黑", Font.PLAIN, 11));
|
|
gbc.gridx = 0;
|
|
gbc.gridy = 0;
|
|
gbc.gridwidth = 2;
|
|
panel.add(infoLabel, gbc);
|
|
|
|
gbc.gridwidth = 1;
|
|
gbc.gridy = 1;
|
|
panel.add(new JLabel("原密码:"), gbc);
|
|
|
|
oldPasswordField = new JPasswordField(15);
|
|
gbc.gridx = 1;
|
|
panel.add(oldPasswordField, gbc);
|
|
|
|
gbc.gridx = 0;
|
|
gbc.gridy = 2;
|
|
panel.add(new JLabel("新密码:"), gbc);
|
|
|
|
newPassword1Field = new JPasswordField(15);
|
|
gbc.gridx = 1;
|
|
panel.add(newPassword1Field, gbc);
|
|
|
|
gbc.gridx = 0;
|
|
gbc.gridy = 3;
|
|
panel.add(new JLabel("确认新密码:"), gbc);
|
|
|
|
newPassword2Field = new JPasswordField(15);
|
|
gbc.gridx = 1;
|
|
panel.add(newPassword2Field, gbc);
|
|
|
|
confirmButton = new JButton("确认修改");
|
|
confirmButton.setPreferredSize(new Dimension(120, 30));
|
|
gbc.gridx = 0;
|
|
gbc.gridy = 4;
|
|
gbc.gridwidth = 2;
|
|
gbc.anchor = GridBagConstraints.CENTER;
|
|
panel.add(confirmButton, gbc);
|
|
|
|
add(panel);
|
|
|
|
confirmButton.addActionListener(e -> handleConfirm());
|
|
getRootPane().setDefaultButton(confirmButton);
|
|
}
|
|
|
|
/**
|
|
* 处理确认修改
|
|
*/
|
|
private void handleConfirm() {
|
|
String oldPassword = new String(oldPasswordField.getPassword());
|
|
String newPassword1 = new String(newPassword1Field.getPassword());
|
|
String newPassword2 = new String(newPassword2Field.getPassword());
|
|
|
|
if (oldPassword.isEmpty() || newPassword1.isEmpty() ||
|
|
newPassword2.isEmpty()) {
|
|
showError("所有字段都不能为空");
|
|
return;
|
|
}
|
|
|
|
if (!newPassword1.equals(newPassword2)) {
|
|
showError("两次输入的新密码不一致");
|
|
return;
|
|
}
|
|
|
|
if (!PASSWORD_PATTERN.matcher(newPassword1).matches()) {
|
|
showError("新密码不符合要求:\n" +
|
|
"长度6-10位,必须包含大小写字母和数字");
|
|
return;
|
|
}
|
|
|
|
if (userManager.changePassword(email, oldPassword, newPassword1)) {
|
|
JOptionPane.showMessageDialog(this,
|
|
"密码修改成功!",
|
|
"成功",
|
|
JOptionPane.INFORMATION_MESSAGE);
|
|
dispose();
|
|
} else {
|
|
showError("原密码错误");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 显示错误信息
|
|
*
|
|
* @param message 错误消息
|
|
*/
|
|
private void showError(String message) {
|
|
JOptionPane.showMessageDialog(this,
|
|
message,
|
|
"错误",
|
|
JOptionPane.ERROR_MESSAGE);
|
|
}
|
|
} |