v1.1 登录、注册接口书写完成

pull/3/head
杨博文 7 months ago
parent 62c59450f5
commit 42d1b8716a

@ -1,132 +0,0 @@
package com.ybw.mathapp;
// UserService.java
import com.ybw.mathapp.entity.User;
import com.ybw.mathapp.util.EmailService;
import com.ybw.mathapp.util.LoginFileUtils;
import java.util.Scanner;
import java.util.regex.Pattern;
public class LoginAndRegister {
private static Scanner scanner = new Scanner(System.in);
// UserService.java 中的注册方法更新
public static boolean register() {
System.out.println("\n=== 用户注册 ===");
// 输入邮箱
System.out.print("请输入邮箱地址: ");
String email = scanner.nextLine().trim();
if (!isValidEmail(email)) {
return false;
}
if (LoginFileUtils.isEmailRegistered(email)) {
System.out.println("该邮箱已注册,请直接登录!");
return false;
}
// 发送、验证验证码
if (!sendAndVerifyCode(email)) {
return false;
}
// 设置密码(其余代码保持不变)
System.out.print("请输入密码: ");
String password1 = scanner.nextLine();
System.out.print("请再次输入密码: ");
String password2 = scanner.nextLine();
if(!isVaildPassword(password1, password2)) {
return false;
}
User user = new User(email, password1);
LoginFileUtils.saveUser(user);
System.out.println("注册成功!您可以使用邮箱和密码登录了。");
return true;
}
// 登录流程
public static boolean login() {
System.out.println("\n=== 用户登录 ===");
System.out.print("请输入邮箱: ");
String email = scanner.nextLine().trim();
System.out.print("请输入密码: ");
String password = scanner.nextLine();
if (LoginFileUtils.validateUser(email, password)) {
System.out.println("登录成功!欢迎回来," + email);
return true;
} else {
System.out.println("邮箱或密码错误!");
return false;
}
}
//
/**
*
* @param email
* @return truefalse
*/
private static boolean isValidEmail(String email) {
if (email.isEmpty()) {
System.out.println("邮箱地址不能为空!");
return false;
}
if (!(email.contains("@") && email.contains("."))) {
System.out.println("邮箱格式不正确!");
return false;
}
return true;
}
/**
*
* @param password1
* @param password2
* @return truefalse
*/
public static boolean isVaildPassword(String password1, String password2) {
if (password1 == null || password1.length() < 6 || password1.length() > 10) {
return false;
}
// 使用正则表达式验证长度6-10只包含字母数字且包含大小写字母和数字
String regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{6,10}$";
if (!Pattern.matches(regex, password1)) {
return false;
}
System.out.print("请再次输入密码: ");
if (!password1.equals(password2)) {
System.out.println("两次输入的密码不一致!");
return false;
}
return true;
}
public static boolean sendAndVerifyCode(String email) {
// 发送真实邮件验证码
String verificationCode = EmailService.generateVerificationCode();
System.out.println("正在发送验证码邮件,请稍候...");
if (!EmailService.sendVerificationCode(email, verificationCode)) {
System.out.println("发送验证码失败,请检查邮箱配置或稍后重试!");
return false;
}
// 验证验证码
System.out.print("请输入收到的验证码: ");
String inputCode = scanner.nextLine().trim();
if (!EmailService.verifyCode(email, inputCode)) {
System.out.println("验证码错误或已过期!");
return false;
}
return true;
}
}

@ -1,6 +1,5 @@
package com.ybw.mathapp.service;
import com.ybw.mathapp.LoginAndRegister;
import com.ybw.mathapp.service.ChoiceGenerator.MultipleChoiceQuestion;
import com.ybw.mathapp.system.LogSystem;
import java.io.IOException;

@ -124,7 +124,7 @@ public class EmailService {
"</html>";
}
// 验证验证码
// 前端接口-验证验证码
public static boolean verifyCode(String email, String inputCode) {
VerificationCodeInfo codeInfo = verificationCodes.get(email);
if (codeInfo == null) {
@ -155,4 +155,35 @@ public class EmailService {
}
}
}
/**
* -
* @param email
* @return truefalse
*/
public static boolean isValidEmail(String email) {
if (email.isEmpty()) {
System.out.println("邮箱地址不能为空!");
return false;
}
if (!(email.contains("@") && email.contains("."))) {
System.out.println("邮箱格式不正确!");
return false;
}
return true;
}
// 前端接口-发送验证码
public static boolean sendCode(String email) {
// 发送真实邮件验证码
String verificationCode = EmailService.generateVerificationCode();
// 验证验证码是否成功发送
if (!EmailService.sendVerificationCode(email, verificationCode)) {
return false;
}
return true;
}
}

@ -0,0 +1,18 @@
package com.ybw.mathapp.util;
import java.util.regex.Pattern;
public class Login {
// 前端接口-登录成功or失败
public static boolean login(String email, String password) {
if (LoginFileUtils.validateUser(email, password)) {
System.out.println("登录成功!欢迎回来," + email);
return true;
} else {
System.out.println("邮箱或密码错误!");
return false;
}
}
}

@ -0,0 +1,41 @@
package com.ybw.mathapp.util;
import com.ybw.mathapp.entity.User;
import java.util.regex.Pattern;
public class Register {
// 前端接口-完成注册
public static boolean register(String email, String password1) {
User user = new User(email, password1);
LoginFileUtils.saveUser(user);
System.out.println("注册成功!您可以使用邮箱和密码登录了。");
return true;
}
/**
* -
* @param password1
* @param password2
* @return truefalse
*/
public static boolean isVaildPassword(String password1, String password2) {
if (password1 == null || password1.length() < 6 || password1.length() > 10) {
return false;
}
// 使用正则表达式验证长度6-10只包含字母数字且包含大小写字母和数字
String regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{6,10}$";
if (!Pattern.matches(regex, password1)) {
return false;
}
System.out.print("请再次输入密码: ");
if (!password1.equals(password2)) {
System.out.println("两次输入的密码不一致!");
return false;
}
return true;
}
}
Loading…
Cancel
Save