NoticeController.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using Masuit.MyBlogs.Core.Common;
  2. using Masuit.MyBlogs.Core.Extensions;
  3. using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
  4. using Masuit.MyBlogs.Core.Models;
  5. using Masuit.MyBlogs.Core.Models.DTO;
  6. using Masuit.MyBlogs.Core.Models.Entity;
  7. using Masuit.MyBlogs.Core.Models.Enum;
  8. using Masuit.MyBlogs.Core.Models.ViewModel;
  9. using Masuit.Tools.Core.Net;
  10. using Masuit.Tools.Html;
  11. using Microsoft.AspNetCore.Hosting;
  12. using Microsoft.AspNetCore.Http;
  13. using Microsoft.AspNetCore.Mvc;
  14. using System;
  15. using System.ComponentModel.DataAnnotations;
  16. using System.Linq;
  17. using System.Threading.Tasks;
  18. namespace Masuit.MyBlogs.Core.Controllers
  19. {
  20. /// <summary>
  21. /// 网站公告
  22. /// </summary>
  23. public class NoticeController : BaseController
  24. {
  25. /// <summary>
  26. /// 公告
  27. /// </summary>
  28. public INoticeService NoticeService { get; set; }
  29. public IWebHostEnvironment HostEnvironment { get; set; }
  30. public ImagebedClient ImagebedClient { get; set; }
  31. /// <summary>
  32. /// 公告列表
  33. /// </summary>
  34. /// <param name="page"></param>
  35. /// <param name="size"></param>
  36. /// <returns></returns>
  37. [Route("notice"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "page", "size" }, VaryByHeader = "Cookie")]
  38. public async Task<ActionResult> Index([Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
  39. {
  40. var list = await NoticeService.GetPagesFromCacheAsync<DateTime, NoticeDto>(page, size, n => n.Status == Status.Display, n => n.ModifyDate, false);
  41. ViewData["page"] = new Pagination(page, size, list.TotalCount);
  42. foreach (var n in list.Data)
  43. {
  44. n.ModifyDate = n.ModifyDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  45. n.PostDate = n.PostDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  46. }
  47. ViewBag.Ads = AdsService.GetByWeightedPrice(AdvertiseType.PostList);
  48. return CurrentUser.IsAdmin ? View("Index_Admin", list.Data) : View(list.Data);
  49. }
  50. /// <summary>
  51. /// 公告详情
  52. /// </summary>
  53. /// <param name="id"></param>
  54. /// <returns></returns>
  55. [Route("n/{id:int}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id" }, VaryByHeader = "Cookie")]
  56. public async Task<ActionResult> Details(int id)
  57. {
  58. var notice = await NoticeService.GetByIdAsync(id) ?? throw new NotFoundException("页面未找到");
  59. if (!HttpContext.Session.TryGetValue("notice" + id, out _))
  60. {
  61. notice.ViewCount += 1;
  62. await NoticeService.SaveChangesAsync();
  63. HttpContext.Session.Set("notice" + id, notice.Title);
  64. }
  65. notice.ModifyDate = notice.ModifyDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  66. notice.PostDate = notice.PostDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  67. ViewBag.Ads = AdsService.GetByWeightedPrice(AdvertiseType.InPage);
  68. return View(notice);
  69. }
  70. /// <summary>
  71. /// 发布公告
  72. /// </summary>
  73. /// <param name="notice"></param>
  74. /// <returns></returns>
  75. [MyAuthorize]
  76. public async Task<ActionResult> Write(Notice notice)
  77. {
  78. notice.Content = await ImagebedClient.ReplaceImgSrc(notice.Content.ClearImgAttributes());
  79. Notice e = NoticeService.AddEntitySaved(notice);
  80. return e != null ? ResultData(null, message: "发布成功") : ResultData(null, false, "发布失败");
  81. }
  82. /// <summary>
  83. /// 删除公告
  84. /// </summary>
  85. /// <param name="id"></param>
  86. /// <returns></returns>
  87. [MyAuthorize]
  88. public async Task<ActionResult> Delete(int id)
  89. {
  90. var notice = await NoticeService.GetByIdAsync(id) ?? throw new NotFoundException("公告已经被删除!");
  91. var srcs = notice.Content.MatchImgSrcs().Where(s => s.StartsWith("/"));
  92. foreach (var path in srcs)
  93. {
  94. try
  95. {
  96. System.IO.File.Delete(HostEnvironment.WebRootPath + path);
  97. }
  98. catch
  99. {
  100. }
  101. }
  102. bool b = await NoticeService.DeleteByIdSavedAsync(id) > 0;
  103. return ResultData(null, b, b ? "删除成功" : "删除失败");
  104. }
  105. /// <summary>
  106. /// 编辑公告
  107. /// </summary>
  108. /// <param name="notice"></param>
  109. /// <returns></returns>
  110. [MyAuthorize]
  111. public async Task<ActionResult> Edit(Notice notice)
  112. {
  113. var entity = await NoticeService.GetByIdAsync(notice.Id) ?? throw new NotFoundException("公告已经被删除!");
  114. entity.ModifyDate = DateTime.Now;
  115. entity.Title = notice.Title;
  116. entity.Content = await ImagebedClient.ReplaceImgSrc(notice.Content.ClearImgAttributes());
  117. bool b = await NoticeService.SaveChangesAsync() > 0;
  118. return ResultData(null, b, b ? "修改成功" : "修改失败");
  119. }
  120. /// <summary>
  121. /// 公告分页数据
  122. /// </summary>
  123. /// <param name="page"></param>
  124. /// <param name="size"></param>
  125. /// <returns></returns>
  126. public ActionResult GetPageData([Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
  127. {
  128. var list = NoticeService.GetPagesNoTracking(page, size, n => true, n => n.ModifyDate, false);
  129. foreach (var n in list.Data)
  130. {
  131. n.ModifyDate = n.ModifyDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  132. n.PostDate = n.PostDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  133. }
  134. return Ok(list);
  135. }
  136. /// <summary>
  137. /// 公告详情
  138. /// </summary>
  139. /// <param name="id"></param>
  140. /// <returns></returns>
  141. [MyAuthorize]
  142. public ActionResult Get(int id)
  143. {
  144. var notice = NoticeService.Get<NoticeDto>(n => n.Id == id);
  145. if (notice != null)
  146. {
  147. notice.ModifyDate = notice.ModifyDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  148. notice.PostDate = notice.PostDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  149. }
  150. return ResultData(notice);
  151. }
  152. /// <summary>
  153. /// 最近一条公告
  154. /// </summary>
  155. /// <returns></returns>
  156. [ResponseCache(Duration = 600, VaryByHeader = "Cookie")]
  157. public async Task<ActionResult> Last()
  158. {
  159. var notice = await NoticeService.GetAsync(n => n.Status == Status.Display, n => n.ModifyDate, false);
  160. if (notice == null)
  161. {
  162. return ResultData(null, false);
  163. }
  164. if (Request.Cookies.TryGetValue("last-notice", out var id) && notice.Id.ToString() == id)
  165. {
  166. return ResultData(null, false);
  167. }
  168. notice.ViewCount += 1;
  169. await NoticeService.SaveChangesAsync();
  170. var dto = notice.Mapper<NoticeDto>();
  171. Response.Cookies.Append("last-notice", dto.Id.ToString(), new CookieOptions()
  172. {
  173. Expires = DateTime.Now.AddYears(1),
  174. SameSite = SameSiteMode.Lax
  175. });
  176. return ResultData(dto);
  177. }
  178. }
  179. }