This commit is contained in:
cjd
2025-11-04 21:09:16 +08:00
parent 8260e293c7
commit bb90a020dc
592 changed files with 61749 additions and 27 deletions

View File

@@ -0,0 +1,84 @@
using System;
using DouyinApi.Common;
using Castle.DynamicProxy;
using System.Linq;
using System.Threading.Tasks;
using DouyinApi.Common.Caches;
using DouyinApi.Common.Caches.Interface;
namespace DouyinApi.AOP
{
/// <summary>
/// 面向切面的缓存使用
/// </summary>
public class BlogCacheAOP : CacheAOPbase
{
//通过注入的方式,把缓存操作接口通过构造函数注入
private readonly ICaching _cache;
public BlogCacheAOP(ICaching cache)
{
_cache = cache;
}
//Intercept方法是拦截的关键所在也是IInterceptor接口中的唯一定义
public override void Intercept(IInvocation invocation)
{
var method = invocation.MethodInvocationTarget ?? invocation.Method;
//对当前方法的特性验证
//如果需要验证
var CachingAttribute = method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute));
if (CachingAttribute is CachingAttribute qCachingAttribute)
{
//获取自定义缓存键
var cacheKey = CustomCacheKey(invocation);
if (_cache.Exists(cacheKey))
{
//将当前获取到的缓存值,赋值给当前执行方法
Type returnType;
if (typeof(Task).IsAssignableFrom(method.ReturnType))
{
returnType = method.ReturnType.GenericTypeArguments.FirstOrDefault();
}
else
{
returnType = method.ReturnType;
}
//根据key获取相应的缓存值
dynamic cacheValue = _cache.Get(returnType, cacheKey);
invocation.ReturnValue = (typeof(Task).IsAssignableFrom(method.ReturnType)) ? Task.FromResult(cacheValue) : cacheValue;
return;
}
//去执行当前的方法
invocation.Proceed();
//存入缓存
if (!string.IsNullOrWhiteSpace(cacheKey))
{
object response;
//Type type = invocation.ReturnValue?.GetType();
var type = invocation.Method.ReturnType;
if (typeof(Task).IsAssignableFrom(type))
{
dynamic result = invocation.ReturnValue;
response = result.Result;
}
else
{
response = invocation.ReturnValue;
}
if (response == null) response = string.Empty;
_cache.Set(cacheKey, response, TimeSpan.FromMinutes(qCachingAttribute.AbsoluteExpiration));
}
}
else
{
invocation.Proceed(); //直接执行被拦截方法
}
}
}
}

View File

@@ -0,0 +1,291 @@
using DouyinApi.Common;
using DouyinApi.Common.LogHelper;
using DouyinApi.Hubs;
using Castle.DynamicProxy;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using StackExchange.Profiling;
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace DouyinApi.AOP
{
/// <summary>
/// 拦截器BlogLogAOP 继承IInterceptor接口
/// </summary>
public class BlogLogAOP : IInterceptor
{
private readonly IHubContext<ChatHub> _hubContext;
private readonly IHttpContextAccessor _accessor;
public BlogLogAOP(IHubContext<ChatHub> hubContext, IHttpContextAccessor accessor)
{
_hubContext = hubContext;
_accessor = accessor;
}
/// <summary>
/// 实例化IInterceptor唯一方法
/// </summary>
/// <param name="invocation">包含被拦截方法的信息</param>
public void Intercept(IInvocation invocation)
{
string UserName = _accessor.HttpContext?.User?.Identity?.Name;
string json;
try
{
json = JsonConvert.SerializeObject(invocation.Arguments);
}
catch (Exception ex)
{
json = "无法序列化,可能是兰姆达表达式等原因造成,按照框架优化代码" + ex.ToString();
}
DateTime startTime = DateTime.Now;
AOPLogInfo apiLogAopInfo = new AOPLogInfo
{
RequestTime = startTime.ToString("yyyy-MM-dd hh:mm:ss fff"),
OpUserName = UserName,
RequestMethodName = invocation.Method.Name,
RequestParamsName = string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray()),
ResponseJsonData = json
};
//测试异常记录
//Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff"));
//记录被拦截方法信息的日志信息
//var dataIntercept = "" +
// $"【当前操作用户】:{ UserName} \r\n" +
// $"【当前执行方法】:{ invocation.Method.Name} \r\n" +
// $"【携带的参数有】: {string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray())} \r\n";
try
{
MiniProfiler.Current.Step($"执行Service方法{invocation.Method.Name}() -> ");
//在被拦截的方法执行完毕后 继续执行当前方法,注意是被拦截的是异步的
invocation.Proceed();
// 异步获取异常,先执行
if (IsAsyncMethod(invocation.Method))
{
#region
//Wait task execution and modify return value
if (invocation.Method.ReturnType == typeof(Task))
{
invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally(
(Task) invocation.ReturnValue,
async () => await SuccessAction(invocation, apiLogAopInfo, startTime), /*成功时执行*/
ex =>
{
LogEx(ex, apiLogAopInfo);
});
}
//Task<TResult>
else
{
invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult(
invocation.Method.ReturnType.GenericTypeArguments[0],
invocation.ReturnValue,
//async () => await SuccessAction(invocation, dataIntercept),/*成功时执行*/
async (o) => await SuccessAction(invocation, apiLogAopInfo, startTime, o), /*成功时执行*/
ex =>
{
LogEx(ex, apiLogAopInfo);
});
}
#endregion
// 如果方案一不行,试试这个方案
//#region 方案二
//var type = invocation.Method.ReturnType;
//var resultProperty = type.GetProperty("Result");
//DateTime endTime = DateTime.Now;
//string ResponseTime = (endTime - startTime).Milliseconds.ToString();
//apiLogAopInfo.ResponseTime = endTime.ToString("yyyy-MM-dd hh:mm:ss fff");
//apiLogAopInfo.ResponseIntervalTime = ResponseTime + "ms";
//apiLogAopInfo.ResponseJsonData = JsonConvert.SerializeObject(resultProperty.GetValue(invocation.ReturnValue));
////dataIntercept += ($"【响应时间】:{ResponseTime}ms\r\n");
////dataIntercept += ($"【执行完成时间】:{endTime.ToString("yyyy-MM-dd hh:mm:ss fff")}\r\n");
////dataIntercept += ($"【执行完成结果】:{JsonConvert.SerializeObject(resultProperty.GetValue(invocation.ReturnValue))}\r\n");
//Parallel.For(0, 1, e =>
//{
// //LogLock.OutLogAOP("AOPLog", new string[] { dataIntercept });
// LogLock.OutLogAOP("AOPLog", new string[] { apiLogAopInfo.GetType().ToString() + " - ResponseJsonDataType:" + type, JsonConvert.SerializeObject(apiLogAopInfo) });
//});
//#endregion
}
else
{
// 同步1
string jsonResult;
try
{
jsonResult = JsonConvert.SerializeObject(invocation.ReturnValue);
}
catch (Exception ex)
{
jsonResult = "无法序列化,可能是兰姆达表达式等原因造成,按照框架优化代码" + ex.ToString();
}
var type = invocation.Method.ReturnType;
var resultProperty = type.GetProperty("Result");
DateTime endTime = DateTime.Now;
string ResponseTime = (endTime - startTime).Milliseconds.ToString();
apiLogAopInfo.ResponseTime = endTime.ToString("yyyy-MM-dd hh:mm:ss fff");
apiLogAopInfo.ResponseIntervalTime = ResponseTime + "ms";
//apiLogAopInfo.ResponseJsonData = JsonConvert.SerializeObject(resultProperty.GetValue(invocation.ReturnValue));
apiLogAopInfo.ResponseJsonData = jsonResult;
//dataIntercept += ($"【执行完成结果】:{jsonResult}");
Parallel.For(0, 1, e =>
{
//LogLock.OutLogAOP("AOPLog", new string[] { dataIntercept });
LogLock.OutLogAOP("AOPLog", _accessor.HttpContext?.TraceIdentifier,
new string[] {apiLogAopInfo.GetType().ToString(), JsonConvert.SerializeObject(apiLogAopInfo)});
});
}
}
catch (Exception ex) // 同步2
{
LogEx(ex, apiLogAopInfo);
throw;
}
if (AppSettings.app(new string[] {"Middleware", "SignalRSendLog", "Enabled"}).ObjToBool())
{
_hubContext.Clients.All.SendAsync("ReceiveUpdate", LogLock.GetLogData()).Wait();
}
}
private async Task SuccessAction(IInvocation invocation, AOPLogInfo apiLogAopInfo, DateTime startTime, object o = null)
{
//invocation.ReturnValue = o;
//var type = invocation.Method.ReturnType;
//if (typeof(Task).IsAssignableFrom(type))
//{
// //var resultProperty = type.GetProperty("Result");
// //类型错误 都可以不要invocation参数直接将o系列化保存到日记中
// dataIntercept += ($"【执行完成结果】:{JsonConvert.SerializeObject(invocation.ReturnValue)}");
//}
//else
//{
// dataIntercept += ($"【执行完成结果】:{invocation.ReturnValue}");
//}
DateTime endTime = DateTime.Now;
string ResponseTime = (endTime - startTime).Milliseconds.ToString();
apiLogAopInfo.ResponseTime = endTime.ToString("yyyy-MM-dd hh:mm:ss fff");
apiLogAopInfo.ResponseIntervalTime = ResponseTime + "ms";
apiLogAopInfo.ResponseJsonData = JsonConvert.SerializeObject(o);
await Task.Run(() =>
{
Parallel.For(0, 1, e =>
{
//LogLock.OutSql2Log("AOPLog", new string[] { JsonConvert.SerializeObject(apiLogAopInfo) });
LogLock.OutLogAOP("AOPLog", _accessor.HttpContext?.TraceIdentifier,
new string[] {apiLogAopInfo.GetType().ToString(), JsonConvert.SerializeObject(apiLogAopInfo)});
});
});
}
private void LogEx(Exception ex, AOPLogInfo dataIntercept)
{
if (ex != null)
{
//执行的 service 中,收录异常
MiniProfiler.Current.CustomTiming("Errors", ex.Message);
//执行的 service 中,捕获异常
//dataIntercept += ($"【执行完成结果】:方法中出现异常:{ex.Message + ex.InnerException}\r\n");
AOPLogExInfo apiLogAopExInfo = new AOPLogExInfo
{
ExMessage = ex.Message,
InnerException = "InnerException-内部异常:\r\n" + (ex.InnerException == null ? "" : ex.InnerException.InnerException.ToString()) +
("\r\nStackTrace-堆栈跟踪:\r\n") + (ex.StackTrace == null ? "" : ex.StackTrace.ToString()),
ApiLogAopInfo = dataIntercept
};
// 异常日志里有详细的堆栈信息
Parallel.For(0, 1, e =>
{
//LogLock.OutLogAOP("AOPLogEx", new string[] { dataIntercept });
LogLock.OutLogAOP("AOPLogEx", _accessor.HttpContext?.TraceIdentifier,
new string[] {apiLogAopExInfo.GetType().ToString(), JsonConvert.SerializeObject(apiLogAopExInfo)});
});
}
}
public static bool IsAsyncMethod(MethodInfo method)
{
return (
method.ReturnType == typeof(Task) ||
(method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>))
);
}
}
internal static class InternalAsyncHelper
{
public static async Task AwaitTaskWithPostActionAndFinally(Task actualReturnValue, Func<Task> postAction, Action<Exception> finalAction)
{
Exception exception = null;
try
{
await actualReturnValue;
await postAction();
}
catch (Exception ex)
{
exception = ex;
}
finally
{
finalAction(exception);
}
}
public static async Task<T> AwaitTaskWithPostActionAndFinallyAndGetResult<T>(Task<T> actualReturnValue, Func<object, Task> postAction,
Action<Exception> finalAction)
{
Exception exception = null;
try
{
var result = await actualReturnValue;
await postAction(result);
return result;
}
catch (Exception ex)
{
exception = ex;
throw;
}
finally
{
finalAction(exception);
}
}
public static object CallAwaitTaskWithPostActionAndFinallyAndGetResult(Type taskReturnType, object actualReturnValue,
Func<object, Task> action, Action<Exception> finalAction)
{
return typeof(InternalAsyncHelper)
.GetMethod("AwaitTaskWithPostActionAndFinallyAndGetResult", BindingFlags.Public | BindingFlags.Static)
.MakeGenericMethod(taskReturnType)
.Invoke(null, new object[] {actualReturnValue, action, finalAction});
}
}
}

View File

@@ -0,0 +1,139 @@
using DouyinApi.Common;
using Castle.DynamicProxy;
using Microsoft.Extensions.Logging;
using System;
using System.Reflection;
using System.Threading.Tasks;
using DouyinApi.Common.DB;
using DouyinApi.Repository.UnitOfWorks;
namespace DouyinApi.AOP
{
/// <summary>
/// 事务拦截器BlogTranAOP 继承IInterceptor接口
/// </summary>
public class BlogTranAOP : IInterceptor
{
private readonly ILogger<BlogTranAOP> _logger;
private readonly IUnitOfWorkManage _unitOfWorkManage;
public BlogTranAOP(IUnitOfWorkManage unitOfWorkManage, ILogger<BlogTranAOP> logger)
{
_unitOfWorkManage = unitOfWorkManage;
_logger = logger;
}
/// <summary>
/// 实例化IInterceptor唯一方法
/// </summary>
/// <param name="invocation">包含被拦截方法的信息</param>
public void Intercept(IInvocation invocation)
{
var method = invocation.MethodInvocationTarget ?? invocation.Method;
//对当前方法的特性验证
//如果需要验证
if (method.GetCustomAttribute<UseTranAttribute>(true) is { } uta)
{
try
{
Before(method, uta.Propagation);
invocation.Proceed();
// 异步获取异常,先执行
if (IsAsyncMethod(invocation.Method))
{
var result = invocation.ReturnValue;
if (result is Task)
{
Task.WaitAll(result as Task);
}
}
After(method);
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
AfterException(method);
throw;
}
}
else
{
invocation.Proceed(); //直接执行被拦截方法
}
}
private void Before(MethodInfo method, Propagation propagation)
{
switch (propagation)
{
case Propagation.Required:
if (_unitOfWorkManage.TranCount <= 0)
{
_logger.LogDebug($"Begin Transaction");
Console.WriteLine($"Begin Transaction");
_unitOfWorkManage.BeginTran(method);
}
break;
case Propagation.Mandatory:
if (_unitOfWorkManage.TranCount <= 0)
{
throw new Exception("事务传播机制为:[Mandatory],当前不存在事务");
}
break;
case Propagation.Nested:
_logger.LogDebug($"Begin Transaction");
Console.WriteLine($"Begin Transaction");
_unitOfWorkManage.BeginTran(method);
break;
default:
throw new ArgumentOutOfRangeException(nameof(propagation), propagation, null);
}
}
private void After(MethodInfo method)
{
_unitOfWorkManage.CommitTran(method);
}
private void AfterException(MethodInfo method)
{
_unitOfWorkManage.RollbackTran(method);
}
/// <summary>
/// 获取变量的默认值
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public object GetDefaultValue(Type type)
{
return type.IsValueType ? Activator.CreateInstance(type) : null;
}
private async Task SuccessAction(IInvocation invocation)
{
await Task.Run(() =>
{
//...
});
}
public static bool IsAsyncMethod(MethodInfo method)
{
return (
method.ReturnType == typeof(Task) ||
(method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>))
);
}
private async Task TestActionAsync(IInvocation invocation)
{
await Task.Run(null);
}
}
}

View File

@@ -0,0 +1,69 @@
using Castle.DynamicProxy;
using Microsoft.AspNetCore.Http;
using System;
namespace DouyinApi.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();
}
}
}
}

View File

@@ -0,0 +1,184 @@
using DouyinApi.Common.Helper;
using Castle.DynamicProxy;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace DouyinApi.AOP
{
public abstract class CacheAOPbase : IInterceptor
{
/// <summary>
/// AOP的拦截方法
/// </summary>
/// <param name="invocation"></param>
public abstract void Intercept(IInvocation invocation);
/// <summary>
/// 自定义缓存的key
/// </summary>
/// <param name="invocation"></param>
/// <returns></returns>
protected string CustomCacheKey(IInvocation invocation)
{
var typeName = invocation.TargetType.Name;
var methodName = invocation.Method.Name;
var methodArguments = invocation.Arguments.Select(GetArgumentValue).Take(3).ToList();//获取参数列表,最多三个
string key = $"{typeName}:{methodName}:";
foreach (var param in methodArguments)
{
key = $"{key}{param}:";
}
return key.TrimEnd(':');
}
/// <summary>
/// object 转 string
/// </summary>
/// <param name="arg"></param>
/// <returns></returns>
protected static string GetArgumentValue(object arg)
{
if (arg is DateTime)
return ((DateTime)arg).ToString("yyyyMMddHHmmss");
if (!arg.IsNotEmptyOrNull())
return arg.ObjToString();
if (arg != null)
{
if (arg is Expression)
{
var obj = arg as Expression;
var result = Resolve(obj);
return MD5Helper.MD5Encrypt16(result);
}
else if (arg.GetType().IsClass)
{
return MD5Helper.MD5Encrypt16(JsonConvert.SerializeObject(arg));
}
return $"value:{arg.ObjToString()}";
}
return string.Empty;
}
private static string Resolve(Expression expression)
{
ExpressionContext expContext = new ExpressionContext();
expContext.Resolve(expression, ResolveExpressType.WhereSingle);
var value = expContext.Result.GetString();
var pars = expContext.Parameters;
pars.ForEach(s =>
{
value = value.Replace(s.ParameterName, s.Value.ObjToString());
});
return value;
}
private static string GetOperator(ExpressionType expressiontype)
{
switch (expressiontype)
{
case ExpressionType.And:
return "and";
case ExpressionType.AndAlso:
return "and";
case ExpressionType.Or:
return "or";
case ExpressionType.OrElse:
return "or";
case ExpressionType.Equal:
return "=";
case ExpressionType.NotEqual:
return "<>";
case ExpressionType.LessThan:
return "<";
case ExpressionType.LessThanOrEqual:
return "<=";
case ExpressionType.GreaterThan:
return ">";
case ExpressionType.GreaterThanOrEqual:
return ">=";
default:
throw new Exception($"不支持{expressiontype}此种运算符查找!");
}
}
private static string ResolveFunc(Expression left, Expression right, ExpressionType expressiontype)
{
var Name = (left as MemberExpression).Member.Name;
var Value = (right as ConstantExpression).Value;
var Operator = GetOperator(expressiontype);
return Name + Operator + Value ?? "null";
}
private static string ResolveLinqToObject(Expression expression, object value, ExpressionType? expressiontype = null)
{
var MethodCall = expression as MethodCallExpression;
var MethodName = MethodCall.Method.Name;
switch (MethodName)
{
case "Contains":
if (MethodCall.Object != null)
return Like(MethodCall);
return In(MethodCall, value);
case "Count":
return Len(MethodCall, value, expressiontype.Value);
case "LongCount":
return Len(MethodCall, value, expressiontype.Value);
default:
throw new Exception($"不支持{MethodName}方法的查找!");
}
}
private static string In(MethodCallExpression expression, object isTrue)
{
var Argument1 = (expression.Arguments[0] as MemberExpression).Expression as ConstantExpression;
var Argument2 = expression.Arguments[1] as MemberExpression;
var Field_Array = Argument1.Value.GetType().GetFields().First();
object[] Array = Field_Array.GetValue(Argument1.Value) as object[];
List<string> SetInPara = new List<string>();
for (int i = 0; i < Array.Length; i++)
{
string Name_para = "InParameter" + i;
string Value = Array[i].ToString();
SetInPara.Add(Value);
}
string Name = Argument2.Member.Name;
string Operator = Convert.ToBoolean(isTrue) ? "in" : " not in";
string CompName = string.Join(",", SetInPara);
string Result = $"{Name} {Operator} ({CompName})";
return Result;
}
private static string Like(MethodCallExpression expression)
{
var Temp = expression.Arguments[0];
LambdaExpression lambda = Expression.Lambda(Temp);
Delegate fn = lambda.Compile();
var tempValue = Expression.Constant(fn.DynamicInvoke(null), Temp.Type);
string Value = $"%{tempValue}%";
string Name = (expression.Object as MemberExpression).Member.Name;
string Result = $"{Name} like {Value}";
return Result;
}
private static string Len(MethodCallExpression expression, object value, ExpressionType expressiontype)
{
object Name = (expression.Arguments[0] as MemberExpression).Member.Name;
string Operator = GetOperator(expressiontype);
string Result = $"len({Name}){Operator}{value.ToString()}";
return Result;
}
}
}