From 1abec425a9cc6e11baf198491e23598a7ecc3398 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Thu, 13 Jan 2022 10:49:38 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20AutoRedirect?= =?UTF-8?q?=20=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/AutoRedirect/AutoRedirect.cs | 101 ++++++++++++++++++ .../Components/AutoRedirect/AutoRedirect.js | 27 +++++ .../wwwroot/js/bootstrap.blazor.bundle.min.js | 2 +- .../wwwroot/js/bootstrap.blazor.min.js | 2 +- 4 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 src/BootstrapBlazor/Components/AutoRedirect/AutoRedirect.cs create mode 100644 src/BootstrapBlazor/Components/AutoRedirect/AutoRedirect.js diff --git a/src/BootstrapBlazor/Components/AutoRedirect/AutoRedirect.cs b/src/BootstrapBlazor/Components/AutoRedirect/AutoRedirect.cs new file mode 100644 index 000000000..3d441427d --- /dev/null +++ b/src/BootstrapBlazor/Components/AutoRedirect/AutoRedirect.cs @@ -0,0 +1,101 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; +using System; +using System.Diagnostics.CodeAnalysis; +using System.Threading.Tasks; + +namespace BootstrapBlazor.Components; + +/// +/// 自动锁定组件 +/// +public class AutoRedirect : BootstrapComponentBase, IDisposable +{ + /// + /// 获得/设置 登出 Controller Url + /// + [Parameter] + public string? LogoutUrl { get; set; } + + /// + /// 获得/设置 自动锁屏间隔单位 秒 默认 60 秒 + /// + [Parameter] + public int Interval { get; set; } = 60; + + /// + /// 获得/设置 NavigationManager 实例 + /// + [Inject] + [NotNull] + private NavigationManager? NavigationManager { get; set; } + + private JSInterop? Interop { get; set; } + + /// + /// OnInitialized 方法 + /// + protected override void OnInitialized() + { + base.OnInitialized(); + + Interop = new JSInterop(JSRuntime); + } + + /// + /// OnAfterRenderAsync 方法 + /// + /// + /// + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (firstRender) + { + if (Interop != null) + { + await Interop.InvokeVoidAsync(this, null, "bb_auto_redirect", Interval, nameof(Lock)); + } + } + } + + /// + /// 锁屏操作由 JS 调用 + /// + [JSInvokable] + public void Lock() + { + if (!string.IsNullOrEmpty(LogoutUrl)) + { + NavigationManager.NavigateTo(LogoutUrl, true); + } + } + + /// + /// Dispose 方法 + /// + /// + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + if (Interop != null) + { + Interop.Dispose(); + Interop = null; + } + } + } + + /// + /// + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } +} diff --git a/src/BootstrapBlazor/Components/AutoRedirect/AutoRedirect.js b/src/BootstrapBlazor/Components/AutoRedirect/AutoRedirect.js new file mode 100644 index 000000000..63bb1873a --- /dev/null +++ b/src/BootstrapBlazor/Components/AutoRedirect/AutoRedirect.js @@ -0,0 +1,27 @@ +(function ($) { + $.extend({ + bb_auto_redirect: function (obj, interval, method) { + var mousePosition = {}; + var count = 1; + var traceMouseOrKey = window.setInterval(function () { + $(document).off('mousemove').one('mousemove', function (e) { + if (mousePosition.screenX !== e.screenX || mousePosition.screenY !== e.screenY) { + mousePosition.screenX = e.screenX; + mousePosition.screenY = e.screenY; + count = 1; + } + }); + $(document).off('keydown').one('keydown', function () { + count = 1; + }) + }, 1000); + var lockHandler = window.setInterval(function () { + if (count++ > interval) { + window.clearInterval(lockHandler); + window.clearInterval(traceMouseOrKey); + obj.invokeMethodAsync(method); + } + }, 1000); + } + }); +})(jQuery); diff --git a/src/BootstrapBlazor/wwwroot/js/bootstrap.blazor.bundle.min.js b/src/BootstrapBlazor/wwwroot/js/bootstrap.blazor.bundle.min.js index 3d497d635..7aed185d8 100644 --- a/src/BootstrapBlazor/wwwroot/js/bootstrap.blazor.bundle.min.js +++ b/src/BootstrapBlazor/wwwroot/js/bootstrap.blazor.bundle.min.js @@ -13,4 +13,4 @@ var QRCode;!function(){function a(a){this.mode=c.MODE_8BIT_BYTE,this.data=a,this /*! Summernote v0.8.18 | (c) 2013- Alan Hong and other contributors | MIT license */ !function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e(require("jquery"));else if("function"==typeof define&&define.amd)define(["jquery"],e);else{var n="object"==typeof exports?e(require("jquery")):e(t.jQuery);for(var o in n)("object"==typeof exports?exports:t)[o]=n[o]}}(self,(function(t){return(()=>{"use strict";var e={9458:e=>{e.exports=t}},n={};function o(t){var i=n[t];if(void 0!==i)return i.exports;var r=n[t]={exports:{}};return e[t](r,r.exports,o),r.exports}o.amdO={},o.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return o.d(e,{a:e}),e},o.d=(t,e)=>{for(var n in e)o.o(e,n)&&!o.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:e[n]})},o.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),o.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var i={};return(()=>{o.r(i);var t=o(9458),e=o.n(t);function n(t){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function r(t,e){for(var n=0;n'),u=s('