You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
double_project/src/controller/UserController.java

156 lines
5.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package controller;
import model.User;
import utils.FileUtil;
import utils.ValidationUtil;
import utils.EmailUtil;
public class UserController extends BaseController implements UserService {
@Override
public String registerUser(String username, String email) {
try {
validateStringLength(username, "用户名", 2, 20);
validateStringNotEmpty(email, "邮箱");
if (!ValidationUtil.isValidEmail(email)) {
return "邮箱格式不正确";
}
if (FileUtil.usernameExists(username)) {
return "用户名已被使用";
}
if (FileUtil.emailExists(email)) {
return "该邮箱已被注册";
}
String registrationCode = EmailUtil.generateRegistrationCode();
User user = new User(username, email, registrationCode);
boolean sendSuccess = EmailUtil.sendRegistrationCode(email, username, registrationCode);
if (sendSuccess) {
FileUtil.saveUser(user);
logInfo("用户注册成功: " + username);
return "注册码已发送到您的邮箱,请查收!";
} else {
return "邮件发送失败,请检查邮箱地址或稍后重试";
}
} catch (IllegalArgumentException e) {
return e.getMessage();
} catch (Exception e) {
logError("用户注册异常: " + e.getMessage());
return "系统错误,请稍后重试";
}
}
@Override
public String completeRegistration(String username, String code, String password, String confirmPassword) {
try {
validateStringNotEmpty(username, "用户名");
validateStringNotEmpty(code, "注册码");
validateStringNotEmpty(password, "密码");
validateStringNotEmpty(confirmPassword, "确认密码");
User user = FileUtil.loadUserByUsername(username);
if (user == null) {
return "用户不存在或注册码未发送";
}
if (!user.getRegistrationCode().equals(code)) {
return "注册码不正确";
}
if (!password.equals(confirmPassword)) {
return "两次输入的密码不一致";
}
if (!ValidationUtil.isValidPassword(password)) {
return "密码必须为6-10位且包含大小写字母和数字";
}
user.setPassword(password);
user.setRegistered(true);
FileUtil.saveUser(user);
logInfo("用户完成注册: " + username);
return "注册成功!";
} catch (Exception e) {
logError("完成注册异常: " + e.getMessage());
return "系统错误,请稍后重试";
}
}
@Override
public User login(String loginId, String password) {
try {
validateStringNotEmpty(loginId, "登录ID");
validateStringNotEmpty(password, "密码");
User user;
if (loginId.contains("@")) {
user = FileUtil.loadUserByEmail(loginId);
} else {
user = FileUtil.loadUserByUsername(loginId);
}
if (user == null) {
logInfo("登录失败: 用户不存在 - " + loginId);
return null;
}
if (user.getPassword() == null || !user.getPassword().equals(password)) {
logInfo("登录失败: 密码错误 - " + loginId);
return null;
}
logInfo("登录成功: " + user.getUsername());
return user;
} catch (Exception e) {
logError("登录异常: " + e.getMessage());
return null;
}
}
@Override
public String changePassword(User user, String oldPassword, String newPassword, String confirmPassword) {
try {
validateNotNull(user, "用户");
validateStringNotEmpty(oldPassword, "原密码");
validateStringNotEmpty(newPassword, "新密码");
validateStringNotEmpty(confirmPassword, "确认密码");
if (!user.getPassword().equals(oldPassword)) {
return "原密码不正确";
}
if (!newPassword.equals(confirmPassword)) {
return "两次输入的新密码不一致";
}
if (!ValidationUtil.isValidPassword(newPassword)) {
return "新密码必须为6-10位且包含大小写字母和数字";
}
user.setPassword(newPassword);
FileUtil.saveUser(user);
logInfo("密码修改成功: " + user.getUsername());
return "密码修改成功";
} catch (Exception e) {
logError("修改密码异常: " + e.getMessage());
return "系统错误,请稍后重试";
}
}
@Override
public void cleanupUnregisteredUsers() {
try {
FileUtil.cleanupUnregisteredUsers();
logInfo("执行未注册用户清理");
} catch (Exception e) {
logError("清理未注册用户异常: " + e.getMessage());
}
}
}