2026-06-05 17:15:30 +08:00
|
|
|
|
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 CommunityController(IWeChatCommunityService communityService, ILogger<CommunityController> logger) : WeChatBaseController
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取社区Feed流(翻页)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="cursor">游标(上一页最后一条消息ID,首次不传)</param>
|
|
|
|
|
|
[HttpGet("feed")]
|
|
|
|
|
|
public async Task<BaseResponse<WxFeedOutput>> GetFeed([FromQuery] long? cursor = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-06-10 13:47:13 +08:00
|
|
|
|
var userId = GetCurrentWxUserId();
|
2026-06-05 17:15:30 +08:00
|
|
|
|
if (userId == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return BaseResponse<WxFeedOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var result = await communityService.GetFeedAsync(userId.Value, cursor);
|
|
|
|
|
|
return Success(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (BusinessException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning(ex, "获取社区Feed业务异常: {Message}", ex.Message);
|
|
|
|
|
|
return BaseResponse<WxFeedOutput>.Fail(ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "获取社区Feed系统异常");
|
|
|
|
|
|
return BaseResponse<WxFeedOutput>.Fail("获取社区内容失败,请稍后重试");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 下拉刷新社区Feed
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpGet("refresh")]
|
|
|
|
|
|
public async Task<BaseResponse<WxFeedOutput>> RefreshFeed()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-06-10 13:47:13 +08:00
|
|
|
|
var userId = GetCurrentWxUserId();
|
2026-06-05 17:15:30 +08:00
|
|
|
|
if (userId == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return BaseResponse<WxFeedOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var result = await communityService.RefreshFeedAsync(userId.Value);
|
|
|
|
|
|
return Success(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (BusinessException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning(ex, "刷新社区Feed业务异常: {Message}", ex.Message);
|
|
|
|
|
|
return BaseResponse<WxFeedOutput>.Fail(ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "刷新社区Feed系统异常");
|
|
|
|
|
|
return BaseResponse<WxFeedOutput>.Fail("刷新社区内容失败,请稍后重试");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 点赞
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpPost("like")]
|
|
|
|
|
|
public async Task<BaseResponse<WxLikeOutput>> Like([FromBody] WxLikeInput input)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-06-10 13:47:13 +08:00
|
|
|
|
var userId = GetCurrentWxUserId();
|
2026-06-05 17:15:30 +08:00
|
|
|
|
if (userId == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return BaseResponse<WxLikeOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var result = await communityService.LikeAsync(userId.Value, input);
|
|
|
|
|
|
return Success(result, "点赞成功");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (BusinessException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning(ex, "点赞业务异常: {Message}", ex.Message);
|
|
|
|
|
|
return BaseResponse<WxLikeOutput>.Fail(ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "点赞系统异常,参数:{Input}", input);
|
|
|
|
|
|
return BaseResponse<WxLikeOutput>.Fail("点赞失败,请稍后重试");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 取消点赞
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpPost("unlike")]
|
|
|
|
|
|
public async Task<BaseResponse<WxLikeOutput>> Unlike([FromBody] WxLikeInput input)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-06-10 13:47:13 +08:00
|
|
|
|
var userId = GetCurrentWxUserId();
|
2026-06-05 17:15:30 +08:00
|
|
|
|
if (userId == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return BaseResponse<WxLikeOutput>.Fail(ResultCode.DENY, "未获取到用户信息");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var result = await communityService.UnlikeAsync(userId.Value, input.MessageId);
|
|
|
|
|
|
return Success(result, "已取消点赞");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (BusinessException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning(ex, "取消点赞业务异常: {Message}", ex.Message);
|
|
|
|
|
|
return BaseResponse<WxLikeOutput>.Fail(ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "取消点赞系统异常,参数:{Input}", input);
|
|
|
|
|
|
return BaseResponse<WxLikeOutput>.Fail("取消点赞失败,请稍后重试");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|