fix(login): 修复登录错误处理并统一后端异常抛出
- 前端登录失败时添加错误处理标识检查,避免重复显示错误消息 - 后端服务层统一使用BusinessException替代RuntimeException - 用户注册时用户名、邮箱、密码验证失败统一抛出业务异常 - 用户登录时用户名密码错误、账号禁用情况统一抛出业务异常 - 用户查询、更新、删除操作时用户不存在情况统一抛出业务异常 - 密码修改时旧密码错误、密码不一致情况统一抛出业务异常
This commit is contained in:
@@ -4,6 +4,7 @@ import cn.dev33.satoken.secure.BCrypt;
|
|||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
import com.test.musichouduan.dto.*;
|
import com.test.musichouduan.dto.*;
|
||||||
import com.test.musichouduan.entity.User;
|
import com.test.musichouduan.entity.User;
|
||||||
|
import com.test.musichouduan.exception.BusinessException;
|
||||||
import com.test.musichouduan.repository.*;
|
import com.test.musichouduan.repository.*;
|
||||||
import com.test.musichouduan.service.UserService;
|
import com.test.musichouduan.service.UserService;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
@@ -49,17 +50,17 @@ public class UserServiceImpl implements UserService {
|
|||||||
public UserDTO register(RegisterRequest request) {
|
public UserDTO register(RegisterRequest request) {
|
||||||
// 检查用户名是否已存在
|
// 检查用户名是否已存在
|
||||||
if (userRepository.existsByUsername(request.getUsername())) {
|
if (userRepository.existsByUsername(request.getUsername())) {
|
||||||
throw new RuntimeException("用户名已存在");
|
throw new BusinessException(400,"用户名已存在");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查邮箱是否已存在
|
// 检查邮箱是否已存在
|
||||||
if (userRepository.existsByEmail(request.getEmail())) {
|
if (userRepository.existsByEmail(request.getEmail())) {
|
||||||
throw new RuntimeException("邮箱已存在");
|
throw new BusinessException(400,"邮箱已存在");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查两次密码是否一致
|
// 检查两次密码是否一致
|
||||||
if (!request.getPassword().equals(request.getConfirmPassword())) {
|
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) {
|
public LoginResultDTO login(LoginRequest request) {
|
||||||
// 根据用户名查找用户
|
// 根据用户名查找用户
|
||||||
User user = userRepository.findByUsername(request.getUsername())
|
User user = userRepository.findByUsername(request.getUsername())
|
||||||
.orElseThrow(() -> new RuntimeException("用户名或密码错误"));
|
.orElseThrow(() -> new BusinessException(400,"用户名或密码错误"));
|
||||||
|
|
||||||
// 检查用户状态
|
// 检查用户状态
|
||||||
if (user.getStatus() == 0) {
|
if (user.getStatus() == 0) {
|
||||||
throw new RuntimeException("账号已被禁用");
|
throw new BusinessException(400,"账号已被禁用");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查密码
|
// 检查密码
|
||||||
if (!BCrypt.checkpw(request.getPassword(), user.getPassword())) {
|
if (!BCrypt.checkpw(request.getPassword(), user.getPassword())) {
|
||||||
throw new RuntimeException("用户名或密码错误");
|
throw new BusinessException(400,"用户名或密码错误");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 登录
|
// 登录
|
||||||
@@ -126,7 +127,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
// 根据ID查找用户
|
// 根据ID查找用户
|
||||||
User user = userRepository.findById(userId)
|
User user = userRepository.findById(userId)
|
||||||
.orElseThrow(() -> new RuntimeException("用户不存在"));
|
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
|
||||||
|
|
||||||
// 返回用户DTO
|
// 返回用户DTO
|
||||||
return getUserStats(userId);
|
return getUserStats(userId);
|
||||||
@@ -136,7 +137,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
public UserDTO getUserById(Long id) {
|
public UserDTO getUserById(Long id) {
|
||||||
// 根据ID查找用户
|
// 根据ID查找用户
|
||||||
User user = userRepository.findById(id)
|
User user = userRepository.findById(id)
|
||||||
.orElseThrow(() -> new RuntimeException("用户不存在"));
|
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
|
||||||
|
|
||||||
// 返回用户DTO
|
// 返回用户DTO
|
||||||
return convertToDTO(user);
|
return convertToDTO(user);
|
||||||
@@ -146,7 +147,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
public UserDTO getUserByUsername(String username) {
|
public UserDTO getUserByUsername(String username) {
|
||||||
// 根据用户名查找用户
|
// 根据用户名查找用户
|
||||||
User user = userRepository.findByUsername(username)
|
User user = userRepository.findByUsername(username)
|
||||||
.orElseThrow(() -> new RuntimeException("用户不存在"));
|
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
|
||||||
|
|
||||||
// 返回用户DTO
|
// 返回用户DTO
|
||||||
return convertToDTO(user);
|
return convertToDTO(user);
|
||||||
@@ -160,7 +161,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
// 根据ID查找用户
|
// 根据ID查找用户
|
||||||
User user = userRepository.findById(userId)
|
User user = userRepository.findById(userId)
|
||||||
.orElseThrow(() -> new RuntimeException("用户不存在"));
|
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
|
||||||
|
|
||||||
// 更新用户信息
|
// 更新用户信息
|
||||||
if (request.getNickname() != null) {
|
if (request.getNickname() != null) {
|
||||||
@@ -197,16 +198,16 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
// 根据ID查找用户
|
// 根据ID查找用户
|
||||||
User user = userRepository.findById(userId)
|
User user = userRepository.findById(userId)
|
||||||
.orElseThrow(() -> new RuntimeException("用户不存在"));
|
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
|
||||||
|
|
||||||
// 检查旧密码
|
// 检查旧密码
|
||||||
if (!BCrypt.checkpw(request.getOldPassword(), user.getPassword())) {
|
if (!BCrypt.checkpw(request.getOldPassword(), user.getPassword())) {
|
||||||
throw new RuntimeException("旧密码错误");
|
throw new BusinessException(400,"旧密码错误");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查两次密码是否一致
|
// 检查两次密码是否一致
|
||||||
if (!request.getNewPassword().equals(request.getConfirmPassword())) {
|
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) {
|
public UserDTO getUserStats(Long userId) {
|
||||||
// 根据ID查找用户
|
// 根据ID查找用户
|
||||||
User user = userRepository.findById(userId)
|
User user = userRepository.findById(userId)
|
||||||
.orElseThrow(() -> new RuntimeException("用户不存在"));
|
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
|
||||||
|
|
||||||
// 转换为DTO
|
// 转换为DTO
|
||||||
UserDTO userDTO = convertToDTO(user);
|
UserDTO userDTO = convertToDTO(user);
|
||||||
@@ -274,7 +275,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
public boolean deleteUser(Long id) {
|
public boolean deleteUser(Long id) {
|
||||||
// 检查用户是否存在
|
// 检查用户是否存在
|
||||||
if (!userRepository.existsById(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) {
|
public boolean updateUserStatus(Long id, Integer status) {
|
||||||
// 根据ID查找用户
|
// 根据ID查找用户
|
||||||
User user = userRepository.findById(id)
|
User user = userRepository.findById(id)
|
||||||
.orElseThrow(() -> new RuntimeException("用户不存在"));
|
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
|
||||||
|
|
||||||
// 更新状态
|
// 更新状态
|
||||||
user.setStatus(status);
|
user.setStatus(status);
|
||||||
@@ -341,7 +342,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Override
|
@Override
|
||||||
public User getById(Long id) {
|
public User getById(Long id) {
|
||||||
return userRepository.findById(id)
|
return userRepository.findById(id)
|
||||||
.orElseThrow(() -> new RuntimeException("用户不存在"));
|
.orElseThrow(() -> new BusinessException(400,"用户不存在"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -81,7 +81,9 @@ const handleLogin = () => {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('登录失败:', error)
|
console.error('登录失败:', error)
|
||||||
ElMessage.error(error.message || '登录失败,请检查用户名和密码')
|
if (!error.__handled) {
|
||||||
|
ElMessage.error(error.message || '登录失败,请检查用户名和密码')
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
loading.value = false
|
loading.value = false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user