| | |
| | | [HttpPost] |
| | | [Route("InboundDataSync")] |
| | | public IHttpActionResult InboundDataSync(InboundDataSyncInfo model) { |
| | | LogHelper.Info("触发API:入库数据同步" + JsonConvert.SerializeObject(model), "API"); |
| | | LogHelper.Info("触发API:InboundDataSync:入库数据同步" + JsonConvert.SerializeObject(model), "API"); |
| | | |
| | | //var headers = Request.Headers; // 暂不考虑对请求HTTP进行校验,默认发来的请求格式都正确 |
| | | |
| | | //return new ErpResult { errCode = 1, errMsg = "测试" }; |
| | | //return CreateSuccessResponse(new ErpResult { errCode = 1, errMsg = "测试"}); |
| | | |
| | | //var headers = Request.Headers; |
| | | //return ProcessInboundData(headers, model); |
| | | |
| | | //return ApiHelper.InboundDataSync(model); |
| | | return NormalProcessInboundData(model); // 暂时使用原本的逻辑 |
| | | var res = ApiHelper.InboundDataSync(model); |
| | | return CreateSuccessResponse(res); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | [HttpPost] |
| | | [Route("OutboundDataSync")] |
| | | public IHttpActionResult OutboundDataSync(OutboundDataSyncInfo model) { |
| | | LogHelper.Info("触发API:出库发料同步" + JsonConvert.SerializeObject(model), "API"); |
| | | |
| | | //var headers = Request.Headers; |
| | | //return ProcessOutboundData(headers, model); |
| | | |
| | | //return ApiHelper.OutboundDataSync(model); |
| | | return NormalProcessOutboundData(model); // 暂时使用原本的逻辑 |
| | | } |
| | | |
| | | private IHttpActionResult NormalProcessInboundData(InboundDataSyncInfo model) { |
| | | var data = ApiHelper.InboundDataSync(model); |
| | | return ResponseMessage(new HttpResponseMessage(HttpStatusCode.OK) { |
| | | Headers = { }, |
| | | Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json") |
| | | }); |
| | | } |
| | | |
| | | private IHttpActionResult NormalProcessOutboundData(OutboundDataSyncInfo model) { |
| | | var data = ApiHelper.OutboundDataSync(model); |
| | | return ResponseMessage(new HttpResponseMessage(HttpStatusCode.OK) { |
| | | Headers = { }, |
| | | Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json") |
| | | }); |
| | | } |
| | | |
| | | private IHttpActionResult ProcessInboundData(System.Net.Http.Headers.HttpRequestHeaders headers, InboundDataSyncInfo model) { |
| | | // 必须字段验证 |
| | | if (!headers.Contains("sourceAppCode") || !headers.Contains("serviceId") || !headers.Contains("msgSendTime")) { |
| | | return CreateErrorResponse("2003", "缺少必要请求头参数"); |
| | | } |
| | | |
| | | var headerModel = new { |
| | | SourceAppCode = headers.GetValues("sourceAppCode").First(), |
| | | Password = headers.Contains("password") ? headers.GetValues("password").First() : null, |
| | | ServiceId = headers.GetValues("serviceId").First(), |
| | | MsgToken = headers.Contains("msgToken") ? headers.GetValues("msgToken").First() : null, |
| | | MsgSendTime = headers.GetValues("msgSendTime").First() |
| | | }; |
| | | |
| | | // 2. 验证Header |
| | | var validationResult = ValidateHeaders(headerModel); |
| | | if (validationResult != null) { |
| | | return validationResult; |
| | | } |
| | | |
| | | // 3. 读取Body |
| | | //string requestBody = await Request.Content.ReadAsStringAsync(); // 不需要异步读取,直接用model |
| | | |
| | | // 4. 验证Body |
| | | if (Request.Content.Headers.ContentLength > 2 * 1024 * 1024) { |
| | | return CreateErrorResponse("2102", "消息内容过长,超过2M"); |
| | | } |
| | | |
| | | // 5. 处理业务逻辑 |
| | | var result = ApiHelper.InboundDataSync(model); |
| | | |
| | | // 6. 返回成功响应 |
| | | return CreateSuccessResponse(result); |
| | | } |
| | | |
| | | private IHttpActionResult ProcessOutboundData(System.Net.Http.Headers.HttpRequestHeaders headers, OutboundDataSyncInfo model) { |
| | | // 必须字段验证 |
| | | if (!headers.Contains("sourceAppCode") || !headers.Contains("serviceId") || !headers.Contains("msgSendTime")) { |
| | | return CreateErrorResponse("2003", "缺少必要请求头参数"); |
| | | } |
| | | |
| | | var headerModel = new { |
| | | SourceAppCode = headers.GetValues("sourceAppCode").First(), |
| | | Password = headers.Contains("password") ? headers.GetValues("password").First() : null, |
| | | ServiceId = headers.GetValues("serviceId").First(), |
| | | MsgToken = headers.Contains("msgToken") ? headers.GetValues("msgToken").First() : null, |
| | | MsgSendTime = headers.GetValues("msgSendTime").First() |
| | | }; |
| | | |
| | | // 2. 验证Header |
| | | var validationResult = ValidateHeaders(headerModel); |
| | | if (validationResult != null) { |
| | | return validationResult; |
| | | } |
| | | |
| | | // 3. 读取Body |
| | | //string requestBody = await Request.Content.ReadAsStringAsync(); |
| | | |
| | | // 4. 验证Body |
| | | if (Request.Content.Headers.ContentLength > 2 * 1024 * 1024) { |
| | | return CreateErrorResponse("2102", "消息内容过长,超过2M"); |
| | | } |
| | | |
| | | // 5. 处理业务逻辑 |
| | | var result = ApiHelper.OutboundDataSync(model); |
| | | |
| | | // 6. 返回成功响应 |
| | | return CreateSuccessResponse(result); |
| | | LogHelper.Info("触发API:OutboundDataSync:出库发料同步" + JsonConvert.SerializeObject(model), "API"); |
| | | |
| | | var res = ApiHelper.OutboundDataSync(model); |
| | | return CreateSuccessResponse(res); |
| | | } |
| | | |
| | | private IHttpActionResult CreateSuccessResponse(object data) { |
| | |
| | | Headers = { { "returnCode", "0" } }, |
| | | Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json") |
| | | }); |
| | | } |
| | | |
| | | private IHttpActionResult CreateErrorResponse(string code, string message) { |
| | | return ResponseMessage(new HttpResponseMessage(HttpStatusCode.OK) { |
| | | Headers = { { "returnCode", code } }, |
| | | Content = new StringContent(message) |
| | | }); |
| | | } |
| | | |
| | | private IHttpActionResult ValidateHeaders(dynamic headers) { |
| | | // 实现所有Header验证逻辑 |
| | | if (!IsRegisteredApp(headers.SourceAppCode, headers.Password)) { |
| | | return CreateErrorResponse("2201", "源调用系统未注册"); |
| | | } |
| | | |
| | | if (!IsValidService(headers.ServiceId)) { |
| | | return CreateErrorResponse("2202", "代理服务未注册"); |
| | | } |
| | | |
| | | if (!IsValidDateTime(headers.MsgSendTime)) { |
| | | return CreateErrorResponse("2101", "时间格式校验失败"); |
| | | } |
| | | |
| | | if (!string.IsNullOrEmpty(headers.MsgToken) && headers.MsgToken.Length > 32) { |
| | | return CreateErrorResponse("2104", "消息令牌过长,超过32位"); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | private bool IsRegisteredApp(string appId, string password) { |
| | | return true; |
| | | } |
| | | |
| | | private bool IsValidService(string serviceId) { |
| | | return true; |
| | | } |
| | | |
| | | private bool IsValidDateTime(string dataTime) { |
| | | return true; |
| | | } |
| | | } |
| | | } |