feat: 新增签到、宠物、期刊绑定、补偿任务等业务模块,优化微信登录流程
本次提交完成了多个核心业务模块的开发与优化: 1. 宠物模块:新增宠物实体、服务接口与实现,支持创建默认宠物、激活、喂养、进化以及喂养记录查询 2. 签到模块:新增签到实体、服务接口、控制器以及相关DTO,支持用户签到和签到信息查询,新增成长值奖励字段 3. 期刊绑定模块:新增用户期刊关联实体、服务接口与控制器,支持扫码绑定期刊、解绑和查询绑定列表 4. 补偿任务模块:新增补偿任务实体、服务接口与实现,用于处理业务失败后的异步重试补偿 5. 优化微信登录流程:拆分登录与快捷登录接口,支持手机号获取,新增首次登录自动创建默认宠物逻辑 6. 调整基础路由与实体状态:修改微信控制器路由前缀,更新宠物状态枚举与默认值
This commit is contained in:
@ -0,0 +1,82 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using QYZH.InteractiveMagazine.IService;
|
||||
using QYZH.InteractiveMagazine.Models.Common;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
using QYZH.InteractiveMagazine.Models.Dto.CheckIn;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.WebApi.Controllers.WeChat;
|
||||
|
||||
/// <summary>
|
||||
/// 签到控制器
|
||||
/// </summary>
|
||||
public class CheckInController : WeChatBaseController
|
||||
{
|
||||
private readonly ICheckInService _checkInService;
|
||||
private readonly ILogger<CheckInController> _logger;
|
||||
|
||||
public CheckInController(ICheckInService checkInService, ILogger<CheckInController> logger)
|
||||
{
|
||||
_checkInService = checkInService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户签到
|
||||
/// </summary>
|
||||
/// <returns>签到结果(含奖励详情和余额)</returns>
|
||||
[HttpPost("checkIn")]
|
||||
public async Task<BaseResponse<CheckInOutput>> CheckInAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<CheckInOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
}
|
||||
|
||||
var result = await _checkInService.CheckInAsync(userId.Value);
|
||||
return Success(result, "签到成功");
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "签到业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<CheckInOutput>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "签到系统异常");
|
||||
return BaseResponse<CheckInOutput>.Fail("签到失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取签到信息(今日状态、连续天数、最近记录)
|
||||
/// </summary>
|
||||
/// <returns>签到信息</returns>
|
||||
[HttpGet("info")]
|
||||
public async Task<BaseResponse<CheckInInfoOutput>> GetCheckInInfoAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<CheckInInfoOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
}
|
||||
|
||||
var result = await _checkInService.GetCheckInInfoAsync(userId.Value);
|
||||
return Success(result);
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "获取签到信息业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<CheckInInfoOutput>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "获取签到信息系统异常");
|
||||
return BaseResponse<CheckInInfoOutput>.Fail("获取签到信息失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,83 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using QYZH.InteractiveMagazine.IService;
|
||||
using QYZH.InteractiveMagazine.Models.Common;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.WebApi.Controllers.WeChat;
|
||||
|
||||
/// <summary>
|
||||
/// 小程序期刊管理控制器
|
||||
/// </summary>
|
||||
public class JournalController : WeChatBaseController
|
||||
{
|
||||
private readonly IUserJournalService _userJournalService;
|
||||
private readonly ILogger<JournalController> _logger;
|
||||
|
||||
public JournalController(IUserJournalService userJournalService, ILogger<JournalController> logger)
|
||||
{
|
||||
_userJournalService = userJournalService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户扫码绑定期刊
|
||||
/// </summary>
|
||||
/// <param name="input">绑定输入(JournalId、JournalInstanceId 从扫码内容解析,Type 默认 Subscribe)</param>
|
||||
/// <returns>绑定结果</returns>
|
||||
[HttpPost("bind")]
|
||||
public async Task<BaseResponse<BindJournalOutput>> BindAsync([FromBody] BindJournalInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<BindJournalOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
}
|
||||
|
||||
var result = await _userJournalService.BindJournalAsync(userId.Value, input);
|
||||
return Success(result, "绑定期刊成功");
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "绑定期刊业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<BindJournalOutput>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "绑定期刊系统异常,参数:{Input}", input);
|
||||
return BaseResponse<BindJournalOutput>.Fail("绑定期刊失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前用户的期刊绑定列表
|
||||
/// </summary>
|
||||
/// <param name="input">查询条件(期刊Id、实例Id、关联类型)</param>
|
||||
/// <returns>分页结果</returns>
|
||||
[HttpPost("list")]
|
||||
public async Task<BaseResponse<PageListModel<BindJournalOutput>>> GetListAsync([FromBody] UserJournalQueryInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<PageListModel<BindJournalOutput>>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
}
|
||||
|
||||
var result = await _userJournalService.GetUserJournalsAsync(userId.Value, input);
|
||||
return Success(result);
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "查询期刊绑定列表业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<PageListModel<BindJournalOutput>>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "查询期刊绑定列表系统异常");
|
||||
return BaseResponse<PageListModel<BindJournalOutput>>.Fail("查询期刊绑定列表失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,114 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using QYZH.InteractiveMagazine.IService;
|
||||
using QYZH.InteractiveMagazine.Models.Common;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
using QYZH.InteractiveMagazine.Models.Dto.Pet;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.WebApi.Controllers.WeChat;
|
||||
|
||||
/// <summary>
|
||||
/// 小程序宠物管理控制器
|
||||
/// </summary>
|
||||
public class PetController : WeChatBaseController
|
||||
{
|
||||
private readonly IPetService _petService;
|
||||
private readonly ILogger<PetController> _logger;
|
||||
|
||||
public PetController(IPetService petService, ILogger<PetController> logger)
|
||||
{
|
||||
_petService = petService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前用户的宠物信息
|
||||
/// </summary>
|
||||
[HttpGet("mine")]
|
||||
public async Task<BaseResponse<PetOutput>> GetMyPetAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<PetOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
}
|
||||
|
||||
var pet = await _petService.GetPetByUserIdAsync(userId.Value);
|
||||
if (pet == null)
|
||||
{
|
||||
return BaseResponse<PetOutput>.Fail("未找到宠物信息");
|
||||
}
|
||||
|
||||
return Success(pet);
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "获取宠物信息业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<PetOutput>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "获取宠物信息系统异常");
|
||||
return BaseResponse<PetOutput>.Fail("获取宠物信息失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 喂养宠物(增加成长值,触发进化检查)
|
||||
/// </summary>
|
||||
[HttpPost("feed")]
|
||||
public async Task<BaseResponse<FeedPetOutput>> FeedPetAsync([FromBody] FeedPetInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<FeedPetOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
}
|
||||
|
||||
var result = await _petService.FeedPetAsync(userId.Value, input);
|
||||
return Success(result, "喂养成功");
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "喂养宠物业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<FeedPetOutput>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "喂养宠物系统异常,参数:{@Input}", input);
|
||||
return BaseResponse<FeedPetOutput>.Fail("喂养宠物失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取宠物喂养记录列表
|
||||
/// </summary>
|
||||
[HttpGet("records/{petId}")]
|
||||
public async Task<BaseResponse<PageListModel<FeedingRecordOutput>>> GetFeedingRecordsAsync(long petId, [FromQuery] PageQueryModel pageQuery)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null)
|
||||
{
|
||||
return BaseResponse<PageListModel<FeedingRecordOutput>>.Fail(ResultCode.DENY, "未获取到用户信息");
|
||||
}
|
||||
|
||||
var result = await _petService.GetFeedingRecordsAsync(userId.Value, petId, pageQuery);
|
||||
return Success(result);
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "查询喂养记录业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<PageListModel<FeedingRecordOutput>>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "查询喂养记录系统异常");
|
||||
return BaseResponse<PageListModel<FeedingRecordOutput>>.Fail("查询喂养记录失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -22,10 +22,10 @@ public class WeChatAuthController : WeChatBaseController
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 微信小程序一键登录
|
||||
/// 微信小程序登录(首次创建用户,非首次直接登录)
|
||||
/// </summary>
|
||||
/// <param name="input">登录输入(含微信 code)</param>
|
||||
/// <returns>登录结果(含 Token 和用户信息)</returns>
|
||||
/// <param name="input">登录输入(含微信 code 和可选的手机号 code)</param>
|
||||
/// <returns>登录结果(含 Token 和用户列表)</returns>
|
||||
[AllowAnonymous]
|
||||
[HttpPost("login")]
|
||||
public async Task<BaseResponse<WeChatLoginOutput>> LoginAsync([FromBody] WeChatLoginInput input)
|
||||
@ -47,6 +47,32 @@ public class WeChatAuthController : WeChatBaseController
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 微信小程序快捷登录(通过 OpenId 直接登录,用户需已存在)
|
||||
/// </summary>
|
||||
/// <param name="input">快捷登录输入(含 OpenId)</param>
|
||||
/// <returns>登录结果(含 Token 和用户列表)</returns>
|
||||
[AllowAnonymous]
|
||||
[HttpPost("quickLogin")]
|
||||
public async Task<BaseResponse<WeChatLoginOutput>> QuickLoginAsync([FromBody] WeChatQuickLoginInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _weChatAuthService.QuickLoginAsync(input);
|
||||
return Success(result);
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "微信快捷登录业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<WeChatLoginOutput>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "微信快捷登录系统异常");
|
||||
return BaseResponse<WeChatLoginOutput>.Fail("快捷登录失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 切换用户(同一 OpenId 下切换身份)
|
||||
/// </summary>
|
||||
|
||||
@ -11,7 +11,7 @@ namespace QYZH.InteractiveMagazine.WebApi.Controllers.WeChat;
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[Route("wechat/api/[controller]")]
|
||||
[ApiExplorerSettings(GroupName = nameof(ApiVersionEnum.Wechat))]
|
||||
public abstract class WeChatBaseController : ControllerBase
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user