81 lines
2.6 KiB
C#
81 lines
2.6 KiB
C#
using System.Text;
|
|
|
|
namespace SimpleAdmin.Web.Core;
|
|
|
|
/// <summary>
|
|
/// 接收契约锁服务器状态通知;第一版按平台要求使用明文回调。
|
|
/// </summary>
|
|
[ApiDescriptionSettings("Application", Tag = "入职电子签回调")]
|
|
[Route("open/onboarding/qiyuesuo")]
|
|
[AllowAnonymous]
|
|
public class OnboardingElectronicSignCallbackController
|
|
{
|
|
private readonly IOnboardingElectronicSignService _service;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
public OnboardingElectronicSignCallbackController(
|
|
IOnboardingElectronicSignService service,
|
|
IHttpContextAccessor httpContextAccessor)
|
|
{
|
|
_service = service;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 接收契约锁回调并返回平台可识别的非统一响应。
|
|
/// </summary>
|
|
[HttpPost("callback")]
|
|
[NonUnify]
|
|
public async Task<IActionResult> Callback()
|
|
{
|
|
try
|
|
{
|
|
var request = _httpContextAccessor.HttpContext?.Request
|
|
?? throw new InvalidOperationException("当前请求上下文不存在");
|
|
var payloadJson = await ReadPayload(request);
|
|
await _service.HandleCallback(payloadJson);
|
|
return new OkObjectResult(new
|
|
{
|
|
code = 0,
|
|
message = "success"
|
|
});
|
|
}
|
|
catch (global::System.Exception exception)
|
|
{
|
|
// 契约锁按 HTTP 状态码判断回调是否成功;返回 500 后平台会继续重试。
|
|
return new ObjectResult(new
|
|
{
|
|
code = -1,
|
|
message = exception.Message
|
|
})
|
|
{
|
|
StatusCode = StatusCodes.Status500InternalServerError
|
|
};
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 契约锁明文回调使用表单格式;同时兼容测试工具直接发送 JSON。
|
|
/// </summary>
|
|
private static async Task<string> ReadPayload(HttpRequest request)
|
|
{
|
|
if (request.HasFormContentType)
|
|
{
|
|
var form = await request.ReadFormAsync();
|
|
var values = form
|
|
.OrderBy(item => item.Key, StringComparer.Ordinal)
|
|
.ToDictionary(
|
|
item => item.Key,
|
|
item => item.Value.ToString());
|
|
return global::System.Text.Json.JsonSerializer.Serialize(values);
|
|
}
|
|
|
|
using var reader = new StreamReader(
|
|
request.Body,
|
|
Encoding.UTF8,
|
|
true,
|
|
leaveOpen: true);
|
|
return await reader.ReadToEndAsync();
|
|
}
|
|
}
|