Browse Source

分页组件优化

懒得勤快 5 years ago
parent
commit
a350c8dcb6

+ 5 - 0
src/Masuit.MyBlogs.Core/Common/CommonHelper.cs

@@ -299,5 +299,10 @@ namespace Masuit.MyBlogs.Core.Common
 
             return summary;
         }
+
+        public static string TrimQuery(this string path)
+        {
+            return path.Split('&').Where(s => s.Split('=', StringSplitOptions.RemoveEmptyEntries).Length == 2).Join("&");
+        }
     }
 }

+ 6 - 0
src/Masuit.MyBlogs.Core/Controllers/HomeController.cs

@@ -2,6 +2,7 @@
 using Masuit.LuceneEFCore.SearchEngine.Linq;
 using Masuit.MyBlogs.Core.Extensions;
 using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
+using Masuit.MyBlogs.Core.Models;
 using Masuit.MyBlogs.Core.Models.DTO;
 using Masuit.MyBlogs.Core.Models.Entity;
 using Masuit.MyBlogs.Core.Models.Enum;
@@ -60,6 +61,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             viewModel.Banner = banners;
             viewModel.Posts = Enumerable.AsEnumerable(postsQuery.Where(p => p.IsFixedTop).OrderByDescending(p => p.ModifyDate)).Union(posts).ToList();
             ViewBag.FastShare = fastShares;
+            ViewData["page"] = new Pagination(1, 15, OrderBy.ModifyDate);
             return View(viewModel);
         }
 
@@ -83,6 +85,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             }
 
             viewModel.Posts = posts;
+            ViewData["page"] = new Pagination(page, size, orderBy);
             return View(viewModel);
         }
 
@@ -103,6 +106,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             ViewBag.Total = temp.Count();
             ViewBag.Tag = id;
             viewModel.Posts = posts;
+            ViewData["page"] = new Pagination(page, size, orderBy);
             return View(viewModel);
         }
 
@@ -125,6 +129,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             ViewBag.Total = temp.Count();
             ViewBag.Author = author;
             viewModel.Posts = posts;
+            ViewData["page"] = new Pagination(page, size, orderBy);
             return View(viewModel);
         }
 
@@ -147,6 +152,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             viewModel.Posts = posts.OrderBy($"{nameof(PostOutputDto.IsFixedTop)} desc,{(orderBy ?? OrderBy.ModifyDate).GetDisplay()} desc").Skip(size * (page - 1)).Take(size).Cacheable().ToList();
             ViewBag.CategoryName = cat.Name;
             ViewBag.Desc = cat.Description;
+            ViewData["page"] = new Pagination(page, size, orderBy);
             return View(viewModel);
         }
 

+ 5 - 20
src/Masuit.MyBlogs.Core/Controllers/NoticeController.cs

@@ -1,6 +1,7 @@
 using Masuit.MyBlogs.Core.Common;
 using Masuit.MyBlogs.Core.Extensions;
 using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
+using Masuit.MyBlogs.Core.Models;
 using Masuit.MyBlogs.Core.Models.DTO;
 using Masuit.MyBlogs.Core.Models.Entity;
 using Masuit.MyBlogs.Core.Models.Enum;
@@ -12,7 +13,6 @@ using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using Newtonsoft.Json;
 using System;
-using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
 
@@ -37,29 +37,14 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// </summary>
         /// <param name="page"></param>
         /// <param name="size"></param>
-        /// <param name="id"></param>
         /// <returns></returns>
-        [Route("notice"), ResponseCache(Duration = 60, VaryByQueryKeys = new[] { "page", "size", "id" }, VaryByHeader = "Cookie")]
-        public ActionResult Index(int page = 1, int size = 10, int id = 0)
+        [Route("notice"), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "page", "size" }, VaryByHeader = "Cookie")]
+        public ActionResult Index(int page = 1, int size = 10)
         {
             var list = NoticeService.GetPages<DateTime, NoticeOutputDto>(page, size, out var total, n => n.Status == Status.Display, n => n.ModifyDate, false).ToList();
             ViewBag.Total = total;
-            if (!CurrentUser.IsAdmin)
-            {
-                return View(list);
-            }
-
-            if (id == 0)
-            {
-                return View("Index_Admin", list);
-            }
-
-            var notice = NoticeService.GetById(id);
-            ViewBag.Total = 1;
-            return View("Index_Admin", new List<NoticeOutputDto>
-            {
-                notice.MapTo<NoticeOutputDto>()
-            });
+            ViewData["page"] = new Pagination(page, size);
+            return CurrentUser.IsAdmin ? View("Index_Admin", list) : View(list);
         }
 
         /// <summary>

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

@@ -10,6 +10,7 @@ using Masuit.MyBlogs.Core.Extensions;
 using Masuit.MyBlogs.Core.Extensions.Hangfire;
 using Masuit.MyBlogs.Core.Infrastructure;
 using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
+using Masuit.MyBlogs.Core.Models;
 using Masuit.MyBlogs.Core.Models.DTO;
 using Masuit.MyBlogs.Core.Models.Entity;
 using Masuit.MyBlogs.Core.Models.Enum;
@@ -103,6 +104,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             ViewBag.Total = total;
             ViewBag.PageCount = Math.Ceiling(total * 1.0 / size).ToInt32();
             ViewBag.Ads = AdsService.GetByWeightedPrice(AdvertiseType.InPage, post.CategoryId);
+            ViewData["page"] = new Pagination(page, size);
             return View(list);
         }
 

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

@@ -2,6 +2,7 @@
 using Masuit.MyBlogs.Core.Common;
 using Masuit.MyBlogs.Core.Extensions;
 using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
+using Masuit.MyBlogs.Core.Models;
 using Masuit.MyBlogs.Core.Models.DTO;
 using Masuit.MyBlogs.Core.Models.Entity;
 using Masuit.Tools;
@@ -80,6 +81,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             }
 
             ViewBag.hotSearches = RedisHelper.Get<List<KeywordsRankOutputDto>>("SearchRank:Week").Take(10).ToList();
+            ViewData["page"] = new Pagination(page, size);
             return View(nul);
         }
 

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

@@ -1,6 +1,7 @@
 using Masuit.MyBlogs.Core.Common;
 using Masuit.MyBlogs.Core.Extensions;
 using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
+using Masuit.MyBlogs.Core.Models;
 using Masuit.MyBlogs.Core.Models.DTO;
 using Masuit.MyBlogs.Core.Models.Entity;
 using Masuit.MyBlogs.Core.Models.Enum;
@@ -51,6 +52,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             ViewBag.Desc = s.Description;
             ViewBag.SubTitle = s.SubTitle;
             ViewBag.Ads = AdsService.GetByWeightedPrice(AdvertiseType.PostList);
+            ViewData["page"] = new Pagination(page, size, orderBy);
             return View(posts);
         }
 

+ 1 - 2
src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.xml

@@ -1134,13 +1134,12 @@
             公告
             </summary>
         </member>
-        <member name="M:Masuit.MyBlogs.Core.Controllers.NoticeController.Index(System.Int32,System.Int32,System.Int32)">
+        <member name="M:Masuit.MyBlogs.Core.Controllers.NoticeController.Index(System.Int32,System.Int32)">
             <summary>
             公告列表
             </summary>
             <param name="page"></param>
             <param name="size"></param>
-            <param name="id"></param>
             <returns></returns>
         </member>
         <member name="M:Masuit.MyBlogs.Core.Controllers.NoticeController.Details(System.Int32)">

+ 18 - 0
src/Masuit.MyBlogs.Core/Models/Pagination.cs

@@ -0,0 +1,18 @@
+using Masuit.MyBlogs.Core.Models.Enum;
+
+namespace Masuit.MyBlogs.Core.Models
+{
+    public class Pagination
+    {
+        public Pagination(int page, int size, OrderBy? orderBy = null)
+        {
+            Page = page;
+            Size = size;
+            OrderBy = orderBy;
+        }
+
+        public int Page { get; set; } = 1;
+        public int Size { get; set; } = 15;
+        public OrderBy? OrderBy { get; set; }
+    }
+}

+ 2 - 3
src/Masuit.MyBlogs.Core/Views/Notice/Details.cshtml

@@ -1,5 +1,4 @@
-@using System.Text.RegularExpressions
-@using Masuit.MyBlogs.Core.Common
+@using Masuit.MyBlogs.Core.Common
 @using Masuit.MyBlogs.Core.Models.DTO
 @using Masuit.MyBlogs.Core.Models.ViewModel
 @using Masuit.Tools.Core.Net
@@ -54,7 +53,7 @@
                     <div class="btn-group">
                         @Html.ActionLink("<<返回公告列表页", "Index", "Notice", null, new { @class = "btn btn-info" })
                         @{
-                            UserInfoOutputDto user = Context.Session.Get<UserInfoOutputDto>(SessionKey.UserInfo) ?? new UserInfoOutputDto();
+                            var user = Context.Session.Get<UserInfoOutputDto>(SessionKey.UserInfo) ?? new UserInfoOutputDto();
                             if (user.IsAdmin)
                             {
                                 <a class="btn btn-primary" asp-controller="Dashboard" asp-action="Index" asp-fragment="/notice/[email protected]" target="_blank">修改</a>

+ 3 - 8
src/Masuit.MyBlogs.Core/Views/Notice/Index.cshtml

@@ -1,5 +1,4 @@
-@using Masuit.MyBlogs.Core.Models.DTO
-@model IList<Masuit.MyBlogs.Core.Models.DTO.NoticeOutputDto>
+@model IList<Masuit.MyBlogs.Core.Models.DTO.NoticeOutputDto>
 @{
     ViewBag.Title = "网站公告栏";
     Layout = "~/Views/Shared/_Layout.cshtml";
@@ -9,11 +8,8 @@
         <li>@Html.ActionLink("首页", "Index", "Home")</li>
         <li class="current"><em>@ViewBag.Title</em></li>
     </ol>
-    @{
-        await Html.RenderPartialAsync("_Pagination");
-    }
     <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
-        @foreach (NoticeOutputDto n in Model)
+        @foreach (var n in Model)
         {
             <div class="panel panel-default">
                 <div class="panel-heading" role="tab" id="[email protected]">
@@ -32,7 +28,6 @@
         }
     </div>
     @{
-        await Html.RenderPartialAsync("_Pagination");
+        await Html.RenderPartialAsync("_Pagination", ViewData["page"]);
     }
-
 </div>

+ 3 - 7
src/Masuit.MyBlogs.Core/Views/Notice/Index_Admin.cshtml

@@ -1,5 +1,4 @@
-@using Masuit.MyBlogs.Core.Models.DTO
-@model IList<Masuit.MyBlogs.Core.Models.DTO.NoticeOutputDto>
+@model IList<Masuit.MyBlogs.Core.Models.DTO.NoticeOutputDto>
 @{
     ViewBag.Title = "网站公告栏";
     Layout = "~/Views/Shared/_Layout.cshtml";
@@ -9,12 +8,9 @@
         <li>@Html.ActionLink("首页", "Index", "Home")</li>
         <li class="current"><em>@ViewBag.Title</em></li>
     </ol>
-    @{
-        await Html.RenderPartialAsync("_Pagination");
-    }
     <a asp-controller="Dashboard" asp-action="Index" asp-fragment="/notice/index" class="btn btn-info">发布新公告</a>
     <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
-        @foreach (NoticeOutputDto n in Model)
+        @foreach (var n in Model)
         {
             <div class="panel panel-default">
                 <div class="panel-heading" role="tab" id="[email protected]">
@@ -37,7 +33,7 @@
         }
     </div>
     @{
-        await Html.RenderPartialAsync("_Pagination");
+        await Html.RenderPartialAsync("_Pagination", ViewData["page"]);
     }
 </div>
 <script>

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

@@ -60,7 +60,7 @@
     </table>
     <div class="col-md-12 text-center">
         @{
-            await Html.RenderPartialAsync("_Pagination");
+            await Html.RenderPartialAsync("_Pagination", ViewData["page"]);
         }
     </div>
     @if (ad != null)

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

@@ -112,7 +112,7 @@
             <h3>Oops!抱歉~本专题没有收录相关的文章或资源!如果您有相关的结果,您可以 @Html.ActionLink("点击这里", "Publish", "Post", null, new { @class = "btn btn-info btn-lg" }) 投稿哦!</h3>
         }
         <div class="col-md-12 text-center">
-            @{ await Html.RenderPartialAsync("_Pagination"); }
+            @{ await Html.RenderPartialAsync("_Pagination", ViewData["page"]); }
         </div>
     </div>
 </div>

+ 2 - 2
src/Masuit.MyBlogs.Core/Views/Shared/_ArticleListPartial.cshtml

@@ -7,7 +7,7 @@
             <div class="orderby">
                 <div class="row">
                     <div class="col-md-10 text-center">
-                        @{ await Html.RenderPartialAsync("_Pagination"); }
+                        @{ await Html.RenderPartialAsync("_Pagination", ViewData["page"]); }
                     </div>
                     <div class="col-md-2">
                         <div class="btn-group pull-right">
@@ -67,7 +67,7 @@
             }
         }
         <div class="col-md-12 text-center">
-            @{ await Html.RenderPartialAsync("_Pagination"); }
+            @{ await Html.RenderPartialAsync("_Pagination", ViewData["page"]); }
         </div>
     </div>
 </div>

+ 2 - 2
src/Masuit.MyBlogs.Core/Views/Shared/_ArticleListPartial_Admin.cshtml

@@ -7,7 +7,7 @@
             <div class="orderby">
                 <div class="row">
                     <div class="col-md-9 text-center">
-                        @{ await Html.RenderPartialAsync("_Pagination"); }
+                        @{ await Html.RenderPartialAsync("_Pagination",ViewData["page"]); }
                     </div>
                     <div class="col-md-3">
                         <div class="btn-group pull-right">
@@ -71,7 +71,7 @@
             }
         }
         <div class="col-md-12 text-center">
-            @{ await Html.RenderPartialAsync("_Pagination"); }
+            @{ await Html.RenderPartialAsync("_Pagination",ViewData["page"]); }
         </div>
     </div>
 </div>

+ 10 - 10
src/Masuit.MyBlogs.Core/Views/Shared/_Pagination.cshtml

@@ -1,10 +1,10 @@
-@using Masuit.Tools
+@model Masuit.MyBlogs.Core.Models.Pagination
+@using Masuit.MyBlogs.Core.Common
+@using Masuit.Tools
 @{
     int count = ViewBag.Total;
-    var size = Context.Request.Query["size"].ToString().ToInt32();
-    int pages = Math.Ceiling(count / (size > 0 ? size : 15.0)).ToInt32();
-    var page = Context.Request.Query["page"].ToString().ToInt32();
-    int current = page > 0 ? page : 1;
+    int pages = Math.Ceiling(count / (Model.Size > 0 ? Model.Size : 15.0)).ToInt32();
+    int current = Model.Page > 0 ? Model.Page : 1;
     int pageStart = current - 4 > 0 ? current - 4 : 1;
     int pageEnd = current + 4 >= pages ? pages : current + 4;
     current = current > pages ? pages : current;
@@ -40,12 +40,12 @@
         else
         {
             <li>
-                <a href="@[email protected]["orderby"]" aria-label="Previous">
+                <a href="@($"{Context.Request.Path}?orderby={Model.OrderBy}".TrimQuery())" aria-label="Previous">
                     首页
                 </a>
             </li>
             <li>
-                <a href="@Context.Request.Path?page=@(Context.Request.Query["page"].ToString().ToInt32() - 1 <= 0 ? 1 : Context.Request.Query["page"].ToString().ToInt32() - 1)&[email protected]["orderby"]" aria-label="Previous">
+                <a href="@($"{Context.Request.Path}?page={(Model.Page - 1 <= 0 ? 1 : Model.Page - 1)}&size={Model.Size}&orderby={Model.OrderBy}".TrimQuery())" aria-label="Previous">
                     <span aria-hidden="true">&laquo;</span>
                 </a>
             </li>
@@ -69,7 +69,7 @@
             else
             {
                 <li>
-                    <a href="@Context.Request.Path?page=@i&[email protected]["orderby"]">@i</a>
+                    <a href="@($"{Context.Request.Path}?page={i}&size={Model.Size}&orderby={Model.OrderBy}".TrimQuery())">@i</a>
                 </li>
             }
         }
@@ -97,12 +97,12 @@
         else
         {
             <li>
-                <a href="@Context.Request.Path?page=@(current + 1)&[email protected]["orderby"]" aria-label="Next">
+                <a href="@($"{Context.Request.Path}?page={current + 1}&size={Model.Size}&orderby={Model.OrderBy}".TrimQuery())" aria-label="Next">
                     <span aria-hidden="true">&raquo;</span>
                 </a>
             </li>
             <li>
-                <a href="@Context.Request.Path?page=@pages&[email protected]["orderby"]" aria-label="Next">
+                <a href="@($"{Context.Request.Path}?page={pages}&size={Model.Size}&orderby={Model.OrderBy}".TrimQuery())" aria-label="Next">
                     末页
                 </a>
             </li>