张世豪
2 天以前 e4b4347318f1f37c64b8055f1f1898da59a167ba
src/user/ZhaohuiMima.java
@@ -1,5 +1,7 @@
package user;
import login.EmailCodeSender;
import login.PasswordReset;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
@@ -20,7 +22,6 @@
    private JLabel tipLabel;
    private Timer timer;
    private int countdown = 60;
    private String generatedCode; // 存储生成的验证码
    
    // 主题颜色 (参考 Denglu.java)
    private final Color THEME_COLOR = new Color(46, 139, 87);
@@ -170,6 +171,13 @@
        });
    }
    // 邮箱验证方法
    private boolean isValidEmail(String email) {
        // 简单的邮箱格式验证
        String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
        return email != null && email.matches(emailRegex);
    }
    private void setupEvents() {
        getCodeButton.addActionListener(new ActionListener() {
            @Override
@@ -177,15 +185,62 @@
                String email = emailField.getText().trim();
                if (email.isEmpty()) {
                    JOptionPane.showMessageDialog(ZhaohuiMima.this, "请输入邮箱", "提示", JOptionPane.WARNING_MESSAGE);
                    emailField.requestFocus();
                    return;
                }
                
                // 这里应该添加发送验证码的逻辑
                // 模拟生成验证码
                generatedCode = String.valueOf((int)((Math.random() * 9 + 1) * 100000));
                // 验证邮箱格式
                if (!isValidEmail(email)) {
                    JOptionPane.showMessageDialog(ZhaohuiMima.this,
                        "请输入有效的邮箱地址",
                        "输入错误",
                        JOptionPane.WARNING_MESSAGE);
                    emailField.requestFocus();
                    return;
                }
                
                startCountdown();
                JOptionPane.showMessageDialog(ZhaohuiMima.this, "验证码已发送: " + generatedCode, "提示", JOptionPane.INFORMATION_MESSAGE);
                // 禁用按钮,防止重复点击
                getCodeButton.setEnabled(false);
                // 在新线程中调用API,避免阻塞UI
                new Thread(() -> {
                    try {
                        // 调用邮件验证码发送API
                        EmailCodeSender.EmailCodeResponse response = EmailCodeSender.sendEmailCode(email);
                        // 在EDT线程中更新UI
                        SwingUtilities.invokeLater(() -> {
                            if (response.isSuccess()) {
                                // 发送成功,显示提示并开始倒计时
                                JOptionPane.showMessageDialog(ZhaohuiMima.this,
                                    "验证码已发送到您的邮箱",
                                    "提示",
                                    JOptionPane.INFORMATION_MESSAGE);
                                // 开始60秒倒计时
                                startCountdown();
                            } else {
                                // 发送失败,显示错误信息并恢复按钮
                                getCodeButton.setEnabled(true);
                                String errorMessage = response.getMessage() != null ?
                                    response.getMessage() : "验证码发送失败";
                                JOptionPane.showMessageDialog(ZhaohuiMima.this,
                                    errorMessage,
                                    "错误",
                                    JOptionPane.ERROR_MESSAGE);
                            }
                        });
                    } catch (Exception ex) {
                        // 异常处理,恢复按钮并显示错误
                        SwingUtilities.invokeLater(() -> {
                            getCodeButton.setEnabled(true);
                            JOptionPane.showMessageDialog(ZhaohuiMima.this,
                                "发送验证码时发生错误: " + ex.getMessage(),
                                "错误",
                                JOptionPane.ERROR_MESSAGE);
                        });
                    }
                }).start();
            }
        });
@@ -280,58 +335,97 @@
    private void savePassword() {
        String email = emailField.getText().trim();
        String code = codeField.getText().trim();
        String password = new String(passwordField.getPassword());
        String confirmPassword = new String(confirmPasswordField.getPassword());
        String newPassword = new String(passwordField.getPassword()).trim();
        String confirmPassword = new String(confirmPasswordField.getPassword()).trim();
        // 输入验证
        if (email.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入邮箱", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (code.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入验证码", "错误", JOptionPane.ERROR_MESSAGE);
            emailField.requestFocus();
            return;
        }
        
        // 验证验证码
        if (generatedCode == null || !generatedCode.equals(code)) {
            JOptionPane.showMessageDialog(this, "验证码错误", "错误", JOptionPane.ERROR_MESSAGE);
        // 验证邮箱格式
        if (!isValidEmail(email)) {
            JOptionPane.showMessageDialog(this, "请输入有效的邮箱地址", "错误", JOptionPane.ERROR_MESSAGE);
            emailField.requestFocus();
            return;
        }
        if (code.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入验证码", "错误", JOptionPane.ERROR_MESSAGE);
            codeField.requestFocus();
            return;
        }
        if (password.length() < 6) {
        if (newPassword.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入新密码", "错误", JOptionPane.ERROR_MESSAGE);
            passwordField.requestFocus();
            return;
        }
        if (newPassword.length() < 6) {
            JOptionPane.showMessageDialog(this, "密码长度不能少于6个字符", "错误", JOptionPane.ERROR_MESSAGE);
            passwordField.requestFocus();
            return;
        }
        if (password.length() > 25) {
        if (newPassword.length() > 25) {
            JOptionPane.showMessageDialog(this, "密码长度不能超过25个字符", "错误", JOptionPane.ERROR_MESSAGE);
            passwordField.requestFocus();
            return;
        }
        if (!password.equals(confirmPassword)) {
        if (confirmPassword.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入确认密码", "错误", JOptionPane.ERROR_MESSAGE);
            confirmPasswordField.requestFocus();
            return;
        }
        if (!newPassword.equals(confirmPassword)) {
            JOptionPane.showMessageDialog(this, "两次输入的密码不一致", "错误", JOptionPane.ERROR_MESSAGE);
            confirmPasswordField.requestFocus();
            return;
        }
        // 验证邮箱是否匹配 (可选,根据需求)
        String savedEmail = Usrdell.getProperty("email");
        if (savedEmail != null && !savedEmail.equals(email)) {
             // 这里假设必须匹配已存邮箱,或者这是一个新功能允许任意邮箱找回?
             // 通常找回密码需要匹配账号绑定的邮箱
             // 如果本地没有存邮箱,或者输入的邮箱不对
             // JOptionPane.showMessageDialog(this, "邮箱不匹配", "错误", JOptionPane.ERROR_MESSAGE);
             // return;
        }
        // 这里应该验证验证码是否正确
        // ...
        // 保存密码
        try {
            Usrdell.updateProperty("password", password);
            JOptionPane.showMessageDialog(this, "密码修改成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            dispose();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "保存失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }
        // 禁用保存按钮,防止重复提交
        saveButton.setEnabled(false);
        // 在新线程中调用API,避免阻塞UI
        new Thread(() -> {
            try {
                // 调用密码重置API
                PasswordReset.ResetPasswordResponse response = PasswordReset.resetPassword(
                    email, code, newPassword, confirmPassword);
                // 在EDT线程中更新UI
                SwingUtilities.invokeLater(() -> {
                    saveButton.setEnabled(true);
                    // 显示返回消息
                    String message = response.getMessage() != null ?
                        response.getMessage() : (response.isSuccess() ? "密码重置成功" : "密码重置失败");
                    int messageType = response.isSuccess() ?
                        JOptionPane.INFORMATION_MESSAGE : JOptionPane.ERROR_MESSAGE;
                    String title = response.isSuccess() ? "成功" : "失败";
                    JOptionPane.showMessageDialog(this, message, title, messageType);
                    // 如果成功,关闭窗口
                    if (response.isSuccess()) {
                        dispose();
                    }
                });
            } catch (Exception e) {
                // 异常处理,恢复按钮并显示错误
                SwingUtilities.invokeLater(() -> {
                    saveButton.setEnabled(true);
                    JOptionPane.showMessageDialog(this,
                        "重置密码时发生错误: " + e.getMessage(),
                        "错误",
                        JOptionPane.ERROR_MESSAGE);
                });
            }
        }).start();
    }
    private JPanel createPasswordPanel() {