增加页面:增加在线用户Online视图页面

This commit is contained in:
Argo-Windows 2019-03-01 02:00:47 +08:00
parent a4520041c9
commit 13cafbc411
6 changed files with 115 additions and 7 deletions

View File

@ -108,6 +108,12 @@ namespace Bootstrap.Admin.Controllers
/// <returns></returns>
public ActionResult Mobile() => View(new NavigatorBarModel(this));
/// <summary>
/// 在线用户
/// </summary>
/// <returns></returns>
public ActionResult Online() => View(new NavigatorBarModel(this));
/// <summary>
/// 用于测试ExceptionFilter
/// </summary>

View File

@ -1,17 +1,19 @@
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// 在线用户接口
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class OnlineUsersController : ControllerBase
{
/// <summary>
///
/// 获取所有在线用户数据
/// </summary>
/// <returns></returns>
[HttpPost()]
@ -19,5 +21,18 @@ namespace Bootstrap.Admin.Controllers.Api
{
return onlineUSers.OnlineUsers;
}
/// <summary>
/// 获取指定IP地址的在线用户请求地址明细数据
/// </summary>
/// <param name="id"></param>
/// <param name="onlineUSers"></param>
/// <returns></returns>
[HttpGet("{id}")]
public IEnumerable<KeyValuePair<DateTime, string>> Get(string id, [FromServices]IOnlineUsers onlineUSers)
{
var user = onlineUSers.OnlineUsers.FirstOrDefault(u => u.Ip == id);
return user?.RequestUrls ?? new KeyValuePair<DateTime, string>[0];
}
}
}

View File

@ -17,12 +17,10 @@ namespace Bootstrap.Admin
/// </summary>
/// <param name="ip"></param>
/// <param name="userName"></param>
/// <param name="method"></param>
public OnlineUser(string ip, string userName, string method)
public OnlineUser(string ip, string userName)
{
Ip = ip;
UserName = userName;
Method = method;
FirstAccessTime = DateTime.Now;
LastAccessTime = DateTime.Now;
_requestUrls = new ConcurrentQueue<KeyValuePair<DateTime, string>>();
@ -53,6 +51,11 @@ namespace Bootstrap.Admin
/// </summary>
public string Ip { get; set; }
/// <summary>
///
/// </summary>
public string RequestUrl { get; set; }
/// <summary>
///
/// </summary>
@ -71,7 +74,7 @@ namespace Bootstrap.Admin
public void AddRequestUrl(string url)
{
_requestUrls.Enqueue(new KeyValuePair<DateTime, string>(DateTime.Now, url));
if (_requestUrls.Count > 10)
if (_requestUrls.Count > 5)
{
_requestUrls.TryDequeue(out _);
}

View File

@ -25,13 +25,16 @@ namespace Microsoft.AspNetCore.Builder
var clientIp = context.Connection.RemoteIpAddress.ToString();
onlineUsers.AddOrUpdate(clientIp, key =>
{
var ou = new OnlineUser(key, context.User.Identity.Name, context.Request.Method);
var ou = new OnlineUser(key, context.User.Identity.Name);
ou.Method = context.Request.Method;
ou.RequestUrl = context.Request.Path;
ou.AddRequestUrl(context.Request.Path);
return ou;
}, (key, v) =>
{
v.LastAccessTime = DateTime.Now;
v.Method = context.Request.Method;
v.RequestUrl = context.Request.Path;
v.AddRequestUrl(context.Request.Path);
return v;
});

View File

@ -0,0 +1,29 @@
@model NavigatorBarModel
@{
ViewBag.Title = "在线用户";
}
@section css {
<environment include="Development">
<link href="~/lib/bootstrap-table/bootstrap-table.css" rel="stylesheet" />
</environment>
<environment exclude="Development">
<link href="~/lib/bootstrap-table/bootstrap-table.min.css" rel="stylesheet" />
</environment>
}
@section javascript {
<environment include="Development">
<script src="~/lib/bootstrap-table/bootstrap-table.js"></script>
<script src="~/lib/bootstrap-table/locale/bootstrap-table-zh-CN.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>
</environment>
<script src="~/js/online.js" asp-append-version="true"></script>
}
<div class="card">
<div class="card-header">在线用户<span class="pull-right"><a id="refreshUsers" href="javascript:;" class="fa fa-refresh" title="点击刷新" data-toggle="tooltip" data-placement="left"></a></span></div>
<div class="card-body" style="padding-top: 25px;">
<table></table>
</div>
</div>

View File

@ -0,0 +1,52 @@
$(function () {
var apiUrl = "api/OnlineUsers";
var $table = $('table').smartTable({
url: apiUrl,
method: "post",
sidePagination: "client",
showToggle: false,
showRefresh: false,
showColumns: false,
columns: [
{
title: "序号", formatter: function (value, row, index) {
var options = $table.bootstrapTable('getOptions');
return options.pageSize * (options.pageNumber - 1) + index + 1;
}
},
{ title: "登陆名称", field: "UserName" },
{ title: "显示名称", field: "DisplayName" },
{ title: "登录时间", field: "FirstAccessTime" },
{ title: "最近操作时间", field: "LastAccessTime" },
{ title: "请求方式", field: "Method" },
{ title: "IP地址", field: "Ip" },
{ title: "访问地址", field: "RequestUrl" },
{
title: "历史地址", field: "Ip", formatter: function (value, row, index, field) {
return $.format('<button type="button" class="btn btn-info" data-id="{0}" data-toggle="popover" data-trigger="focus" data-html="true" data-title="访问记录">明细</button >', value);
}
}
]
}).on('click', 'button[data-id]', function () {
var $this = $(this);
if (!$this.data($.fn.popover.Constructor.DATA_KEY)) {
var id = $this.attr('data-id');
$.bc({
id: id, url: apiUrl,
callback: function (result) {
if (!result) return;
var content = result.map(function (item) {
return $.format("<tr><td>{0}</td><td>{1}</td></tr>", item.Key, item.Value);
}).join('');
content = $.format('<div class="fixed-table-container"><table class="table table-hover table-sm mb-0"><thead><tr><th class="p-1"><b>访问时间</b></th><th class="p-1">访问地址</th></tr></thead><tbody>{0}</tbody></table></div>', content);
$this.lgbPopover({ content: content, placement: $(window).width() < 768 ? 'top' : 'left' });
$this.popover('show');
}
});
}
});
$('#refreshUsers').tooltip().on('click', function () {
$table.bootstrapTable('refresh');
});
});