pull/2/head
玖兮冉 7 months ago
parent f063724d4b
commit 5558669058

@ -4,14 +4,14 @@ import com.wsf.mathapp.view.*;
import javafx.stage.Stage;
public class SceneManager {
private Stage primaryStage;
private LoginView loginView;
private RegisterView registerView;
private MainMenuView mainMenuView;
private LevelSelectionView levelSelectionView;
private QuestionCountView questionCountView;
private QuizView quizView;
private ResultView resultView;
private final Stage primaryStage;
private final LoginView loginView;
private final RegisterView registerView;
private final MainMenuView mainMenuView;
private final LevelSelectionView levelSelectionView;
private final QuestionCountView questionCountView;
private final QuizView quizView;
private final ResultView resultView;
public SceneManager(Stage primaryStage) {
this.primaryStage = primaryStage;

@ -11,8 +11,8 @@ import javafx.scene.text.Font;
public class LoginView {
private Scene scene;
private SceneManager sceneManager;
private UserService userService;
private final SceneManager sceneManager;
private final UserService userService;
public LoginView(SceneManager sceneManager) {
this.sceneManager = sceneManager;

@ -1,19 +1,22 @@
package com.wsf.mathapp.view;
import com.wsf.mathapp.controller.SceneManager;
import com.wsf.mathapp.service.UserService;
import com.ybw.mathapp.service.EmailService;
import com.ybw.mathapp.LoginAndRegister;
import com.ybw.mathapp.util.EmailService;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import com.ybw.mathapp.util.LoginFileUtils;
public class RegisterView {
private Scene scene;
private SceneManager sceneManager;
private UserService userService;
private final SceneManager sceneManager;
private final UserService userService;
public RegisterView(SceneManager sceneManager) {
this.sceneManager = sceneManager;
@ -65,18 +68,17 @@ public class RegisterView {
sendCodeButton.setOnAction(e -> {
String email = emailField.getText().trim();
if (email.isEmpty() || !email.contains("@") || !email.contains(".")) {
if (!email.contains("@") || !email.contains(".")) {
showError(statusLabel, "请输入有效的邮箱地址!");
return;
}
if (LoginFileUtils.isEmailRegistered(email)){
showError(statusLabel, "该邮箱已注册,请直接登录!");
}
// 生成并发送验证码
String verificationCode = EmailService.generateVerificationCode();
boolean sent = EmailService.sendVerificationCode(email, verificationCode);
boolean sent = LoginAndRegister.sendAndVerifyCode(email);
if (sent) {
showSuccess(statusLabel, "验证码已发送到您的邮箱!");
// 禁用发送按钮60秒
sendCodeButton.setDisable(true);
startCountdown(sendCodeButton);
@ -101,8 +103,10 @@ public class RegisterView {
showError(statusLabel, "验证码错误或已过期!");
return;
}
boolean success = userService.register(email, password, confirmPassword);
if (!register.confirmerror(password)) {
showError(statusLabel, "密码要");
}
//boolean success = EmailService.(email,password,confirmPassword);
if (success) {
showSuccess(statusLabel, "注册成功!");
sceneManager.showLoginView();

@ -2,7 +2,7 @@ package com.ybw.mathapp;
// UserService.java
import com.ybw.mathapp.entity.User;
import com.ybw.mathapp.service.EmailService;
import com.ybw.mathapp.util.EmailService;
import com.ybw.mathapp.util.LoginFileUtils;
import java.util.Scanner;
import java.util.regex.Pattern;

@ -1,149 +0,0 @@
package com.ybw.mathapp.service;
import com.ybw.mathapp.config.EmailConfig;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Random;
//import javax.mail.*;
//import javax.mail.internet.*;
public class EmailService {
private static Map<String, VerificationCodeInfo> verificationCodes = new HashMap<>();
private static class VerificationCodeInfo {
String code;
long timestamp;
VerificationCodeInfo(String code, long timestamp) {
this.code = code;
this.timestamp = timestamp;
}
}
public static String generateVerificationCode() {
Random random = new Random();
int code = 100000 + random.nextInt(900000);
return String.valueOf(code);
}
public static boolean sendVerificationCode(String recipientEmail, String code) {
/*
try {
// 创建邮件会话
Properties props = new Properties();
props.put("mail.smtp.host", EmailConfig.SMTP_HOST);
props.put("mail.smtp.port", EmailConfig.SMTP_PORT);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
// 创建认证器
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
EmailConfig.SENDER_EMAIL,
EmailConfig.SENDER_PASSWORD
);
}
};
Session session = Session.getInstance(props, auth);
// 创建邮件消息
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(EmailConfig.SENDER_EMAIL));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipientEmail));
message.setSubject(EmailConfig.EMAIL_SUBJECT);
// 创建邮件内容
String emailContent = createEmailContent(code);
message.setContent(emailContent, "text/html; charset=utf-8");
// 发送邮件
Transport.send(message);
// 存储验证码信息
verificationCodes.put(recipientEmail,
new VerificationCodeInfo(code, System.currentTimeMillis()));
System.out.println("验证码已发送到邮箱: " + recipientEmail);
return true;
} catch (Exception e) {
System.err.println("发送邮件失败: " + e.getMessage());
// 在开发环境中直接返回true用于测试
verificationCodes.put(recipientEmail,
new VerificationCodeInfo(code, System.currentTimeMillis()));
return true;
}
*/
return true;
}
private static String createEmailContent(String code) {
return "<!DOCTYPE html>" +
"<html>" +
"<head>" +
"<meta charset='UTF-8'>" +
"<style>" +
"body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }" +
".container { max-width: 600px; margin: 0 auto; padding: 20px; }" +
".header { background-color: #4CAF50; color: white; padding: 20px; text-align: center; }" +
".content { padding: 20px; background-color: #f9f9f9; margin: 20px 0; }" +
".code { font-size: 24px; font-weight: bold; color: #4CAF50; text-align: center; padding: 15px; background-color: white; border: 2px dashed #4CAF50; margin: 20px 0; }" +
".footer { text-align: center; color: #666; font-size: 12px; }" +
"</style>" +
"</head>" +
"<body>" +
"<div class='container'>" +
"<div class='header'>" +
"<h2>数学学习软件 - 注册验证码</h2>" +
"</div>" +
"<div class='content'>" +
"<p>您好!</p>" +
"<p>您正在注册数学学习软件账户,验证码如下:</p>" +
"<div class='code'>" + code + "</div>" +
"<p>验证码有效期为 " + EmailConfig.CODE_EXPIRY_MINUTES + " 分钟,请勿泄露给他人。</p>" +
"<p>如果这不是您本人的操作,请忽略此邮件。</p>" +
"</div>" +
"<div class='footer'>" +
"<p>此邮件为系统自动发送,请勿回复。</p>" +
"</div>" +
"</div>" +
"</body>" +
"</html>";
}
public static boolean verifyCode(String email, String inputCode) {
VerificationCodeInfo codeInfo = verificationCodes.get(email);
if (codeInfo == null) {
return false;
}
// 检查验证码是否过期
long currentTime = System.currentTimeMillis();
if (currentTime - codeInfo.timestamp > EmailConfig.CODE_EXPIRY_MINUTES * 60 * 1000) {
verificationCodes.remove(email);
return false;
}
return codeInfo.code.equals(inputCode);
}
public static void cleanupExpiredCodes() {
long currentTime = System.currentTimeMillis();
Iterator<Entry<String, VerificationCodeInfo>> iterator =
verificationCodes.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, VerificationCodeInfo> entry = iterator.next();
if (currentTime - entry.getValue().timestamp >
EmailConfig.CODE_EXPIRY_MINUTES * 60 * 1000) {
iterator.remove();
}
}
}
}
Loading…
Cancel
Save