|
|
|
|
@ -1,132 +0,0 @@
|
|
|
|
|
package com.ybw.mathapp;
|
|
|
|
|
|
|
|
|
|
// UserService.java
|
|
|
|
|
import com.ybw.mathapp.entity.User;
|
|
|
|
|
import com.ybw.mathapp.service.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 true表示邮箱格式正确,false表示邮箱格式错误
|
|
|
|
|
*/
|
|
|
|
|
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 true表示符合要求,false表示不符合
|
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|