From 74b2f618c586a9171706c4d6479c390f3eb02eb8 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Mon, 2 Aug 2021 16:14:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(wxapi):=20=E5=8F=8D=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=E5=BE=AE=E4=BF=A1=E5=9B=9E=E8=B0=83=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E6=97=B6=E6=94=AF=E6=8C=81=E5=8F=8D=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96=E6=88=90=20WechatApiEvent=20=E5=9F=BA?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...tApiClientEventSerializationExtensions.cs} | 82 ++++++++++++++----- 1 file changed, 60 insertions(+), 22 deletions(-) rename src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/{WechatApiClientEventExtensions.cs => WechatApiClientEventSerializationExtensions.cs} (84%) diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventSerializationExtensions.cs similarity index 84% rename from src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs rename to src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventSerializationExtensions.cs index 41b33339..6f637353 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Extensions/WechatApiClientEventSerializationExtensions.cs @@ -9,9 +9,9 @@ using System.Xml.Serialization; namespace SKIT.FlurlHttpClient.Wechat.Api { /// - /// 为 提供回调通知事件的扩展方法。 + /// 为 提供回调通知事件序列化的扩展方法。 /// - public static class WechatApiClientEventExtensions + public static class WechatApiClientEventSerializationExtensions { private class EncryptedWechatApiEvent { @@ -33,15 +33,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Api public string Signature { get; set; } = default!; } - /// - /// 从 JSON 反序列化得到 对象。 - /// - /// - /// - /// 是否是安全模式(即是否需要解密)。 - /// - public static TEvent DeserializeEventFromJson(this WechatApiClient client, string callbackJson, bool safety = false) - where TEvent : WechatApiEvent, WechatApiEvent.Types.IJsonSerializable, new() + private static TEvent InnerDeserializeEventFromJson(this WechatApiClient client, string callbackJson, bool safety) + where TEvent : WechatApiEvent { if (client == null) throw new ArgumentNullException(nameof(client)); if (string.IsNullOrEmpty(callbackJson)) throw new ArgumentNullException(callbackJson); @@ -58,8 +51,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Api throw new Exceptions.WechatApiEventSerializationException("Encrypt event failed, because of empty encrypted data."); callbackJson = Utilities.WxBizMsgCryptor.AESDecrypt( - cipherText: encryptedEvent.EncryptedData, - encodingAESKey: client.Credentials.PushEncodingAESKey!, + cipherText: encryptedEvent.EncryptedData, + encodingAESKey: client.Credentials.PushEncodingAESKey!, out _ ); } @@ -76,15 +69,8 @@ namespace SKIT.FlurlHttpClient.Wechat.Api } } - /// - /// 从 XML 反序列化得到 对象。 - /// - /// - /// - /// 是否是安全模式(即是否需要解密)。 - /// - public static TEvent DeserializeEventFromXml(this WechatApiClient client, string callbackXml, bool safety = false) - where TEvent : WechatApiEvent, WechatApiEvent.Types.IXmlSerializable, new() + private static TEvent InnerDeserializeEventFromXml(this WechatApiClient client, string callbackXml, bool safety) + where TEvent : WechatApiEvent { if (client == null) throw new ArgumentNullException(nameof(client)); if (string.IsNullOrEmpty(callbackXml)) throw new ArgumentNullException(callbackXml); @@ -112,6 +98,58 @@ namespace SKIT.FlurlHttpClient.Wechat.Api } } + /// + /// 从 JSON 反序列化得到 对象。 + /// + /// + /// + /// + /// 是否是安全模式(即是否需要解密)。 + /// + public static TEvent DeserializeEventFromJson(this WechatApiClient client, string callbackJson, bool safety = false) + where TEvent : WechatApiEvent, WechatApiEvent.Types.IJsonSerializable, new() + { + return InnerDeserializeEventFromJson(client, callbackJson, safety); + } + + /// + /// 从 JSON 反序列化得到 对象。 + /// + /// + /// + /// 是否是安全模式(即是否需要解密)。 + /// + public static WechatApiEvent DeserializeEventFromJson(this WechatApiClient client, string callbackJson, bool safety = false) + { + return InnerDeserializeEventFromJson(client, callbackJson, safety); + } + + /// + /// 从 XML 反序列化得到 对象。 + /// + /// + /// + /// + /// 是否是安全模式(即是否需要解密)。 + /// + public static TEvent DeserializeEventFromXml(this WechatApiClient client, string callbackXml, bool safety = false) + where TEvent : WechatApiEvent, WechatApiEvent.Types.IXmlSerializable, new() + { + return InnerDeserializeEventFromXml(client, callbackXml, safety); + } + + /// + /// 从 XML 反序列化得到 对象。 + /// + /// + /// + /// 是否是安全模式(即是否需要解密)。 + /// + public static WechatApiEvent DeserializeEventFromXml(this WechatApiClient client, string callbackXml, bool safety = false) + { + return InnerDeserializeEventFromXml(client, callbackXml, safety); + } + /// /// 将 对象序列化成 JSON。 ///