From 04aa632a9454813a24e8e8927db17426ba5de834 Mon Sep 17 00:00:00 2001 From: zhangqi <2794379662@qq.com> Date: Tue, 13 Jul 2021 16:17:30 +0800 Subject: [PATCH] =?UTF-8?q?update:=E6=93=8D=E4=BD=9C=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E7=89=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api/Ewide.Core/Attributes/OpAttribute.cs | 18 +++++++++++ Api/Ewide.Core/Ewide.Core.xml | 7 +++++ Api/Ewide.Core/Filter/RequestActionFilter.cs | 33 +++++++++++++++++++- Api/Ewide.Core/Service/Auth/AuthService.cs | 4 ++- 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 Api/Ewide.Core/Attributes/OpAttribute.cs diff --git a/Api/Ewide.Core/Attributes/OpAttribute.cs b/Api/Ewide.Core/Attributes/OpAttribute.cs new file mode 100644 index 0000000..897608b --- /dev/null +++ b/Api/Ewide.Core/Attributes/OpAttribute.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Attributes +{ + public class OpAttribute : Attribute + { + private readonly LogOpType logOpType; + public OpAttribute(LogOpType logOpType) + { + this.logOpType = logOpType; + } + public int OpType { get => (int)logOpType; } + } +} diff --git a/Api/Ewide.Core/Ewide.Core.xml b/Api/Ewide.Core/Ewide.Core.xml index 8d6ebb4..4411fa2 100644 --- a/Api/Ewide.Core/Ewide.Core.xml +++ b/Api/Ewide.Core/Ewide.Core.xml @@ -2784,6 +2784,13 @@ 请求日志拦截 + + + 使用路由推断 + + + + 用户管理 diff --git a/Api/Ewide.Core/Filter/RequestActionFilter.cs b/Api/Ewide.Core/Filter/RequestActionFilter.cs index 6a37036..567e09a 100644 --- a/Api/Ewide.Core/Filter/RequestActionFilter.cs +++ b/Api/Ewide.Core/Filter/RequestActionFilter.cs @@ -10,6 +10,9 @@ using System.Threading.Tasks; using UAParser; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using System.Reflection; +using Ewide.Core.Attributes; +using System.Linq; namespace Ewide.Core { @@ -36,6 +39,11 @@ namespace Ewide.Core var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor; var descAtt = Attribute.GetCustomAttribute(actionDescriptor.MethodInfo, typeof(DescriptionAttribute)) as DescriptionAttribute; + //获取操作类型 + var method = actionDescriptor.MethodInfo; + var opAttr = method.GetCustomAttribute(); + var opType = opAttr?.OpType ?? InferOpType(httpRequest); + var message = "请求成功"; if (isRequestSucceed) { @@ -56,7 +64,7 @@ namespace Ewide.Core var sysOpLog = new SysLogOp { Name = descAtt != null ? descAtt.Description : actionDescriptor.ActionName, - OpType = 1, + OpType = opType, Success = isRequestSucceed, Message = message, Ip = httpContext.GetRemoteIpAddressToIPv4(), @@ -75,5 +83,28 @@ namespace Ewide.Core }; await sysOpLog.InsertNowAsync(); } + /// + /// 使用路由推断 + /// + /// + /// + int InferOpType(HttpRequest request) + { + var lastRoute = request.Path.Value.Split("/").Last(); + var opNames = Enum.GetNames(typeof(LogOpType)); + foreach (var op in opNames) + { + if (lastRoute.Contains(op, StringComparison.OrdinalIgnoreCase)) + { + return (int)Enum.Parse(typeof(LogOpType), op); + } + } + if (lastRoute.Contains("page", StringComparison.OrdinalIgnoreCase) || + lastRoute.Contains("list", StringComparison.OrdinalIgnoreCase)) + { + return (int)LogOpType.QUERY; + } + return 0; + } } } diff --git a/Api/Ewide.Core/Service/Auth/AuthService.cs b/Api/Ewide.Core/Service/Auth/AuthService.cs index 7894894..a23914a 100644 --- a/Api/Ewide.Core/Service/Auth/AuthService.cs +++ b/Api/Ewide.Core/Service/Auth/AuthService.cs @@ -1,4 +1,5 @@ -using Ewide.Core.Util; +using Ewide.Core.Attributes; +using Ewide.Core.Util; using Furion; using Furion.DatabaseAccessor; using Furion.DatabaseAccessor.Extensions; @@ -69,6 +70,7 @@ namespace Ewide.Core.Service /// [HttpPost("/login")] [AllowAnonymous] + [Op(LogOpType.GRANT)] public async Task LoginAsync([Required] LoginInput input) { var password = RSAHandler.RSADecrypt(input.Password);