feat(#IWYJR): 登录日志增加时间段过滤条件

#Comment
comment #IWYJR

#Issue
close https://gitee.com/LongbowEnterprise/dashboard/issues?id=IWYJR
This commit is contained in:
Argo-MacBookPro 2019-05-24 12:27:14 +08:00
parent 571b593e7b
commit 0391a20b57
7 changed files with 110 additions and 19 deletions

View File

@ -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
/// </summary>
public class QueryLoginOption : PaginationOption
{
/// <summary>
///
/// </summary>
public DateTime? StartTime { get; set; }
/// <summary>
///
/// </summary>
public DateTime? EndTime { get; set; }
/// <summary>
/// 登录IP地址
/// </summary>
@ -19,7 +30,7 @@ namespace Bootstrap.Admin.Query
/// <returns></returns>
public QueryData<LoginUser> RetrieveData()
{
var data = LoginHelper.Retrieves(this, LoginIP);
var data = LoginHelper.RetrievePages(this, StartTime, EndTime, LoginIP);
return new QueryData<LoginUser>
{
total = data.TotalItems,

View File

@ -4,9 +4,11 @@
}
@section css {
<environment include="Development">
<link href="~/lib/datetimepicker/css//bootstrap-datetimepicker.css" rel="stylesheet" />
<link href="~/lib/bootstrap-table/bootstrap-table.css" rel="stylesheet" />
</environment>
<environment exclude="Development">
<link href="~/lib/datetimepicker/css//bootstrap-datetimepicker.min.css" rel="stylesheet" />
<link href="~/lib/bootstrap-table/bootstrap-table.min.css" rel="stylesheet" />
</environment>
}
@ -14,11 +16,14 @@
<environment include="Development">
<script src="~/lib/bootstrap-table/bootstrap-table.js"></script>
<script src="~/lib/bootstrap-table/locale/bootstrap-table-zh-CN.js"></script>
<script src="~/lib/datetimepicker/js/bootstrap-datetimepicker.js"></script>
</environment>
<environment exclude="Development">
<script src="~/lib/bootstrap-table/bootstrap-table.min.js"></script>
<script src="~/lib/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="~/lib/datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
</environment>
<script src="~/lib/datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
<script src="~/js/logins.js" asp-append-version="true"></script>
}
<div class="card">
@ -26,11 +31,35 @@
<div class="card-body">
<form class="form-inline">
<div class="row">
<div class="form-group col-sm-6 col-md-auto">
<div class="form-group col-sm-auto">
<label class="control-label" for="txt_operate_start">起始时间</label>
<div class="input-group date">
<input id="txt_operate_start" class="form-control" size="16" type="text" value="" readonly>
<div class="input-group-append input-group-addon">
<div class="input-group-text"><span class="fa fa-times"></span></div>
</div>
<div class="input-group-append input-group-addon">
<div class="input-group-text"><span class="fa fa-calendar"></span></div>
</div>
</div>
</div>
<div class="form-group col-sm-auto">
<label class="control-label" for="txt_operate_end">终止时间</label>
<div class="input-group date">
<input id="txt_operate_end" class="form-control" size="16" type="text" value="" readonly>
<div class="input-group-append input-group-addon">
<div class="input-group-text"><span class="fa fa-times"></span></div>
</div>
<div class="input-group-append input-group-addon">
<div class="input-group-text"><span class="fa fa-calendar"></span></div>
</div>
</div>
</div>
<div class="form-group col-sm-auto">
<label class="control-label" for="txt_ip">请求IP</label>
<input type="text" class="form-control" id="txt_ip" />
</div>
<div class="form-group col-sm-6 col-md-auto flex-md-fill justify-content-md-end">
<div class="form-group col-sm-auto flex-md-fill justify-content-md-end">
<button type="button" id="btn_query" class="btn btn-primary btn-fill"><i class="fa fa-search" aria-hidden="true"></i><span>查询</span></button>
</div>
</div>

View File

@ -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) {

View File

@ -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
/// </summary>
/// <param name="po"></param>
/// <returns></returns>
public override Page<DataAccess.LoginUser> Retrieves(PaginationOption po, string ip)
public override Page<DataAccess.LoginUser> RetrieveByPages(PaginationOption po, DateTime? startTime, DateTime? endTime, string ip)
{
var logs = DbManager.LoginUsers
.Find(Builders<DataAccess.LoginUser>.Filter.Eq("Ip", ip))
.Sort(Builders<DataAccess.LoginUser>.Sort.Descending(t => t.LoginTime))
.ToList();
var logs = Retrieves(startTime, endTime, ip);
return new Page<DataAccess.LoginUser>()
{
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()
};
}
/// <summary>
/// 获取所有登录数据
/// </summary>
/// <returns></returns>
public override IEnumerable<DataAccess.LoginUser> Retrieves(DateTime? startTime, DateTime? endTime, string ip)
{
var filterBuilder = Builders<DataAccess.LoginUser>.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<DataAccess.LoginUser>.Sort.Descending(t => t.LoginTime)).ToList();
}
}
}

View File

@ -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<LoginUser>().Log(user);
/// <summary>
/// 查询所有登录日志
/// 查询指定页码登录日志
/// </summary>
/// <param name="po"></param>
/// <param name="startTime"></param>
/// <param name="endTime"></param>
/// <param name="ip"></param>
public static Page<LoginUser> Retrieves(PaginationOption po, string ip) => DbContextManager.Create<LoginUser>().Retrieves(po, ip);
public static Page<LoginUser> RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime, string ip) => DbContextManager.Create<LoginUser>().RetrieveByPages(po, startTime, endTime, ip);
/// <summary>
/// 查询所有登录日志
/// </summary>
/// <param name="startTime"></param>
/// <param name="endTime"></param>
/// <param name="ip"></param>
/// <returns></returns>
public static IEnumerable<LoginUser> RetrieveAll(DateTime? startTime, DateTime? endTime, string ip)
{
return DbContextManager.Create<LoginUser>().Retrieves(startTime, endTime, ip);
}
}
}

View File

@ -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
///
/// </summary>
/// <param name="po"></param>
/// <param name="startTime"></param>
/// <param name="endTime"></param>
/// <param name="ip"></param>
/// <returns></returns>
public virtual Page<LoginUser> Retrieves(PaginationOption po, string ip)
public virtual Page<LoginUser> 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<LoginUser>(po.PageIndex, po.Limit, sql);
}
/// <summary>
/// 获取所有登录数据
/// </summary>
/// <returns></returns>
public virtual IEnumerable<LoginUser> Retrieves(DateTime? startTime, DateTime? endTime, string ip) => DbManager.Create().Fetch<LoginUser>();
}
}

View File

@ -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);
}
}
}