fix(login): 修复登录错误处理并统一后端异常抛出

- 前端登录失败时添加错误处理标识检查,避免重复显示错误消息
- 后端服务层统一使用BusinessException替代RuntimeException
- 用户注册时用户名、邮箱、密码验证失败统一抛出业务异常
- 用户登录时用户名密码错误、账号禁用情况统一抛出业务异常
- 用户查询、更新、删除操作时用户不存在情况统一抛出业务异常
- 密码修改时旧密码错误、密码不一致情况统一抛出业务异常
This commit is contained in:
ikmkj
2026-03-11 00:05:46 +08:00
parent efccff567a
commit b29cc352cc
2 changed files with 21 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ import cn.dev33.satoken.secure.BCrypt;
import cn.dev33.satoken.stp.StpUtil;
import com.test.musichouduan.dto.*;
import com.test.musichouduan.entity.User;
import com.test.musichouduan.exception.BusinessException;
import com.test.musichouduan.repository.*;
import com.test.musichouduan.service.UserService;
import org.springframework.beans.BeanUtils;
@@ -49,17 +50,17 @@ public class UserServiceImpl implements UserService {
public UserDTO register(RegisterRequest request) {
// 检查用户名是否已存在
if (userRepository.existsByUsername(request.getUsername())) {
throw new RuntimeException("用户名已存在");
throw new BusinessException(400,"用户名已存在");
}
// 检查邮箱是否已存在
if (userRepository.existsByEmail(request.getEmail())) {
throw new RuntimeException("邮箱已存在");
throw new BusinessException(400,"邮箱已存在");
}
// 检查两次密码是否一致
if (!request.getPassword().equals(request.getConfirmPassword())) {
throw new RuntimeException("两次密码不一致");
throw new BusinessException(400,"两次密码不一致");
}
// 创建用户
@@ -83,16 +84,16 @@ public class UserServiceImpl implements UserService {
public LoginResultDTO login(LoginRequest request) {
// 根据用户名查找用户
User user = userRepository.findByUsername(request.getUsername())
.orElseThrow(() -> new RuntimeException("用户名或密码错误"));
.orElseThrow(() -> new BusinessException(400,"用户名或密码错误"));
// 检查用户状态
if (user.getStatus() == 0) {
throw new RuntimeException("账号已被禁用");
throw new BusinessException(400,"账号已被禁用");
}
// 检查密码
if (!BCrypt.checkpw(request.getPassword(), user.getPassword())) {
throw new RuntimeException("用户名或密码错误");
throw new BusinessException(400,"用户名或密码错误");
}
// 登录
@@ -126,7 +127,7 @@ public class UserServiceImpl implements UserService {
// 根据ID查找用户
User user = userRepository.findById(userId)
.orElseThrow(() -> new RuntimeException("用户不存在"));
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
// 返回用户DTO
return getUserStats(userId);
@@ -136,7 +137,7 @@ public class UserServiceImpl implements UserService {
public UserDTO getUserById(Long id) {
// 根据ID查找用户
User user = userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("用户不存在"));
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
// 返回用户DTO
return convertToDTO(user);
@@ -146,7 +147,7 @@ public class UserServiceImpl implements UserService {
public UserDTO getUserByUsername(String username) {
// 根据用户名查找用户
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new RuntimeException("用户不存在"));
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
// 返回用户DTO
return convertToDTO(user);
@@ -160,7 +161,7 @@ public class UserServiceImpl implements UserService {
// 根据ID查找用户
User user = userRepository.findById(userId)
.orElseThrow(() -> new RuntimeException("用户不存在"));
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
// 更新用户信息
if (request.getNickname() != null) {
@@ -197,16 +198,16 @@ public class UserServiceImpl implements UserService {
// 根据ID查找用户
User user = userRepository.findById(userId)
.orElseThrow(() -> new RuntimeException("用户不存在"));
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
// 检查旧密码
if (!BCrypt.checkpw(request.getOldPassword(), user.getPassword())) {
throw new RuntimeException("旧密码错误");
throw new BusinessException(400,"旧密码错误");
}
// 检查两次密码是否一致
if (!request.getNewPassword().equals(request.getConfirmPassword())) {
throw new RuntimeException("两次密码不一致");
throw new BusinessException(400,"两次密码不一致");
}
// 更新密码
@@ -222,7 +223,7 @@ public class UserServiceImpl implements UserService {
public UserDTO getUserStats(Long userId) {
// 根据ID查找用户
User user = userRepository.findById(userId)
.orElseThrow(() -> new RuntimeException("用户不存在"));
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
// 转换为DTO
UserDTO userDTO = convertToDTO(user);
@@ -274,7 +275,7 @@ public class UserServiceImpl implements UserService {
public boolean deleteUser(Long id) {
// 检查用户是否存在
if (!userRepository.existsById(id)) {
throw new RuntimeException("用户不存在");
throw new BusinessException(400,"用户不存在");
}
// 删除用户
@@ -288,7 +289,7 @@ public class UserServiceImpl implements UserService {
public boolean updateUserStatus(Long id, Integer status) {
// 根据ID查找用户
User user = userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("用户不存在"));
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
// 更新状态
user.setStatus(status);
@@ -341,7 +342,7 @@ public class UserServiceImpl implements UserService {
@Override
public User getById(Long id) {
return userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("用户不存在"));
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
}
/**