cxb #5

Merged
plks47r9b merged 3 commits from chenxiangbo_branch into develop 2 months ago

@ -64,6 +64,19 @@ public class Login {
return null; return null;
} }
public static boolean isEmailExists(String email){
if(email == null || email.trim().isEmpty()){
return false;
}
for(Account acc:accounts.values()){
if(acc.email != null && acc.email.equalsIgnoreCase(email.trim())){
return true;
}
}
return false;
}
// 注册GUI 调用),若用户名已存在返回 false // 注册GUI 调用),若用户名已存在返回 false
// 注册GUI 调用),若用户名或邮箱已存在返回 false // 注册GUI 调用),若用户名或邮箱已存在返回 false
public static synchronized boolean register(String username, String password, Level level, public static synchronized boolean register(String username, String password, Level level,
@ -73,6 +86,10 @@ public class Login {
return false; return false;
} }
if(isEmailExists(email)){
return false;
}
// 检查邮箱是否重复 // 检查邮箱是否重复
for (Account existing : accounts.values()) { for (Account existing : accounts.values()) {
if (existing.email != null && existing.email.equalsIgnoreCase(email)) { if (existing.email != null && existing.email.equalsIgnoreCase(email)) {

@ -1,6 +1,6 @@
package view; package view;
import java.awt.GridLayout; import java.awt.*;
import java.util.Properties; import java.util.Properties;
import java.util.Random; import java.util.Random;
@ -25,7 +25,6 @@ import jakarta.mail.internet.MimeMessage;
import model.Login; import model.Login;
import model.LanguageSwitch; import model.LanguageSwitch;
/** /**
* 使 QQ * 使 QQ
*/ */
@ -33,19 +32,21 @@ public class RegisterFrame extends JDialog {
private JTextField usernameField; private JTextField usernameField;
private JTextField emailField; private JTextField emailField;
private JComboBox<String> levelBox;
private JPasswordField pwdField; private JPasswordField pwdField;
private JPasswordField pwdField2; private JPasswordField pwdField2;
private JTextField codeField; private JTextField codeField;
private String lastCode; private String lastCode;
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.matches(emailRegex);
}
// QQ 邮箱配置 // QQ 邮箱配置
private static final String FROM_EMAIL = "songqifeng.sqf@qq.com"; private static final String FROM_EMAIL = "songqifeng.sqf@qq.com";
private static final String AUTH_CODE = "gcyschltjgxedgjd"; // ⚠️ 在 QQ 邮箱里申请的授权码 private static final String AUTH_CODE = "gcyschltjgxedgjd"; // ⚠️ 在 QQ 邮箱里申请的授权码
public RegisterFrame(JFrame owner) { public RegisterFrame(JFrame owner) {
super(owner, "注册新用户", true); super(owner, "注册新用户", true);
setSize(420, 320); setSize(420, 300);
setLocationRelativeTo(owner); setLocationRelativeTo(owner);
JPanel p = new JPanel(new GridLayout(7, 2, 6, 6)); JPanel p = new JPanel(new GridLayout(7, 2, 6, 6));
@ -58,11 +59,7 @@ public class RegisterFrame extends JDialog {
emailField = new JTextField(); emailField = new JTextField();
p.add(emailField); p.add(emailField);
p.add(new JLabel("年级:")); p.add(new JLabel("密码 (6-10位需含大小写与数字):"));
levelBox = new JComboBox<>(new String[]{"小学", "初中", "高中"});
p.add(levelBox);
p.add(new JLabel("密码 (6-10位含大小写与数字):"));
pwdField = new JPasswordField(); pwdField = new JPasswordField();
p.add(pwdField); p.add(pwdField);
@ -70,11 +67,15 @@ public class RegisterFrame extends JDialog {
pwdField2 = new JPasswordField(); pwdField2 = new JPasswordField();
p.add(pwdField2); p.add(pwdField2);
JButton sendCodeBtn = new JButton("发送注册码"); p.add(new JLabel("请输入注册码:"));
p.add(sendCodeBtn);
codeField = new JTextField(); codeField = new JTextField();
p.add(codeField); p.add(codeField);
JButton sendCodeBtn = new JButton("发送注册码");
p.add(sendCodeBtn);
p.add(new JLabel());
JButton regBtn = new JButton("注册"); JButton regBtn = new JButton("注册");
p.add(regBtn); p.add(regBtn);
JButton cancelBtn = new JButton("取消"); JButton cancelBtn = new JButton("取消");
@ -84,10 +85,16 @@ public class RegisterFrame extends JDialog {
sendCodeBtn.addActionListener(e -> { sendCodeBtn.addActionListener(e -> {
String email = emailField.getText().trim(); String email = emailField.getText().trim();
if (email.isEmpty() || !email.contains("@")) { if (email.isEmpty() || !isValidEmail(email)) {
JOptionPane.showMessageDialog(this, "请输入有效邮箱"); JOptionPane.showMessageDialog(this, "请输入有效邮箱");
return; return;
} }
if(Login.isEmailExists(email)){
JOptionPane.showMessageDialog(this,"该邮箱已经被注册,请使用其他邮箱");
return;
}
lastCode = String.format("%04d", new Random().nextInt(10000)); lastCode = String.format("%04d", new Random().nextInt(10000));
boolean sent = sendEmail(email, lastCode); boolean sent = sendEmail(email, lastCode);
if (sent) { if (sent) {
@ -108,6 +115,10 @@ public class RegisterFrame extends JDialog {
JOptionPane.showMessageDialog(this, "请填写完整信息并输入注册码"); JOptionPane.showMessageDialog(this, "请填写完整信息并输入注册码");
return; return;
} }
if(Login.isEmailExists(email)){
JOptionPane.showMessageDialog(this,"该邮箱已被注册,请使用其他邮箱");
return;
}
if (!code.equals(lastCode)) { if (!code.equals(lastCode)) {
JOptionPane.showMessageDialog(this, "注册码错误,请重新输入"); JOptionPane.showMessageDialog(this, "注册码错误,请重新输入");
return; return;
@ -120,8 +131,9 @@ public class RegisterFrame extends JDialog {
JOptionPane.showMessageDialog(this, "密码不满足要求6-10位且包含大写、小写和数字"); JOptionPane.showMessageDialog(this, "密码不满足要求6-10位且包含大写、小写和数字");
return; return;
} }
String levelStr = (String) levelBox.getSelectedItem();
Login.Level lv = LanguageSwitch.chineseToLevel(levelStr); Login.Level lv = Login.Level.PRIMARY;
boolean ok = Login.register(u, pwd, lv, email); boolean ok = Login.register(u, pwd, lv, email);
if (!ok) { if (!ok) {
JOptionPane.showMessageDialog(this, "用户名或邮箱已存在,请换一个用户名"); JOptionPane.showMessageDialog(this, "用户名或邮箱已存在,请换一个用户名");

Loading…
Cancel
Save