This commit is contained in:
xiaolipro 2025-02-06 17:34:51 +08:00
parent e51f5509ea
commit 5785d39e04
3 changed files with 24 additions and 23 deletions

View File

@ -3,9 +3,21 @@
[Serializable] [Serializable]
public class InterfaceRelayUnifyResultDto public class InterfaceRelayUnifyResultDto
{ {
public InterfaceRelayUnifyResultDto()
{
}
public InterfaceRelayUnifyResultDto(bool isSuccess, object data)
{
this.Code = isSuccess? 200.ToString(): 400.ToString();
this.Data = data;
this.Msg = isSuccess? "成功": "失败";
}
public string Code { get; set; } public string Code { get; set; }
public string Msg { get; set; } public string Msg { get; set; }
public object Data { get; set; }
/// <summary> /// <summary>
/// 请求Id /// 请求Id

View File

@ -10,9 +10,9 @@ public class BatchForwardEvent
public string ServiceProviderCode { get; set; } = null!; public string ServiceProviderCode { get; set; } = null!;
public string Attach { get; set; } public string? Attach { get; set; }
public string ResSubscribeName { get; set; } public string? ResSubscribeName { get; set; }
public bool IsReturnMessage { get; set; } public bool IsReturnMessage { get; set; }

View File

@ -1,8 +1,6 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using InterfaceForward.Application.Contracts;
using InterfaceForward.Application.Contracts.ForwardCore; using InterfaceForward.Application.Contracts.ForwardCore;
using InterfaceForward.Application.Filters; using InterfaceForward.Application.Filters;
using InterfaceForward.Application.Rabbit; using InterfaceForward.Application.Rabbit;
@ -39,7 +37,6 @@ public class InterfaceForwardService : ApplicationService, IInterfaceForwardServ
_httpContextAccessor = httpContextAccessor; _httpContextAccessor = httpContextAccessor;
} }
/// <inheritdoc />
[HttpPost("BatchForwardThenPushToQueue")] [HttpPost("BatchForwardThenPushToQueue")]
public async Task<InterfaceRelayUnifyResultDto> BatchForwardThenPushToQueueAsync( public async Task<InterfaceRelayUnifyResultDto> BatchForwardThenPushToQueueAsync(
[FromQuery] [Required] string upStreamCode, [FromQuery] [Required] string upStreamCode,
@ -105,7 +102,7 @@ public class InterfaceForwardService : ApplicationService, IInterfaceForwardServ
_rabbitClient.Publish(GlobalConst.InterfaceRelayExchange, queueName, eventBytes); _rabbitClient.Publish(GlobalConst.InterfaceRelayExchange, queueName, eventBytes);
} }
return new InterfaceRelayUnifyResultDto return new InterfaceRelayUnifyResultDto(true, _logRepository.AppRequestLog.RequestId)
{ {
RequestId = _logRepository.AppRequestLog.RequestId, RequestId = _logRepository.AppRequestLog.RequestId,
OriginalMessage = null, OriginalMessage = null,
@ -118,9 +115,9 @@ public class InterfaceForwardService : ApplicationService, IInterfaceForwardServ
[FromQuery] [Required] string upStreamCode, [FromQuery] [Required] string upStreamCode,
[FromQuery] [Required] string serviceProviderCode, [FromQuery] [Required] string serviceProviderCode,
[FromBody] [Required] object data, [FromBody] [Required] object data,
[FromHeader] string attach = null, [FromHeader] string? attach = null,
[FromHeader] bool isReturnMessage = false, [FromHeader] bool isReturnMessage = false,
[FromHeader] string failedSubscribeName = null) [FromHeader] string? failedSubscribeName = null)
{ {
if (JToken.FromObject(data) is not JObject jobject) if (JToken.FromObject(data) is not JObject jobject)
{ {
@ -134,14 +131,14 @@ public class InterfaceForwardService : ApplicationService, IInterfaceForwardServ
? jobject ? jobject
: new JObject : new JObject
{ {
[upStreamCode!] = jobject [upStreamCode] = jobject
}; };
var res = contextList.Count == 1 var res = contextList.Count == 1
? await _forwardCommon.InternalForwardAsync(context) ? await _forwardCommon.InternalForwardAsync(context)
: await _forwardCommon.InternalForward2Async(contextList); : await _forwardCommon.InternalForward2Async(contextList);
return new InterfaceRelayUnifyResultDto return new InterfaceRelayUnifyResultDto(true, res)
{ {
RequestId = context.ServiceProviderRequestLog.RequestId, RequestId = context.ServiceProviderRequestLog.RequestId,
OriginalMessage = isReturnMessage ? context.ServiceProviderRequestLog.Response : null, OriginalMessage = isReturnMessage ? context.ServiceProviderRequestLog.Response : null,
@ -155,15 +152,10 @@ public class InterfaceForwardService : ApplicationService, IInterfaceForwardServ
[FromQuery] [Required] string serviceProviderCode, [FromQuery] [Required] string serviceProviderCode,
[FromHeader] [Required] Guid requestId, [FromHeader] [Required] Guid requestId,
[FromBody] [Required] object data, [FromBody] [Required] object data,
[FromHeader] string attach = null, [FromHeader] string? attach = null,
[FromHeader] bool isReturnMessage = false, [FromHeader] bool isReturnMessage = false,
[FromHeader] string failedSubscribeName = null) [FromHeader] string? failedSubscribeName = null)
{ {
if (JToken.FromObject(data) is not JObject jobject)
{
throw new BusinessException(message: "非法结构,请传对象");
}
var logs = await _logRepository.GetRequestLogsAsync(requestId); var logs = await _logRepository.GetRequestLogsAsync(requestId);
var failedLog = logs.FirstOrDefault(x => !x.IsSuccess); var failedLog = logs.FirstOrDefault(x => !x.IsSuccess);
@ -173,11 +165,8 @@ public class InterfaceForwardService : ApplicationService, IInterfaceForwardServ
var context = contextList.First(); var context = contextList.First();
context.OriginalInterfaceInput = contextList.Count == 1 context.OriginalInterfaceInput = contextList.Count == 1
? jobject ? JObject.FromObject(data)
: new JObject : JArray.FromObject(data);
{
[upStreamCode!] = jobject
};
// 从第一个失败的开始重试 // 从第一个失败的开始重试
var requestLogs = logs.Where(x => !x.IsApp).ToList(); var requestLogs = logs.Where(x => !x.IsApp).ToList();
@ -185,7 +174,7 @@ public class InterfaceForwardService : ApplicationService, IInterfaceForwardServ
? await _forwardCommon.InternalForwardAsync(context) ? await _forwardCommon.InternalForwardAsync(context)
: await _forwardCommon.InternalForward3Async(contextList, requestLogs); : await _forwardCommon.InternalForward3Async(contextList, requestLogs);
return new InterfaceRelayUnifyResultDto return new InterfaceRelayUnifyResultDto(true, res)
{ {
RequestId = context.ServiceProviderRequestLog.RequestId, RequestId = context.ServiceProviderRequestLog.RequestId,
OriginalMessage = isReturnMessage ? _logRepository.ServiceProviderRequestLog.Response : null, OriginalMessage = isReturnMessage ? _logRepository.ServiceProviderRequestLog.Response : null,