refactor: 统一业务异常处理,标准化结果码和错误响应

1.  新增并完善ResultCode枚举,补充标准HTTP状态码对应的业务状态码
2.  重构BusinessException,新增基于ResultCode的构造函数和ThrowIf扩展方法
3.  替换所有硬编码的HTTP状态码为统一的ResultCode枚举
4.  优化全局异常中间件,根据业务状态码映射对应HTTP状态码并规范化JSON响应
5.  修复OssImageHelper和AutoDotCodeConsumer中的OSS文件处理逻辑
6.  新增用户答题快照实体类
7.  清理废弃的宠物模块迁移脚本
This commit is contained in:
glz
2026-06-29 16:34:26 +08:00
parent 4579fefef9
commit bdaa6a0dc8
35 changed files with 693 additions and 364 deletions

View File

@ -25,31 +25,31 @@ public class AdminAuthService(BaseRepository<AdminUser> adminUserRepository, ICo
if (string.IsNullOrWhiteSpace(input.UserName))
{
throw new BusinessException("用户名不能为空", 400);
throw new BusinessException("用户名不能为空", ResultCode.BAD_REQUEST);
}
if (string.IsNullOrWhiteSpace(input.Password))
{
throw new BusinessException("密码不能为空", 400);
throw new BusinessException("密码不能为空", ResultCode.BAD_REQUEST);
}
var adminUser = await adminUserRepository.GetFirstAsync(a => a.UserName == input.UserName);
if (adminUser == null)
{
logger.LogWarning("管理员登录失败,用户名不存在: {UserName}", input.UserName);
throw new BusinessException("用户名或密码错误", 401);
throw new BusinessException("用户名或密码错误", ResultCode.DENY);
}
if (!BCrypt.Net.BCrypt.Verify(input.Password, adminUser.PasswordHash))
{
logger.LogWarning("管理员登录失败,密码错误: {UserName}", input.UserName);
throw new BusinessException("用户名或密码错误", 401);
throw new BusinessException("用户名或密码错误", ResultCode.DENY);
}
if (adminUser.Status != 1)
{
logger.LogWarning("管理员登录失败,账号已禁用: {UserName}", input.UserName);
throw new BusinessException("账号已被禁用,请联系系统管理员", 403);
throw new BusinessException("账号已被禁用,请联系系统管理员", ResultCode.FORBIDDEN);
}
var jwtSettings = GetJwtSettings();
@ -86,7 +86,7 @@ public class AdminAuthService(BaseRepository<AdminUser> adminUserRepository, ICo
if (adminUser == null)
{
logger.LogWarning("未找到管理员ID: {UserId}", userId);
throw new BusinessException("用户不存在", 404);
throw new BusinessException("用户不存在", ResultCode.NOT_FOUND);
}
return new AdminUserInfoOutput
@ -104,25 +104,25 @@ public class AdminAuthService(BaseRepository<AdminUser> adminUserRepository, ICo
if (string.IsNullOrWhiteSpace(oldPassword))
{
throw new BusinessException("原密码不能为空", 400);
throw new BusinessException("原密码不能为空", ResultCode.BAD_REQUEST);
}
if (string.IsNullOrWhiteSpace(newPassword))
{
throw new BusinessException("新密码不能为空", 400);
throw new BusinessException("新密码不能为空", ResultCode.BAD_REQUEST);
}
var adminUser = await adminUserRepository.GetByIdAsync(userId);
if (adminUser == null)
{
logger.LogWarning("未找到管理员ID: {UserId}", userId);
throw new BusinessException("用户不存在", 404);
throw new BusinessException("用户不存在", ResultCode.NOT_FOUND);
}
if (!BCrypt.Net.BCrypt.Verify(oldPassword, adminUser.PasswordHash))
{
logger.LogWarning("管理员修改密码失败原密码错误ID: {UserId}", userId);
throw new BusinessException("原密码错误", 400);
throw new BusinessException("原密码错误", ResultCode.DENY);
}
adminUser.PasswordHash = BCrypt.Net.BCrypt.HashPassword(newPassword);
@ -130,7 +130,7 @@ public class AdminAuthService(BaseRepository<AdminUser> adminUserRepository, ICo
var result = await adminUserRepository.UpdateAsync(adminUser);
if (!result)
{
throw new BusinessException("修改密码失败", 500);
throw new BusinessException("修改密码失败", ResultCode.GLOBAL_ERROR);
}
await RedisHelper.DelAsync($"{TokenKeyPrefix}:{userId}");
@ -151,7 +151,7 @@ public class AdminAuthService(BaseRepository<AdminUser> adminUserRepository, ICo
if (string.IsNullOrWhiteSpace(jwtSettings.SecretKey))
{
throw new BusinessException("JWT 配置不完整", 500);
throw new BusinessException("JWT 配置不完整", ResultCode.GLOBAL_ERROR);
}
return jwtSettings;