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.
194 lines
7.0 KiB
194 lines
7.0 KiB
package com.mathlearning.view;
|
|
|
|
import com.mathlearning.controller.AuthController;
|
|
import com.mathlearning.controller.NavigationController;
|
|
import com.mathlearning.util.EmailUtil;
|
|
import com.mathlearning.util.UsernameValidator;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
|
|
public class RegisterView extends JPanel {
|
|
private NavigationController navigationController;
|
|
private AuthController authController;
|
|
|
|
private JTextField emailField;
|
|
private JTextField usernameField;
|
|
private JButton sendCodeButton;
|
|
private JButton verifyButton;
|
|
private JButton backButton;
|
|
private JLabel statusLabel;
|
|
private JTextField codeField;
|
|
private String currentVerificationCode;
|
|
|
|
public RegisterView(NavigationController navigationController) {
|
|
this.navigationController = navigationController;
|
|
this.authController = new AuthController();
|
|
initializeUI();
|
|
}
|
|
|
|
private void initializeUI() {
|
|
setLayout(new GridBagLayout());
|
|
GridBagConstraints gbc = new GridBagConstraints();
|
|
gbc.insets = new Insets(10, 10, 10, 10);
|
|
gbc.fill = GridBagConstraints.HORIZONTAL;
|
|
|
|
JLabel titleLabel = new JLabel("用户注册", JLabel.CENTER);
|
|
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
|
|
gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2;
|
|
add(titleLabel, gbc);
|
|
|
|
gbc.gridwidth = 1;
|
|
gbc.gridx = 0; gbc.gridy = 1;
|
|
add(new JLabel("用户名:"), gbc);
|
|
|
|
gbc.gridx = 1; gbc.gridy = 1;
|
|
usernameField = new JTextField(20);
|
|
add(usernameField, gbc);
|
|
|
|
gbc.gridx = 0; gbc.gridy = 2;
|
|
add(new JLabel("邮箱地址:"), gbc);
|
|
|
|
gbc.gridx = 1; gbc.gridy = 2;
|
|
emailField = new JTextField(20);
|
|
add(emailField, gbc);
|
|
|
|
gbc.gridx = 0; gbc.gridy = 3;
|
|
add(new JLabel("验证码:"), gbc);
|
|
|
|
gbc.gridx = 1; gbc.gridy = 3;
|
|
JPanel codePanel = new JPanel(new BorderLayout());
|
|
codeField = new JTextField(10);
|
|
codePanel.add(codeField, BorderLayout.CENTER);
|
|
|
|
sendCodeButton = new JButton("发送验证码");
|
|
sendCodeButton.addActionListener(e -> handleSendCode());
|
|
codePanel.add(sendCodeButton, BorderLayout.EAST);
|
|
|
|
add(codePanel, gbc);
|
|
|
|
gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 2;
|
|
JLabel usernameReqLabel = new JLabel(UsernameValidator.getRequirements(), JLabel.CENTER);
|
|
usernameReqLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
|
|
usernameReqLabel.setForeground(Color.GRAY);
|
|
add(usernameReqLabel, gbc);
|
|
|
|
gbc.gridy = 5;
|
|
JLabel emailReqLabel = new JLabel(EmailUtil.getEmailRequirements(), JLabel.CENTER);
|
|
emailReqLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
|
|
emailReqLabel.setForeground(Color.GRAY);
|
|
add(emailReqLabel, gbc);
|
|
|
|
gbc.gridy = 6;
|
|
statusLabel = new JLabel("", JLabel.CENTER);
|
|
statusLabel.setForeground(Color.BLUE);
|
|
add(statusLabel, gbc);
|
|
|
|
gbc.gridy = 7;
|
|
verifyButton = new JButton("验证并继续");
|
|
verifyButton.addActionListener(e -> handleVerify());
|
|
add(verifyButton, gbc);
|
|
|
|
gbc.gridy = 8;
|
|
backButton = new JButton("返回登录");
|
|
backButton.addActionListener(e -> navigationController.showLoginView());
|
|
add(backButton, gbc);
|
|
}
|
|
|
|
private void handleSendCode() {
|
|
String email = emailField.getText().trim();
|
|
String username = usernameField.getText().trim();
|
|
|
|
if (username.isEmpty() || email.isEmpty()) {
|
|
showError("请输入用户名和邮箱地址");
|
|
return;
|
|
}
|
|
|
|
if (!EmailUtil.isValidEmail(email)) {
|
|
showError("请输入有效的邮箱地址");
|
|
return;
|
|
}
|
|
|
|
if (!UsernameValidator.isValid(username)) {
|
|
showError(UsernameValidator.getRequirements());
|
|
return;
|
|
}
|
|
|
|
if (!authController.isEmailAvailable(email)) {
|
|
showError("该邮箱已被注册");
|
|
return;
|
|
}
|
|
|
|
if (!authController.isUsernameAvailable(username)) {
|
|
showError("该用户名已被使用");
|
|
return;
|
|
}
|
|
|
|
sendCodeButton.setEnabled(false);
|
|
sendCodeButton.setText("发送中...");
|
|
statusLabel.setText("正在发送验证码,请稍候...");
|
|
statusLabel.setForeground(Color.BLUE);
|
|
|
|
SwingWorker<String, Void> worker = new SwingWorker<String, Void>() {
|
|
@Override
|
|
protected String doInBackground() throws Exception {
|
|
return authController.sendVerificationCode(email, username);
|
|
}
|
|
|
|
@Override
|
|
protected void done() {
|
|
try {
|
|
currentVerificationCode = get();
|
|
if (currentVerificationCode != null) {
|
|
statusLabel.setText("验证码已发送!请在下方输入验证码");
|
|
statusLabel.setForeground(Color.GREEN);
|
|
codeField.setEnabled(true);
|
|
verifyButton.setEnabled(true);
|
|
} else {
|
|
showError("发送验证码失败,请检查邮箱地址或稍后重试");
|
|
}
|
|
} catch (Exception e) {
|
|
showError("发送验证码时发生错误: " + e.getMessage());
|
|
} finally {
|
|
sendCodeButton.setEnabled(true);
|
|
sendCodeButton.setText("发送验证码");
|
|
}
|
|
}
|
|
};
|
|
|
|
worker.execute();
|
|
}
|
|
|
|
private void handleVerify() {
|
|
String email = emailField.getText().trim();
|
|
String username = usernameField.getText().trim();
|
|
String code = codeField.getText().trim();
|
|
|
|
if (code.isEmpty()) {
|
|
showError("请输入验证码");
|
|
return;
|
|
}
|
|
|
|
if (currentVerificationCode != null && code.equals(currentVerificationCode)) {
|
|
boolean success = authController.createUnregisteredUser(email, username, currentVerificationCode);
|
|
if (success) {
|
|
showSuccess("验证成功!");
|
|
navigationController.showSetPasswordView(email, currentVerificationCode);
|
|
} else {
|
|
showError("创建用户失败");
|
|
}
|
|
} else {
|
|
showError("验证码错误,请重新输入");
|
|
}
|
|
}
|
|
|
|
private void showError(String message) {
|
|
statusLabel.setText(message);
|
|
statusLabel.setForeground(Color.RED);
|
|
}
|
|
|
|
private void showSuccess(String message) {
|
|
statusLabel.setText(message);
|
|
statusLabel.setForeground(Color.GREEN);
|
|
}
|
|
} |