123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #region Apache License Version 2.0
- /*----------------------------------------------------------------
- Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
- except in compliance with the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software distributed under the
- License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- either express or implied. See the License for the specific language governing permissions
- and limitations under the License.
- Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
- ----------------------------------------------------------------*/
- #endregion Apache License Version 2.0
- /*----------------------------------------------------------------
- Copyright (C) 2019 Senparc
-
- 文件名:MessageHandlerAsync.cs
- 文件功能描述:微信请求【异步方法】的集中处理方法
-
-
- 创建标识:Senparc - 20180122
-
- ----------------------------------------------------------------*/
- #if !NET35 && !NET40
- using System;
- using System.IO;
- using System.Xml.Linq;
- using Senparc.NeuChar.Context;
- using Senparc.Weixin.Exceptions;
- using Senparc.NeuChar.MessageHandlers;
- using Senparc.Weixin.MP.AppStore;
- using Senparc.Weixin.MP.Entities;
- using Senparc.Weixin.MP.Entities.Request;
- using Senparc.Weixin.MP.Helpers;
- using System.Threading.Tasks;
- using Senparc.NeuChar;
- using System.Collections.Generic;
- using Senparc.CO2NET.Extensions;
- using Senparc.CO2NET.Trace;
- using Senparc.NeuChar.Entities;
- using System.Threading;
- namespace Senparc.Weixin.MP.MessageHandlers
- {
- /// <summary>
- /// 微信请求的集中处理方法
- /// 此方法中所有过程,都基于Senparc.Weixin.MP的基础功能,只为简化代码而设。
- /// </summary>
- public abstract partial class MessageHandler<TC> :
- MessageHandler<TC, IRequestMessageBase, IResponseMessageBase>, IMessageHandler
- where TC : class, IMessageContext<IRequestMessageBase, IResponseMessageBase>, new()
- {
- /// <summary>
- /// 自动判断默认异步方法调用(在没有override的情况下调用的默认方法)
- /// </summary>
- /// <param name="requestMessage">requestMessage</param>
- /// <param name="syncMethod">同名的同步方法(DefaultMessageHandlerAsyncEvent值为SelfSynicMethod时调用)</param>
- /// <returns></returns>
- private async Task<IResponseMessageBase> DefaultAsyncMethod(IRequestMessageBase requestMessage, Func<IResponseMessageBase> syncMethod)
- {
- return (base.DefaultMessageHandlerAsyncEvent == DefaultMessageHandlerAsyncEvent.DefaultResponseMessageAsync
- ? await DefaultResponseMessageAsync(requestMessage)
- : await Task.Run(syncMethod));
- }
- /// <summary>
- /// 【异步方法】执行微信请求
- /// </summary>
- public override async Task BuildResponseMessageAsync(CancellationToken cancellationToken)
- {
- #region NeuChar 执行过程
- var weixinAppId = this._postModel == null ? "" : this._postModel.AppId;
- switch (RequestMessage.MsgType)
- {
- case RequestMsgType.Text:
- {
- try
- {
- var requestMessage = RequestMessage as RequestMessageText;
- ResponseMessage = await CurrentMessageHandlerNode.ExecuteAsync(requestMessage, this, weixinAppId)
- ?? ((await (OnTextOrEventRequestAsync(requestMessage))
- ?? (await OnTextRequestAsync(requestMessage))));
- }
- catch (Exception ex)
- {
- SenparcTrace.SendCustomLog("mp-response error", ex.Message + "\r\n|||\r\n" + (ex.InnerException != null ? ex.InnerException.ToString() : ""));
- }
- }
- break;
- case RequestMsgType.Location:
- ResponseMessage = await OnLocationRequestAsync(RequestMessage as RequestMessageLocation);
- break;
- case RequestMsgType.Image:
- WeixinTrace.SendCustomLog("NeuChar Image", $"appid:{weixinAppId}");
- ResponseMessage = await CurrentMessageHandlerNode.ExecuteAsync(RequestMessage, this, weixinAppId) ?? await OnImageRequestAsync(RequestMessage as RequestMessageImage);
- break;
- case RequestMsgType.Voice:
- ResponseMessage = await OnVoiceRequestAsync(RequestMessage as RequestMessageVoice);
- break;
- case RequestMsgType.Video:
- ResponseMessage = await OnVideoRequestAsync(RequestMessage as RequestMessageVideo);
- break;
- case RequestMsgType.Link:
- ResponseMessage = await OnLinkRequestAsync(RequestMessage as RequestMessageLink);
- break;
- case RequestMsgType.ShortVideo:
- ResponseMessage = await OnShortVideoRequestAsync(RequestMessage as RequestMessageShortVideo);
- break;
- case RequestMsgType.File:
- ResponseMessage = await OnFileRequestAsync(RequestMessage as RequestMessageFile);
- break;
- case RequestMsgType.NeuChar:
- ResponseMessage = await OnNeuCharRequestAsync(RequestMessage as RequestMessageNeuChar);
- break;
- case RequestMsgType.Unknown:
- ResponseMessage = await OnUnknownTypeRequestAsync(RequestMessage as RequestMessageUnknownType);
- break;
- case RequestMsgType.Event:
- {
- var requestMessageText = (RequestMessage as IRequestMessageEventBase).ConvertToRequestMessageText();
- ResponseMessage = await CurrentMessageHandlerNode.ExecuteAsync(RequestMessage, this, weixinAppId) ??
- await OnTextOrEventRequestAsync(requestMessageText) ??
- (await OnEventRequestAsync(RequestMessage as IRequestMessageEventBase));
- }
- break;
- default:
- Weixin.WeixinTrace.SendCustomLog("NeuChar", "未知的MsgType请求类型" + RequestMessage.MsgType);
- //throw new UnknownRequestMsgTypeException("未知的MsgType请求类型", null);
- break;
- }
- #endregion
- }
- /// <summary>
- /// 【异步方法】OnExecutingAsync()
- /// </summary>
- /// <returns></returns>
- public override async Task OnExecutingAsync(CancellationToken cancellationToken)
- {
- //已放入Init()方法中
- //#region 消息去重
- //if ((OmitRepeatedMessageFunc == null || OmitRepeatedMessageFunc(RequestMessage) == true)
- //&& OmitRepeatedMessage && CurrentMessageContext.RequestMessages.Count > 1
- ////&& !(RequestMessage is RequestMessageEvent_Merchant_Order)批量订单的MsgId可能会相同
- //)
- //{
- // var currentIndex = CurrentMessageContext.RequestMessages.FindLastIndex(z=>z.)
- // var lastMessage = CurrentMessageContext.RequestMessages[CurrentMessageContext.RequestMessages.Count - 2];
- // if (
- // //使用MsgId去重
- // (lastMessage.MsgId != 0 && lastMessage.MsgId == RequestMessage.MsgId)
- // //使用CreateTime去重(OpenId对象已经是同一个)
- // || (lastMessage.MsgId == RequestMessage.MsgId
- // && lastMessage.CreateTime == RequestMessage.CreateTime
- // && lastMessage.MsgType == RequestMessage.MsgType)
- // )
- // {
- // CancelExcute = true;//重复消息,取消执行
- // MessageIsRepeated = true;
- // return;
- // }
- //}
- //#endregion
- await base.OnExecutingAsync(cancellationToken);
- //判断是否已经接入开发者信息
- if (DeveloperInfo != null || CurrentMessageContext.AppStoreState == AppStoreState.Enter)
- {
- //优先请求云端应用商店资源
- }
- }
- /// <summary>
- /// 【异步方法】OnExecutedAsync()
- /// </summary>
- /// <returns></returns>
- public override async Task OnExecutedAsync(CancellationToken cancellationToken)
- {
- await base.OnExecutedAsync(cancellationToken);
- }
- }
- }
- #endif
|