FrontArticleController.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using Microsoft.AspNetCore.Mvc;
  2. using ZR.Model.Content;
  3. using ZR.Model.Content.Dto;
  4. using ZR.Service.Content.IService;
  5. using ZR.Service.Social.IService;
  6. namespace ZR.Admin.WebApi.Controllers
  7. {
  8. /// <summary>
  9. /// 内容管理前端接口
  10. /// </summary>
  11. [Route("front/article")]
  12. [ApiExplorerSettings(GroupName = "article")]
  13. public class FrontArticleController : BaseController
  14. {
  15. /// <summary>
  16. /// 文章接口
  17. /// </summary>
  18. private readonly IArticleService _ArticleService;
  19. private readonly IArticleCategoryService _ArticleCategoryService;
  20. private readonly IArticlePraiseService _ArticlePraiseService;
  21. private readonly ISysUserService _SysUserService;
  22. private readonly IArticleTopicService _ArticleTopicService;
  23. private readonly IArticleUserCirclesService _ArticleUserCirclesService;
  24. private readonly ISocialFansInfoService _FansInfoService;
  25. /// <summary>
  26. ///
  27. /// </summary>
  28. /// <param name="ArticleService"></param>
  29. /// <param name="articleCategoryService"></param>
  30. /// <param name="articlePraiseService"></param>
  31. /// <param name="sysUserService"></param>
  32. /// <param name="articleTopicService"></param>
  33. /// <param name="articleUserCirclesService"></param>
  34. /// <param name="fansInfoService"></param>
  35. public FrontArticleController(
  36. IArticleService ArticleService,
  37. IArticleCategoryService articleCategoryService,
  38. IArticlePraiseService articlePraiseService,
  39. ISysUserService sysUserService,
  40. IArticleTopicService articleTopicService,
  41. IArticleUserCirclesService articleUserCirclesService,
  42. ISocialFansInfoService fansInfoService)
  43. {
  44. _ArticleService = ArticleService;
  45. _ArticleCategoryService = articleCategoryService;
  46. _ArticleService = ArticleService;
  47. _ArticlePraiseService = articlePraiseService;
  48. _SysUserService = sysUserService;
  49. _ArticleTopicService = articleTopicService;
  50. _ArticleUserCirclesService = articleUserCirclesService;
  51. _FansInfoService = fansInfoService;
  52. }
  53. /// <summary>
  54. /// 前台查询文章列表
  55. /// </summary>
  56. /// <returns></returns>
  57. [HttpGet("homeList")]
  58. [AllowAnonymous]
  59. public IActionResult QueryHomeList([FromQuery] ArticleQueryDto parm)
  60. {
  61. parm.UserId = HttpContext.GetUId();
  62. var response = _ArticleService.GetArticleList(parm);
  63. return SUCCESS(response);
  64. }
  65. /// <summary>
  66. /// 查询最新文章列表
  67. /// </summary>
  68. /// <returns></returns>
  69. [HttpGet("newList")]
  70. [AllowAnonymous]
  71. public IActionResult QueryNew()
  72. {
  73. return SUCCESS(_ArticleService.GetNewArticleList());
  74. }
  75. /// <summary>
  76. /// 点赞
  77. /// </summary>
  78. /// <param name="id"></param>
  79. /// <param name="authorId"></param>
  80. /// <returns></returns>
  81. [HttpPost("praise/{id}")]
  82. [ActionPermissionFilter(Permission = "common")]
  83. public IActionResult Praise(int id = 0, long authorId = 0)
  84. {
  85. ArticlePraise addModel = new()
  86. {
  87. UserId = HttpContext.GetUId(),
  88. UserIP = HttpContext.GetClientUserIp(),
  89. ArticleId = id,
  90. ToUserId = authorId
  91. };
  92. addModel.Location = HttpContextExtension.GetIpInfo(addModel.UserIP);
  93. return SUCCESS(_ArticlePraiseService.Praise(addModel));
  94. }
  95. /// <summary>
  96. /// 置顶
  97. /// </summary>
  98. /// <returns></returns>
  99. [HttpPut("top")]
  100. [ActionPermissionFilter(Permission = "common")]
  101. public IActionResult Top([FromBody] Article parm)
  102. {
  103. var response = _ArticleService.TopArticle(parm);
  104. return SUCCESS(response);
  105. }
  106. /// <summary>
  107. /// 修改可见范围
  108. /// </summary>
  109. /// <returns></returns>
  110. [HttpPut("changePublic")]
  111. [ActionPermissionFilter(Permission = "common")]
  112. public IActionResult ChangePublic([FromBody] Article parm)
  113. {
  114. if (parm == null) { return ToResponse(ResultCode.CUSTOM_ERROR); }
  115. var userId = HttpContext.GetUId();
  116. if (userId != parm.UserId)
  117. {
  118. return ToResponse(ResultCode.CUSTOM_ERROR, "操作失败");
  119. }
  120. var response = _ArticleService.ChangeArticlePublic(parm);
  121. return SUCCESS(response);
  122. }
  123. /// <summary>
  124. /// 修改评论权限
  125. /// </summary>
  126. /// <returns></returns>
  127. [HttpPut("changeComment")]
  128. [ActionPermissionFilter(Permission = "common")]
  129. public IActionResult ChangeComment([FromBody] Article parm)
  130. {
  131. if (parm == null) { return ToResponse(ResultCode.CUSTOM_ERROR); }
  132. var userId = HttpContext.GetUId();
  133. if (userId != parm.UserId)
  134. {
  135. return ToResponse(ResultCode.CUSTOM_ERROR, "操作失败");
  136. }
  137. var response = _ArticleService.ChangeComment(parm);
  138. return SUCCESS(response);
  139. }
  140. /// <summary>
  141. /// 删除
  142. /// </summary>
  143. /// <returns></returns>
  144. [HttpDelete("del/{id}")]
  145. [ActionPermissionFilter(Permission = "common")]
  146. public IActionResult Delete(long id = 0)
  147. {
  148. var userId = HttpContext.GetUId();
  149. var info = _ArticleService.GetId(id);
  150. if (info == null || info.UserId != userId)
  151. {
  152. return ToResponse(ResultCode.CUSTOM_ERROR, "删除失败(-1)");
  153. }
  154. var response = _ArticleService.Delete(id);
  155. return SUCCESS(response);
  156. }
  157. /// <summary>
  158. /// 查询文章详情
  159. /// </summary>
  160. /// <param name="id"></param>
  161. /// <returns></returns>
  162. [HttpGet("{id}")]
  163. [AllowAnonymous]
  164. public IActionResult Get(int id)
  165. {
  166. long userId = HttpContext.GetUId();
  167. var model = _ArticleService.GetArticle(id, userId);
  168. var user = _SysUserService.GetById(model.UserId);
  169. ApiResult apiResult = ApiResult.Success(model);
  170. model.User = new ArticleUser()
  171. {
  172. Avatar = user.Avatar,
  173. NickName = user.NickName,
  174. Sex = user.Sex,
  175. };
  176. return ToResponse(apiResult);
  177. }
  178. /// <summary>
  179. /// 前台查询话题
  180. /// </summary>
  181. /// <param name="parm"></param>
  182. /// <returns></returns>
  183. [HttpGet("topicList")]
  184. [AllowAnonymous]
  185. public IActionResult QueryTopicList([FromQuery] ArticleTopicQueryDto parm)
  186. {
  187. var response = _ArticleTopicService.GetTopicList(parm);
  188. return SUCCESS(response);
  189. }
  190. /// <summary>
  191. /// 查询文章话题详情
  192. /// </summary>
  193. /// <param name="TopicId"></param>
  194. /// <returns></returns>
  195. [HttpGet("topic/{TopicId}")]
  196. [AllowAnonymous]
  197. public IActionResult GetArticleTopic(long TopicId)
  198. {
  199. var response = _ArticleTopicService.GetInfo(TopicId);
  200. _ArticleTopicService.Update(w => w.TopicId == TopicId, it => new ArticleTopic() { ViewNum = it.ViewNum + 1 });
  201. var info = response.Adapt<ArticleTopicDto>();
  202. return SUCCESS(info);
  203. }
  204. /// <summary>
  205. /// 查询文章目录详情
  206. /// </summary>
  207. /// <param name="CategoryId"></param>
  208. /// <returns></returns>
  209. [HttpGet("QueryCircle/{CategoryId}")]
  210. [AllowAnonymous]
  211. public IActionResult GetArticleCategory(int CategoryId)
  212. {
  213. var userId = HttpContext.GetUId();
  214. var info = _ArticleCategoryService.GetFirst(x => x.CategoryId == CategoryId);
  215. var infoDto = info.Adapt<CircleInfoDto>();
  216. if (infoDto != null)
  217. {
  218. infoDto.IsJoin = _ArticleUserCirclesService.IsJoin((int)userId, CategoryId);
  219. }
  220. return SUCCESS(infoDto);
  221. }
  222. /// <summary>
  223. /// 发布文章
  224. /// </summary>
  225. /// <returns></returns>
  226. [HttpPost("publish")]
  227. [ActionPermissionFilter(Permission = "common")]
  228. public IActionResult Create([FromBody] ArticleDto parm)
  229. {
  230. if (parm == null) { return ToResponse(ResultCode.PARAM_ERROR); }
  231. var addModel = parm.Adapt<Article>().ToCreate(context: HttpContext);
  232. addModel.ArticleType = Model.Enum.ArticleTypeEnum.Article;
  233. addModel.Tags = parm.TopicName;
  234. return SUCCESS(_ArticleService.Publish(addModel));
  235. }
  236. /// <summary>
  237. /// 获取我的成就信息
  238. /// </summary>
  239. /// <returns></returns>
  240. [HttpGet("GetUserWidget")]
  241. public IActionResult GetUserWidget(int toUserid)
  242. {
  243. var userId = HttpContext.GetUId();
  244. var fansInfo = _FansInfoService.GetFirst(f => f.Userid == toUserid) ?? new Model.social.SocialFansInfo() { };
  245. return SUCCESS(new { fansInfo });
  246. }
  247. }
  248. }