From 15a6b2356403eeb02a7d2b8d2de2647959e5eabe Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Wed, 1 May 2019 12:07:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(#IVYSF):=20=E7=99=BB=E5=BD=95=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E4=BD=BF=E7=94=A8=E5=88=86=E9=A1=B5=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit comment #IVYSF 使用后台分页提高数据量大时程序性能 close https://gitee.com/LongbowEnterprise/dashboard/issues?id=IVYSF --- .../Controllers/Api/LoginController.cs | 130 ++++++++++-------- Bootstrap.Admin/wwwroot/js/logins.js | 2 - Bootstrap.DataAccess.MongoDB/LoginUser.cs | 27 +++- Bootstrap.DataAccess/Helper/LoginHelper.cs | 25 ++-- Bootstrap.DataAccess/LoginUser.cs | 7 +- 5 files changed, 105 insertions(+), 86 deletions(-) diff --git a/Bootstrap.Admin/Controllers/Api/LoginController.cs b/Bootstrap.Admin/Controllers/Api/LoginController.cs index 373e10d1..f51c7ddc 100644 --- a/Bootstrap.Admin/Controllers/Api/LoginController.cs +++ b/Bootstrap.Admin/Controllers/Api/LoginController.cs @@ -1,61 +1,69 @@ -using Bootstrap.DataAccess; -using Bootstrap.Security; -using Longbow.Web; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Newtonsoft.Json.Linq; -using System.Collections.Generic; - -namespace Bootstrap.Admin.Controllers.Api -{ - /// - /// - /// - /// - /// - /// - [Route("api/[controller]")] - [ApiController] - public class LoginController : ControllerBase - { - /// - /// 获得登录历史记录 - /// - /// - [HttpGet] - public IEnumerable Get() => LoginHelper.Retrieves(); - - /// - /// - /// - /// - /// - /// - /// - [AllowAnonymous] - [HttpPost] - public string Post([FromServices]IOnlineUsers onlineUserSvr, [FromServices]IIPLocatorProvider ipLocator, [FromBody]JObject value) - { - string token = null; - dynamic user = value; - string userName = user.userName; - string password = user.password; - if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password) && UserHelper.Authenticate(userName, password, loginUser => AccountController.CreateLoginUser(onlineUserSvr, ipLocator, HttpContext, loginUser))) - { - token = BootstrapAdminJwtTokenHandler.CreateToken(userName); - } - return token; - } - - /// - /// - /// - /// - [AllowAnonymous] - [HttpOptions] - public string Options() - { - return null; - } - } -} +using Bootstrap.DataAccess; +using Bootstrap.Security; +using Longbow.Web; +using Longbow.Web.Mvc; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json.Linq; + +namespace Bootstrap.Admin.Controllers.Api +{ + /// + /// + /// + /// + /// + /// + [Route("api/[controller]")] + [ApiController] + public class LoginController : ControllerBase + { + /// + /// 获得登录历史记录 + /// + /// + /// + [HttpGet] + public QueryData Get([FromQuery]PaginationOption po) + { + var data = LoginHelper.Retrieves(po); + var ret = new QueryData(); + ret.total = data.TotalItems; + ret.rows = data.Items; + return ret; + } + + /// + /// + /// + /// + /// + /// + /// + [AllowAnonymous] + [HttpPost] + public string Post([FromServices]IOnlineUsers onlineUserSvr, [FromServices]IIPLocatorProvider ipLocator, [FromBody]JObject value) + { + string token = null; + dynamic user = value; + string userName = user.userName; + string password = user.password; + if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password) && UserHelper.Authenticate(userName, password, loginUser => AccountController.CreateLoginUser(onlineUserSvr, ipLocator, HttpContext, loginUser))) + { + token = BootstrapAdminJwtTokenHandler.CreateToken(userName); + } + return token; + } + + /// + /// + /// + /// + [AllowAnonymous] + [HttpOptions] + public string Options() + { + return null; + } + } +} diff --git a/Bootstrap.Admin/wwwroot/js/logins.js b/Bootstrap.Admin/wwwroot/js/logins.js index 76213990..de563242 100644 --- a/Bootstrap.Admin/wwwroot/js/logins.js +++ b/Bootstrap.Admin/wwwroot/js/logins.js @@ -3,8 +3,6 @@ $(function () { var apiUrl = "api/Login"; var $table = $('table').smartTable({ url: apiUrl, - method: "get", - sidePagination: "client", showToggle: false, showRefresh: false, showColumns: false, diff --git a/Bootstrap.DataAccess.MongoDB/LoginUser.cs b/Bootstrap.DataAccess.MongoDB/LoginUser.cs index 5d46434f..47c56dd7 100644 --- a/Bootstrap.DataAccess.MongoDB/LoginUser.cs +++ b/Bootstrap.DataAccess.MongoDB/LoginUser.cs @@ -1,6 +1,10 @@ -using MongoDB.Driver; +using Longbow.Web.Mvc; +using MongoDB.Driver; +using PetaPoco; +using System; using System.Collections.Generic; - +using System.Linq; + namespace Bootstrap.DataAccess.MongoDB { /// @@ -22,7 +26,24 @@ namespace Bootstrap.DataAccess.MongoDB /// /// /// + /// /// - public override IEnumerable Retrieves() => DbManager.LoginUsers.Find(FilterDefinition.Empty).SortByDescending(user => user.LoginTime).ToList(); + public override Page Retrieves(PaginationOption po) + { + var logs = DbManager.LoginUsers + .Find(Builders.Filter.Empty) + .Sort(Builders.Sort.Descending(t => t.LoginTime)) + .ToList(); + + return new Page() + { + Context = logs, + CurrentPage = po.PageIndex, + ItemsPerPage = po.Limit, + TotalItems = logs.Count, + TotalPages = (long)Math.Ceiling(logs.Count * 1.0 / po.Limit), + Items = logs.Skip(po.Offset).Take(po.Limit).ToList() + }; + } } } diff --git a/Bootstrap.DataAccess/Helper/LoginHelper.cs b/Bootstrap.DataAccess/Helper/LoginHelper.cs index 20844f2a..7712e6f4 100644 --- a/Bootstrap.DataAccess/Helper/LoginHelper.cs +++ b/Bootstrap.DataAccess/Helper/LoginHelper.cs @@ -1,7 +1,7 @@ -using Longbow.Cache; -using Longbow.Data; -using System.Collections.Generic; - +using Longbow.Data; +using Longbow.Web.Mvc; +using PetaPoco; + namespace Bootstrap.DataAccess { /// @@ -9,26 +9,17 @@ namespace Bootstrap.DataAccess /// public static class LoginHelper { - /// - /// - /// - public const string RetrieveLoginLogsDataKey = "LoginHelper-Retrieves"; - /// /// /// /// /// - public static bool Log(LoginUser user) - { - var ret = DbContextManager.Create().Log(user); - if (ret) CacheManager.Clear(RetrieveLoginLogsDataKey); - return ret; - } + public static bool Log(LoginUser user) => DbContextManager.Create().Log(user); /// - /// 查询一个月内所有登录信息 + /// 查询所有登录日志 /// - public static IEnumerable Retrieves() => CacheManager.GetOrAdd(RetrieveLoginLogsDataKey, key => DbContextManager.Create().Retrieves()); + /// + public static Page Retrieves(PaginationOption po) => DbContextManager.Create().Retrieves(po); } } diff --git a/Bootstrap.DataAccess/LoginUser.cs b/Bootstrap.DataAccess/LoginUser.cs index ae2f4b65..5b3f4bc1 100644 --- a/Bootstrap.DataAccess/LoginUser.cs +++ b/Bootstrap.DataAccess/LoginUser.cs @@ -1,6 +1,6 @@ -using PetaPoco; +using Longbow.Web.Mvc; +using PetaPoco; using System; -using System.Collections.Generic; namespace Bootstrap.DataAccess { @@ -70,7 +70,8 @@ namespace Bootstrap.DataAccess /// /// /// + /// /// - public virtual IEnumerable Retrieves() => DbManager.Create().Fetch("Where LoginTime > @0 Order by LoginTime desc", DateTime.Today.AddMonths(0 - DictHelper.RetrieveLoginLogsPeriod())); + public virtual Page Retrieves(PaginationOption po) => DbManager.Create().Page(po.PageIndex, po.Limit, "select UserName, LoginTime, Ip, Browser, OS, City, Result from LoginLogs Order by LoginTime desc"); } }