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

@ -1,6 +1,7 @@
using Microsoft.Extensions.Logging;
using QYZH.InteractiveMagazine.IService;
using QYZH.InteractiveMagazine.Models.Common;
using QYZH.InteractiveMagazine.Models.Dto;
using QYZH.InteractiveMagazine.Models.Dto.Points;
using QYZH.InteractiveMagazine.Models.Dto.UserAnswerTaskService;
using QYZH.InteractiveMagazine.Models.Entity;
@ -939,7 +940,7 @@ public class UserAnswerTaskService(
if (relatedTasks == null || relatedTasks.Count == 0)
{
throw new BusinessException("任务不存在", 404);
throw new BusinessException("任务不存在", ResultCode.NOT_FOUND);
}
// ============================================
@ -972,7 +973,7 @@ public class UserAnswerTaskService(
if (userAnswers == null || userAnswers.Count == 0)
{
throw new BusinessException("请先完成答题再领取积分", 400);
throw new BusinessException("请先完成答题再领取积分", ResultCode.BAD_REQUEST);
}
// ============================================
@ -988,7 +989,7 @@ public class UserAnswerTaskService(
var completedAnswer = userAnswers.FirstOrDefault(a => a.JournalPageTaskId == task.Id && a.Status == (int)UserAnswerStatusEnum.Complete);
if (completedAnswer == null)
{
throw new BusinessException("跨页题目需要完成所有任务才能领取积分", 400);
throw new BusinessException("跨页题目需要完成所有任务才能领取积分", ResultCode.BAD_REQUEST);
}
}
}
@ -997,7 +998,7 @@ public class UserAnswerTaskService(
// 普通题目:只需要 Status=1 即可
if (userAnswers.FirstOrDefault()?.Status != (int)UserAnswerStatusEnum.Complete)
{
throw new BusinessException("请先完成答题再领取积分", 400);
throw new BusinessException("请先完成答题再领取积分", ResultCode.BAD_REQUEST);
}
}
@ -1020,7 +1021,7 @@ public class UserAnswerTaskService(
if (totalPoints <= 0)
{
throw new BusinessException("该任务无积分可领取", 400);
throw new BusinessException("该任务无积分可领取", ResultCode.BAD_REQUEST);
}
// ============================================
@ -1054,7 +1055,7 @@ public class UserAnswerTaskService(
if (existingRecord != null)
{
throw new BusinessException("积分已领取,请勿重复领取", 400);
throw new BusinessException("积分已领取,请勿重复领取", ResultCode.BAD_REQUEST);
}
// ============================================
@ -1110,7 +1111,7 @@ public class UserAnswerTaskService(
if (input == null || input.GroupIds == null || input.GroupIds.Count == 0)
{
throw new BusinessException("任务分组Id列表不能为空", 400);
throw new BusinessException("任务分组Id列表不能为空", ResultCode.BAD_REQUEST);
}
var groupIds = input.GroupIds.Distinct().ToList();