Exemplo n.º 1
0
        public void OnActionExecuted(ActionExecutedContext context)
        {
            var api = context.ActionDescriptor.AttributeRouteInfo.Template;
            ResultDto <object> resultValue = null;
            ObjectResult       result      = null;
            var isEnableLog = IsEnableLog(context);

            try
            {
                // 异常返回
                if (context.Exception != null)
                {
                    return;
                }
                if (AppManager._proxyAppException != null)
                {
                    var exceptionResult = ResultBuilder.AsFail(AppManager._proxyAppException, app.ReuqestID);
                    AppManager._proxyAppException = null;
                    context.Result = new JsonResult(exceptionResult);
                    return;
                }
                if (app.ExceptionInfo != null)
                {
                    var exceptionResult = ResultBuilder.AsFail(app.ExceptionInfo, app.ReuqestID);
                    context.Result = new JsonResult(exceptionResult);
                    return;
                }

                if (!IsPackageResultDto(context))
                {
                    return;
                }

                result = (context.Result as ObjectResult);
                if (result == null)
                {
                    resultValue = new ResultDto <object>()
                    {
                        Data    = "此接口未定义返回数据,调用结果默认为成功。",
                        Success = true,
                        Message = "操作成功",
                    };
                }
                else
                {
                    resultValue = ResultBuilder.AsSuccess(result.Value);
                }

                resultValue.RequestId = app.ReuqestID;
                if (!string.IsNullOrEmpty(app.Message))
                {
                    resultValue.Message = app.Message;
                }

                context.Result = new JsonResult(resultValue);
                if (isEnableLog)
                {
                    app.LogResponseResult((object)resultValue ?? context.Result);
                }

                // todo 异步
                if (resultValue.Success)
                {
                    //// 推送系统消息
                    //var systemMessage = (context.ActionDescriptor as ControllerActionDescriptor).MethodInfo.GetCustomAttributes(typeof(SystemMessageAttribute), true);
                    //if (systemMessage.Count() == 1)
                    //{
                    //    var message = systemMessage[0] as SystemMessageAttribute;
                    //    message.RelationID = int.Parse(ValueExpression.GetValue(message.RelationIDExpression, app.ActionArguments, resultValue.Data));
                    //    message.ToUserID = int.Parse(ValueExpression.GetValue(message.ToUserIDExpression, app.ActionArguments, resultValue.Data));
                    //    app.PushSystemMessage(message);
                    //}

                    // 记录用户行为
                    var userBehavior = (context.ActionDescriptor as ControllerActionDescriptor).MethodInfo.GetCustomAttributes(typeof(UserBehaviorAttribute), true);
                    if (userBehavior.Count() == 1)
                    {
                        var behavior = userBehavior[0] as UserBehaviorAttribute;
                        behavior.BehaviorValue = ValueExpression.GetValue(behavior.BehaviorValueExpression, app.ActionArguments, resultValue.Data);
                        behavior.RelationID    = int.Parse(ValueExpression.GetValue(behavior.RelationIDExpression, app.ActionArguments, resultValue.Data));
                        app.AddUserBehavior(behavior);
                    }
                }
            }
            catch (Exception ex) { Lib.Log.WriteExceptionLog($"OnActionExecuted:Message:{ex.Message}  <br> StackTrace:{ex.StackTrace}"); }
            finally
            {
                if (isEnableLog)
                {
                    app.EndRequest();
                }
                CleanOnActionClose();
                if (resultValue != null)
                {
                    if (resultValue.Success)
                    {
                        CacheManager.WriteCache(resultValue, context, app.AppID);
                    }
                }
                else if (result != null && result.Value != null)
                {
                    CacheManager.WriteCache(result.Value, context, app.AppID);
                }
            }
        }