update:操作日志特性

This commit is contained in:
2021-07-13 16:17:30 +08:00
parent 1011b98bab
commit 04aa632a94
4 changed files with 60 additions and 2 deletions

View File

@@ -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; }
}
}

View File

@@ -2784,6 +2784,13 @@
请求日志拦截
</summary>
</member>
<member name="M:Ewide.Core.RequestActionFilter.InferOpType(Microsoft.AspNetCore.Http.HttpRequest)">
<summary>
使用路由推断
</summary>
<param name="request"></param>
<returns></returns>
</member>
<member name="T:Ewide.Core.UserManager">
<summary>
用户管理

View File

@@ -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<OpAttribute>();
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();
}
/// <summary>
/// 使用路由推断
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
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;
}
}
}

View File

@@ -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
/// <returns></returns>
[HttpPost("/login")]
[AllowAnonymous]
[Op(LogOpType.GRANT)]
public async Task<LoginOutput> LoginAsync([Required] LoginInput input)
{
var password = RSAHandler.RSADecrypt(input.Password);