MessageHandlerAsync.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:MessageHandlerAsync.cs
  17. 文件功能描述:微信请求【异步方法】的集中处理方法
  18. 创建标识:Senparc - 20180122
  19. ----------------------------------------------------------------*/
  20. #if !NET35 && !NET40
  21. using System;
  22. using System.IO;
  23. using System.Xml.Linq;
  24. using Senparc.NeuChar.Context;
  25. using Senparc.Weixin.Exceptions;
  26. using Senparc.NeuChar.MessageHandlers;
  27. using Senparc.Weixin.MP.AppStore;
  28. using Senparc.Weixin.MP.Entities;
  29. using Senparc.Weixin.MP.Entities.Request;
  30. using Senparc.Weixin.MP.Helpers;
  31. using System.Threading.Tasks;
  32. using Senparc.NeuChar;
  33. using System.Collections.Generic;
  34. using Senparc.CO2NET.Extensions;
  35. using Senparc.CO2NET.Trace;
  36. using Senparc.NeuChar.Entities;
  37. using System.Threading;
  38. namespace Senparc.Weixin.MP.MessageHandlers
  39. {
  40. /// <summary>
  41. /// 微信请求的集中处理方法
  42. /// 此方法中所有过程,都基于Senparc.Weixin.MP的基础功能,只为简化代码而设。
  43. /// </summary>
  44. public abstract partial class MessageHandler<TC> :
  45. MessageHandler<TC, IRequestMessageBase, IResponseMessageBase>, IMessageHandler
  46. where TC : class, IMessageContext<IRequestMessageBase, IResponseMessageBase>, new()
  47. {
  48. /// <summary>
  49. /// 自动判断默认异步方法调用(在没有override的情况下调用的默认方法)
  50. /// </summary>
  51. /// <param name="requestMessage">requestMessage</param>
  52. /// <param name="syncMethod">同名的同步方法(DefaultMessageHandlerAsyncEvent值为SelfSynicMethod时调用)</param>
  53. /// <returns></returns>
  54. private async Task<IResponseMessageBase> DefaultAsyncMethod(IRequestMessageBase requestMessage, Func<IResponseMessageBase> syncMethod)
  55. {
  56. return (base.DefaultMessageHandlerAsyncEvent == DefaultMessageHandlerAsyncEvent.DefaultResponseMessageAsync
  57. ? await DefaultResponseMessageAsync(requestMessage)
  58. : await Task.Run(syncMethod));
  59. }
  60. /// <summary>
  61. /// 【异步方法】执行微信请求
  62. /// </summary>
  63. public override async Task BuildResponseMessageAsync(CancellationToken cancellationToken)
  64. {
  65. #region NeuChar 执行过程
  66. var weixinAppId = this._postModel == null ? "" : this._postModel.AppId;
  67. switch (RequestMessage.MsgType)
  68. {
  69. case RequestMsgType.Text:
  70. {
  71. try
  72. {
  73. var requestMessage = RequestMessage as RequestMessageText;
  74. ResponseMessage = await CurrentMessageHandlerNode.ExecuteAsync(requestMessage, this, weixinAppId)
  75. ?? ((await (OnTextOrEventRequestAsync(requestMessage))
  76. ?? (await OnTextRequestAsync(requestMessage))));
  77. }
  78. catch (Exception ex)
  79. {
  80. SenparcTrace.SendCustomLog("mp-response error", ex.Message + "\r\n|||\r\n" + (ex.InnerException != null ? ex.InnerException.ToString() : ""));
  81. }
  82. }
  83. break;
  84. case RequestMsgType.Location:
  85. ResponseMessage = await OnLocationRequestAsync(RequestMessage as RequestMessageLocation);
  86. break;
  87. case RequestMsgType.Image:
  88. WeixinTrace.SendCustomLog("NeuChar Image", $"appid:{weixinAppId}");
  89. ResponseMessage = await CurrentMessageHandlerNode.ExecuteAsync(RequestMessage, this, weixinAppId) ?? await OnImageRequestAsync(RequestMessage as RequestMessageImage);
  90. break;
  91. case RequestMsgType.Voice:
  92. ResponseMessage = await OnVoiceRequestAsync(RequestMessage as RequestMessageVoice);
  93. break;
  94. case RequestMsgType.Video:
  95. ResponseMessage = await OnVideoRequestAsync(RequestMessage as RequestMessageVideo);
  96. break;
  97. case RequestMsgType.Link:
  98. ResponseMessage = await OnLinkRequestAsync(RequestMessage as RequestMessageLink);
  99. break;
  100. case RequestMsgType.ShortVideo:
  101. ResponseMessage = await OnShortVideoRequestAsync(RequestMessage as RequestMessageShortVideo);
  102. break;
  103. case RequestMsgType.File:
  104. ResponseMessage = await OnFileRequestAsync(RequestMessage as RequestMessageFile);
  105. break;
  106. case RequestMsgType.NeuChar:
  107. ResponseMessage = await OnNeuCharRequestAsync(RequestMessage as RequestMessageNeuChar);
  108. break;
  109. case RequestMsgType.Unknown:
  110. ResponseMessage = await OnUnknownTypeRequestAsync(RequestMessage as RequestMessageUnknownType);
  111. break;
  112. case RequestMsgType.Event:
  113. {
  114. var requestMessageText = (RequestMessage as IRequestMessageEventBase).ConvertToRequestMessageText();
  115. ResponseMessage = await CurrentMessageHandlerNode.ExecuteAsync(RequestMessage, this, weixinAppId) ??
  116. await OnTextOrEventRequestAsync(requestMessageText) ??
  117. (await OnEventRequestAsync(RequestMessage as IRequestMessageEventBase));
  118. }
  119. break;
  120. default:
  121. Weixin.WeixinTrace.SendCustomLog("NeuChar", "未知的MsgType请求类型" + RequestMessage.MsgType);
  122. //throw new UnknownRequestMsgTypeException("未知的MsgType请求类型", null);
  123. break;
  124. }
  125. #endregion
  126. }
  127. /// <summary>
  128. /// 【异步方法】OnExecutingAsync()
  129. /// </summary>
  130. /// <returns></returns>
  131. public override async Task OnExecutingAsync(CancellationToken cancellationToken)
  132. {
  133. //已放入Init()方法中
  134. //#region 消息去重
  135. //if ((OmitRepeatedMessageFunc == null || OmitRepeatedMessageFunc(RequestMessage) == true)
  136. //&& OmitRepeatedMessage && CurrentMessageContext.RequestMessages.Count > 1
  137. ////&& !(RequestMessage is RequestMessageEvent_Merchant_Order)批量订单的MsgId可能会相同
  138. //)
  139. //{
  140. // var currentIndex = CurrentMessageContext.RequestMessages.FindLastIndex(z=>z.)
  141. // var lastMessage = CurrentMessageContext.RequestMessages[CurrentMessageContext.RequestMessages.Count - 2];
  142. // if (
  143. // //使用MsgId去重
  144. // (lastMessage.MsgId != 0 && lastMessage.MsgId == RequestMessage.MsgId)
  145. // //使用CreateTime去重(OpenId对象已经是同一个)
  146. // || (lastMessage.MsgId == RequestMessage.MsgId
  147. // && lastMessage.CreateTime == RequestMessage.CreateTime
  148. // && lastMessage.MsgType == RequestMessage.MsgType)
  149. // )
  150. // {
  151. // CancelExcute = true;//重复消息,取消执行
  152. // MessageIsRepeated = true;
  153. // return;
  154. // }
  155. //}
  156. //#endregion
  157. await base.OnExecutingAsync(cancellationToken);
  158. //判断是否已经接入开发者信息
  159. if (DeveloperInfo != null || CurrentMessageContext.AppStoreState == AppStoreState.Enter)
  160. {
  161. //优先请求云端应用商店资源
  162. }
  163. }
  164. /// <summary>
  165. /// 【异步方法】OnExecutedAsync()
  166. /// </summary>
  167. /// <returns></returns>
  168. public override async Task OnExecutedAsync(CancellationToken cancellationToken)
  169. {
  170. await base.OnExecutedAsync(cancellationToken);
  171. }
  172. }
  173. }
  174. #endif