Files
Yi.Framework/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/FuwuhaoService.cs

67 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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; }
}