using Microsoft.AspNetCore.Mvc; using QYZH.InteractiveMagazine.IService; using QYZH.InteractiveMagazine.Models.Common; using QYZH.InteractiveMagazine.Models.Dto; namespace QYZH.InteractiveMagazine.WeChatApi.Controllers; /// /// 小程序社区控制器 /// public class CommunityController(IWeChatCommunityService communityService, ILogger logger) : WeChatBaseController { /// /// 获取社区Feed流(翻页) /// /// 游标(上一页最后一条消息ID,首次不传) [HttpGet("feed")] public async Task> GetFeed([FromQuery] long? cursor = null) { try { var userId = GetCurrentUserId(); if (userId == 0) { return BaseResponse.Fail(ResultCode.DENY, "未获取到用户信息"); } var result = await communityService.GetFeedAsync(userId, cursor); return Success(result); } catch (BusinessException ex) { logger.LogWarning(ex, "获取社区Feed业务异常: {Message}", ex.Message); return BaseResponse.Fail(ex.Message); } catch (Exception ex) { logger.LogError(ex, "获取社区Feed系统异常"); return BaseResponse.Fail("获取社区内容失败,请稍后重试"); } } /// /// 下拉刷新社区Feed /// [HttpGet("refresh")] public async Task> RefreshFeed() { try { var userId = GetCurrentUserId(); if (userId == 0) { return BaseResponse.Fail(ResultCode.DENY, "未获取到用户信息"); } var result = await communityService.RefreshFeedAsync(userId); return Success(result); } catch (BusinessException ex) { logger.LogWarning(ex, "刷新社区Feed业务异常: {Message}", ex.Message); return BaseResponse.Fail(ex.Message); } catch (Exception ex) { logger.LogError(ex, "刷新社区Feed系统异常"); return BaseResponse.Fail("刷新社区内容失败,请稍后重试"); } } /// /// 点赞 /// [HttpPost("like")] public async Task> Like([FromBody] WxLikeInput input) { try { var userId = GetCurrentUserId(); if (userId == 0) { return BaseResponse.Fail(ResultCode.DENY, "未获取到用户信息"); } var result = await communityService.LikeAsync(userId, input); return Success(result, "点赞成功"); } catch (BusinessException ex) { logger.LogWarning(ex, "点赞业务异常: {Message}", ex.Message); return BaseResponse.Fail(ex.Message); } catch (Exception ex) { logger.LogError(ex, "点赞系统异常,参数:{Input}", input); return BaseResponse.Fail("点赞失败,请稍后重试"); } } /// /// 取消点赞 /// [HttpPost("unlike")] public async Task> Unlike([FromBody] WxLikeInput input) { try { var userId = GetCurrentUserId(); if (userId == 0) { return BaseResponse.Fail(ResultCode.DENY, "未获取到用户信息"); } var result = await communityService.UnlikeAsync(userId, input.MessageId); return Success(result, "已取消点赞"); } catch (BusinessException ex) { logger.LogWarning(ex, "取消点赞业务异常: {Message}", ex.Message); return BaseResponse.Fail(ex.Message); } catch (Exception ex) { logger.LogError(ex, "取消点赞系统异常,参数:{Input}", input); return BaseResponse.Fail("取消点赞失败,请稍后重试"); } } }