diff --git a/Bootstrap.Admin/Query/QueryLoginOption.cs b/Bootstrap.Admin/Query/QueryLoginOption.cs index 024e26c8..e5823fd9 100644 --- a/Bootstrap.Admin/Query/QueryLoginOption.cs +++ b/Bootstrap.Admin/Query/QueryLoginOption.cs @@ -1,5 +1,6 @@ using Bootstrap.DataAccess; using Longbow.Web.Mvc; +using System; namespace Bootstrap.Admin.Query { @@ -8,6 +9,16 @@ namespace Bootstrap.Admin.Query /// public class QueryLoginOption : PaginationOption { + /// + /// + /// + public DateTime? StartTime { get; set; } + + /// + /// + /// + public DateTime? EndTime { get; set; } + /// /// 登录IP地址 /// @@ -19,7 +30,7 @@ namespace Bootstrap.Admin.Query /// public QueryData RetrieveData() { - var data = LoginHelper.Retrieves(this, LoginIP); + var data = LoginHelper.RetrievePages(this, StartTime, EndTime, LoginIP); return new QueryData { total = data.TotalItems, diff --git a/Bootstrap.Admin/Views/Admin/Logins.cshtml b/Bootstrap.Admin/Views/Admin/Logins.cshtml index 168a687e..c6ab2713 100644 --- a/Bootstrap.Admin/Views/Admin/Logins.cshtml +++ b/Bootstrap.Admin/Views/Admin/Logins.cshtml @@ -4,9 +4,11 @@ } @section css { + + } @@ -14,11 +16,14 @@ + + + } @@ -26,11 +31,35 @@ - + + 起始时间 + + + + + + + + + + + + 终止时间 + + + + + + + + + + + 请求IP - + 查询 diff --git a/Bootstrap.Admin/wwwroot/js/logins.js b/Bootstrap.Admin/wwwroot/js/logins.js index db578bc6..d8bacf67 100644 --- a/Bootstrap.Admin/wwwroot/js/logins.js +++ b/Bootstrap.Admin/wwwroot/js/logins.js @@ -1,12 +1,15 @@ // 登录日志 $(function () { var apiUrl = "api/Login"; - var $table = $('table').smartTable({ + var $table = $('.card-body table'); + $table.smartTable({ url: apiUrl, showToggle: false, showRefresh: false, showColumns: false, - queryParams: function (params) { return $.extend(params, { loginIp: $('#txt_ip').val() }); }, + sortName: 'LoginTime', + sortOrder: "desc", + queryParams: function (params) { return $.extend(params, { startTime: $("#txt_operate_start").val(), endTime: $("#txt_operate_end").val(), loginIp: $('#txt_ip').val() }); }, columns: [ { title: "序号", formatter: function (value, row, index) { diff --git a/Bootstrap.DataAccess.MongoDB/LoginUser.cs b/Bootstrap.DataAccess.MongoDB/LoginUser.cs index 2cb54140..204ec75e 100644 --- a/Bootstrap.DataAccess.MongoDB/LoginUser.cs +++ b/Bootstrap.DataAccess.MongoDB/LoginUser.cs @@ -2,6 +2,7 @@ using MongoDB.Driver; using PetaPoco; using System; +using System.Collections.Generic; using System.Linq; namespace Bootstrap.DataAccess.MongoDB @@ -27,22 +28,35 @@ namespace Bootstrap.DataAccess.MongoDB /// /// /// - public override Page Retrieves(PaginationOption po, string ip) + public override Page RetrieveByPages(PaginationOption po, DateTime? startTime, DateTime? endTime, string ip) { - var logs = DbManager.LoginUsers - .Find(Builders.Filter.Eq("Ip", ip)) - .Sort(Builders.Sort.Descending(t => t.LoginTime)) - .ToList(); - + var logs = Retrieves(startTime, endTime, ip); return new Page() { Context = logs, CurrentPage = po.PageIndex, ItemsPerPage = po.Limit, - TotalItems = logs.Count, - TotalPages = (long)Math.Ceiling(logs.Count * 1.0 / po.Limit), + TotalItems = logs.Count(), + TotalPages = (long)Math.Ceiling(logs.Count() * 1.0 / po.Limit), Items = logs.Skip(po.Offset).Take(po.Limit).ToList() }; } + + /// + /// 获取所有登录数据 + /// + /// + public override IEnumerable Retrieves(DateTime? startTime, DateTime? endTime, string ip) + { + var filterBuilder = Builders.Filter; + var filter = filterBuilder.Empty; + if (startTime.HasValue) filter = filterBuilder.Gte(l => l.LoginTime, startTime.Value); + if (endTime.HasValue) filter = filterBuilder.Lt(l => l.LoginTime, endTime.Value.AddDays(1)); + if (!string.IsNullOrEmpty(ip)) filter = filterBuilder.Eq(l => l.Ip, ip); + + return DbManager.LoginUsers + .Find(filter) + .Sort(Builders.Sort.Descending(t => t.LoginTime)).ToList(); + } } } diff --git a/Bootstrap.DataAccess/Helper/LoginHelper.cs b/Bootstrap.DataAccess/Helper/LoginHelper.cs index 1a24a483..0b48be0c 100644 --- a/Bootstrap.DataAccess/Helper/LoginHelper.cs +++ b/Bootstrap.DataAccess/Helper/LoginHelper.cs @@ -1,6 +1,8 @@ using Longbow.Data; using Longbow.Web.Mvc; using PetaPoco; +using System; +using System.Collections.Generic; namespace Bootstrap.DataAccess { @@ -17,10 +19,24 @@ namespace Bootstrap.DataAccess public static bool Log(LoginUser user) => DbContextManager.Create().Log(user); /// - /// 查询所有登录日志 + /// 查询指定页码登录日志 /// /// + /// + /// /// - public static Page Retrieves(PaginationOption po, string ip) => DbContextManager.Create().Retrieves(po, ip); + public static Page RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime, string ip) => DbContextManager.Create().RetrieveByPages(po, startTime, endTime, ip); + + /// + /// 查询所有登录日志 + /// + /// + /// + /// + /// + public static IEnumerable RetrieveAll(DateTime? startTime, DateTime? endTime, string ip) + { + return DbContextManager.Create().Retrieves(startTime, endTime, ip); + } } } diff --git a/Bootstrap.DataAccess/LoginUser.cs b/Bootstrap.DataAccess/LoginUser.cs index 3b2847a0..3a9f3fcf 100644 --- a/Bootstrap.DataAccess/LoginUser.cs +++ b/Bootstrap.DataAccess/LoginUser.cs @@ -1,6 +1,7 @@ using Longbow.Web.Mvc; using PetaPoco; using System; +using System.Collections.Generic; namespace Bootstrap.DataAccess { @@ -71,14 +72,24 @@ namespace Bootstrap.DataAccess /// /// /// + /// + /// /// /// - public virtual Page Retrieves(PaginationOption po, string ip) + public virtual Page RetrieveByPages(PaginationOption po, DateTime? startTime, DateTime? endTime, string ip) { var sql = new Sql("select UserName, LoginTime, Ip, Browser, OS, City, Result from LoginLogs"); + if (startTime.HasValue) sql.Where("LoginTime >= @0", startTime.Value); + if (endTime.HasValue) sql.Where("LoginTime < @0", endTime.Value.AddDays(1)); if (!string.IsNullOrEmpty(ip)) sql.Where("ip = @0", ip); - sql.OrderBy("LoginTime desc"); + sql.OrderBy($"{po.Sort} {po.Order}"); return DbManager.Create().Page(po.PageIndex, po.Limit, sql); } + + /// + /// 获取所有登录数据 + /// + /// + public virtual IEnumerable Retrieves(DateTime? startTime, DateTime? endTime, string ip) => DbManager.Create().Fetch(); } } diff --git a/UnitTest/Bootstrap.DataAccess/UsersTest.cs b/UnitTest/Bootstrap.DataAccess/UsersTest.cs index de633ee6..9045da87 100644 --- a/UnitTest/Bootstrap.DataAccess/UsersTest.cs +++ b/UnitTest/Bootstrap.DataAccess/UsersTest.cs @@ -172,11 +172,18 @@ namespace Bootstrap.DataAccess.SqlServer Assert.True(UserHelper.ResetPassword(newUser.UserName, "123")); } + [Fact] + public void RetrievePageLoginUsers_Ok() + { + var data = LoginHelper.RetrievePages(new PaginationOption() { Limit = 20, Offset = 0 }, null, null, ""); + Assert.NotNull(data.Items); + } + [Fact] public void RetrieveLoginUsers_Ok() { - var data = LoginHelper.Retrieves(new PaginationOption() { Limit = 20, Offset = 0 }, ""); - Assert.NotNull(data.Items); + var data = LoginHelper.RetrieveAll(null, null, ""); + Assert.NotNull(data); } } }