mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-09-19 01:58:14 +08:00
feat(wxapi): SubscribeMessagePopupEvent
兼容微信返回的多种数据格式
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using System;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Api.Events
|
||||
{
|
||||
/// <summary>
|
||||
@@ -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<Types.EventData.Types.EventItem[]?>
|
||||
{
|
||||
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<Types.EventData.Types.EventItem[]>(reader);
|
||||
}
|
||||
|
||||
case Newtonsoft.Json.JsonToken.StartObject:
|
||||
{
|
||||
Types.EventData.Types.EventItem[] array = new Types.EventData.Types.EventItem[1];
|
||||
array[0] = serializer.Deserialize<Types.EventData.Types.EventItem>(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<Types.EventData.Types.EventItem[]?>
|
||||
{
|
||||
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<Types.EventData.Types.EventItem[]>(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<Types.EventData.Types.EventItem>(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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置事件数据。
|
||||
@@ -65,27 +151,19 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Events
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置用户订阅通知弹窗数据列表。
|
||||
/// <para>等效于 <see cref="Types.EventData.EventList"/> </para>
|
||||
/// <para>等效于 <see cref="Types.EventData.EventList"/>。</para>
|
||||
/// </summary>
|
||||
[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!;
|
||||
}
|
||||
}
|
||||
|
@@ -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"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user