MiscController.cs 7.8 KB

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