From 13cafbc41145f447199a4db9d821824cca9c9437 Mon Sep 17 00:00:00 2001
From: Argo-Windows <5196060@qq.com>
Date: Fri, 1 Mar 2019 02:00:47 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=A1=B5=E9=9D=A2=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9C=A8=E7=BA=BF=E7=94=A8=E6=88=B7Online?=
=?UTF-8?q?=E8=A7=86=E5=9B=BE=E9=A1=B5=E9=9D=A2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Controllers/AdminController.cs | 6 +++
.../Controllers/Api/OnlineUsersController.cs | 19 ++++++-
Bootstrap.Admin/OnlineUsers/OnlineUser.cs | 11 ++--
.../OnlineUsersMiddlewareExtensions.cs | 5 +-
Bootstrap.Admin/Views/Admin/Online.cshtml | 29 +++++++++++
Bootstrap.Admin/wwwroot/js/online.js | 52 +++++++++++++++++++
6 files changed, 115 insertions(+), 7 deletions(-)
create mode 100644 Bootstrap.Admin/Views/Admin/Online.cshtml
create mode 100644 Bootstrap.Admin/wwwroot/js/online.js
diff --git a/Bootstrap.Admin/Controllers/AdminController.cs b/Bootstrap.Admin/Controllers/AdminController.cs
index 270b1772..a4ea5266 100644
--- a/Bootstrap.Admin/Controllers/AdminController.cs
+++ b/Bootstrap.Admin/Controllers/AdminController.cs
@@ -108,6 +108,12 @@ namespace Bootstrap.Admin.Controllers
///
public ActionResult Mobile() => View(new NavigatorBarModel(this));
+ ///
+ /// 在线用户
+ ///
+ ///
+ public ActionResult Online() => View(new NavigatorBarModel(this));
+
///
/// 用于测试ExceptionFilter
///
diff --git a/Bootstrap.Admin/Controllers/Api/OnlineUsersController.cs b/Bootstrap.Admin/Controllers/Api/OnlineUsersController.cs
index 131969eb..55409a44 100644
--- a/Bootstrap.Admin/Controllers/Api/OnlineUsersController.cs
+++ b/Bootstrap.Admin/Controllers/Api/OnlineUsersController.cs
@@ -1,17 +1,19 @@
using Microsoft.AspNetCore.Mvc;
+using System;
using System.Collections.Generic;
+using System.Linq;
namespace Bootstrap.Admin.Controllers.Api
{
///
- ///
+ /// 在线用户接口
///
[Route("api/[controller]")]
[ApiController]
public class OnlineUsersController : ControllerBase
{
///
- ///
+ /// 获取所有在线用户数据
///
///
[HttpPost()]
@@ -19,5 +21,18 @@ namespace Bootstrap.Admin.Controllers.Api
{
return onlineUSers.OnlineUsers;
}
+
+ ///
+ /// 获取指定IP地址的在线用户请求地址明细数据
+ ///
+ ///
+ ///
+ ///
+ [HttpGet("{id}")]
+ public IEnumerable> Get(string id, [FromServices]IOnlineUsers onlineUSers)
+ {
+ var user = onlineUSers.OnlineUsers.FirstOrDefault(u => u.Ip == id);
+ return user?.RequestUrls ?? new KeyValuePair[0];
+ }
}
}
diff --git a/Bootstrap.Admin/OnlineUsers/OnlineUser.cs b/Bootstrap.Admin/OnlineUsers/OnlineUser.cs
index 39720d73..5e783877 100644
--- a/Bootstrap.Admin/OnlineUsers/OnlineUser.cs
+++ b/Bootstrap.Admin/OnlineUsers/OnlineUser.cs
@@ -17,12 +17,10 @@ namespace Bootstrap.Admin
///
///
///
- ///
- 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>();
@@ -53,6 +51,11 @@ namespace Bootstrap.Admin
///
public string Ip { get; set; }
+ ///
+ ///
+ ///
+ public string RequestUrl { get; set; }
+
///
///
///
@@ -71,7 +74,7 @@ namespace Bootstrap.Admin
public void AddRequestUrl(string url)
{
_requestUrls.Enqueue(new KeyValuePair(DateTime.Now, url));
- if (_requestUrls.Count > 10)
+ if (_requestUrls.Count > 5)
{
_requestUrls.TryDequeue(out _);
}
diff --git a/Bootstrap.Admin/OnlineUsers/OnlineUsersMiddlewareExtensions.cs b/Bootstrap.Admin/OnlineUsers/OnlineUsersMiddlewareExtensions.cs
index 9275d165..c1783293 100644
--- a/Bootstrap.Admin/OnlineUsers/OnlineUsersMiddlewareExtensions.cs
+++ b/Bootstrap.Admin/OnlineUsers/OnlineUsersMiddlewareExtensions.cs
@@ -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;
});
diff --git a/Bootstrap.Admin/Views/Admin/Online.cshtml b/Bootstrap.Admin/Views/Admin/Online.cshtml
new file mode 100644
index 00000000..57cd7357
--- /dev/null
+++ b/Bootstrap.Admin/Views/Admin/Online.cshtml
@@ -0,0 +1,29 @@
+@model NavigatorBarModel
+@{
+ ViewBag.Title = "在线用户";
+}
+@section css {
+
+
+
+
+
+
+}
+@section javascript {
+
+
+
+
+
+
+
+
+
+}
+
\ No newline at end of file
diff --git a/Bootstrap.Admin/wwwroot/js/online.js b/Bootstrap.Admin/wwwroot/js/online.js
new file mode 100644
index 00000000..040a100a
--- /dev/null
+++ b/Bootstrap.Admin/wwwroot/js/online.js
@@ -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('', 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("{0} | {1} |
", item.Key, item.Value);
+ }).join('');
+ content = $.format('', content);
+ $this.lgbPopover({ content: content, placement: $(window).width() < 768 ? 'top' : 'left' });
+ $this.popover('show');
+ }
+ });
+ }
+ });
+
+ $('#refreshUsers').tooltip().on('click', function () {
+ $table.bootstrapTable('refresh');
+ });
+});
\ No newline at end of file