You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.3 KiB
C#

using Castle.DynamicProxy;
using Microsoft.AspNetCore.Http;
using System;
namespace Admin.Core.Extensions
{
/// <summary>
/// 面向切面的缓存使用
/// </summary>
public class AdminUserAuditAOP : CacheAOPbase
{
private readonly IHttpContextAccessor _accessor;
public AdminUserAuditAOP(IHttpContextAccessor accessor)
{
_accessor = accessor;
}
public override void Intercept(IInvocation invocation)
{
string UserName = _accessor.HttpContext?.User?.Identity?.Name;
//对当前方法的特性验证
if (invocation.Method.Name?.ToLower() == "add" || invocation.Method.Name?.ToLower() == "update")
{
if (invocation.Arguments.Length == 1)
{
if (invocation.Arguments[0].GetType().IsClass)
{
dynamic argModel = invocation.Arguments[0];
var getType = argModel.GetType();
if (invocation.Method.Name?.ToLower() == "add")
{
if (getType.GetProperty("CreateBy") != null)
{
argModel.CreateBy = UserName;
}
if (getType.GetProperty("bCreateTime") != null)
{
argModel.bCreateTime = DateTime.Now;
}
}
if (getType.GetProperty("bUpdateTime") != null)
{
argModel.bUpdateTime = DateTime.Now;
}
if (getType.GetProperty("ModifyBy") != null)
{
argModel.ModifyBy = UserName;
}
if (getType.GetProperty("bsubmitter") != null)
{
argModel.bsubmitter = UserName;
}
invocation.Arguments[0] = argModel;
}
}
invocation.Proceed();
}
else
{
invocation.Proceed();
}
}
}
}