WYB
2021-03-22 91b8cdad021ab052e4991f3d41834a6f0ddc36b8
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
using Castle.DynamicProxy;
using Microsoft.AspNetCore.Http;
using System;
 
namespace JiepeiWMS.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();
            }
        }
    }
 
}