Beispiel #1
0
        /// <summary>
        ///     执行命令
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private ZeroOperatorStateType ExecCommand(ApiCallItem item)
        {
            if (!_apiActions.TryGetValue(item.ApiName.Trim(), out var action))
            {
                item.Result = ApiResult.NoFindJson;
                item.Status = OperatorStatus.NotFind;
                return(ZeroOperatorStateType.NotFind);
            }

            //1 还原调用上下文
            try
            {
                if (!string.IsNullOrWhiteSpace(item.ContextJson))
                {
                    ApiContext.SetContext(JsonConvert.DeserializeObject <ApiContext>(item.ContextJson));
                }

                ApiContext.SetRequestContext(item.GlobalId, item.Requester, item.RequestId);
            }
            catch (Exception e)
            {
                ZeroTrace.WriteException(StationName, e, item.ApiName, "restory context", item.ContextJson);
                item.Result = ApiResult.ArgumentErrorJson;
                item.Status = OperatorStatus.FormalError;
                return(ZeroOperatorStateType.LocalException);
            }

            //2 确定调用方法及对应权限
            if (action.NeedLogin && (ApiContext.Customer == null || ApiContext.Customer.UserId <= 0))
            {
                item.Result = ApiResult.DenyAccessJson;
                item.Status = OperatorStatus.DenyAccess;
                return(ZeroOperatorStateType.DenyAccess);
            }

            //3 参数校验
            try
            {
                if (!action.RestoreArgument(item.Argument ?? "{}"))
                {
                    item.Result = ApiResult.ArgumentErrorJson;
                    item.Status = OperatorStatus.FormalError;
                    return(ZeroOperatorStateType.ArgumentInvalid);
                }
            }
            catch (Exception e)
            {
                ZeroTrace.WriteException(StationName, e, item.ApiName, "restory argument", item.Argument);
                item.Result = ApiResult.LocalExceptionJson;
                item.Status = OperatorStatus.FormalError;
                return(ZeroOperatorStateType.LocalException);
            }

            try
            {
                if (!action.Validate(out var message))
                {
                    item.Result = JsonConvert.SerializeObject(ApiResult.Error(ErrorCode.LogicalError, message));
                    item.Status = OperatorStatus.LogicalError;
                    return(ZeroOperatorStateType.ArgumentInvalid);
                }
            }
            catch (Exception e)
            {
                ZeroTrace.WriteException(StationName, e, item.ApiName, "invalidate argument", item.Argument);
                item.Result = ApiResult.LocalExceptionJson;
                item.Status = OperatorStatus.LocalException;
                return(ZeroOperatorStateType.LocalException);
            }

            //4 方法执行
            try
            {
                var result = action.Execute();
                if (result != null)
                {
                    if (result.Status == null)
                    {
                        result.Status = new ApiStatsResult {
                            InnerMessage = item.GlobalId
                        }
                    }
                    ;
                    else
                    {
                        result.Status.InnerMessage = item.GlobalId;
                    }
                }

                item.Result = result == null ? ApiResult.SucceesJson : JsonConvert.SerializeObject(result);
                item.Status = result == null || result.Success ? OperatorStatus.Success : OperatorStatus.LogicalError;
                return(result == null || result.Success ? ZeroOperatorStateType.Ok : ZeroOperatorStateType.Failed);
            }
            catch (Exception e)
            {
                ZeroTrace.WriteException(StationName, e, item.ApiName, "execute", JsonConvert.SerializeObject(item));
                item.Result = ApiResult.LocalExceptionJson;
                item.Status = OperatorStatus.LocalException;
                return(ZeroOperatorStateType.LocalException);
            }
        }