using Masuit.MyBlogs.Core.Common; using Masuit.MyBlogs.Core.Extensions; using Masuit.Tools.AspNetCore.ModelBinder; using Microsoft.AspNetCore.Mvc; using System.Text; namespace Masuit.MyBlogs.Core.Controllers; /// /// 杂项页 /// public sealed class MiscController : BaseController { /// /// MiscService /// public IMiscService MiscService { get; set; } public IWebHostEnvironment HostEnvironment { get; set; } public ImagebedClient ImagebedClient { get; set; } /// /// 杂项页 /// /// /// [Route("misc/{id:int}"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id" }, VaryByHeader = "Cookie")] public async Task Index(int id) { var misc = await MiscService.GetFromCacheAsync(m => m.Id == id) ?? throw new NotFoundException("页面未找到"); misc.ModifyDate = misc.ModifyDate.ToTimeZone(HttpContext.Session.Get(SessionKey.TimeZone)); misc.PostDate = misc.PostDate.ToTimeZone(HttpContext.Session.Get(SessionKey.TimeZone)); misc.Content = ReplaceVariables(misc.Content); return View(misc); } /// /// 打赏 /// /// [Route("donate")] public async Task Donate() { var ads = AdsService.GetsByWeightedPrice(2, AdvertiseType.InPage, Request.Location()); if (bool.Parse(CommonHelper.SystemSettings.GetOrAdd("EnableDonate", "true"))) { ViewBag.Ads = ads; var text = await new FileInfo(Path.Combine(HostEnvironment.WebRootPath, "template", "donate.html")).ShareReadWrite().ReadAllTextAsync(Encoding.UTF8); return CurrentUser.IsAdmin ? View("Donate_Admin", text) : View(model: text); } return Redirect(ads.FirstOrDefault()?.Url ?? "/"); } /// /// 打赏列表 /// /// /// /// /// [Route("donatelist")] public ActionResult DonateList([FromServices] IDonateService donateService, int page = 1, int size = 10) { if (bool.Parse(CommonHelper.SystemSettings.GetOrAdd("EnableDonate", "true"))) { var list = donateService.GetPagesFromCache(page, size, d => true, d => d.DonateTime, false); if (!CurrentUser.IsAdmin) { foreach (var item in list.Data.Where(item => !(item.QQorWechat + item.Email).Contains("匿名"))) { item.QQorWechat = item.QQorWechat?.Mask(); item.Email = item.Email?.MaskEmail(); } } return Ok(list); } return Ok(); } /// /// 关于 /// /// [Route("about"), ResponseCache(Duration = 600, VaryByHeader = "Cookie")] public async Task About() { var text = await new FileInfo(Path.Combine(HostEnvironment.WebRootPath, "template", "about.html")).ShareReadWrite().ReadAllTextAsync(Encoding.UTF8); return View(model: text); } /// /// 评论及留言须知 /// /// [Route("agreement"), ResponseCache(Duration = 600, VaryByHeader = "Cookie")] public async Task Agreement() { var text = await new FileInfo(Path.Combine(HostEnvironment.WebRootPath, "template", "agreement.html")).ShareReadWrite().ReadAllTextAsync(Encoding.UTF8); return View(model: text); } /// /// 声明 /// /// [Route("disclaimer"), ResponseCache(Duration = 600, VaryByHeader = "Cookie")] public async Task Disclaimer() { var text = await new FileInfo(Path.Combine(HostEnvironment.WebRootPath, "template", "disclaimer.html")).ShareReadWrite().ReadAllTextAsync(Encoding.UTF8); return View(model: text); } /// /// 创建页面 /// /// /// [MyAuthorize] public async Task Write([FromBodyOrDefault] Misc model, CancellationToken cancellationToken) { model.Content = await ImagebedClient.ReplaceImgSrc(await model.Content.Trim().ClearImgAttributes(), cancellationToken); var e = MiscService.AddEntitySaved(model); return e != null ? ResultData(null, message: "发布成功") : ResultData(null, false, "发布失败"); } /// /// 删除页面 /// /// /// [MyAuthorize] public async Task Delete(int id) { bool b = await MiscService.DeleteByIdAsync(id) > 0; return ResultData(null, b, b ? "删除成功" : "删除失败"); } /// /// 编辑页面 /// /// /// [MyAuthorize] public async Task Edit([FromBodyOrDefault] Misc misc, CancellationToken cancellationToken) { var entity = await MiscService.GetByIdAsync(misc.Id) ?? throw new NotFoundException("杂项页未找到"); entity.ModifyDate = DateTime.Now; entity.Title = misc.Title; entity.Content = await ImagebedClient.ReplaceImgSrc(await misc.Content.ClearImgAttributes(), cancellationToken); bool b = await MiscService.SaveChangesAsync() > 0; return ResultData(null, b, b ? "修改成功" : "修改失败"); } /// /// 分页数据 /// /// /// /// [MyAuthorize] public ActionResult GetPageData(int page = 1, int size = 10) { var list = MiscService.GetPages(page, size, n => true, n => n.ModifyDate, false); foreach (var item in list.Data) { item.ModifyDate = item.ModifyDate.ToTimeZone(HttpContext.Session.Get(SessionKey.TimeZone)); item.PostDate = item.PostDate.ToTimeZone(HttpContext.Session.Get(SessionKey.TimeZone)); } return Ok(list); } /// /// 详情 /// /// /// [MyAuthorize] public async Task Get(int id) { var misc = await MiscService.GetByIdAsync(id); if (misc != null) { misc.ModifyDate = misc.ModifyDate.ToTimeZone(HttpContext.Session.Get(SessionKey.TimeZone)); misc.PostDate = misc.PostDate.ToTimeZone(HttpContext.Session.Get(SessionKey.TimeZone)); } return ResultData(misc.Mapper()); } }