feat: 统一响应格式与消息国际化

This commit is contained in:
2025-11-29 11:08:31 +08:00
parent 26a587f20b
commit e9ee523147
21 changed files with 198 additions and 128 deletions

View File

@ -0,0 +1,8 @@
package day.gitlab.dolphin.auth.constants;
public class Exceptions {
public static final String USERNAME_OR_PASSWORD_INCORRECT = "00010101";
public static final String USER_NOT_ENABLED = "00010102";
}

View File

@ -1,6 +1,7 @@
package day.gitlab.dolphin.auth.controller;
import day.gitlab.dolphin.auth.entity.dto.LoginDTO;
import day.gitlab.dolphin.auth.entity.vo.LoginVO;
import day.gitlab.dolphin.auth.service.AuthService;
import day.gitlab.dolphin.common.core.entity.Result;
import day.gitlab.dolphin.common.security.annotation.AuthorityIgnore;
@ -19,7 +20,7 @@ public class AuthController {
@AuthorityIgnore
@PostMapping("/login")
public Result login(@RequestBody LoginDTO loginDTO) {
public Result<LoginVO> login(@RequestBody LoginDTO loginDTO) {
return Result.success(authService.login(loginDTO));
}
}

View File

@ -1,10 +1,11 @@
package day.gitlab.dolphin.auth.service.impl;
import cn.hutool.v7.crypto.digest.BCrypt;
import day.gitlab.dolphin.auth.constants.Exceptions;
import day.gitlab.dolphin.auth.entity.dto.LoginDTO;
import day.gitlab.dolphin.auth.entity.vo.LoginVO;
import day.gitlab.dolphin.auth.service.AuthService;
import day.gitlab.dolphin.common.core.exception.BusinessException;
import day.gitlab.dolphin.common.core.i18n.MessagesHelper;
import day.gitlab.dolphin.common.security.AuthenticationProvider;
import day.gitlab.dolphin.common.security.UserPrincipal;
import day.gitlab.dolphin.common.security.jwt.JwtInfo;
@ -28,14 +29,17 @@ public class AuthServiceImpl implements AuthService, AuthenticationProvider {
@Resource
private JwtInfo jwtInfo;
@Resource
private MessagesHelper messagesHelper;
@Override
public LoginVO login(LoginDTO loginDTO) {
User dbUser = userMapper.findByUsername(loginDTO.getUsername());
if (dbUser == null || !BCrypt.checkpw(loginDTO.getPassword(), dbUser.getPassword())) {
throw new BusinessException("Username or password is incorrect");
throw messagesHelper.newBusinessException(Exceptions.USERNAME_OR_PASSWORD_INCORRECT);
}
if (!UserEnabled.ENABLED.equals(dbUser.getEnabled())) {
throw new BusinessException("User is not enabled");
throw messagesHelper.newBusinessException(Exceptions.USER_NOT_ENABLED);
}
return LoginVO.builder()