2026-06-04 15:54:30 +08:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using QYZH.InteractiveMagazine.IService;
|
|
|
|
|
using QYZH.InteractiveMagazine.Models.Common;
|
|
|
|
|
using QYZH.InteractiveMagazine.Models.Dto;
|
|
|
|
|
using QYZH.InteractiveMagazine.Models.Dto.Pet;
|
|
|
|
|
|
2026-07-06 14:21:21 +08:00
|
|
|
namespace QYZH.InteractiveMagazine.WeChatApi.Controllers;
|
2026-06-04 15:54:30 +08:00
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
|
{
|
2026-06-10 17:53:38 +08:00
|
|
|
var userId = GetCurrentUserId();
|
|
|
|
|
if (userId == 0)
|
2026-06-04 15:54:30 +08:00
|
|
|
{
|
|
|
|
|
return BaseResponse<PetOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-10 17:53:38 +08:00
|
|
|
var pet = await _petService.GetPetByUserIdAsync(userId);
|
2026-06-04 15:54:30 +08:00
|
|
|
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
|
|
|
|
|
{
|
2026-06-10 17:53:38 +08:00
|
|
|
var userId = GetCurrentUserId();
|
|
|
|
|
if (userId == 0)
|
2026-06-04 15:54:30 +08:00
|
|
|
{
|
|
|
|
|
return BaseResponse<FeedPetOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-10 17:53:38 +08:00
|
|
|
var result = await _petService.FeedPetAsync(userId, input);
|
2026-06-04 15:54:30 +08:00
|
|
|
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
|
|
|
|
|
{
|
2026-06-10 17:53:38 +08:00
|
|
|
var userId = GetCurrentUserId();
|
|
|
|
|
if (userId == 0)
|
2026-06-04 15:54:30 +08:00
|
|
|
{
|
|
|
|
|
return BaseResponse<PageListModel<FeedingRecordOutput>>.Fail(ResultCode.DENY, "未获取到用户信息");
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-10 17:53:38 +08:00
|
|
|
var result = await _petService.GetFeedingRecordsAsync(userId, petId, pageQuery);
|
2026-06-04 15:54:30 +08:00
|
|
|
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("查询喂养记录失败,请稍后重试");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|