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

@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging;
using QYZH.InteractiveMagazine.Common.Extensions;
using QYZH.InteractiveMagazine.IService;
using QYZH.InteractiveMagazine.Models.Common;
using QYZH.InteractiveMagazine.Models.Dto;
using QYZH.InteractiveMagazine.Models.Dto.CheckIn;
using QYZH.InteractiveMagazine.Models.Dto.Compensation;
using QYZH.InteractiveMagazine.Models.Dto.Pet;
@ -44,7 +45,7 @@ public class CheckInService(
if (alreadyCheckedIn)
{
throw new BusinessException("今日已签到,请明天再来", 400);
throw new BusinessException("今日已签到,请明天再来", ResultCode.BAD_REQUEST);
}
// 2. 计算连续签到天数
@ -60,7 +61,7 @@ public class CheckInService(
if (user == null)
{
throw new BusinessException("用户不存在", 404);
throw new BusinessException("用户不存在", ResultCode.NOT_FOUND);
}
// 5. 查询用户宠物(如果有)
@ -226,7 +227,7 @@ public class CheckInService(
targetDate = targetDate.Date;
if (targetDate >= DateTime.Now.Date)
throw new BusinessException("只能补签过去的日期", 400);
throw new BusinessException("只能补签过去的日期", ResultCode.BAD_REQUEST);
// 检查目标日期是否已有签到记录
var alreadyCheckedIn = await checkInRecordRepository.Context.Queryable<CheckInRecord>()
@ -235,7 +236,7 @@ public class CheckInService(
.AnyAsync();
if (alreadyCheckedIn)
throw new BusinessException($"{targetDate:yyyy-MM-dd} 已签到,无需补签", 400);
throw new BusinessException($"{targetDate:yyyy-MM-dd} 已签到,无需补签", ResultCode.BAD_REQUEST);
// 检查用户背包中是否有补签卡
var makeUpCard = await checkInRecordRepository.Context.Queryable<UserBag>()
@ -248,7 +249,7 @@ public class CheckInService(
.FirstAsync();
if (makeUpCard == null)
throw new BusinessException("补签卡不足,无法补签", 400);
throw new BusinessException("补签卡不足,无法补签", ResultCode.BAD_REQUEST);
// 查询用户信息
var user = await checkInRecordRepository.Context.Queryable<Users>()
@ -256,7 +257,7 @@ public class CheckInService(
.FirstAsync();
if (user == null)
throw new BusinessException("用户不存在", 404);
throw new BusinessException("用户不存在", ResultCode.NOT_FOUND);
// 查询宠物
var pet = await checkInRecordRepository.Context.Queryable<UserPet>()