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.
280 lines
9.8 KiB
280 lines
9.8 KiB
package com.mathlearning.view;
|
|
|
|
import com.mathlearning.controller.AuthController;
|
|
import com.mathlearning.service.EmailService;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.awt.event.FocusEvent;
|
|
import java.awt.event.FocusListener;
|
|
|
|
public class RegisterFrame extends JFrame {
|
|
private AuthController authController;
|
|
private JTextField emailField;
|
|
private JLabel resultLabel;
|
|
private JButton registerButton;
|
|
private Timer countdownTimer;
|
|
private int remainingTime = 0;
|
|
private JTextField codeField;
|
|
private JButton submitButton;
|
|
private String currentEmail; // 存储当前正在注册的邮箱
|
|
|
|
public RegisterFrame() {
|
|
this.authController = new AuthController();
|
|
initializeUI();
|
|
}
|
|
|
|
private void initializeUI() {
|
|
setTitle("数学学习软件 - 注册");
|
|
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
setSize(500, 300); // 保持原有宽度以显示更长的提示信息
|
|
setLocationRelativeTo(null);
|
|
setResizable(false);
|
|
|
|
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
|
|
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
|
|
|
|
// 添加顶部返回按钮
|
|
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
|
JButton backButton = new JButton("← 返回登录");
|
|
backButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
|
|
backButton.setBackground(new Color(200, 250, 190));
|
|
backButton.addActionListener(e -> backToLogin());
|
|
topPanel.add(backButton);
|
|
mainPanel.add(topPanel, BorderLayout.NORTH);
|
|
|
|
JLabel titleLabel = new JLabel("用户注册", JLabel.CENTER);
|
|
titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 18));
|
|
mainPanel.add(titleLabel, BorderLayout.CENTER);
|
|
|
|
JPanel formPanel = new JPanel(new GridLayout(3, 2, 10, 10));
|
|
formPanel.setLayout(null);
|
|
JLabel emailLabel = new JLabel("邮箱地址:");
|
|
emailLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
|
|
emailLabel.setBounds(20, 20, 100, 20);
|
|
emailField = new JTextField();
|
|
emailField.setBounds(100, 20, 180, 30);
|
|
// 添加邮箱输入框的焦点监听器,实现实时验证
|
|
emailField.addFocusListener(
|
|
new FocusListener() {
|
|
@Override
|
|
public void focusGained(FocusEvent e) {
|
|
// 获得焦点时不清除内容
|
|
}
|
|
|
|
@Override
|
|
public void focusLost(FocusEvent e) {
|
|
// 失去焦点时验证邮箱格式
|
|
validateEmailInRealTime();
|
|
}
|
|
});
|
|
|
|
// 添加输入监听器,实现输入时实时验证
|
|
emailField
|
|
.getDocument()
|
|
.addDocumentListener(
|
|
new javax.swing.event.DocumentListener() {
|
|
public void insertUpdate(javax.swing.event.DocumentEvent e) {
|
|
validateEmailInRealTime();
|
|
}
|
|
|
|
public void removeUpdate(javax.swing.event.DocumentEvent e) {
|
|
validateEmailInRealTime();
|
|
}
|
|
|
|
public void changedUpdate(javax.swing.event.DocumentEvent e) {
|
|
validateEmailInRealTime();
|
|
}
|
|
});
|
|
|
|
registerButton = new JButton("获取注册码");
|
|
registerButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
|
|
registerButton.setBounds(285, 20, 140, 30);
|
|
registerButton.setEnabled(false); // 初始时禁用
|
|
JLabel codeLabel = new JLabel("注册码:");
|
|
codeLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
|
|
codeLabel.setBounds(20, 60, 100, 20);
|
|
codeField = new JTextField();
|
|
codeField.setBounds(100, 60, 180, 30);
|
|
codeField.setEnabled(false); // 初始时禁用
|
|
|
|
submitButton = new JButton("注册");
|
|
submitButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
|
|
submitButton.setBounds(285, 60, 140, 30);
|
|
submitButton.setEnabled(false); // 初始时禁用
|
|
|
|
formPanel.add(emailLabel);
|
|
formPanel.add(emailField);
|
|
formPanel.add(new JLabel());
|
|
formPanel.add(registerButton);
|
|
formPanel.add(codeLabel);
|
|
formPanel.add(codeField);
|
|
formPanel.add(submitButton);
|
|
|
|
resultLabel = new JLabel("请输入有效的邮箱地址", JLabel.CENTER);
|
|
resultLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
|
|
resultLabel.setForeground(Color.BLUE);
|
|
|
|
mainPanel.add(formPanel, BorderLayout.CENTER);
|
|
mainPanel.add(resultLabel, BorderLayout.SOUTH);
|
|
registerButton.addActionListener(new RegisterAction());
|
|
submitButton.addActionListener(new SubmitAction());
|
|
countdownTimer = new Timer(1000, new CountdownAction());
|
|
|
|
add(mainPanel);
|
|
}
|
|
|
|
// 实时验证邮箱格式
|
|
private void validateEmailInRealTime() {
|
|
try {
|
|
String email = emailField.getText().trim();
|
|
|
|
if (email.isEmpty()) {
|
|
resultLabel.setText("请输入邮箱地址");
|
|
resultLabel.setForeground(Color.BLUE);
|
|
registerButton.setEnabled(false);
|
|
return;
|
|
}
|
|
|
|
String validationMessage = authController.getEmailValidationMessage(email);
|
|
|
|
if (validationMessage.equals("邮箱格式正确")) {
|
|
resultLabel.setText("✓ 邮箱格式正确");
|
|
resultLabel.setForeground(new Color(0, 150, 0)); // 绿色
|
|
registerButton.setEnabled(true);
|
|
} else {
|
|
resultLabel.setText("✗ " + validationMessage);
|
|
resultLabel.setForeground(Color.RED);
|
|
registerButton.setEnabled(false);
|
|
}
|
|
} catch (Exception e) {
|
|
resultLabel.setText("✗ 邮箱验证服务暂时不可用");
|
|
resultLabel.setForeground(Color.RED);
|
|
registerButton.setEnabled(false);
|
|
}
|
|
}
|
|
|
|
private class RegisterAction implements ActionListener {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
if (EmailService.isNetworkAvailable()) {
|
|
String email = emailField.getText().trim();
|
|
if (email.isEmpty()) {
|
|
resultLabel.setText("请输入邮箱地址");
|
|
resultLabel.setForeground(Color.RED);
|
|
return;
|
|
}
|
|
String validationMessage = authController.getEmailValidationMessage(email); // 再次验证邮箱格式
|
|
if (!validationMessage.equals("邮箱格式正确")) {
|
|
resultLabel.setText("✗ " + validationMessage);
|
|
resultLabel.setForeground(Color.RED);
|
|
return;
|
|
}
|
|
String result = authController.register(email);
|
|
resultLabel.setText(result);
|
|
if (result.contains("发送")) {
|
|
resultLabel.setForeground(Color.BLUE);
|
|
currentEmail = email; // 保存当前邮箱
|
|
remainingTime = 120; // 开始倒计时
|
|
registerButton.setEnabled(false);
|
|
codeField.setEnabled(true); // 启用验证码输入框
|
|
submitButton.setEnabled(true); // 启用注册按钮
|
|
updateRegisterButtonText();
|
|
countdownTimer.start();
|
|
} else {
|
|
resultLabel.setForeground(Color.RED);
|
|
}
|
|
} else {
|
|
showDialog();
|
|
}
|
|
}
|
|
}
|
|
|
|
private class SubmitAction implements ActionListener {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
String code = codeField.getText().trim();
|
|
|
|
if (code.isEmpty()) {
|
|
resultLabel.setText("请输入验证码");
|
|
resultLabel.setForeground(Color.RED);
|
|
return;
|
|
}
|
|
|
|
if (currentEmail == null) {
|
|
resultLabel.setText("请先获取验证码");
|
|
resultLabel.setForeground(Color.RED);
|
|
return;
|
|
}
|
|
|
|
// 使用AuthController验证验证码
|
|
if (authController.isCodeRegistered(currentEmail, code)) {
|
|
countdownTimer.stop();
|
|
resultLabel.setText("验证码正确,正在跳转...");
|
|
resultLabel.setForeground(new Color(0, 150, 0));
|
|
|
|
// 延迟跳转到密码设置界面
|
|
Timer openTimer =
|
|
new Timer(
|
|
1000,
|
|
evt -> {
|
|
openPasswordSetupFrame(currentEmail, code);
|
|
});
|
|
openTimer.setRepeats(false);
|
|
openTimer.start();
|
|
} else {
|
|
resultLabel.setText("验证码错误或已过期,请重新输入");
|
|
resultLabel.setForeground(Color.RED);
|
|
}
|
|
}
|
|
}
|
|
|
|
private class CountdownAction implements ActionListener {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
remainingTime--;
|
|
updateRegisterButtonText();
|
|
|
|
if (remainingTime <= 0) {
|
|
countdownTimer.stop();
|
|
registerButton.setEnabled(true);
|
|
registerButton.setText("获取注册码");
|
|
codeField.setEnabled(false);
|
|
submitButton.setEnabled(false);
|
|
resultLabel.setText("验证码已过期,请重新获取");
|
|
resultLabel.setForeground(Color.RED);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void updateRegisterButtonText() {
|
|
registerButton.setText(String.format("重新发送(%ds)", remainingTime));
|
|
}
|
|
|
|
private void backToLogin() {
|
|
if (countdownTimer != null && countdownTimer.isRunning()) {
|
|
countdownTimer.stop();
|
|
}
|
|
LoginFrame loginFrame = new LoginFrame();
|
|
loginFrame.setVisible(true);
|
|
this.dispose();
|
|
}
|
|
|
|
private void openPasswordSetupFrame(String email, String code) {
|
|
try {
|
|
PasswordSetupFrame passwordFrame = new PasswordSetupFrame(email, code, authController);
|
|
passwordFrame.setVisible(true);
|
|
this.dispose();
|
|
} catch (Exception e) {
|
|
System.out.println("打开密码设置界面时发生异常: " + e.getMessage());
|
|
JOptionPane.showMessageDialog(this, "打开密码设置界面失败,请重试", "错误", JOptionPane.ERROR_MESSAGE);
|
|
}
|
|
}
|
|
|
|
private void showDialog() {
|
|
JOptionPane.showMessageDialog(this, "网络连接失败,请稍后再试", "错误", JOptionPane.ERROR_MESSAGE);
|
|
}
|
|
}
|