forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathBlogUserAuditAOP.cs
More file actions
73 lines (66 loc) · 2.4 KB
/
BlogUserAuditAOP.cs
File metadata and controls
73 lines (66 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using Blog.Core.Common;
using Blog.Core.Common.MemoryCache;
using Castle.DynamicProxy;
using Microsoft.AspNetCore.Http;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Blog.Core.AOP
{
/// <summary>
/// 面向切面的缓存使用
/// </summary>
public class BlogUserAuditAOP : CacheAOPbase
{
private readonly IHttpContextAccessor _accessor;
public BlogUserAuditAOP(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();
}
}
}
}