From 00f4e4fc6e53a26cf3dc67d57d8b00536634d707 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期五, 26 十二月 2025 17:36:11 +0800
Subject: [PATCH] 优化了注册账号

---
 src/denglu/RegistrationFrame.java |  320 +++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 237 insertions(+), 83 deletions(-)

diff --git a/src/denglu/RegistrationFrame.java b/src/denglu/RegistrationFrame.java
index 7e5cb96..919877d 100644
--- a/src/denglu/RegistrationFrame.java
+++ b/src/denglu/RegistrationFrame.java
@@ -1,11 +1,16 @@
 package denglu;
 
 import ui.UIConfig;
+import login.EmailCodeSender;
+import login.UserRegister;
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 import java.util.HashMap;
 import java.util.Map;
+import javax.swing.event.DocumentEvent;
+import javax.swing.event.DocumentListener;
+import java.util.Arrays;
 
 public class RegistrationFrame extends JFrame {
     private JTextField userField;
@@ -17,6 +22,7 @@
     private JButton cancelButton;
     private JButton sendCodeButton;
     private JLabel userLabel, emailLabel, verificationCodeLabel, passLabel, confirmPassLabel;
+    private JLabel passwordHintLabel, matchStatusLabel, emailHintLabel;
     
     // 涓婚棰滆壊
     private final Color THEME_COLOR = new Color(46, 139, 87);
@@ -30,7 +36,6 @@
     // 楠岃瘉鐮佺浉鍏�
     private Timer countdownTimer;
     private int countdownSeconds = 60;
-    private String generatedVerificationCode;
 
     public RegistrationFrame(Denglu parent, String languageCode) {
         this.parentFrame = parent;
@@ -225,13 +230,23 @@
         emailLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
         
         emailField = new JTextField();
-        emailField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
         emailField.setFont(new Font("PingFang SC", Font.PLAIN, 14));
         emailField.setBorder(BorderFactory.createCompoundBorder(
             BorderFactory.createLineBorder(new Color(200, 200, 200)),
             BorderFactory.createEmptyBorder(5, 8, 5, 8)
         ));
-        emailField.setAlignmentX(Component.LEFT_ALIGNMENT);
+        
+        JPanel emailContainer = new JPanel(new BorderLayout());
+        emailContainer.setBackground(Color.WHITE);
+        emailContainer.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
+        emailContainer.setAlignmentX(Component.LEFT_ALIGNMENT);
+        emailContainer.add(emailField, BorderLayout.CENTER);
+        
+        emailHintLabel = new JLabel("");
+        emailHintLabel.setFont(new Font("PingFang SC", Font.PLAIN, 12));
+        emailHintLabel.setForeground(Color.RED);
+        emailHintLabel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
+        emailContainer.add(emailHintLabel, BorderLayout.EAST);
         
         // 楠岃瘉鐮� - 鏍囩宸﹀榻�
         verificationCodeLabel = new JLabel("楠岃瘉鐮�");
@@ -281,13 +296,8 @@
         passLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
         
         passField = new JPasswordField();
-        passField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
         passField.setFont(new Font("PingFang SC", Font.PLAIN, 14));
-        passField.setBorder(BorderFactory.createCompoundBorder(
-            BorderFactory.createLineBorder(new Color(200, 200, 200)),
-            BorderFactory.createEmptyBorder(5, 8, 5, 8)
-        ));
-        passField.setAlignmentX(Component.LEFT_ALIGNMENT);
+        JPanel passPanel = createPasswordPanel(passField);
         
         // 纭瀵嗙爜 - 鏍囩宸﹀榻�
         confirmPassLabel = new JLabel("纭瀵嗙爜");
@@ -296,13 +306,20 @@
         confirmPassLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
         
         confirmPassField = new JPasswordField();
-        confirmPassField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
         confirmPassField.setFont(new Font("PingFang SC", Font.PLAIN, 14));
-        confirmPassField.setBorder(BorderFactory.createCompoundBorder(
-            BorderFactory.createLineBorder(new Color(200, 200, 200)),
-            BorderFactory.createEmptyBorder(5, 8, 5, 8)
-        ));
-        confirmPassField.setAlignmentX(Component.LEFT_ALIGNMENT);
+        JPanel confirmPassPanel = createPasswordPanel(confirmPassField);
+        
+        // 瀵嗙爜涓嶄竴鑷存彁绀�
+        matchStatusLabel = new JLabel(" ");
+        matchStatusLabel.setFont(new Font("PingFang SC", Font.PLAIN, 12));
+        matchStatusLabel.setForeground(Color.RED);
+        matchStatusLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
+
+        // 瀵嗙爜闀垮害鎻愮ず
+        passwordHintLabel = new JLabel(" ");
+        passwordHintLabel.setFont(new Font("PingFang SC", Font.PLAIN, 12));
+        passwordHintLabel.setForeground(Color.RED);
+        passwordHintLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
         
         // 娉ㄥ唽鎸夐挳 - 鍗曠嫭涓�琛岋紝闀垮害涓庢枃鏈鐩稿悓
         registerButton = new JButton("娉ㄥ唽");
@@ -343,7 +360,7 @@
         formPanel.add(Box.createRigidArea(new Dimension(0, 10)));
         formPanel.add(emailLabel);
         formPanel.add(Box.createRigidArea(new Dimension(0, 3)));
-        formPanel.add(emailField);
+        formPanel.add(emailContainer);
         formPanel.add(Box.createRigidArea(new Dimension(0, 10)));
         formPanel.add(verificationCodeLabel);
         formPanel.add(Box.createRigidArea(new Dimension(0, 3)));
@@ -353,12 +370,16 @@
         formPanel.add(Box.createRigidArea(new Dimension(0, 10)));
         formPanel.add(passLabel);
         formPanel.add(Box.createRigidArea(new Dimension(0, 3)));
-        formPanel.add(passField);
+        formPanel.add(passPanel);
         formPanel.add(Box.createRigidArea(new Dimension(0, 10)));
         formPanel.add(confirmPassLabel);
         formPanel.add(Box.createRigidArea(new Dimension(0, 3)));
-        formPanel.add(confirmPassField);
-        formPanel.add(Box.createRigidArea(new Dimension(0, 20)));
+        formPanel.add(confirmPassPanel);
+        formPanel.add(Box.createRigidArea(new Dimension(0, 5)));
+        formPanel.add(matchStatusLabel);
+        formPanel.add(Box.createRigidArea(new Dimension(0, 5)));
+        formPanel.add(passwordHintLabel);
+        formPanel.add(Box.createRigidArea(new Dimension(0, 5)));
         formPanel.add(registerButton);
         formPanel.add(Box.createRigidArea(new Dimension(0, 8)));
         formPanel.add(cancelButton);
@@ -383,6 +404,61 @@
         
         // 鍥炶溅閿敞鍐�
         confirmPassField.addActionListener(e -> performRegistration());
+        
+        // 瀵嗙爜涓�鑷存�ф鏌�
+        DocumentListener matchListener = new DocumentListener() {
+            private void checkMatch() {
+                char[] p1 = passField.getPassword();
+                char[] p2 = confirmPassField.getPassword();
+                if (p2.length > 0 && p2.length == p1.length) {
+                    if (!Arrays.equals(p1, p2)) {
+                        matchStatusLabel.setText(getTranslationValue(currentLanguageCode, "password_mismatch_error", "涓ゆ杈撳叆鐨勫瘑鐮佷笉涓�鑷�"));
+                    } else {
+                        matchStatusLabel.setText(" ");
+                    }
+                } else {
+                    matchStatusLabel.setText(" ");
+                }
+            }
+            @Override
+            public void insertUpdate(DocumentEvent e) { checkMatch(); }
+            @Override
+            public void removeUpdate(DocumentEvent e) { checkMatch(); }
+            @Override
+            public void changedUpdate(DocumentEvent e) { checkMatch(); }
+        };
+        confirmPassField.getDocument().addDocumentListener(matchListener);
+        passField.getDocument().addDocumentListener(matchListener);
+        
+        // 瀵嗙爜闀垮害妫�鏌�
+        passField.getDocument().addDocumentListener(new DocumentListener() {
+            private void checkLength() {
+                char[] password = passField.getPassword();
+                if (password.length > 0 && password.length < 6) {
+                     passwordHintLabel.setText(getTranslationValue(currentLanguageCode, "password_length_error", "瀵嗙爜闀垮害涓嶈兘灏戜簬6浣�"));
+                } else {
+                     passwordHintLabel.setText(" ");
+                }
+            }
+            public void insertUpdate(DocumentEvent e) { checkLength(); }
+            public void removeUpdate(DocumentEvent e) { checkLength(); }
+            public void changedUpdate(DocumentEvent e) { checkLength(); }
+        });
+        
+        // 閭鏍煎紡妫�鏌�
+        emailField.getDocument().addDocumentListener(new DocumentListener() {
+            private void validate() {
+                String email = emailField.getText().trim();
+                if (!email.isEmpty() && !isValidEmail(email)) {
+                    emailHintLabel.setText(getTranslationValue(currentLanguageCode, "invalid_email_error", "閭鏍煎紡閿欒"));
+                } else {
+                    emailHintLabel.setText("");
+                }
+            }
+            public void insertUpdate(DocumentEvent e) { validate(); }
+            public void removeUpdate(DocumentEvent e) { validate(); }
+            public void changedUpdate(DocumentEvent e) { validate(); }
+        });
     }
 
     private void updateLanguage(String languageCode) {
@@ -399,6 +475,9 @@
             registerButton.setText(getTranslationValue(languageCode, "register_button", registerButton.getText()));
             cancelButton.setText(getTranslationValue(languageCode, "cancel_button", cancelButton.getText()));
             sendCodeButton.setText(getTranslationValue(languageCode, "send_code_button", sendCodeButton.getText()));
+            if (passwordHintLabel != null) {
+                passwordHintLabel.setText(getTranslationValue(languageCode, "password_length_error", passwordHintLabel.getText()));
+            }
         }
     }
 
@@ -437,35 +516,51 @@
             return;
         }
         
-        // 鐢熸垚闅忔満楠岃瘉鐮侊紙瀹為檯搴旂敤涓簲璇ラ�氳繃閭欢鍙戦�侊級
-        generatedVerificationCode = generateRandomCode();
+        // 绂佺敤鎸夐挳锛岄槻姝㈤噸澶嶇偣鍑�
+        sendCodeButton.setEnabled(false);
         
-        // 鍦ㄥ疄闄呭簲鐢ㄤ腑锛岃繖閲屽簲璇ヨ皟鐢ㄩ偖浠跺彂閫佹湇鍔�
-        // 杩欓噷浠呮ā鎷熷彂閫佽繃绋�
-        System.out.println("楠岃瘉鐮� " + generatedVerificationCode + " 宸插彂閫佸埌 " + email);
-        
-        // 鑾峰彇褰撳墠璇█鐨勭炕璇�
-        Map<String, String> translationMap = translations.get(currentLanguageCode);
-        if (translationMap != null) {
-            JOptionPane.showMessageDialog(this, 
-                translationMap.get("verification_code_sent"), 
-                "鎻愮ず", 
-                JOptionPane.INFORMATION_MESSAGE);
-        } else {
-            JOptionPane.showMessageDialog(this, 
-                "楠岃瘉鐮佸凡鍙戦�佸埌鎮ㄧ殑閭", 
-                "鎻愮ず", 
-                JOptionPane.INFORMATION_MESSAGE);
-        }
-        
-        // 寮�濮嬪�掕鏃�
-        startCountdown();
-    }
-    
-    // 鐢熸垚闅忔満楠岃瘉鐮�
-    private String generateRandomCode() {
-        // 鐢熸垚6浣嶉殢鏈烘暟瀛�
-        return String.valueOf((int)((Math.random() * 9 + 1) * 100000));
+        // 鍦ㄦ柊绾跨▼涓皟鐢ˋPI锛岄伩鍏嶉樆濉濽I
+        new Thread(() -> {
+            try {
+                // 璋冪敤閭欢楠岃瘉鐮佸彂閫丄PI
+                EmailCodeSender.EmailCodeResponse response = EmailCodeSender.sendEmailCode(email);
+                
+                // 鍦‥DT绾跨▼涓洿鏂癠I
+                SwingUtilities.invokeLater(() -> {
+                    if (response.isSuccess()) {
+                        // 鍙戦�佹垚鍔燂紝鏄剧ず鎻愮ず骞跺紑濮嬪�掕鏃�
+                        Map<String, String> translationMap = translations.get(currentLanguageCode);
+                        String successMessage = translationMap != null ? 
+                            translationMap.get("verification_code_sent") : "楠岃瘉鐮佸凡鍙戦�佸埌鎮ㄧ殑閭";
+                        JOptionPane.showMessageDialog(this, 
+                            successMessage, 
+                            "鎻愮ず", 
+                            JOptionPane.INFORMATION_MESSAGE);
+                        
+                        // 寮�濮�60绉掑�掕鏃�
+                        startCountdown();
+                    } else {
+                        // 鍙戦�佸け璐ワ紝鏄剧ず閿欒淇℃伅骞舵仮澶嶆寜閽�
+                        sendCodeButton.setEnabled(true);
+                        String errorMessage = response.getMessage() != null ? 
+                            response.getMessage() : "楠岃瘉鐮佸彂閫佸け璐�";
+                        JOptionPane.showMessageDialog(this, 
+                            errorMessage, 
+                            "閿欒", 
+                            JOptionPane.ERROR_MESSAGE);
+                    }
+                });
+            } catch (Exception e) {
+                // 寮傚父澶勭悊锛屾仮澶嶆寜閽苟鏄剧ず閿欒
+                SwingUtilities.invokeLater(() -> {
+                    sendCodeButton.setEnabled(true);
+                    JOptionPane.showMessageDialog(this, 
+                        "鍙戦�侀獙璇佺爜鏃跺彂鐢熼敊璇�: " + e.getMessage(), 
+                        "閿欒", 
+                        JOptionPane.ERROR_MESSAGE);
+                });
+            }
+        }).start();
     }
     
     // 寮�濮嬪�掕鏃�
@@ -506,17 +601,19 @@
     }
 
     private void performRegistration() {
-        String username = userField.getText().trim();
+        String nickname = userField.getText().trim();
         String email = emailField.getText().trim();
-        String verificationCode = verificationCodeField.getText().trim();
-        String password = new String(passField.getPassword());
-        String confirmPassword = new String(confirmPassField.getPassword());
+        String code = verificationCodeField.getText().trim();
+        
+        // 鑾峰彇瀵嗙爜瀛楁鐨勫�硷紙涓庣櫥褰曢〉闈繚鎸佷竴鑷寸殑澶勭悊鏂瑰紡锛�
+        String password = new String(passField.getPassword()).trim();
+        String confirmPassword = new String(confirmPassField.getPassword()).trim();
         
         // 鑾峰彇褰撳墠璇█鐨勭炕璇�
         Map<String, String> translationMap = translations.get(currentLanguageCode);
         
         // 杈撳叆楠岃瘉
-        if (username.isEmpty() || email.isEmpty() || verificationCode.isEmpty() || 
+        if (nickname.isEmpty() || email.isEmpty() || code.isEmpty() || 
             password.isEmpty() || confirmPassword.isEmpty()) {
             String errorMessage = translationMap != null ? 
                 translationMap.get("empty_fields_error") : "璇峰~鍐欐墍鏈夊瓧娈�";
@@ -553,19 +650,7 @@
             return;
         }
         
-        // 楠岃瘉楠岃瘉鐮�
-        if (!verificationCode.equals(generatedVerificationCode)) {
-            String errorMessage = translationMap != null ? 
-                translationMap.get("invalid_verification_code") : "楠岃瘉鐮侀敊璇�";
-            JOptionPane.showMessageDialog(this, 
-                errorMessage, 
-                "杈撳叆閿欒", 
-                JOptionPane.WARNING_MESSAGE);
-            verificationCodeField.setText("");
-            verificationCodeField.requestFocus();
-            return;
-        }
-        
+        // 楠岃瘉涓ゆ杈撳叆鐨勫瘑鐮佹槸鍚︿竴鑷�
         if (!password.equals(confirmPassword)) {
             String errorMessage = translationMap != null ? 
                 translationMap.get("password_mismatch_error") : "涓ゆ杈撳叆鐨勫瘑鐮佷笉涓�鑷�";
@@ -579,25 +664,51 @@
             return;
         }
         
-        // 淇濆瓨鐢ㄦ埛淇℃伅
-        UserChuShiHua.updateProperty("userName", username);
-        UserChuShiHua.updateProperty("email", email);
-        UserChuShiHua.updateProperty("password", password);
+        // 绂佺敤娉ㄥ唽鎸夐挳锛岄槻姝㈤噸澶嶆彁浜�
+        registerButton.setEnabled(false);
         
-        String successMessage = translationMap != null ? 
-            translationMap.get("success_message") : "娉ㄥ唽鎴愬姛锛�";
-        JOptionPane.showMessageDialog(this, 
-            successMessage, 
-            "鎴愬姛", 
-            JOptionPane.INFORMATION_MESSAGE);
-        
-        dispose();
-        
-        // 鑷姩濉厖鐧诲綍琛ㄥ崟
-        if (parentFrame != null) {
-            // 杩欓噷闇�瑕佽闂埗绐楀彛鐨剈sernameField锛屼絾鐢变簬灏佽鎬э紝鎴戜滑鍙兘闇�瑕佹坊鍔犲叕鍏辨柟娉�
-            // 鏆傛椂鐣欑┖锛屽彲浠ユ牴鎹渶瑕佸疄鐜�
-        }
+        // 鍦ㄦ柊绾跨▼涓皟鐢ˋPI锛岄伩鍏嶉樆濉濽I
+        new Thread(() -> {
+            try {
+                // 璋冪敤鐢ㄦ埛娉ㄥ唽API
+                UserRegister.RegisterResponse response = UserRegister.register(email, password, code, nickname,confirmPassword);
+                
+                // 鍦‥DT绾跨▼涓洿鏂癠I
+                SwingUtilities.invokeLater(() -> {
+                    registerButton.setEnabled(true);
+                    
+                    if (response.isSuccess()) {
+                        // 娉ㄥ唽鎴愬姛锛屾樉绀烘垚鍔熸彁绀�
+                        String successMessage = translationMap != null ? 
+                            translationMap.get("success_message") : "娉ㄥ唽鎴愬姛锛�";
+                        JOptionPane.showMessageDialog(this, 
+                            successMessage, 
+                            "鎴愬姛", 
+                            JOptionPane.INFORMATION_MESSAGE);
+                        
+                        // 鍏抽棴娉ㄥ唽绐楀彛
+                        dispose();
+                    } else {
+                        // 娉ㄥ唽澶辫触锛屾樉绀洪敊璇俊鎭�
+                        String errorMessage = response.getMessage() != null ? 
+                            response.getMessage() : "娉ㄥ唽澶辫触";
+                        JOptionPane.showMessageDialog(this, 
+                            errorMessage, 
+                            "娉ㄥ唽澶辫触", 
+                            JOptionPane.ERROR_MESSAGE);
+                    }
+                });
+            } catch (Exception e) {
+                // 寮傚父澶勭悊锛屾仮澶嶆寜閽苟鏄剧ず閿欒
+                SwingUtilities.invokeLater(() -> {
+                    registerButton.setEnabled(true);
+                    JOptionPane.showMessageDialog(this, 
+                        "娉ㄥ唽鏃跺彂鐢熼敊璇�: " + e.getMessage(), 
+                        "閿欒", 
+                        JOptionPane.ERROR_MESSAGE);
+                });
+            }
+        }).start();
     }
     
     @Override
@@ -608,4 +719,47 @@
         }
         super.dispose();
     }
+    
+    private JPanel createPasswordPanel(JPasswordField passField) {
+        JPanel panel = new JPanel(new BorderLayout());
+        panel.setBackground(Color.WHITE);
+        panel.setBorder(BorderFactory.createCompoundBorder(
+            BorderFactory.createLineBorder(new Color(200, 200, 200)),
+            BorderFactory.createEmptyBorder(1, 1, 1, 1)
+        ));
+        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
+        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
+        
+        passField.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 8));
+        panel.add(passField, BorderLayout.CENTER);
+        
+        JButton toggleBtn = new JButton();
+        try {
+            ImageIcon icon = new ImageIcon("image/look.png");
+            if (icon.getIconWidth() > 0) {
+                Image img = icon.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
+                toggleBtn.setIcon(new ImageIcon(img));
+            } else {
+                toggleBtn.setText("馃憗");
+            }
+        } catch (Exception e) {
+            toggleBtn.setText("馃憗");
+        }
+        
+        toggleBtn.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
+        toggleBtn.setContentAreaFilled(false);
+        toggleBtn.setFocusPainted(false);
+        toggleBtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
+        
+        toggleBtn.addActionListener(e -> {
+            if (passField.getEchoChar() != 0) {
+                passField.setEchoChar((char) 0);
+            } else {
+                passField.setEchoChar('鈥�');
+            }
+        });
+        
+        panel.add(toggleBtn, BorderLayout.EAST);
+        return panel;
+    }
 }
\ No newline at end of file

--
Gitblit v1.10.0