浏览代码

三级分类

懒得勤快 3 年之前
父节点
当前提交
8142ab8399

+ 0 - 2
src/Masuit.MyBlogs.Core/Controllers/CategoryController.cs

@@ -25,7 +25,6 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// 获取所有分类
         /// </summary>
         /// <returns></returns>
-        [ResponseCache(Duration = 600)]
         public ActionResult GetCategories()
         {
             var list = CategoryService.GetQuery<string, CategoryDto>(c => c.Status == Status.Available && c.ParentId == null, c => c.Name).NotCacheable().ToList();
@@ -37,7 +36,6 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// </summary>
         /// <param name="id"></param>
         /// <returns></returns>
-        [ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "id" })]
         public async Task<ActionResult> Get(int id)
         {
             var model = await CategoryService.GetByIdAsync(id) ?? throw new NotFoundException("分类不存在!");

+ 1 - 1
src/Masuit.MyBlogs.Core/Controllers/PostController.cs

@@ -622,7 +622,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             Expression<Func<Post, bool>> where = p => true;
             if (cid.HasValue)
             {
-                where = where.And(p => p.CategoryId == cid.Value || p.Category.ParentId == cid.Value);
+                where = where.And(p => p.CategoryId == cid.Value || p.Category.ParentId == cid.Value || p.Category.Parent.ParentId == cid.Value);
             }
 
             if (!string.IsNullOrEmpty(kw))

+ 2 - 2
src/Masuit.MyBlogs.Core/Infrastructure/Services/AdvertisementService.cs

@@ -59,8 +59,8 @@ namespace Masuit.MyBlogs.Core.Infrastructure.Services
                 where = where.And(a => a.RegionMode == RegionLimitMode.All || (a.RegionMode == RegionLimitMode.AllowRegion ? Regex.IsMatch(location, a.Regions) : !Regex.IsMatch(location, a.Regions)));
                 if (cid.HasValue)
                 {
-                    var pids = CategoryRepository.GetQuery(c => c.Id == cid).Select(c => c.ParentId).Cacheable().ToArray();
-                    var scid = pids.Append(cid.Value).Join("|");
+                    var pids = CategoryRepository.GetQuery(c => c.Id == cid).Select(c => new[] { c.ParentId, c.Parent.ParentId }).Cacheable().ToArray();
+                    var scid = pids.SelectMany(i => i).Where(i => i.HasValue).Append(cid.Value).Join("|");
                     if (Any(a => Regex.IsMatch(a.CategoryIds, scid)))
                     {
                         where = where.And(a => Regex.IsMatch(a.CategoryIds, scid) || string.IsNullOrEmpty(a.CategoryIds));

+ 1 - 1
src/Masuit.MyBlogs.Core/Views/Error/Index.cshtml

@@ -85,7 +85,7 @@
                 Random r = new Random();
                 string imgPath = $"/Assets/images/404/404{r.StrictNext(Directory.GetFiles(_hostEnvironment.WebRootPath + "/Assets/images/404").Length) + 1}.jpg";
             }
-            <img class="img img-responsive img-thumbnail" width="100%" src="@imgPath" alt="@ViewBag.Keywords" />
+            <img class="img img-responsive img-thumbnail" width="100%" src="@imgPath" alt="@ViewBag.Keywords" decoding="async"/>
             <h3 class="margintop20">
                 <em>
                         <span class="STYLE1">404 Error&nbsp;</span>

+ 1 - 1
src/Masuit.MyBlogs.Core/Views/Error/ServiceUnavailable.cshtml

@@ -85,7 +85,7 @@
                 Random r = new Random();
                 string imgPath = $"/Assets/images/503/503{r.StrictNext(Directory.GetFiles(_hostEnvironment.WebRootPath + "/Assets/images/503").Length) + 1}.jpg";
             }
-            <img class="img img-responsive img-thumbnail" width="100%" src="@imgPath" alt="@ViewBag.Keywords" />
+            <img class="img img-responsive img-thumbnail" width="100%" src="@imgPath" alt="@ViewBag.Keywords" decoding="async"/>
             <h3 class="margintop20">
                 <em>
                     <span class="STYLE1">503 Service Unavailable&nbsp;</span>

+ 114 - 83
src/Masuit.MyBlogs.Core/Views/Home/Category.cshtml

@@ -6,100 +6,131 @@
 @using Masuit.MyBlogs.Core.Infrastructure.Services.Interface
 @using Masuit.MyBlogs.Core.Models.Enum
 @using EFCoreSecondLevelCacheInterceptor
+@using Masuit.Tools.Models
 @model Masuit.MyBlogs.Core.Models.ViewModel.HomePageViewModel
 @inject ICategoryService CategoryService
 @{
-	Category cat = ViewBag.Category;
-	ViewBag.Title = "分类_" + cat.Name;
-	Layout = "~/Views/Shared/_Layout.cshtml";
-	Random r = new Random();
-	var alllist = CategoryService.GetQuery(c => c.Status == Status.Available && c.ParentId == null&&c.Post.Count>0, c => c.Name).Select(c => new{c.Id,c.Name}).Cacheable().ToList();
-	var children = cat.ParentId==null?cat.Children:cat.Parent.Children.Where(c => c.Status == Status.Available && c.Post.Count>0).OrderBy(c => c.Id).ToList();
+    Category cat = ViewBag.Category;
+    ViewBag.Title = "分类_" + cat.Path();
+    Layout = "~/Views/Shared/_Layout.cshtml";
+    var level = cat.Level();
+    List<Category> children2 = new List<Category>();
+    List<Category> children3 = new List<Category>();
+    var parentId = cat.ParentId;
+    switch (level) {
+        case 1:
+            children2.AddRange(cat.Children.Where(c => c.Status == Status.Available).OrderBy(c => c.Id).ToList());
+            break;
+        case 2:
+            children2.AddRange(CategoryService.GetQuery(c => c.Status == Status.Available && c.ParentId == parentId, c => c.Name).Cacheable().ToList());
+            children3.AddRange(cat.Children.Where(c => c.Status == Status.Available).OrderBy(c => c.Id).ToList());
+            break;
+        case 3:
+            var topid = cat.Parent.ParentId;
+            children2.AddRange(CategoryService.GetQuery(c => c.Status == Status.Available && c.ParentId == topid, c => c.Name).Cacheable().ToList());
+            children3.AddRange(CategoryService.GetQuery(c => c.Status == Status.Available && c.ParentId == parentId, c => c.Name).Cacheable().ToList());
+            break;
+    }
+    var alllist = CategoryService.GetQuery(c => c.Status == Status.Available && c.ParentId == null, c => c.Name).Select(c => new{c.Id,c.Name}).Cacheable().ToList();
 }
 <style>
-	.bg-title {
-		height: 50vh;
-		max-height: 400px;
-		position: relative;
-		background: url(/Content/images/@(r.Next(1,9)).jpg) no-repeat center;
-		background-size:cover;
-		background-attachment: fixed;
-		margin-bottom: 10px;
-	}
-	.header-content {
-		position: absolute;
-		left: 50%;
-		top: 50%;
-		transform: translate(-50%,-50%);
-	}
-	.header-content .divider {
-		width: inherit;
-		height: 20px;
-		margin-top: 20px;
-		border-top: 3px solid rebeccapurple;
-	}
+    .bg-title {
+        height: 50vh;
+        max-height: 400px;
+        position: relative;
+        background: url(/Content/images/@(new Random().Next(1,9)).jpg) no-repeat center;
+        background-size:cover;
+        background-attachment: fixed;
+        margin-bottom: 10px;
+    }
+    .header-content {
+        position: absolute;
+        left: 50%;
+        top: 50%;
+        transform: translate(-50%,-50%);
+    }
+    .header-content .divider {
+        width: inherit;
+        height: 20px;
+        margin-top: 20px;
+        border-top: 3px solid rebeccapurple;
+    }
 </style>
 <div class="container">
-	<ol class="cd-breadcrumb triangle">
-		<li><a asp-controller="Home" asp-action="Index">首页</a></li>
-		<li><a asp-controller="Home" asp-action="Post">文章列表</a></li>
-		@if(cat.ParentId>0) {
-			<li><a asp-controller="Home" asp-action="Category" asp-route-id="@cat.ParentId">@cat.Parent.Name</a></li>
-		}
-		<li class="current"><em>@cat.Name</em></li>
-	</ol>
+    <ol class="cd-breadcrumb triangle">
+        <li><a asp-controller="Home" asp-action="Index">首页</a></li>
+        <li><a asp-controller="Home" asp-action="Post">文章列表</a></li>
+        @if(cat.ParentId>0) {
+            if(cat.Parent.ParentId>0) {
+                <li><a asp-controller="Home" asp-action="Category" asp-route-id="@cat.Parent.ParentId">@cat.Parent.Parent.Name</a></li>
+            }
+            <li><a asp-controller="Home" asp-action="Category" asp-route-id="@cat.ParentId">@cat.Parent.Name</a></li>
+        }
+        <li class="current"><em>@cat.Name</em></li>
+    </ol>
 </div>
 <div class="bg-title">
-	<div class="header-content text-center">
-		<h1 class="size48">
-			@if(cat.ParentId>0) {
-			    @(cat.Parent.Name+" / ")
-			}
-			@cat.Name
-			@if(CommonHelper.SystemSettings.GetOrAdd("EnableRss", "true") == "true"){
-				<a class="btn btn-lg btn-success" asp-controller="Subscribe" asp-action="CategoryRss" asp-route-id="@cat.Id" target="_blank">
-					<i class="icon-rss4"></i>
-				</a>
-			}
-			<a class="btn btn-lg btn-danger" onclick='blockCategory("@cat.Id","@cat.Name")'>
-				<i class="icon-blocked"></i>
-			</a>
-		</h1>
-		<div class="divider"></div>
-		<p class="size24">
-			@cat.Description
-		</p>
-	</div>
+    <div class="header-content text-center">
+        <h1 class="size48">
+            @cat.Path()
+            @if(CommonHelper.SystemSettings.GetOrAdd("EnableRss", "true") == "true"){
+                <a class="btn btn-lg btn-success" asp-controller="Subscribe" asp-action="CategoryRss" asp-route-id="@cat.Id" target="_blank">
+                    <i class="icon-rss4"></i>
+                </a>
+            }
+            <a class="btn btn-lg btn-danger" onclick='blockCategory("@cat.Id","@cat.Name")'>
+                <i class="icon-blocked"></i>
+            </a>
+        </h1>
+        <div class="divider"></div>
+        <p class="size24">
+            @cat.Description
+        </p>
+    </div>
 </div>
 <div class="container">
-	<p>
-		一级分类:
-		@foreach (var category in alllist)
-		{
-			if (cat.Id==category.Id||cat.ParentId==category.Id) {
-				<a class="text-red" asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
-			} else {
-				<a asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
-			}
-		}
-	</p>
-	@if(children.Count>0) {
-		<hr />
-		<p>
-			<text>二级分类:</text>
-			@foreach (var category in children)
-			{
-				if (cat.Id==category.Id) {
-					<a class="text-red" asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
-				} else {
-					<a asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
-				}
-			}
-		</p>
-	}
-	<hr />
+    <p>
+        一级分类:
+        @foreach (var category in alllist)
+        {
+            if (cat.Id==category.Id||cat.ParentId==category.Id|| (level==3&&cat.Parent.ParentId == category.Id)) {
+                <a class="text-red" asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
+            } else {
+                <a asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
+            }
+        }
+    </p>
+    @if(children2.Count>0) {
+        <hr />
+        <p>
+            <text>二级分类:</text>
+            @foreach (var category in children2)
+            {
+                if (cat.Id==category.Id||cat.ParentId==category.Id) {
+                    <a class="text-red" asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
+                } else {
+                    <a asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
+                }
+            }
+        </p>
+    }
+    @if(children3.Count>0) {
+        <hr />
+        <p>
+            <text>三级分类:</text>
+            @foreach (var category in children3)
+            {
+                if (cat.Id==category.Id) {
+                    <a class="text-red" asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
+                } else {
+                    <a asp-controller="Home" asp-action="Category" asp-route-id="@category.Id">@category.Name</a><text> | </text>
+                }
+            }
+        </p>
+    }
+    <hr />
 </div>
 @{
-	var user = Context.Session.Get<UserInfoDto>(SessionKey.UserInfo) ?? new UserInfoDto();
-	await Html.RenderPartialAsync(user.IsAdmin ? "_MainContainer_Admin" : "_MainContainer", Model);
+    var user = Context.Session.Get<UserInfoDto>(SessionKey.UserInfo) ?? new UserInfoDto();
+    await Html.RenderPartialAsync(user.IsAdmin ? "_MainContainer_Admin" : "_MainContainer", Model);
 }

+ 1 - 1
src/Masuit.MyBlogs.Core/Views/Home/Index.cshtml

@@ -37,7 +37,7 @@
                 Random r = new Random();
                 <div class="item @(i == 0 ? "active" : "")" class="img-responsive" id="@Stopwatch.GetTimestamp()">
                     <a asp-controller="Advertisement" asp-action="Redirect" asp-route-id="@p.Id" target="_blank" data-animation="animated @ani[r.Next(ani.Length)]">
-                        <img src="@p.ImageUrl" title="@p.Title" alt="@p.Description" />
+                        <img src="@p.ImageUrl" title="@p.Title" alt="@p.Description" decoding="async"/>
                     </a>
                 </div>
             }

+ 3 - 0
src/Masuit.MyBlogs.Core/Views/Post/CompareVersion.cshtml

@@ -28,6 +28,9 @@
 		<li><a asp-controller="Home" asp-action="Index">首页</a></li>
 		<li><a asp-controller="Home" asp-action="Post">文章列表</a></li>
 		@if(Model[0].Category.ParentId>0) {
+		    if(Model[0].Category.Parent.ParentId>0) {
+			    <li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model[0].Category.Parent.ParentId">@Model[0].Category.Parent.Parent.Name</a></li>
+		    }
 			<li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model[0].Category.ParentId">@Model[0].Category.Parent.Name</a></li>
 		}
 		<li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model[0].CategoryId">@Model[0].Category.Name</a></li>

+ 3 - 0
src/Masuit.MyBlogs.Core/Views/Post/Details.cshtml

@@ -34,6 +34,9 @@
 		<li><a asp-controller="Home" asp-action="Index">首页</a></li>
 		<li><a asp-controller="Home" asp-action="Post">文章列表</a></li>
 		@if(Model.Category.ParentId>0) {
+		    if(Model.Category.Parent.ParentId>0) {
+			    <li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.Category.Parent.ParentId">@Model.Category.Parent.Parent.Name</a></li>
+		    }
 			<li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.Category.ParentId">@Model.Category.Parent.Name</a></li>
 		}
 		<li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.CategoryId">@Model.Category.Name</a></li>

+ 3 - 0
src/Masuit.MyBlogs.Core/Views/Post/Details_Admin.cshtml

@@ -34,6 +34,9 @@
 		<li><a asp-controller="Home" asp-action="Index">首页</a></li>
 		<li><a asp-controller="Home" asp-action="Post">文章列表</a></li>
 		@if(Model.Category.ParentId>0) {
+		    if(Model.Category.Parent.ParentId>0) {
+			    <li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.Category.Parent.ParentId">@Model.Category.Parent.Parent.Name</a></li>
+		    }
 			<li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.Category.ParentId">@Model.Category.Parent.Name</a></li>
 		}
 		<li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.CategoryId">@Model.Category.Name</a></li>

+ 3 - 0
src/Masuit.MyBlogs.Core/Views/Post/History.cshtml

@@ -12,6 +12,9 @@
 		<li><a asp-controller="Home" asp-action="Index">首页</a></li>
 		<li><a asp-controller="Home" asp-action="Post">文章列表</a></li>
 		@if(post.Category.ParentId>0) {
+		    if(post.Category.Parent.ParentId>0) {
+			    <li><a asp-controller="Home" asp-action="Category" asp-route-id="@post.Category.Parent.ParentId">@post.Category.Parent.Parent.Name</a></li>
+		    }
 			<li><a asp-controller="Home" asp-action="Category" asp-route-id="@post.Category.ParentId">@post.Category.Parent.Name</a></li>
 		}
 		<li><a asp-controller="Home" asp-action="Category" asp-route-id="@post.CategoryId">@post.Category.Name</a></li>

+ 3 - 0
src/Masuit.MyBlogs.Core/Views/Post/HistoryVersion.cshtml

@@ -29,6 +29,9 @@
 		<li><a asp-controller="Home" asp-action="Index">首页</a></li>
 		<li><a asp-controller="Home" asp-action="Post">文章列表</a></li>
 		@if(Model.Category.ParentId>0) {
+		    if(Model.Category.Parent.ParentId>0) {
+			    <li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.Category.Parent.ParentId">@Model.Category.Parent.Parent.Name</a></li>
+		    }
 			<li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.Category.ParentId">@Model.Category.Parent.Name</a></li>
 		}
 		<li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.CategoryId">@Model.Category.Name</a></li>

+ 3 - 0
src/Masuit.MyBlogs.Core/Views/Post/HistoryVersion_Admin.cshtml

@@ -26,6 +26,9 @@
 		<li><a asp-controller="Home" asp-action="Index">首页</a></li>
 		<li><a asp-controller="Home" asp-action="Post">文章列表</a></li>
 		@if(Model.Category.ParentId>0) {
+		    if(Model.Category.Parent.ParentId>0) {
+			    <li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.Category.Parent.ParentId">@Model.Category.Parent.Parent.Name</a></li>
+		    }
 			<li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.Category.ParentId">@Model.Category.Parent.Name</a></li>
 		}
 		<li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.CategoryId">@Model.Category.Name</a></li>

+ 3 - 0
src/Masuit.MyBlogs.Core/Views/Post/PushMerge.cshtml

@@ -10,6 +10,9 @@
 		<li><a asp-controller="Home" asp-action="Index">首页</a></li>
 		<li><a asp-controller="Home" asp-action="Post">文章列表</a></li>
 		@if(Model.Category.ParentId>0) {
+		    if(Model.Category.Parent.ParentId>0) {
+			    <li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.Category.Parent.ParentId">@Model.Category.Parent.Parent.Name</a></li>
+		    }
 			<li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.Category.ParentId">@Model.Category.Parent.Name</a></li>
 		}
 		<li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.CategoryId">@Model.Category.Name</a></li>

+ 3 - 0
src/Masuit.MyBlogs.Core/Views/Post/RepushMerge.cshtml

@@ -10,6 +10,9 @@
 		<li><a asp-controller="Home" asp-action="Index">首页</a></li>
 		<li><a asp-controller="Home" asp-action="Post">文章列表</a></li>
 		@if(Model.Post.Category.ParentId>0) {
+		    if(Model.Post.Category.Parent.ParentId>0) {
+			    <li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.Post.Category.Parent.ParentId">@Model.Post.Category.Parent.Parent.Name</a></li>
+		    }
 			<li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.Post.Category.ParentId">@Model.Post.Category.Parent.Name</a></li>
 		}
 		<li><a asp-controller="Home" asp-action="Category" asp-route-id="@Model.Post.CategoryId">@Model.Post.Category.Name</a></li>

+ 1 - 1
src/Masuit.MyBlogs.Core/Views/Shared/_ArticleListAdvertisement.cshtml

@@ -10,7 +10,7 @@
         <a asp-controller="Advertisement" asp-action="Redirect" asp-route-id="@Model.Id" target="_blank" id="@Stopwatch.GetTimestamp()">
             <div class="row padding-bot10">
                 <div class="col-sm-2 hidden-xs paddingright-clear">
-                    <img class="img-thumbnail img-responsive thumb" src="@Model.ThumbImgUrl" alt="@Model.Title" title="@Model.Title">
+                    <img class="img-thumbnail img-responsive thumb" src="@Model.ThumbImgUrl" alt="@Model.Title" title="@Model.Title" decoding="async">
                 </div>
                 <div class="col-sm-10 col-xs-12">
                     <p>

+ 1 - 1
src/Masuit.MyBlogs.Core/Views/Shared/_Aside.cshtml

@@ -69,7 +69,7 @@
 				</div>
 				<div class="panel-body" id="@Stopwatch.GetTimestamp()">
 					<a asp-controller="Advertisement" asp-action="Redirect" asp-route-id="@ad.Id" target="_blank" id="@Stopwatch.GetTimestamp()">
-						<img class="img img-responsive" src="@ad.ThumbImgUrl" alt="@ad.Title" title="@ad.Title" />
+						<img class="img img-responsive" src="@ad.ThumbImgUrl" alt="@ad.Title" title="@ad.Title" decoding="async"/>
 					</a>
 				</div>
 			</article>

+ 3 - 3
src/Masuit.MyBlogs.Core/Views/Shared/_Layout.cshtml

@@ -109,7 +109,7 @@
     </div>
     <div class="header">
         <header class="cd-main-header">
-            <a class="cd-logo" href="/"><img src="@CommonHelper.SystemSettings["logo"]" alt="@CommonHelper.SystemSettings["Title"]"><h1 class="slogan">@CommonHelper.SystemSettings["Slogan"]</h1></a>
+            <a class="cd-logo" href="/"><img src="@CommonHelper.SystemSettings["logo"]" alt="@CommonHelper.SystemSettings["Title"]" decoding="async"><h1 class="slogan">@CommonHelper.SystemSettings["Slogan"]</h1></a>
             <ul class="cd-header-buttons">
                 <li><a class="cd-search-trigger" href="#cd-search">搜索<span></span></a></li>
                 <li><a class="cd-nav-trigger" href="#cd-primary-nav">菜单<span></span></a></li>
@@ -218,7 +218,7 @@
                                             {
                                                 <li>
                                                     <a class="cd-nav-item" href="@mm.Url" target="_blank">
-                                                        <img src="@mm.Icon" alt="@mm.Name">
+                                                        <img src="@mm.Icon" alt="@mm.Name" decoding="async">
                                                         <h3>@mm.Name</h3>
                                                     </a>
                                                 </li>
@@ -227,7 +227,7 @@
                                             {
                                                 <li>
                                                     <a class="cd-nav-item" href="@mm.Url">
-                                                        <img src="@mm.Icon" alt="@mm.Name">
+                                                        <img src="@mm.Icon" alt="@mm.Name" decoding="async">
                                                         <h3>@mm.Name</h3>
                                                     </a>
                                                 </li>