feat: 新增服务号回调处理服务及数据模型
This commit is contained in:
@@ -0,0 +1,67 @@
|
|||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Xml.Serialization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Volo.Abp.Application.Services;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Application.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 服务号服务
|
||||||
|
/// </summary>
|
||||||
|
public class FuwuhaoService : ApplicationService
|
||||||
|
{
|
||||||
|
private readonly ILogger<FuwuhaoService> _logger;
|
||||||
|
private readonly IHttpContextAccessor _accessor;
|
||||||
|
|
||||||
|
public FuwuhaoService(ILogger<FuwuhaoService> logger, IHttpContextAccessor accessor)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_accessor = accessor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 查询已登录的账户信息
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("fuwuhao/callback")]
|
||||||
|
public async Task<string> GetCallbackAsync([FromQuery] string signature, [FromQuery] string timestamp,
|
||||||
|
[FromQuery] string nonce, [FromQuery] string echostr)
|
||||||
|
{
|
||||||
|
|
||||||
|
return echostr;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("fuwuhao/callback")]
|
||||||
|
public async Task<string> PostCallbackAsync([FromQuery] string signature, [FromQuery] string timestamp,
|
||||||
|
[FromQuery] string nonce)
|
||||||
|
{
|
||||||
|
var request = _accessor.HttpContext.Request;
|
||||||
|
// 1. 读取原始 XML 内容
|
||||||
|
using var reader = new StreamReader(request.Body, Encoding.UTF8);
|
||||||
|
var xmlString = await reader.ReadToEndAsync();
|
||||||
|
|
||||||
|
var serializer = new XmlSerializer(typeof(FuwuhaoCallModel));
|
||||||
|
using var stringReader = new StringReader(xmlString);
|
||||||
|
var body = (FuwuhaoCallModel)serializer.Deserialize(stringReader);
|
||||||
|
_logger.LogError("服务号Post回调通知:" + JsonSerializer.Serialize(body, new JsonSerializerOptions()
|
||||||
|
{
|
||||||
|
WriteIndented = true
|
||||||
|
}));
|
||||||
|
return "success";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[XmlRoot("xml")]
|
||||||
|
public class FuwuhaoCallModel
|
||||||
|
{
|
||||||
|
[XmlElement("ToUserName")] public string ToUserName { get; set; }
|
||||||
|
[XmlElement("FromUserName")] public string FromUserName { get; set; }
|
||||||
|
[XmlElement("CreateTime")] public string CreateTime { get; set; }
|
||||||
|
[XmlElement("MsgType")] public string MsgType { get; set; }
|
||||||
|
[XmlElement("Event")] public string Event { get; set; }
|
||||||
|
[XmlElement("EventKey")] public string EventKey { get; set; }
|
||||||
|
[XmlElement("Ticket")] public string Ticket { get; set; }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user