MiscController.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using Masuit.MyBlogs.Core.Common;
  2. using Masuit.MyBlogs.Core.Extensions;
  3. using Masuit.Tools.AspNetCore.ModelBinder;
  4. using Microsoft.AspNetCore.Mvc;
  5. using System.Text;
  6. namespace Masuit.MyBlogs.Core.Controllers;
  7. /// <summary>
  8. /// 杂项页
  9. /// </summary>
  10. public sealed class MiscController : BaseController
  11. {
  12. /// <summary>
  13. /// MiscService
  14. /// </summary>
  15. public IMiscService MiscService { get; set; }
  16. public IWebHostEnvironment HostEnvironment { get; set; }
  17. public ImagebedClient ImagebedClient { get; set; }
  18. /// <summary>
  19. /// 杂项页
  20. /// </summary>
  21. /// <param name="id"></param>
  22. /// <returns></returns>
  23. [Route("misc/{id:int}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id" }, VaryByHeader = "Cookie")]
  24. public async Task<ActionResult> Index(int id)
  25. {
  26. var misc = await MiscService.GetFromCacheAsync(m => m.Id == id) ?? throw new NotFoundException("页面未找到");
  27. misc.ModifyDate = misc.ModifyDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  28. misc.PostDate = misc.PostDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  29. misc.Content = ReplaceVariables(misc.Content);
  30. return View(misc);
  31. }
  32. /// <summary>
  33. /// 打赏
  34. /// </summary>
  35. /// <returns></returns>
  36. [Route("donate")]
  37. public async Task<ActionResult> Donate()
  38. {
  39. var ads = AdsService.GetsByWeightedPrice(2, AdvertiseType.InPage, Request.Location());
  40. if (bool.Parse(CommonHelper.SystemSettings.GetOrAdd("EnableDonate", "true")))
  41. {
  42. ViewBag.Ads = ads;
  43. var text = await new FileInfo(Path.Combine(HostEnvironment.WebRootPath, "template", "donate.html")).ShareReadWrite().ReadAllTextAsync(Encoding.UTF8);
  44. return CurrentUser.IsAdmin ? View("Donate_Admin", text) : View(model: text);
  45. }
  46. return Redirect(ads.FirstOrDefault()?.Url ?? "/");
  47. }
  48. /// <summary>
  49. /// 打赏列表
  50. /// </summary>
  51. /// <param name="donateService"></param>
  52. /// <param name="page"></param>
  53. /// <param name="size"></param>
  54. /// <returns></returns>
  55. [Route("donatelist")]
  56. public ActionResult DonateList([FromServices] IDonateService donateService, int page = 1, int size = 10)
  57. {
  58. if (bool.Parse(CommonHelper.SystemSettings.GetOrAdd("EnableDonate", "true")))
  59. {
  60. var list = donateService.GetPagesFromCache<DateTime, DonateDto>(page, size, d => true, d => d.DonateTime, false);
  61. if (!CurrentUser.IsAdmin)
  62. {
  63. foreach (var item in list.Data.Where(item => !(item.QQorWechat + item.Email).Contains("匿名")))
  64. {
  65. item.QQorWechat = item.QQorWechat?.Mask();
  66. item.Email = item.Email?.MaskEmail();
  67. }
  68. }
  69. return Ok(list);
  70. }
  71. return Ok();
  72. }
  73. /// <summary>
  74. /// 关于
  75. /// </summary>
  76. /// <returns></returns>
  77. [Route("about"), ResponseCache(Duration = 600, VaryByHeader = "Cookie")]
  78. public async Task<ActionResult> About()
  79. {
  80. var text = await new FileInfo(Path.Combine(HostEnvironment.WebRootPath, "template", "about.html")).ShareReadWrite().ReadAllTextAsync(Encoding.UTF8);
  81. return View(model: text);
  82. }
  83. /// <summary>
  84. /// 评论及留言须知
  85. /// </summary>
  86. /// <returns></returns>
  87. [Route("agreement"), ResponseCache(Duration = 600, VaryByHeader = "Cookie")]
  88. public async Task<ActionResult> Agreement()
  89. {
  90. var text = await new FileInfo(Path.Combine(HostEnvironment.WebRootPath, "template", "agreement.html")).ShareReadWrite().ReadAllTextAsync(Encoding.UTF8);
  91. return View(model: text);
  92. }
  93. /// <summary>
  94. /// 声明
  95. /// </summary>
  96. /// <returns></returns>
  97. [Route("disclaimer"), ResponseCache(Duration = 600, VaryByHeader = "Cookie")]
  98. public async Task<ActionResult> Disclaimer()
  99. {
  100. var text = await new FileInfo(Path.Combine(HostEnvironment.WebRootPath, "template", "disclaimer.html")).ShareReadWrite().ReadAllTextAsync(Encoding.UTF8);
  101. return View(model: text);
  102. }
  103. /// <summary>
  104. /// 创建页面
  105. /// </summary>
  106. /// <param name="model"></param>
  107. /// <returns></returns>
  108. [MyAuthorize]
  109. public async Task<ActionResult> Write([FromBodyOrDefault] Misc model, CancellationToken cancellationToken)
  110. {
  111. model.Content = await ImagebedClient.ReplaceImgSrc(await model.Content.Trim().ClearImgAttributes(), cancellationToken);
  112. var e = MiscService.AddEntitySaved(model);
  113. return e != null ? ResultData(null, message: "发布成功") : ResultData(null, false, "发布失败");
  114. }
  115. /// <summary>
  116. /// 删除页面
  117. /// </summary>
  118. /// <param name="id"></param>
  119. /// <returns></returns>
  120. [MyAuthorize]
  121. public async Task<ActionResult> Delete(int id)
  122. {
  123. bool b = await MiscService.DeleteByIdAsync(id) > 0;
  124. return ResultData(null, b, b ? "删除成功" : "删除失败");
  125. }
  126. /// <summary>
  127. /// 编辑页面
  128. /// </summary>
  129. /// <param name="misc"></param>
  130. /// <returns></returns>
  131. [MyAuthorize]
  132. public async Task<ActionResult> Edit([FromBodyOrDefault] Misc misc, CancellationToken cancellationToken)
  133. {
  134. var entity = await MiscService.GetByIdAsync(misc.Id) ?? throw new NotFoundException("杂项页未找到");
  135. entity.ModifyDate = DateTime.Now;
  136. entity.Title = misc.Title;
  137. entity.Content = await ImagebedClient.ReplaceImgSrc(await misc.Content.ClearImgAttributes(), cancellationToken);
  138. bool b = await MiscService.SaveChangesAsync() > 0;
  139. return ResultData(null, b, b ? "修改成功" : "修改失败");
  140. }
  141. /// <summary>
  142. /// 分页数据
  143. /// </summary>
  144. /// <param name="page"></param>
  145. /// <param name="size"></param>
  146. /// <returns></returns>
  147. [MyAuthorize]
  148. public ActionResult GetPageData(int page = 1, int size = 10)
  149. {
  150. var list = MiscService.GetPages(page, size, n => true, n => n.ModifyDate, false);
  151. foreach (var item in list.Data)
  152. {
  153. item.ModifyDate = item.ModifyDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  154. item.PostDate = item.PostDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  155. }
  156. return Ok(list);
  157. }
  158. /// <summary>
  159. /// 详情
  160. /// </summary>
  161. /// <param name="id"></param>
  162. /// <returns></returns>
  163. [MyAuthorize]
  164. public async Task<ActionResult> Get(int id)
  165. {
  166. var misc = await MiscService.GetByIdAsync(id);
  167. if (misc != null)
  168. {
  169. misc.ModifyDate = misc.ModifyDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  170. misc.PostDate = misc.PostDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone));
  171. }
  172. return ResultData(misc.Mapper<MiscDto>());
  173. }
  174. }