public static JsonSerializer GetJsonSerializer() { if (_jsonSerializer == null) { _jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings() { Converters = new List <JsonConverter>(WebApiUtils.GetJsonConverters()) }); } return(_jsonSerializer); }
public string GetParamJson() { return(JsonConvert.SerializeObject(this, WebApiUtils.GetJsonConverters())); }
public ActionResult Index() { IResponse response; try { string appKey = null; string key = null; string str = null; string strB = null; string accessToken = null; DateTime dateTime = DateTime.MinValue; NameValueCollection form = this.Request.Form; Dictionary <string, string> dictionary = new Dictionary <string, string>(); foreach (string allKey in form.AllKeys) { string s = form[allKey]; switch (allKey) { case "app_key": appKey = s; break; case "param_json": str = s; break; case "method": key = s; break; case "timestamp": long result; if (long.TryParse(s, out result)) { dateTime = new DateTime(1970, 1, 1).ToLocalTime().AddMilliseconds((double)result); break; } break; case "sign": strB = s; break; case "access_token": accessToken = s; break; } if (allKey != "sign") { dictionary.Add(allKey, s); } } if (dateTime < DateTime.Now.AddMinutes(-15.0)) { response = new BaseResponse() { ErrCode = "005" }; } else { AppInfo app = ServiceHelper.LoadService <IAppService>().GetApp(appKey); if (app != null) { if (string.Compare(WebApiUtils.SignYrqRequest(dictionary, app.AppSecret), strB, true) == 0) { Dictionary <string, ApiMethodInfo> apiMethods = Util.Utils.GetApiMethods(); if (apiMethods.ContainsKey(key)) { ApiMethodInfo apiMethodInfo = apiMethods[key]; IRequest <IResponse> request = (IRequest <IResponse>)JsonConvert.DeserializeObject(str ?? "{}", apiMethodInfo.RequestType, WebApiUtils.GetJsonConverters()); request.Validate(); this.Context.AppId = app.AppId; if (request is ICraftsReqeust) { ServiceHelper.LoadService <ICraftDbFactory>().CraftNO = ((ICraftsReqeust)request).CraftNO; } if (request is IUploadRequest) { IUploadRequest uploadRequest = (IUploadRequest)request; IDictionary <string, FileItem> fileParameters = new Dictionary <string, FileItem>(); foreach (string allKey in this.Request.Files.AllKeys) { HttpPostedFileBase httpPostedFileBase = this.Request.Files[allKey]; byte[] numArray = new byte[httpPostedFileBase.InputStream.Length]; httpPostedFileBase.InputStream.Read(numArray, 0, numArray.Length); fileParameters.Add(allKey, new FileItem(httpPostedFileBase.FileName, numArray)); httpPostedFileBase.InputStream.Dispose(); } uploadRequest.SetFileParamaters(fileParameters); } if (apiMethodInfo.IsCheckSession) { AuthInfo auth = ServiceHelper.LoadService <IAuthService>().GetAuth(accessToken); if (auth != null && auth.AppId == this.Context.AppId) { this.Context.AuthId = auth.AppId; this.Context.UserId = auth.UserId; response = (IResponse)apiMethodInfo.Method.Invoke(this, new object[1] { request }); } else { response = new BaseResponse() { ErrCode = "004" } }; } else { response = (IResponse)apiMethodInfo.Method.Invoke(this, new object[1] { request }); } } else { response = new BaseResponse() { ErrCode = "003" } }; } else { response = new BaseResponse() { ErrCode = "002" } }; } else { response = new BaseResponse() { ErrCode = "008" } }; } } catch (TargetInvocationException ex) { LogUtil.LogError("处理请求异常", ex.GetBaseException()); response = new BaseResponse() { ErrCode = "001", ErrMsg = ex.GetBaseException().Message }; } catch (Exception ex) { LogUtil.LogError("处理请求异常", ex); response = new BaseResponse() { ErrCode = "001", ErrMsg = ex.Message }; } return(Content(JsonConvert.SerializeObject(response, WebApiUtils.GetJsonConverters()))); }