diff --git a/src/Masuit.MyBlogs.Core/Common/CommonHelper.cs b/src/Masuit.MyBlogs.Core/Common/CommonHelper.cs index 653f7d6c..0f3a566c 100644 --- a/src/Masuit.MyBlogs.Core/Common/CommonHelper.cs +++ b/src/Masuit.MyBlogs.Core/Common/CommonHelper.cs @@ -293,6 +293,7 @@ public static async Task ReplaceImgAttribute(this string html, string ti string src = node.Attributes["src"].Value; node.RemoveAttribute("src"); node.SetAttribute("data-src", src); + node.SetAttribute("decoding", "async"); node.SetAttribute("class", node.Attributes["class"]?.Value + " lazyload"); node.SetAttribute("loading", "lazy"); node.SetAttribute("alt", SystemSettings["Title"]); diff --git a/src/Masuit.MyBlogs.Core/Controllers/BaseController.cs b/src/Masuit.MyBlogs.Core/Controllers/BaseController.cs index f87ac551..e16969ac 100644 --- a/src/Masuit.MyBlogs.Core/Controllers/BaseController.cs +++ b/src/Masuit.MyBlogs.Core/Controllers/BaseController.cs @@ -34,8 +34,6 @@ public class BaseController : Controller { public IUserInfoService UserInfoService { get; set; } - public IMenuService MenuService { get; set; } - public ILinksService LinksService { get; set; } public IAdvertisementService AdsService { get; set; } @@ -104,9 +102,7 @@ protected string ReplaceVariables(string text) return text; } - /// 在调用操作方法前调用。 - /// 有关当前请求和操作的信息。 - public override void OnActionExecuting(ActionExecutingContext filterContext) + public override Task OnActionExecutionAsync(ActionExecutingContext filterContext, ActionExecutionDelegate next) { ViewBag.Desc = CommonHelper.SystemSettings["Description"]; var user = filterContext.HttpContext.Session.Get(SessionKey.UserInfo); @@ -117,11 +113,13 @@ public override void OnActionExecuting(ActionExecutingContext filterContext) if (CommonHelper.SystemSettings.GetOrAdd("CloseSite", "false") == "true" && user?.IsAdmin != true) { filterContext.Result = RedirectToAction("ComingSoon", "Error"); + return Task.CompletedTask; } if (Request.Method == HttpMethods.Post && !Request.Path.Value.Contains("get", StringComparison.InvariantCultureIgnoreCase) && CommonHelper.SystemSettings.GetOrAdd("DataReadonly", "false") == "true" && !filterContext.Filters.Any(m => m.ToString().Contains(nameof(MyAuthorizeAttribute)))) { filterContext.Result = ResultData("网站当前处于数据写保护状态,无法提交任何数据,如有疑问请联系网站管理员!", false, "网站当前处于数据写保护状态,无法提交任何数据,如有疑问请联系网站管理员!", user != null, HttpStatusCode.BadRequest); + return Task.CompletedTask; } if (user == null && Request.Cookies.ContainsKey("username") && Request.Cookies.ContainsKey("password")) //执行自动登录 @@ -145,28 +143,14 @@ public override void OnActionExecuting(ActionExecutingContext filterContext) } } - if (ModelState.IsValid) return; + if (ModelState.IsValid) return next(); var errmsgs = ModelState.SelectMany(kv => kv.Value.Errors.Select(e => e.ErrorMessage)).Select((s, i) => $"{i + 1}. {s}").ToList(); filterContext.Result = true switch { _ when Request.HasJsonContentType() || Request.Method == HttpMethods.Post => ResultData(errmsgs, false, "数据校验失败,错误信息:" + errmsgs.Join(" | "), user != null, HttpStatusCode.BadRequest), _ => base.BadRequest("参数错误:" + errmsgs.Join(" | ")) }; - } - - /// 在调用操作方法后调用。 - /// 有关当前请求和操作的信息。 - public override void OnActionExecuted(ActionExecutedContext filterContext) - { - if (filterContext.Result is ViewResult) - { - ViewBag.menus = MenuService.GetQueryFromCache(m => m.ParentId == null && m.Status == Status.Available).OrderBy(m => m.Sort).ToList(); //菜单 - var model = new PageFootViewModel //页脚 - { - Links = LinksService.GetQuery(l => l.Status == Status.Available).OrderByDescending(l => l.Recommend).ThenByDescending(l => l.Loopbacks.Count).Take(30).ProjectTo(MapperConfig).Cacheable().ToList() - }; - ViewBag.Footer = model; - } + return Task.CompletedTask; } /// diff --git a/src/Masuit.MyBlogs.Core/Controllers/Drive/DriveController.cs b/src/Masuit.MyBlogs.Core/Controllers/Drive/DriveController.cs index 3dfc8ca6..8fe90733 100644 --- a/src/Masuit.MyBlogs.Core/Controllers/Drive/DriveController.cs +++ b/src/Masuit.MyBlogs.Core/Controllers/Drive/DriveController.cs @@ -1,5 +1,7 @@ -using Masuit.MyBlogs.Core.Extensions.Firewall; +using Masuit.MyBlogs.Core.Common; +using Masuit.MyBlogs.Core.Extensions.Firewall; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; namespace Masuit.MyBlogs.Core.Controllers.Drive { @@ -11,5 +13,15 @@ public IActionResult Index() { return View(); } + + public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) + { + if (CommonHelper.SystemSettings.GetOrAdd("CloseSite", "false") == "true") + { + context.Result = RedirectToAction("ComingSoon", "Error"); + return Task.CompletedTask; + } + return next(); + } } -} \ No newline at end of file +} diff --git a/src/Masuit.MyBlogs.Core/Controllers/Drive/SitesController.cs b/src/Masuit.MyBlogs.Core/Controllers/Drive/SitesController.cs index 5c893caf..e604c7ca 100644 --- a/src/Masuit.MyBlogs.Core/Controllers/Drive/SitesController.cs +++ b/src/Masuit.MyBlogs.Core/Controllers/Drive/SitesController.cs @@ -1,3 +1,4 @@ +using Masuit.MyBlogs.Core.Common; using Masuit.MyBlogs.Core.Extensions.DriveHelpers; using Masuit.MyBlogs.Core.Extensions.Firewall; using Masuit.MyBlogs.Core.Infrastructure.Drive; @@ -6,6 +7,7 @@ using Masuit.MyBlogs.Core.Models.ViewModel; using Masuit.Tools.Core.Net; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; @@ -29,8 +31,6 @@ public SitesController(IDriveAccountService siteService, IDriveService driveServ this._setting = setting; } - #region Actions - /// /// 返回所有sites /// @@ -250,6 +250,14 @@ public async Task GetUploadUrl(string siteName, string path, stri } } - #endregion Actions + public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) + { + if (CommonHelper.SystemSettings.GetOrAdd("CloseSite", "false") == "true") + { + context.Result = new BadRequestObjectResult(new { code = 403 }); + return Task.CompletedTask; + } + return next(); + } } } diff --git a/src/Masuit.MyBlogs.Core/Controllers/Drive/UserController.cs b/src/Masuit.MyBlogs.Core/Controllers/Drive/UserController.cs index 87e6b5c6..08b461da 100644 --- a/src/Masuit.MyBlogs.Core/Controllers/Drive/UserController.cs +++ b/src/Masuit.MyBlogs.Core/Controllers/Drive/UserController.cs @@ -1,5 +1,4 @@ using Hangfire; -using Masuit.MyBlogs.Core.Common; using Masuit.MyBlogs.Core.Configs; using Masuit.MyBlogs.Core.Extensions.Hangfire; using Masuit.MyBlogs.Core.Infrastructure.Services.Interface; diff --git a/src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj b/src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj index 27afbe64..7b323bdc 100644 --- a/src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj +++ b/src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj @@ -63,7 +63,7 @@ - + diff --git a/src/Masuit.MyBlogs.Core/Views/Home/Index.cshtml b/src/Masuit.MyBlogs.Core/Views/Home/Index.cshtml index 82e60b97..ebd34621 100644 --- a/src/Masuit.MyBlogs.Core/Views/Home/Index.cshtml +++ b/src/Masuit.MyBlogs.Core/Views/Home/Index.cshtml @@ -61,7 +61,7 @@

网站最新公告

- @Html.Raw(Model.Notices[0].Content.Replace("img src=", $"img class='lazyload' title='{CommonHelper.SystemSettings["Title"]}' alt='{CommonHelper.SystemSettings["Title"]}' loading='lazy' data-src=")) + @Html.Raw(Model.Notices[0].Content.Replace("img src=", $"img class='lazyload' title='{CommonHelper.SystemSettings["Title"]}' alt='{CommonHelper.SystemSettings["Title"]}' decoding='async' loading='lazy' data-src=")) } @if (shares.Any()) diff --git a/src/Masuit.MyBlogs.Core/Views/Shared/_ArticleListItem.cshtml b/src/Masuit.MyBlogs.Core/Views/Shared/_ArticleListItem.cshtml index 9e70dff8..e5ae51ac 100644 --- a/src/Masuit.MyBlogs.Core/Views/Shared/_ArticleListItem.cshtml +++ b/src/Masuit.MyBlogs.Core/Views/Shared/_ArticleListItem.cshtml @@ -31,7 +31,7 @@ if (!string.IsNullOrEmpty(imgSrc)) { } } diff --git a/src/Masuit.MyBlogs.Core/Views/Shared/_ArticleListItem_Admin.cshtml b/src/Masuit.MyBlogs.Core/Views/Shared/_ArticleListItem_Admin.cshtml index 4f767039..67f0e51a 100644 --- a/src/Masuit.MyBlogs.Core/Views/Shared/_ArticleListItem_Admin.cshtml +++ b/src/Masuit.MyBlogs.Core/Views/Shared/_ArticleListItem_Admin.cshtml @@ -32,7 +32,7 @@ if (!string.IsNullOrEmpty(imgSrc)) { } } diff --git a/src/Masuit.MyBlogs.Core/Views/Shared/_Layout.cshtml b/src/Masuit.MyBlogs.Core/Views/Shared/_Layout.cshtml index 310f7d48..b96232bc 100644 --- a/src/Masuit.MyBlogs.Core/Views/Shared/_Layout.cshtml +++ b/src/Masuit.MyBlogs.Core/Views/Shared/_Layout.cshtml @@ -1,4 +1,7 @@ -@using Masuit.MyBlogs.Core.Common +@inject IMenuService _menuService; +@inject ILinksService _linksService; +@using Masuit.MyBlogs.Core.Common +@using Masuit.MyBlogs.Core.Infrastructure.Services.Interface @using Masuit.MyBlogs.Core.Models.DTO @using Masuit.MyBlogs.Core.Models.Entity @using Masuit.MyBlogs.Core.Models.Enum @@ -6,11 +9,12 @@ @using Masuit.MyBlogs.Core.Views.Shared @using Masuit.Tools @using Masuit.Tools.Core.Net +@using EFCoreSecondLevelCacheInterceptor @{ string[] colors = { "success", "info", "warning", "danger", "default" }; - List menus = ViewBag.menus; - UserInfoDto user = Context.Session.Get(SessionKey.UserInfo) ?? new UserInfoDto(); - PageFootViewModel footer = ViewBag.Footer; + List menus = _menuService.GetQueryFromCache(m => m.ParentId == null && m.Status == Status.Available).OrderBy(m => m.Sort).ToList(); + var user = Context.Session.Get(SessionKey.UserInfo) ?? new UserInfoDto(); + var links = _linksService.GetQuery(l => l.Status == Status.Available).OrderByDescending(l => l.Recommend).ThenByDescending(l => l.Loopbacks.Count).Take(30).Select(e => new{e.Url,e.Name}).Cacheable().ToList(); } @@ -290,7 +294,7 @@ 任务管理器 | } - foreach (var link in footer.Links) + foreach (var link in links) { @link.Name |