NoticeController.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.Tools;
  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 Newtonsoft.Json;
  15. using System;
  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 ActionResult Index(int page = 1, int size = 10)
  39. {
  40. var list = NoticeService.GetPages<DateTime, NoticeOutputDto>(page, size, out var total, n => n.Status == Status.Display, n => n.ModifyDate, false).ToList();
  41. ViewBag.Total = total;
  42. ViewData["page"] = new Pagination(page, size);
  43. return CurrentUser.IsAdmin ? View("Index_Admin", list) : View(list);
  44. }
  45. /// <summary>
  46. /// 公告详情
  47. /// </summary>
  48. /// <param name="id"></param>
  49. /// <returns></returns>
  50. [Route("n/{id:int}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id" }, VaryByHeader = "Cookie")]
  51. public ActionResult Details(int id)
  52. {
  53. var notice = NoticeService.GetById(id) ?? throw new NotFoundException("页面未找到");
  54. if (!HttpContext.Session.TryGetValue("notice" + id, out _))
  55. {
  56. notice.ViewCount += 1;
  57. NoticeService.SaveChanges();
  58. HttpContext.Session.Set("notice" + id, notice.Title);
  59. }
  60. return View(notice);
  61. }
  62. /// <summary>
  63. /// 发布公告
  64. /// </summary>
  65. /// <param name="notice"></param>
  66. /// <returns></returns>
  67. [MyAuthorize]
  68. public async Task<ActionResult> Write(Notice notice)
  69. {
  70. notice.Content = await ImagebedClient.ReplaceImgSrc(notice.Content.ClearImgAttributes());
  71. Notice e = NoticeService.AddEntitySaved(notice);
  72. return e != null ? ResultData(null, message: "发布成功") : ResultData(null, false, "发布失败");
  73. }
  74. /// <summary>
  75. /// 删除公告
  76. /// </summary>
  77. /// <param name="id"></param>
  78. /// <returns></returns>
  79. [MyAuthorize]
  80. public ActionResult Delete(int id)
  81. {
  82. var notice = NoticeService.GetById(id) ?? throw new NotFoundException("公告已经被删除!");
  83. var srcs = notice.Content.MatchImgSrcs().Where(s => s.StartsWith("/"));
  84. foreach (var path in srcs)
  85. {
  86. try
  87. {
  88. System.IO.File.Delete(HostEnvironment.WebRootPath + path);
  89. }
  90. catch
  91. {
  92. }
  93. }
  94. bool b = NoticeService.DeleteByIdSaved(id);
  95. return ResultData(null, b, b ? "删除成功" : "删除失败");
  96. }
  97. /// <summary>
  98. /// 编辑公告
  99. /// </summary>
  100. /// <param name="notice"></param>
  101. /// <returns></returns>
  102. [MyAuthorize]
  103. public async Task<ActionResult> Edit(Notice notice)
  104. {
  105. var entity = NoticeService.GetById(notice.Id) ?? throw new NotFoundException("公告已经被删除!");
  106. entity.ModifyDate = DateTime.Now;
  107. entity.Title = notice.Title;
  108. entity.Content = await ImagebedClient.ReplaceImgSrc(notice.Content.ClearImgAttributes());
  109. bool b = NoticeService.SaveChanges() > 0;
  110. return ResultData(null, b, b ? "修改成功" : "修改失败");
  111. }
  112. /// <summary>
  113. /// 公告分页数据
  114. /// </summary>
  115. /// <param name="page"></param>
  116. /// <param name="size"></param>
  117. /// <returns></returns>
  118. public ActionResult GetPageData(int page = 1, int size = 10)
  119. {
  120. var list = NoticeService.GetPagesNoTracking(page, size, out int total, n => true, n => n.ModifyDate, false).ToList();
  121. var pageCount = Math.Ceiling(total * 1.0 / size).ToInt32();
  122. return PageResult(list, pageCount, total);
  123. }
  124. /// <summary>
  125. /// 公告详情
  126. /// </summary>
  127. /// <param name="id"></param>
  128. /// <returns></returns>
  129. [MyAuthorize]
  130. public ActionResult Get(int id)
  131. {
  132. return ResultData(NoticeService.Get<NoticeOutputDto>(n => n.Id == id));
  133. }
  134. /// <summary>
  135. /// 最近一条公告
  136. /// </summary>
  137. /// <returns></returns>
  138. [ResponseCache(Duration = 600, VaryByHeader = "Cookie")]
  139. public ActionResult Last()
  140. {
  141. var notice = NoticeService.Get(n => n.Status == Status.Display, n => n.ModifyDate, false);
  142. if (notice == null)
  143. {
  144. return ResultData(null, false);
  145. }
  146. if (Request.Cookies.TryGetValue("last-notice", out var json))
  147. {
  148. var data = JsonConvert.DeserializeObject<NoticeOutputDto>(json);
  149. if (notice.Id == data.Id)
  150. {
  151. return ResultData(data);
  152. }
  153. }
  154. notice.ViewCount += 1;
  155. NoticeService.SaveChanges();
  156. var dto = notice.Mapper<NoticeOutputDto>();
  157. Response.Cookies.Append("last-notice", dto.ToJsonString(), new CookieOptions()
  158. {
  159. Expires = DateTime.Now.AddMonths(1)
  160. });
  161. return ResultData(dto);
  162. }
  163. }
  164. }