From 55273e792d55181b1d25a52f73f1995d26eebd68 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 1 Sep 2019 20:06:38 +0800 Subject: [PATCH] =?UTF-8?q?fix(#I11GTJ):=20=E5=89=8D=E5=8F=B0=E5=B7=A5?= =?UTF-8?q?=E7=A8=8B=E5=A2=9E=E5=8A=A0=E8=AE=BF=E9=97=AE=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #Comment comment #I11GTJ #Issue close https://gitee.com/LongbowEnterprise/dashboard/issues?id=I11GTJ --- .../Controllers/Api/TracesController.cs | 13 +++++ .../Bootstrap.Client.DataAccess.csproj | 1 + .../Helper/DictHelper.cs | 18 +++++++ .../Helper/TraceHelper.cs | 29 ++++++++++ .../Helper/TraceHttpClient.cs | 53 +++++++++++++++++++ Bootstrap.Client/Bootstrap.Client.csproj | 1 - Bootstrap.Client/Startup.cs | 5 ++ .../Bootstrap.DataAccess.csproj | 2 +- Bootstrap.DataAccess/Helper/TraceHelper.cs | 1 - 9 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 Bootstrap.Client.DataAccess/Helper/TraceHelper.cs create mode 100644 Bootstrap.Client.DataAccess/Helper/TraceHttpClient.cs diff --git a/Bootstrap.Admin/Controllers/Api/TracesController.cs b/Bootstrap.Admin/Controllers/Api/TracesController.cs index 10c623d5..4ca068da 100644 --- a/Bootstrap.Admin/Controllers/Api/TracesController.cs +++ b/Bootstrap.Admin/Controllers/Api/TracesController.cs @@ -1,5 +1,6 @@ using Bootstrap.Admin.Query; using Bootstrap.DataAccess; +using Longbow.Web; using Longbow.Web.Mvc; using Microsoft.AspNetCore.Mvc; @@ -22,5 +23,17 @@ namespace Bootstrap.Admin.Controllers.Api { return value.RetrieveData(); } + + /// + /// 在线用户访问保存方法,前台系统调用 + /// + /// + /// + [HttpPost] + public bool Post([FromBody]OnlineUser user) + { + TraceHelper.Save(HttpContext, user); + return true; + } } } diff --git a/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj b/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj index e77e1a5d..948f83c0 100644 --- a/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj +++ b/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj @@ -7,6 +7,7 @@ + diff --git a/Bootstrap.Client.DataAccess/Helper/DictHelper.cs b/Bootstrap.Client.DataAccess/Helper/DictHelper.cs index dad142a5..52b47694 100644 --- a/Bootstrap.Client.DataAccess/Helper/DictHelper.cs +++ b/Bootstrap.Client.DataAccess/Helper/DictHelper.cs @@ -2,6 +2,8 @@ using Bootstrap.Security.DataAccess; using Longbow.Cache; using Longbow.Data; +using Longbow.Web; +using System; using System.Collections.Generic; namespace Bootstrap.Client.DataAccess @@ -106,5 +108,21 @@ namespace Bootstrap.Client.DataAccess /// /// public static string RetrieveNotisUrl() => DbContextManager.Create().RetrieveNotisUrl(); + + /// + /// 配置 IP 地理位置查询配置项 注入方法调用此方法 + /// + /// + public static void ConfigIPLocator(IPLocatorOption op) + { + var name = RetrieveLocaleIPSvr(); + if (!string.IsNullOrEmpty(name) && !name.Equals("None", StringComparison.OrdinalIgnoreCase)) + { + var url = RetrieveLocaleIPSvrUrl(name); + op.Locator = string.IsNullOrEmpty(url) ? null : DefaultIPLocatorProvider.CreateLocator(name); + op.Url = string.IsNullOrEmpty(url) ? string.Empty : $"{url}{op.IP}"; + if (int.TryParse(RetrieveLocaleIPSvrCachePeriod(), out var period) && period > 0) op.Period = period * 60 * 1000; + } + } } } diff --git a/Bootstrap.Client.DataAccess/Helper/TraceHelper.cs b/Bootstrap.Client.DataAccess/Helper/TraceHelper.cs new file mode 100644 index 00000000..afdfdd28 --- /dev/null +++ b/Bootstrap.Client.DataAccess/Helper/TraceHelper.cs @@ -0,0 +1,29 @@ +using Longbow.Configuration; +using Longbow.Web; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using System.Net.Http; + +namespace Bootstrap.Client.DataAccess +{ + /// + /// 访问网页跟踪帮助类 + /// + public static class TraceHelper + { + /// + /// 保存访问历史记录 + /// + /// + /// + public static void Save(HttpContext context, OnlineUser user) + { + if (context.User.Identity.IsAuthenticated) + { + var client = context.RequestServices.GetRequiredService(); + client.Post(context, user); + } + } + } +} diff --git a/Bootstrap.Client.DataAccess/Helper/TraceHttpClient.cs b/Bootstrap.Client.DataAccess/Helper/TraceHttpClient.cs new file mode 100644 index 00000000..369f6edc --- /dev/null +++ b/Bootstrap.Client.DataAccess/Helper/TraceHttpClient.cs @@ -0,0 +1,53 @@ +using Longbow.Configuration; +using Longbow.Web; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Http; +using System; +using System.Linq; +using System.Net.Http; + +namespace Bootstrap.Client.DataAccess +{ + /// + /// Bootstrap TraceHttpClient 操作类 + /// + public class TraceHttpClient + { + HttpClient _client; + + /// + /// 构造函数 + /// + /// + /// + public TraceHttpClient(HttpClient client, IHttpContextAccessor httpContextAccessor) + { + client.Timeout = TimeSpan.FromSeconds(10); + client.DefaultRequestHeaders.Connection.Add("keep-alive"); + + // set auth + var cookieValues = httpContextAccessor.HttpContext.Request.Cookies.Select(cookie => $"{cookie.Key}={cookie.Value}"); + client.DefaultRequestHeaders.Add("Cookie", cookieValues); + + var authHost = ConfigurationManager.Get().AuthHost.TrimEnd(new char[] { '/' }); + var url = $"{authHost}/api/Traces"; + client.BaseAddress = new Uri(url); + + _client = client; + } + + /// + /// 提交数据到后台访问网页接口 + /// + /// + /// + public async void Post(HttpContext context, OnlineUser user) + { + // 调用 后台跟踪接口 + // http://localhost:50852/api/Traces + user.RequestUrl = context.Request.AbsoluteUrl(); + + await _client.PostAsJsonAsync("", user, context.RequestAborted); + } + } +} diff --git a/Bootstrap.Client/Bootstrap.Client.csproj b/Bootstrap.Client/Bootstrap.Client.csproj index 6bd315f0..af17522d 100644 --- a/Bootstrap.Client/Bootstrap.Client.csproj +++ b/Bootstrap.Client/Bootstrap.Client.csproj @@ -9,7 +9,6 @@ - diff --git a/Bootstrap.Client/Startup.cs b/Bootstrap.Client/Startup.cs index 5073f336..ed3c4914 100644 --- a/Bootstrap.Client/Startup.cs +++ b/Bootstrap.Client/Startup.cs @@ -50,6 +50,10 @@ namespace Bootstrap.Client services.AddConfigurationManager(); services.AddCacheManager(); services.AddDbAdapter(); + services.AddHttpClient("BootstrapAdmin", client => client.DefaultRequestHeaders.Connection.Add("keep-alive")); + services.AddIPLocator(DictHelper.ConfigIPLocator); + services.AddOnlineUsers(); + services.AddHttpClient(); services.AddSignalR().AddJsonProtocalDefault(); services.AddResponseCompression(); services.AddBootstrapAdminAuthentication(); @@ -91,6 +95,7 @@ namespace Bootstrap.Client app.UseCookiePolicy(); app.UseBootstrapAdminAuthentication(RoleHelper.RetrievesByUserName, RoleHelper.RetrievesByUrl, AppHelper.RetrievesByUserName); app.UseCacheManager(); + app.UseOnlineUsers(callback: TraceHelper.Save); app.UseSignalR(routes => { routes.MapHub("/NotiHub"); }); app.UseMvc(routes => { diff --git a/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj b/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj index fc09c352..2798d869 100644 --- a/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj +++ b/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj @@ -10,7 +10,7 @@ - + diff --git a/Bootstrap.DataAccess/Helper/TraceHelper.cs b/Bootstrap.DataAccess/Helper/TraceHelper.cs index f90efc84..0d438342 100644 --- a/Bootstrap.DataAccess/Helper/TraceHelper.cs +++ b/Bootstrap.DataAccess/Helper/TraceHelper.cs @@ -13,7 +13,6 @@ namespace Bootstrap.DataAccess /// public static class TraceHelper { - /// /// 保存访问历史记录 ///