Exemplo n.º 1
0
        public IActionResult DownUrlOption(DownUrlModel item)
        {
            using (var db = new DataContext(App.DbKey.Api))
            {
                var success = true;
                var model   = new ApiGatewayDownParam();
                model.Key           = item.Key;
                model.IsBody        = (int)item.IsBody;
                model.IsDecode      = (int)item.IsDecode;
                model.IsResult      = (int)item.IsResult;
                model.Method        = item.Method;
                model.Name          = item.Name;
                model.OrderBy       = item.OrderBy;
                model.Protocol      = item.Protocol;
                model.SoapMethod    = item.SoapMethod;
                model.SoapParamName = item.SoapParamName;
                model.SourceParam   = item.SourceParam;
                model.Url           = item.Url;
                model.WaitHour      = item.WaitHour;

                if (FastRead.Query <ApiGatewayDownParam>(a => a.Key.ToLower() == item.Key.ToLower() && a.OrderBy == item.OrderBy).ToCount(db) > 0)
                {
                    success = db.Update <ApiGatewayDownParam>(model, a => a.Key.ToLower() == item.Key.ToLower() && a.OrderBy == item.OrderBy).writeReturn.IsSuccess;
                }
                else
                {
                    success = db.Add(model).writeReturn.IsSuccess;
                }

                if (success)
                {
                    return(Json(new { success = true, msg = "操作成功" }));
                }
                else
                {
                    return(Json(new { success = false, msg = "操作失败" }));
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 处理请求
        /// </summary>
        /// <param name="downparam">下游</param>
        /// <param name="param">请求参数</param>
        /// <param name="content">请求参数body</param>
        /// <returns></returns>
        private static ReturnModel GetReuslt(ApiGatewayDownParam downparam, string param, string key, int isTextLog, int isDbLog, DataContext db, HttpContext context, string ActionId, int OrderBy, IHttpClientFactory client)
        {
            var info = FastRead.Query <ApiGatewayWait>(a => a.Key.ToLower() == key.ToLower() && a.Url.ToLower() == downparam.Url.ToLower()).ToItem <ApiGatewayWait>(db) ?? new ApiGatewayWait();

            if (info.Key.ToStr().ToLower() == key.ToLower() && DateTime.Compare(info.NextAction, DateTime.Now) > 0)
            {
                //return time out
                var dic = new Dictionary <string, object>();
                dic.Add("success", false);
                dic.Add("result", "等待恢复");
                return(new ReturnModel {
                    msg = BaseJson.ModelToJson(dic), status = 408
                });
            }
            else
            {
                var result    = new ReturnModel();
                var stopwatch = new Stopwatch();
                stopwatch.Start();

                if (downparam.Protocol.ToLower() == "soap")
                {
                    result = BaseUrl.SoapUrl(downparam.Url, downparam.SoapParamName, downparam.SoapMethod, param, client, key, downparam.SoapNamespace);
                }
                else if (downparam.Protocol.ToLower() == "http")
                {
                    //http
                    if (downparam.Method.ToStr().ToLower() == "post")
                    {
                        if (downparam.IsBody == 1)
                        {
                            result = BaseUrl.PostContent(downparam.Url, param, key, client);
                        }
                        else
                        {
                            result = BaseUrl.PostUrl(downparam.Url, param, key, client);
                        }
                    }
                    else if (downparam.Method.ToStr().ToLower() == "get")
                    {
                        result = BaseUrl.GetUrl(downparam.Url, param, key, client);
                    }
                }
                //else if (downparam.Protocol.ToLower() == "mq")
                //    //mq
                //    result = BaseUrl.RabbitUrl(downparam.QueueName, param);
                else
                {
                    result.status = 408;
                }

                if (result.status == 408)
                {
                    //time out
                    var wait = new ApiGatewayWait();
                    wait.Key        = key;
                    wait.Url        = downparam.Url;
                    wait.WaitHour   = downparam.WaitHour.ToInt(0);
                    wait.ErrorMsg   = result.msg;
                    wait.NextAction = DateTime.Now.AddHours(wait.WaitHour.ToStr().ToDouble(0));

                    FastWrite.Add(wait, db);
                }
                else if (info.Key.ToStr().ToLower() == key.ToLower())
                {
                    FastWrite.Delete <ApiGatewayWait>(a => a.Key.ToLower() == key.ToLower(), db);
                }

                stopwatch.Stop();

                var logInfo = new ApiGatewayLog();
                logInfo.ActionId     = ActionId;
                logInfo.OrderBy      = OrderBy;
                logInfo.Key          = key;
                logInfo.ActionTime   = DateTime.Now;
                logInfo.Url          = downparam.Url;
                logInfo.Protocol     = downparam.Protocol;
                logInfo.Success      = result.status == 200 ? 1 : 0;
                logInfo.Result       = result.msg;
                logInfo.Milliseconds = stopwatch.Elapsed.TotalMilliseconds;
                logInfo.ActionIp     = GetClientIp(context);
                logInfo.ActionParam  = param;

                if (isDbLog == 1)
                {
                    FastWrite.Add(logInfo, null, DbApi);
                }

                if (isTextLog == 1)
                {
                    BaseLog.SaveLog(BaseJson.ModelToJson(logInfo), logInfo.Key);
                }

                return(result);
            }
        }