|
|
package ui;
|
|
|
|
|
|
import controller.AppController;
|
|
|
import controller.UserManager;
|
|
|
import util.EmailUtil;
|
|
|
|
|
|
import javax.swing.*;
|
|
|
import java.awt.*;
|
|
|
|
|
|
/**
|
|
|
* 注册界面
|
|
|
*/
|
|
|
public class RegisterPanel extends JPanel {
|
|
|
private JTextField emailField;
|
|
|
private JTextField codeField;
|
|
|
private JPasswordField passwordField;
|
|
|
private JPasswordField confirmField;
|
|
|
private JTextField usernameField; // 新增用户名输入框
|
|
|
|
|
|
private String sentCode = null;
|
|
|
|
|
|
public RegisterPanel(AppController controller) {
|
|
|
setLayout(new GridBagLayout());
|
|
|
GridBagConstraints gbc = new GridBagConstraints();
|
|
|
gbc.insets = new Insets(8, 8, 8, 8);
|
|
|
gbc.fill = GridBagConstraints.HORIZONTAL;
|
|
|
|
|
|
JLabel title = new JLabel("用户注册", JLabel.CENTER);
|
|
|
title.setFont(new Font("微软雅黑", Font.BOLD, 22));
|
|
|
gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2;
|
|
|
add(title, gbc);
|
|
|
|
|
|
gbc.gridwidth = 1;
|
|
|
gbc.gridx = 0; gbc.gridy = 1;
|
|
|
add(new JLabel("邮箱:"), gbc);
|
|
|
|
|
|
gbc.gridx = 1;
|
|
|
emailField = new JTextField(20);
|
|
|
add(emailField, gbc);
|
|
|
|
|
|
gbc.gridx = 0; gbc.gridy = 2;
|
|
|
add(new JLabel("用户名:"), gbc); // 修改标签
|
|
|
gbc.gridx = 1;
|
|
|
usernameField = new JTextField(20); // 新增用户名输入框
|
|
|
add(usernameField, gbc);
|
|
|
|
|
|
gbc.gridx = 0; gbc.gridy = 3;
|
|
|
add(new JLabel("验证码:"), gbc);
|
|
|
|
|
|
gbc.gridx = 1;
|
|
|
codeField = new JTextField(10);
|
|
|
add(codeField, gbc);
|
|
|
|
|
|
gbc.gridx = 0; gbc.gridy = 4;
|
|
|
add(new JLabel("密码:"), gbc);
|
|
|
|
|
|
gbc.gridx = 1;
|
|
|
passwordField = new JPasswordField(20);
|
|
|
add(passwordField, gbc);
|
|
|
|
|
|
gbc.gridx = 0; gbc.gridy = 5;
|
|
|
add(new JLabel("确认密码:"), gbc);
|
|
|
|
|
|
gbc.gridx = 1;
|
|
|
confirmField = new JPasswordField(20);
|
|
|
add(confirmField, gbc);
|
|
|
|
|
|
JButton sendCodeButton = new JButton("发送验证码");
|
|
|
gbc.gridx = 0; gbc.gridy = 6;
|
|
|
add(sendCodeButton, gbc);
|
|
|
|
|
|
JButton registerButton = new JButton("注册");
|
|
|
gbc.gridx = 1;
|
|
|
add(registerButton, gbc);
|
|
|
|
|
|
JButton backButton = new JButton("返回登录");
|
|
|
gbc.gridx = 0; gbc.gridy = 7; gbc.gridwidth = 2;
|
|
|
add(backButton, gbc);
|
|
|
|
|
|
sendCodeButton.addActionListener(e -> {
|
|
|
String email = emailField.getText().trim();
|
|
|
if (!email.matches("^[A-Za-z0-9+_.-]+@(.+)$")) {
|
|
|
JOptionPane.showMessageDialog(this, "邮箱格式不正确!");
|
|
|
return;
|
|
|
}
|
|
|
sentCode = EmailUtil.sendVerificationCode(email);
|
|
|
JOptionPane.showMessageDialog(this, "验证码已发送(调试模式控制台查看)!");
|
|
|
});
|
|
|
|
|
|
registerButton.addActionListener(e -> {
|
|
|
String email = emailField.getText().trim();
|
|
|
String code = codeField.getText().trim();
|
|
|
String password = new String(passwordField.getPassword());
|
|
|
String confirm = new String(confirmField.getPassword());
|
|
|
String username = usernameField.getText().trim(); // 获取用户名
|
|
|
|
|
|
if (sentCode == null) {
|
|
|
JOptionPane.showMessageDialog(this, "请先获取验证码!");
|
|
|
return;
|
|
|
}
|
|
|
if (!code.equals(sentCode)) {
|
|
|
JOptionPane.showMessageDialog(this, "验证码错误!");
|
|
|
return;
|
|
|
}
|
|
|
if (!password.equals(confirm)) {
|
|
|
JOptionPane.showMessageDialog(this, "两次密码不一致!");
|
|
|
return;
|
|
|
}
|
|
|
if (!password.matches("(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[A-Za-z\\d]{6,10}")) {
|
|
|
JOptionPane.showMessageDialog(this, "密码需包含大小写字母与数字,长度6-10位!");
|
|
|
return;
|
|
|
}
|
|
|
//防止用户名重复
|
|
|
if (controller.getUserManager().isUsernameExists(username)) {
|
|
|
JOptionPane.showMessageDialog(this, "用户名已存在,请更换!");
|
|
|
return;
|
|
|
}
|
|
|
if (username.isEmpty() || password.isEmpty() || email.isEmpty() || code.isEmpty()) {
|
|
|
JOptionPane.showMessageDialog(this, "请填写完整信息!");
|
|
|
return;
|
|
|
}
|
|
|
controller.handleRegister(email, password, username); // 传递username
|
|
|
});
|
|
|
|
|
|
backButton.addActionListener(e -> controller.showLoginPanel());
|
|
|
}
|
|
|
} |