From 8d119f98970cc0f4e439a27ef3976d4e1ccc7a36 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 30 Jan 2020 14:32:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Blazor=20=E6=A8=A1=E5=BC=8F=E4=B8=8B?= =?UTF-8?q?=E5=AE=8C=E5=96=84=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 --- .../Components/DefaultLayoutComponentBase.cs | 24 +++++++++++++++++-- .../Pages/Components/PageContent.cs | 9 ------- .../Pages/Extensions/HttpContextExtensions.cs | 4 ++-- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/admin/Bootstrap.Admin/Pages/Components/DefaultLayoutComponentBase.cs b/src/admin/Bootstrap.Admin/Pages/Components/DefaultLayoutComponentBase.cs index a98af7eb..27887919 100644 --- a/src/admin/Bootstrap.Admin/Pages/Components/DefaultLayoutComponentBase.cs +++ b/src/admin/Bootstrap.Admin/Pages/Components/DefaultLayoutComponentBase.cs @@ -1,9 +1,11 @@ -using Bootstrap.Admin.Pages.Extensions; -using Bootstrap.Admin.Models; +using Bootstrap.Admin.Models; +using Bootstrap.Admin.Pages.Extensions; using Bootstrap.Admin.Pages.Shared; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; +using Microsoft.AspNetCore.Components.Routing; using Microsoft.AspNetCore.Components.Server; +using Microsoft.AspNetCore.Http; using Microsoft.JSInterop; using System; using System.Net; @@ -28,6 +30,12 @@ namespace Bootstrap.Admin.Pages.Components [Inject] public NavigationManager? NavigationManager { get; set; } + /// + /// 获得/设置 组件名字 + /// + [Inject] + protected IHttpContextAccessor? HttpContextAccessor { get; set; } + /// /// /// @@ -80,6 +88,12 @@ namespace Bootstrap.Admin.Pages.Components /// protected override async Task OnInitializedAsync() { + // 网页跳转监控 + if (NavigationManager != null) + { + NavigationManager.LocationChanged += NavigationManager_LocationChanged; + } + var state = await AuthenticationStateProvider.GetAuthenticationStateAsync(); if (!state.User.Identity.IsAuthenticated) { @@ -92,6 +106,12 @@ namespace Bootstrap.Admin.Pages.Components } } + private void NavigationManager_LocationChanged(object? sender, LocationChangedEventArgs e) + { + var name = $"/{NavigationManager?.ToBaseRelativePath(e.Location)}"; + if (HttpContextAccessor != null) HttpContextAccessor.HttpContext?.SaveOnlineUser(name); + } + /// /// 设置参数方法 /// diff --git a/src/admin/Bootstrap.Admin/Pages/Components/PageContent.cs b/src/admin/Bootstrap.Admin/Pages/Components/PageContent.cs index 73204e77..169fbeed 100644 --- a/src/admin/Bootstrap.Admin/Pages/Components/PageContent.cs +++ b/src/admin/Bootstrap.Admin/Pages/Components/PageContent.cs @@ -1,6 +1,5 @@ using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Rendering; -using Microsoft.AspNetCore.Http; using System; namespace Bootstrap.Admin.Pages.Components @@ -10,12 +9,6 @@ namespace Bootstrap.Admin.Pages.Components /// public class PageContent : ComponentBase { - /// - /// 获得/设置 组件名字 - /// - [Inject] - protected IHttpContextAccessor? HttpContextAccessor { get; set; } - /// /// 获得/设置 组件名字 /// @@ -34,8 +27,6 @@ namespace Bootstrap.Admin.Pages.Components var t = Type.GetType($"Bootstrap.Admin.Pages.Views.{name}"); if (t != null) { - // 访问日志 - if (HttpContextAccessor != null) HttpContextAccessor.HttpContext?.SaveOnlineUser($"/Pages/{Name}"); builder.OpenComponent(0, t); builder.CloseComponent(); diff --git a/src/admin/Bootstrap.Admin/Pages/Extensions/HttpContextExtensions.cs b/src/admin/Bootstrap.Admin/Pages/Extensions/HttpContextExtensions.cs index 441757d9..44ba4537 100644 --- a/src/admin/Bootstrap.Admin/Pages/Extensions/HttpContextExtensions.cs +++ b/src/admin/Bootstrap.Admin/Pages/Extensions/HttpContextExtensions.cs @@ -1,9 +1,9 @@ -using System; -using System.Net; using Bootstrap.DataAccess; using Longbow.Cache; using Longbow.Web; using Microsoft.Extensions.DependencyInjection; +using System; +using System.Net; namespace Microsoft.AspNetCore.Http {