From a2d4a58b6b54b5b262b3a51de5c64a822f95f92a Mon Sep 17 00:00:00 2001 From: zpc Date: Fri, 29 Nov 2024 19:39:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Filter/CustomResultFilter.cs | 118 ++++++++++-------- 1 file changed, 64 insertions(+), 54 deletions(-) diff --git a/src/CloudGaming/Code/CloudGaming.Code/Filter/CustomResultFilter.cs b/src/CloudGaming/Code/CloudGaming.Code/Filter/CustomResultFilter.cs index 1be4ee3..d115e2a 100644 --- a/src/CloudGaming/Code/CloudGaming.Code/Filter/CustomResultFilter.cs +++ b/src/CloudGaming/Code/CloudGaming.Code/Filter/CustomResultFilter.cs @@ -32,66 +32,76 @@ public class CustomResultFilter : IResultFilter /// public void OnResultExecuting(ResultExecutingContext context) { - var httpContext = context.HttpContext; - var path = httpContext.Request.Path.Value ?? ""; - var apiPrefix = path.Replace('/', '.').TrimStart('.'); - var sw = Stopwatch.StartNew(); - CloudGamingBase cloudGamingBase = new CloudGamingBase(_serviceProvider); - //// 获取当前用户的信息 - //var user = httpContext.User.Identity.IsAuthenticated ? httpContext.User.Identity.Name : "Anonymous"; - if (context.Result is ObjectResult objectResult && objectResult.Value != null) + if (context.Result is ObjectResult objectResult) { - var x = objectResult.Value.GetType(); - object? value = null; - if (!x.FullName.Contains("HuanMeng.DotNetCore.Base.BaseResponse")) + if (objectResult.Value == null) { - //特殊处理 - if (objectResult.Value is string) - { - BaseResponse baseStringResponse = new BaseResponse(ResponseCode.Success, "", objectResult.Value.ToString()); - value = baseStringResponse; - if (context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor) - { - var messageAttribute = MessageAttributeExtend.GetMessageAttribute(controllerActionDescriptor); - // 如果存在MessageAttribute,则设置响应消息 - if (messageAttribute != null) - { - baseStringResponse.Message = messageAttribute.Message; - } - } - sw.Stop(); - context.HttpContext.Response.Headers.TryAdd("X-Request-Duration-Filter", sw.Elapsed.TotalMilliseconds.ToString()); - objectResult.Value = baseStringResponse.ToString(); - return; - - } - else - { - BaseResponse baseResponse = new BaseResponse(ResponseCode.Success, "", objectResult.Value); - value = baseResponse; - // 获取当前执行的Action方法的信息并进行类型检查 - if (context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor) - { - var messageAttribute = MessageAttributeExtend.GetMessageAttribute(controllerActionDescriptor); - // 如果存在MessageAttribute,则设置响应消息 - if (messageAttribute != null) - { - baseResponse.Message = messageAttribute.Message; - } - } - } - var dic = value.ToDictionaryOrList(apiPrefix, it => cloudGamingBase.Cache.ImageEntityCache[it]); - objectResult.Value = dic; + BaseResponse baseStringResponse = new BaseResponse(ResponseCode.Success, "", null); + objectResult.Value = baseStringResponse.ToString(); + return; } - //else - //{ - // //value = objectResult.Value; - //} + + // 获取当前用户的信息 + if (objectResult.Value != null) + { + var httpContext = context.HttpContext; + var path = httpContext.Request.Path.Value ?? ""; + var apiPrefix = path.Replace('/', '.').TrimStart('.'); + var sw = Stopwatch.StartNew(); + CloudGamingBase cloudGamingBase = new CloudGamingBase(_serviceProvider); + var x = objectResult.Value.GetType(); + object? value = null; + if (!x.FullName.Contains("HuanMeng.DotNetCore.Base.BaseResponse")) + { + //特殊处理 + if (objectResult.Value is string) + { + BaseResponse baseStringResponse = new BaseResponse(ResponseCode.Success, "", objectResult.Value.ToString()); + value = baseStringResponse; + if (context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor) + { + var messageAttribute = MessageAttributeExtend.GetMessageAttribute(controllerActionDescriptor); + // 如果存在MessageAttribute,则设置响应消息 + if (messageAttribute != null) + { + baseStringResponse.Message = messageAttribute.Message; + } + } + sw.Stop(); + context.HttpContext.Response.Headers.TryAdd("X-Request-Duration-Filter", sw.Elapsed.TotalMilliseconds.ToString()); + objectResult.Value = baseStringResponse.ToString(); + return; + + } + else + { + BaseResponse baseResponse = new BaseResponse(ResponseCode.Success, "", objectResult.Value); + value = baseResponse; + // 获取当前执行的Action方法的信息并进行类型检查 + if (context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor) + { + var messageAttribute = MessageAttributeExtend.GetMessageAttribute(controllerActionDescriptor); + // 如果存在MessageAttribute,则设置响应消息 + if (messageAttribute != null) + { + baseResponse.Message = messageAttribute.Message; + } + } + } + var dic = value.ToDictionaryOrList(apiPrefix, it => cloudGamingBase.Cache.ImageEntityCache[it]); + objectResult.Value = dic; + } + //else + //{ + // //value = objectResult.Value; + //} - sw.Stop(); - context.HttpContext.Response.Headers.TryAdd("X-Request-Duration-Filter", sw.Elapsed.TotalMilliseconds.ToString()); + sw.Stop(); + context.HttpContext.Response.Headers.TryAdd("X-Request-Duration-Filter", sw.Elapsed.TotalMilliseconds.ToString()); + } } + } ///