From 6bc96f815774e661193cac25a7e0588810576f1d Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Mon, 29 Jul 2024 17:03:14 +0800 Subject: [PATCH] =?UTF-8?q?feat(wxapi):=20`SubscribeMessagePopupEvent`=20?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E5=BE=AE=E4=BF=A1=E8=BF=94=E5=9B=9E=E7=9A=84?= =?UTF-8?q?=E5=A4=9A=E7=A7=8D=E6=95=B0=E6=8D=AE=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SubscribeMessagePopupEvent.cs | 104 +++++++++++++++--- ...n => SubscribeMessagePopupEvent.List.json} | 0 .../SubscribeMessagePopupEvent.Object.json | 12 ++ 3 files changed, 103 insertions(+), 13 deletions(-) rename test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/EventSamples/MpSubscribeMessage/{SubscribeMessagePopupEvent.json => SubscribeMessagePopupEvent.List.json} (100%) create mode 100644 test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/EventSamples/MpSubscribeMessage/SubscribeMessagePopupEvent.Object.json diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/Events/MpSubscribeMessage/SubscribeMessagePopupEvent.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/Events/MpSubscribeMessage/SubscribeMessagePopupEvent.cs index 62a92354..3be9403d 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.Api/Events/MpSubscribeMessage/SubscribeMessagePopupEvent.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/Events/MpSubscribeMessage/SubscribeMessagePopupEvent.cs @@ -1,3 +1,5 @@ +using System; + namespace SKIT.FlurlHttpClient.Wechat.Api.Events { /// @@ -53,7 +55,91 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Events } } - private readonly object _lockObj = new object(); + internal static class Converters + { + internal class ResponsePropertyEventDataListNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanWrite + { + get { return false; } + } + + public override Types.EventData.Types.EventItem[]? ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, Types.EventData.Types.EventItem[]? existingValue, bool hasExistingValue, Newtonsoft.Json.JsonSerializer serializer) + { + /* + * NOTICE: + * 按官方文档的说法,当数组中只存在一个元素时,将以对象形式返回。 + * 此转换器为了同时兼容数组和对象两种形式的数据结构。 + */ + + switch (reader.TokenType) + { + case Newtonsoft.Json.JsonToken.Null: + { + return existingValue; + } + + case Newtonsoft.Json.JsonToken.StartArray: + { + return serializer.Deserialize(reader); + } + + case Newtonsoft.Json.JsonToken.StartObject: + { + Types.EventData.Types.EventItem[] array = new Types.EventData.Types.EventItem[1]; + array[0] = serializer.Deserialize(reader)!; + return array; + } + } + + throw new Newtonsoft.Json.JsonException(); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, Types.EventData.Types.EventItem[]? value, Newtonsoft.Json.JsonSerializer serializer) + { + throw new NotImplementedException(); + } + } + + internal class ResponsePropertyEventDataListSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override Types.EventData.Types.EventItem[]? Read(ref System.Text.Json.Utf8JsonReader reader, Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + /* + * NOTICE: + * 按官方文档的说法,当数组中只存在一个元素时,将以对象形式返回。 + * 此转换器为了同时兼容数组和对象两种形式的数据结构。 + */ + + switch (reader.TokenType) + { + case System.Text.Json.JsonTokenType.Null: + { + return default; + } + + case System.Text.Json.JsonTokenType.StartArray: + { + return System.Text.Json.JsonSerializer.Deserialize(ref reader, options); + } + + case System.Text.Json.JsonTokenType.StartObject: + { + Types.EventData.Types.EventItem[] array = new Types.EventData.Types.EventItem[1]; + array[0] = System.Text.Json.JsonSerializer.Deserialize(ref reader, options)!; + return array; + } + } + + throw new System.Text.Json.JsonException(); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, Types.EventData.Types.EventItem[]? value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteRawValue(System.Text.Json.JsonSerializer.Serialize(value, typeof(Types.EventData.Types.EventItem[]), options)); + } + } + } /// /// 获取或设置事件数据。 @@ -65,27 +151,19 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Events /// /// 获取或设置用户订阅通知弹窗数据列表。 - /// 等效于 + /// 等效于 /// [Newtonsoft.Json.JsonProperty("List")] + [Newtonsoft.Json.JsonConverter(typeof(Converters.ResponsePropertyEventDataListNewtonsoftJsonConverter))] [System.Text.Json.Serialization.JsonPropertyName("List")] + [System.Text.Json.Serialization.JsonConverter(typeof(Converters.ResponsePropertyEventDataListSystemTextJsonConverter))] [System.Xml.Serialization.XmlIgnore] public Types.EventData.Types.EventItem[]? EventDataList { get { return this.EventData?.EventList; } set { - if (this.EventData is null) - { - lock (_lockObj) - { - if (this.EventData is null) - { - this.EventData = new Types.EventData(); - } - } - } - + this.EventData ??= new Types.EventData(); this.EventData.EventList = value!; } } diff --git a/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/EventSamples/MpSubscribeMessage/SubscribeMessagePopupEvent.json b/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/EventSamples/MpSubscribeMessage/SubscribeMessagePopupEvent.List.json similarity index 100% rename from test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/EventSamples/MpSubscribeMessage/SubscribeMessagePopupEvent.json rename to test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/EventSamples/MpSubscribeMessage/SubscribeMessagePopupEvent.List.json diff --git a/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/EventSamples/MpSubscribeMessage/SubscribeMessagePopupEvent.Object.json b/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/EventSamples/MpSubscribeMessage/SubscribeMessagePopupEvent.Object.json new file mode 100644 index 00000000..2608838d --- /dev/null +++ b/test/SKIT.FlurlHttpClient.Wechat.Api.UnitTests/EventSamples/MpSubscribeMessage/SubscribeMessagePopupEvent.Object.json @@ -0,0 +1,12 @@ +{ + "ToUserName": "gh_123456789abc", + "FromUserName": "o7esq5OI1Uej6Xixw1lA2H7XDVbc", + "CreateTime": "1620973045", + "MsgType": "event", + "Event": "subscribe_msg_popup_event", + "List": { + "TemplateId": "hD-ixGOhYmUfjOnI8MCzQMPshzGVeux_2vzyvQu7O68", + "SubscribeStatusString": "accept", + "PopupScene": "0" + } +}