package view; import java.awt.*; import java.util.Properties; import java.util.Random; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.BorderFactory; import jakarta.mail.Authenticator; import jakarta.mail.Message; import jakarta.mail.PasswordAuthentication; import jakarta.mail.Session; import jakarta.mail.Transport; import jakarta.mail.internet.InternetAddress; import jakarta.mail.internet.MimeMessage; import model.Login; import model.LanguageSwitch; /** * 注册界面(使用 QQ 邮箱发送验证码) */ public class RegisterFrame extends JDialog { private JTextField usernameField; private JTextField emailField; private JPasswordField pwdField; private JPasswordField pwdField2; private JTextField codeField; 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 邮箱配置 private static final String FROM_EMAIL = "songqifeng.sqf@qq.com"; private static final String AUTH_CODE = "gcyschltjgxedgjd"; // ⚠️ 在 QQ 邮箱里申请的授权码 public RegisterFrame(JFrame owner) { super(owner, "注册新用户", true); setSize(420, 300); setLocationRelativeTo(owner); JPanel p = new JPanel(new GridLayout(7, 2, 6, 6)); p.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); p.add(new JLabel("用户名:")); usernameField = new JTextField(); p.add(usernameField); p.add(new JLabel("邮箱(接收注册码):")); emailField = new JTextField(); p.add(emailField); p.add(new JLabel("密码 (6-10位,需含大小写与数字):")); pwdField = new JPasswordField(); p.add(pwdField); p.add(new JLabel("再次输入密码:")); pwdField2 = new JPasswordField(); p.add(pwdField2); p.add(new JLabel("请输入注册码:")); codeField = new JTextField(); p.add(codeField); JButton sendCodeBtn = new JButton("发送注册码"); p.add(sendCodeBtn); p.add(new JLabel()); JButton regBtn = new JButton("注册"); p.add(regBtn); JButton cancelBtn = new JButton("取消"); p.add(cancelBtn); add(p); sendCodeBtn.addActionListener(e -> { String email = emailField.getText().trim(); if (email.isEmpty() || !isValidEmail(email)) { JOptionPane.showMessageDialog(this, "请输入有效邮箱"); return; } if(Login.isEmailExists(email)){ JOptionPane.showMessageDialog(this,"该邮箱已经被注册,请使用其他邮箱"); return; } lastCode = String.format("%04d", new Random().nextInt(10000)); boolean sent = sendEmail(email, lastCode); if (sent) { JOptionPane.showMessageDialog(this, "注册码已发送,请检查邮箱。"); } else { JOptionPane.showMessageDialog(this, "发送邮件失败,请检查网络或邮箱配置。"); } }); regBtn.addActionListener(e -> { String u = usernameField.getText().trim(); String email = emailField.getText().trim(); String pwd = new String(pwdField.getPassword()).trim(); String pwd2 = new String(pwdField2.getPassword()).trim(); String code = codeField.getText().trim(); if (u.isEmpty() || email.isEmpty() || pwd.isEmpty() || pwd2.isEmpty() || code.isEmpty()) { JOptionPane.showMessageDialog(this, "请填写完整信息并输入注册码"); return; } if(Login.isEmailExists(email)){ JOptionPane.showMessageDialog(this,"该邮箱已被注册,请使用其他邮箱"); return; } if (!code.equals(lastCode)) { JOptionPane.showMessageDialog(this, "注册码错误,请重新输入"); return; } if (!pwd.equals(pwd2)) { JOptionPane.showMessageDialog(this, "两次密码不一致"); return; } if (!Login.validatePasswordRules(pwd)) { JOptionPane.showMessageDialog(this, "密码不满足要求:6-10位且包含大写、小写和数字"); return; } Login.Level lv = Login.Level.PRIMARY; boolean ok = Login.register(u, pwd, lv, email); if (!ok) { JOptionPane.showMessageDialog(this, "用户名或邮箱已存在,请换一个用户名"); return; } JOptionPane.showMessageDialog(this, "注册成功,请用新用户登录"); dispose(); }); cancelBtn.addActionListener(e -> dispose()); setVisible(true); } private boolean sendEmail(String to, String code) { try { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.qq.com"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.ssl.enable", "true"); // ← 开启 SSL Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(FROM_EMAIL, AUTH_CODE); } }); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(FROM_EMAIL)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject("数学卷子生成器 - 注册验证码"); message.setText("您的注册码为: " + code + "\n有效期 5 分钟。"); Transport.send(message); return true; } catch (Exception e) { e.printStackTrace(); return false; } } }