mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-09-21 02:58:06 +08:00
feat(wxapi): 封装公众平台、开放平台接口
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Api
|
||||
{
|
||||
/// <summary>
|
||||
/// 为 <see cref="WechatApiClient"/> 提供回调通知事件的扩展方法。
|
||||
/// </summary>
|
||||
public static class WechatApiClientEventExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>从 JSON 反序列化得到 <see cref="WechatApiEvent"/> 对象。</para>
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="callbackJson"></param>
|
||||
/// <returns></returns>
|
||||
public static TEvent DeserializeEventFromJson<TEvent>(this WechatApiClient client, string callbackJson)
|
||||
where TEvent : WechatApiEvent, WechatApiEvent.Types.IJsonSerializable, new()
|
||||
{
|
||||
if (client == null) throw new ArgumentNullException(nameof(client));
|
||||
if (string.IsNullOrEmpty(callbackJson)) throw new ArgumentNullException(callbackJson);
|
||||
|
||||
return client.JsonSerializer.Deserialize<TEvent>(callbackJson);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>从 XML 反序列化得到 <see cref="WechatApiEvent"/> 对象。</para>
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="callbackXml"></param>
|
||||
/// <returns></returns>
|
||||
public static TEvent DeserializeEventFromXml<TEvent>(this WechatApiClient client, string callbackXml)
|
||||
where TEvent : WechatApiEvent, WechatApiEvent.Types.IXmlSerializable, new()
|
||||
{
|
||||
if (client == null) throw new ArgumentNullException(nameof(client));
|
||||
if (string.IsNullOrEmpty(callbackXml)) throw new ArgumentNullException(callbackXml);
|
||||
|
||||
using StringReader reader = new StringReader(callbackXml);
|
||||
XmlSerializer xmlSerializer = new XmlSerializer(typeof(TEvent), new XmlRootAttribute("xml"));
|
||||
return (TEvent)xmlSerializer.Deserialize(reader);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user