public override void Init(XDocument requestDocument) { RequestDocument = requestDocument; RequestMessage = RequestMessageFactory.GetRequestEntity(RequestDocument); //记录上下文 if (WeixinContextGlobal.UseWeixinContext) { WeixinContext.InsertMessage(RequestMessage); } }
public override XDocument Init(XDocument postDataDocument, object postData = null) { RequestMessage = RequestMessageFactory.GetRequestEntity(postDataDocument); //记录上下文 if (WeixinContextGlobal.UseWeixinContext) { WeixinContext.InsertMessage(RequestMessage); } return(postDataDocument); }
public override XDocument Init(XDocument postDataDocument, object postData = null) { //进行加密判断并处理 _postModel = postData as PostModel; var postDataStr = postDataDocument.ToString(); XDocument decryptDoc = postDataDocument; if (_postModel != null && postDataDocument.Root.Element("Encrypt") != null && !string.IsNullOrEmpty(postDataDocument.Root.Element("Encrypt").Value)) { //使用了加密 UsingEcryptMessage = true; EcryptRequestDocument = postDataDocument; WXBizMsgCrypt msgCrype = new WXBizMsgCrypt(_postModel.Token, _postModel.EncodingAESKey, _postModel.AppId); string msgXml = null; var result = msgCrype.DecryptMsg(_postModel.Msg_Signature, _postModel.Timestamp, _postModel.Nonce, postDataStr, ref msgXml); //判断result类型 if (result != 0) { //验证没有通过,取消执行 CancelExcute = true; return(null); } if (postDataDocument.Root.Element("FromUserName") != null && !string.IsNullOrEmpty(postDataDocument.Root.Element("FromUserName").Value)) { //TODO:使用了兼容模式,进行验证即可 UsingCompatibilityModelEcryptMessage = true; } decryptDoc = XDocument.Parse(msgXml);//完成解密 } RequestMessage = RequestMessageFactory.GetRequestEntity(decryptDoc); if (UsingEcryptMessage) { RequestMessage.Encrypt = postDataDocument.Root.Element("Encrypt").Value; } //记录上下文 if (WeixinContextGlobal.UseWeixinContext) { WeixinContext.InsertMessage(RequestMessage); } return(decryptDoc); }
public override XDocument Init(XDocument postDataDocument, object postData) { _postModel = postData as PostModel; var postDataStr = postDataDocument.ToString(); AfterDecryptData(postDataStr, _postModel); EncryptPostData = RequestMessageFactory.GetEncryptPostData(postDataStr); AfterGetData(EncryptPostData, _postModel); //解密:获得明文字符串 WXBizMsgCrypt msgCrype = new WXBizMsgCrypt(_postModel.Token, _postModel.EncodingAESKey, _postModel.CorpId); string msgXml = null; var result = msgCrype.DecryptMsg(_postModel.Msg_Signature, _postModel.Timestamp, _postModel.Nonce, postDataStr, ref msgXml); /* msgXml * <xml><ToUserName><![CDATA[wx7618c0a6d9358622]]></ToUserName> * <FromUserName><![CDATA[001]]></FromUserName> * <CreateTime>1412585107</CreateTime> * <MsgType><![CDATA[text]]></MsgType> * <Content><![CDATA[你好]]></Content> * <MsgId>4299263624800632834</MsgId> * <AgentID>2</AgentID> * </xml> */ //判断result类型 if (result != 0) { //验证没有通过,取消执行 CancelExcute = true; return(null); } AfterDecryptData(msgXml, _postModel); var requestDocument = XDocument.Parse(msgXml); RequestMessage = RequestMessageFactory.GetRequestEntity(requestDocument); //记录上下文,Chat还是不记录吧,太多了 if (WeixinContextGlobal.UseWeixinContext && !(RequestMessage is RequestMessageChat)) { WeixinContext.InsertMessage(RequestMessage); } return(requestDocument); }
/// <summary> /// 执行微信请求 /// </summary> public override void Execute() { if (CancelExcute) { return; } OnExecuting(); if (CancelExcute) { return; } try { if (RequestMessage == null) { return; } switch (RequestMessage.MsgType) { case RequestMsgType.Text: { var requestMessage = RequestMessage as RequestMessageText; ResponseMessage = OnTextOrEventRequest(requestMessage) ?? OnTextRequest(requestMessage); } break; case RequestMsgType.Location: ResponseMessage = OnLocationRequest(RequestMessage as RequestMessageLocation); break; case RequestMsgType.Image: ResponseMessage = OnImageRequest(RequestMessage as RequestMessageImage); break; case RequestMsgType.Voice: ResponseMessage = OnVoiceRequest(RequestMessage as RequestMessageVoice); break; case RequestMsgType.Video: ResponseMessage = OnVideoRequest(RequestMessage as RequestMessageVideo); break; case RequestMsgType.Link: ResponseMessage = OnLinkRequest(RequestMessage as RequestMessageLink); break; case RequestMsgType.ShortVideo: ResponseMessage = OnShortVideoRequest(RequestMessage as RequestMessageShortVideo); break; case RequestMsgType.Event: { var requestMessageText = (RequestMessage as IRequestMessageEventBase).ConvertToRequestMessageText(); ResponseMessage = OnTextOrEventRequest(requestMessageText) ?? OnEventRequest(RequestMessage as IRequestMessageEventBase); } break; default: throw new UnknownRequestMsgTypeException("未知的MsgType请求类型", null); } //记录上下文 if (WeixinContextGlobal.UseWeixinContext && ResponseMessage != null) { WeixinContext.InsertMessage(ResponseMessage); } } catch (Exception ex) { throw new MessageHandlerException("MessageHandler中Execute()过程发生错误:" + ex.Message, ex); } finally { OnExecuted(); } }
/// <summary> /// 执行微信请求 /// </summary> public void Execute() { if (CancelExcute) { return; } OnExecuting(); if (CancelExcute) { return; } try { if (RequestMessage == null) { return; } switch (RequestMessage.MsgType) { case RequestMsgType.Text: ResponseMessage = OnTextRequest(RequestMessage as RequestMessageText); break; case RequestMsgType.Location: ResponseMessage = OnLocationRequest(RequestMessage as RequestMessageLocation); break; case RequestMsgType.Image: ResponseMessage = OnImageRequest(RequestMessage as RequestMessageImage); break; case RequestMsgType.Voice: ResponseMessage = OnVoiceRequest(RequestMessage as RequestMessageVoice); break; case RequestMsgType.Video: ResponseMessage = OnVideoRequest(RequestMessage as RequestMessageVideo); break; case RequestMsgType.Event: ResponseMessage = OnEventRequest(RequestMessage as RequestMessageEventBase); break; default: throw new UnknownRequestMsgTypeException("未知的MsgType请求类型", null); } //记录上下文 if (WeixinContextGlobal.UseWeixinContext && ResponseMessage != null) { WeixinContext.InsertMessage(ResponseMessage); } } catch (Exception ex) { throw ex; } finally { OnExecuted(); } }
public override XDocument Init(XDocument postDataDocument, object postData = null) { //进行加密判断并处理 _postModel = postData as PostModel; var postDataStr = postDataDocument.ToString(); XDocument decryptDoc = postDataDocument; if (_postModel != null && postDataDocument.Root.Element("Encrypt") != null && !string.IsNullOrEmpty(postDataDocument.Root.Element("Encrypt").Value)) { //使用了加密 UsingEcryptMessage = true; EcryptRequestDocument = postDataDocument; WXBizMsgCrypt msgCrype = new WXBizMsgCrypt(_postModel.Token, _postModel.EncodingAESKey, _postModel.AppId); string msgXml = null; var result = msgCrype.DecryptMsg(_postModel.Msg_Signature, _postModel.Timestamp, _postModel.Nonce, postDataStr, ref msgXml); //判断result类型 if (result != 0) { //验证没有通过,取消执行 CancelExcute = true; return(null); } if (postDataDocument.Root.Element("FromUserName") != null && !string.IsNullOrEmpty(postDataDocument.Root.Element("FromUserName").Value)) { //TODO:使用了兼容模式,进行验证即可 UsingCompatibilityModelEcryptMessage = true; } decryptDoc = XDocument.Parse(msgXml);//完成解密 } RequestMessage = RequestMessageFactory.GetRequestEntity(decryptDoc); if (UsingEcryptMessage) { RequestMessage.Encrypt = postDataDocument.Root.Element("Encrypt").Value; } //TODO:分布式系统中本地的上下文会有同步问题,需要同步使用远程的储存 if (WeixinContextGlobal.UseWeixinContext) { var omit = OmitRepeatedMessageFunc == null || OmitRepeatedMessageFunc(RequestMessage); lock (WeixinContextGlobal.OmitRepeatLock)//TODO:使用分布式锁 { #region 消息去重 if (omit && OmitRepeatedMessage && CurrentMessageContext.RequestMessages.Count > 0 //&& !(RequestMessage is RequestMessageEvent_Merchant_Order)批量订单的MsgId可能会相同 ) { //lastMessage必定有值(除非极端小的过期时间条件下,几乎不可能发生) var lastMessage = CurrentMessageContext.RequestMessages[CurrentMessageContext.RequestMessages.Count - 1]; if ( //使用MsgId去重 (lastMessage.MsgId != 0 && lastMessage.MsgId == RequestMessage.MsgId) || //使用CreateTime去重(OpenId对象已经是同一个) (lastMessage.MsgId == RequestMessage.MsgId && lastMessage.CreateTime == RequestMessage.CreateTime && lastMessage.MsgType == RequestMessage.MsgType) ) { MarkRepeatedMessage();//标记为已重复 } //判断特殊事件 if (!MessageIsRepeated && lastMessage is RequestMessageEventBase && RequestMessage is RequestMessageEventBase && (lastMessage as RequestMessageEventBase).Event == (RequestMessage as RequestMessageEventBase).Event ) { var lastEventMessage = lastMessage as RequestMessageEventBase; var currentEventMessage = RequestMessage as RequestMessageEventBase; switch (lastEventMessage.Event) { case Event.user_get_card: //领取事件推送 //文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1451025274 //问题反馈:https://github.com/JeffreySu/WeiXinMPSDK/issues/1106 var lastGetUserCardMessage = lastMessage as RequestMessageEvent_User_Get_Card; var currentGetUserCardMessage = RequestMessage as RequestMessageEvent_User_Get_Card; if (lastGetUserCardMessage.UserCardCode == currentGetUserCardMessage.UserCardCode && lastGetUserCardMessage.CardId == currentGetUserCardMessage.CardId) { MarkRepeatedMessage(); //标记为已重复 } break; default: break; } } } #endregion //在消息没有被去重的情况下记录上下文 if (!MessageIsRepeated) { WeixinContext.InsertMessage(RequestMessage); } } } return(decryptDoc); }
/// <summary> /// 执行微信请求 /// </summary> public override void Execute() { if (CancelExcute) { return; } OnExecuting(); if (CancelExcute) { return; } try { if (RequestMessage == null) { return; } switch (RequestMessage.MsgType) { case RequestMsgType.Text: { ResponseMessage = OnTextRequest(RequestMessage as RequestMessageText); } break; case RequestMsgType.Image: { ResponseMessage = OnImageRequest(RequestMessage as RequestMessageImage); } break; case RequestMsgType.Event: { OnEventRequest(RequestMessage as IRequestMessageEventBase); } break; default: throw new UnknownRequestMsgTypeException("未知的MsgType请求类型", null); } //记录上下文 //此处修改 if (WeixinContextGlobal.UseWeixinContext && ResponseMessage != null && !string.IsNullOrEmpty(ResponseMessage.FromUserName)) { WeixinContext.InsertMessage(ResponseMessage); } } catch (Exception ex) { throw new MessageHandlerException("MessageHandler中Execute()过程发生错误:" + ex.Message, ex); } finally { OnExecuted(); } }
public override void Execute() { if (CancelExcute) { return; } OnExecuting(); if (CancelExcute) { return; } try { if (RequestMessage == null) { return; } switch (RequestMessage.MsgType) { case RequestMsgType.DEFAULT: //第三方回调 { if (RequestMessage is IThirdPartyInfoBase) { var thirdPartyInfo = RequestMessage as IThirdPartyInfoBase; switch (thirdPartyInfo.InfoType) { case ThirdPartyInfo.SUITE_TICKET: break; case ThirdPartyInfo.CHANGE_AUTH: break; case ThirdPartyInfo.CANCEL_AUTH: break; default: throw new UnknownRequestMsgTypeException("未知的InfoType请求类型", null); } TextResponseMessage = "success"; //设置文字类型返回 } else { throw new WeixinException("没有找到合适的消息类型。"); } } break; //以下是普通信息 case RequestMsgType.Text: { var requestMessage = RequestMessage as RequestMessageText; ResponseMessage = OnTextOrEventRequest(requestMessage) ?? OnTextRequest(requestMessage); } break; case RequestMsgType.Location: ResponseMessage = OnLocationRequest(RequestMessage as RequestMessageLocation); break; case RequestMsgType.Image: ResponseMessage = OnImageRequest(RequestMessage as RequestMessageImage); break; case RequestMsgType.Voice: ResponseMessage = OnVoiceRequest(RequestMessage as RequestMessageVoice); break; case RequestMsgType.Video: ResponseMessage = OnVideoRequest(RequestMessage as RequestMessageVideo); break; case RequestMsgType.Event: { var requestMessageText = (RequestMessage as IRequestMessageEventBase).ConvertToRequestMessageText(); ResponseMessage = OnTextOrEventRequest(requestMessageText) ?? OnEventRequest(RequestMessage as IRequestMessageEventBase); } break; default: throw new UnknownRequestMsgTypeException("未知的MsgType请求类型", null); } //记录上下文 if (WeixinContextGlobal.UseWeixinContext && ResponseMessage != null) { WeixinContext.InsertMessage(ResponseMessage); } } catch (Exception ex) { throw new MessageHandlerException("MessageHandler中Execute()过程发生错误:" + ex.Message, ex); } finally { OnExecuted(); } }
public override void Execute() { if (CancelExcute) { return; } OnExecuting(); if (CancelExcute) { return; } try { if (RequestMessage == null) { return; } switch (RequestMessage.MsgType) { case RequestMsgType.Text: { var requestMessage = RequestMessage as RequestMessageText; ResponseMessage = OnTextOrEventRequest(requestMessage) ?? OnTextRequest(requestMessage); break; } case RequestMsgType.Event: { var requestMessageText = (RequestMessage as IRequestMessageEventBase).ConvertToRequestMessageText(); ResponseMessage = OnTextOrEventRequest(requestMessageText) ?? OnEventRequest(RequestMessage as IRequestMessageEventBase); break; } case RequestMsgType.Location: ResponseMessage = OnLocationRequest(RequestMessage as RequestMessageLocation); break; case RequestMsgType.Image: ResponseMessage = OnImageRequest(RequestMessage as RequestMessageImage); break; //case RequestMsgType.Link: // ResponseMessage = OnLinkRequest(RequestMessage as RequestMessageLink); // break; default: { //var responseMessage = CreateResponseMessage<ResponseMessageText>(); //var requestMessage = RequestMessage as RequestMessageText; ResponseMessage = DefaultResponseMessage(RequestMessage); break; } } //记录上下文 if (WeixinContextGlobal.UseWeixinContext && ResponseMessage != null) { WeixinContext.InsertMessage(ResponseMessage); } } //catch(WeChatException ex) //{ // (ResponseMessage as ResponseMessageText).Content = ex.Message; // Ctx.Response.Output.Write(ResponseDocument.ToString()); // //throw; //} //catch (Exception ex) //{ // throw new MessageHandlerException("MessageHandler中Execute()过程发生错误:" + ex.Message, ex); //} finally { OnExecuted(); //if (ResponseMessage != null) //{ // string respmsg = string.Concat(ResponseMessage.ToUserName, "@@@@", ResponseDocument.ToString()); // MyFBADiagnosticsService.Local.WriteTrace(0, MyFBADiagnosticsService.FBADiagnosticsCategory.Weixin, Microsoft.SharePoint.Administration.TraceSeverity.Medium, string.Concat("RespondDoc:", respmsg)); //} } }
/// <summary> /// 执行微信请求 /// </summary> public override async Task ExecuteAsync() { if (CancelExcute) { return; } await OnExecutingAsync(); if (CancelExcute) { return; } try { if (RequestMessage == null) { return; } switch (RequestMessage.MsgType) { case RequestMsgType.Text: { var requestMessage = RequestMessage as RequestMessageText; ResponseMessage = (await(OnTextOrEventRequestAsync(requestMessage)) ?? (await OnTextRequestAsync(requestMessage))); } break; case RequestMsgType.Location: ResponseMessage = await OnLocationRequestAsync(RequestMessage as RequestMessageLocation); break; case RequestMsgType.Image: ResponseMessage = 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.Unknown: ResponseMessage = await OnUnknownTypeRequestAsync(RequestMessage as RequestMessageUnknownType); break; case RequestMsgType.Event: { var requestMessageText = (RequestMessage as IRequestMessageEventBase).ConvertToRequestMessageText(); ResponseMessage = (await(OnTextOrEventRequestAsync(requestMessageText))) ?? (await OnEventRequestAsync(RequestMessage as IRequestMessageEventBase)); } break; default: throw new UnknownRequestMsgTypeException("未知的MsgType请求类型", null); } //记录上下文 //此处修改 if (WeixinContextGlobal.UseWeixinContext && ResponseMessage != null && !string.IsNullOrEmpty(ResponseMessage.FromUserName)) { WeixinContext.InsertMessage(ResponseMessage); } } catch (Exception ex) { throw new MessageHandlerException("MessageHandler中Execute()过程发生错误:" + ex.Message, ex); } finally { await OnExecutedAsync(); } }
public override XDocument Init(XDocument postDataDocument, object postData = null) { //进行加密判断并处理 _postModel = postData as PostModel; var postDataStr = postDataDocument.ToString(); XDocument decryptDoc = postDataDocument; if (_postModel != null && postDataDocument.Root.Element("Encrypt") != null && !string.IsNullOrEmpty(postDataDocument.Root.Element("Encrypt").Value)) { //使用了加密 UsingEcryptMessage = true; EcryptRequestDocument = postDataDocument; WXBizMsgCrypt msgCrype = new WXBizMsgCrypt(_postModel.Token, _postModel.EncodingAESKey, _postModel.AppId); string msgXml = null; var result = msgCrype.DecryptMsg(_postModel.Msg_Signature, _postModel.Timestamp, _postModel.Nonce, postDataStr, ref msgXml); //判断result类型 if (result != 0) { //验证没有通过,取消执行 CancelExcute = true; return(null); } if (postDataDocument.Root.Element("FromUserName") != null && !string.IsNullOrEmpty(postDataDocument.Root.Element("FromUserName").Value)) { //TODO:使用了兼容模式,进行验证即可 UsingCompatibilityModelEcryptMessage = true; } decryptDoc = XDocument.Parse(msgXml);//完成解密 } RequestMessage = RequestMessageFactory.GetRequestEntity(decryptDoc); if (UsingEcryptMessage) { RequestMessage.Encrypt = postDataDocument.Root.Element("Encrypt").Value; } //TODO:分布式系统中本地的上下文会有同步问题,需要同步使用远程的储存 if (WeixinContextGlobal.UseWeixinContext) { var omit = OmitRepeatedMessageFunc == null || OmitRepeatedMessageFunc(RequestMessage); lock (WeixinContextGlobal.OmitRepeatLock)//TODO:使用分布式锁 { #region 消息去重 if (omit && OmitRepeatedMessage && CurrentMessageContext.RequestMessages.Count > 0 //&& !(RequestMessage is RequestMessageEvent_Merchant_Order)批量订单的MsgId可能会相同 ) { //lastMessage必定有值(除非极端小的过期时间条件下,几乎不可能发生) var lastMessage = CurrentMessageContext.RequestMessages[CurrentMessageContext.RequestMessages.Count - 1]; 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; } } #endregion //在消息没有被去重的情况下记录上下文 if (!MessageIsRepeated) { WeixinContext.InsertMessage(RequestMessage); } } } return(decryptDoc); }
public override void Execute() { if (CancelExcute) { return; } OnExecuting(); if (CancelExcute) { return; } try { if (RequestMessage == null) { return; } //var handlers=QyMessageHandlerEvent.GetInvocationList(); //Log.Debug(); // 先处理插件注册的事件 if (QyMessageHandlerEvent != null) { Log.Debug("plugins event have been registed"); QyMessageHandlerEvent(this); } else { Log.Debug("no plugins event register"); } // 如果response里有东西了,说明已经处理过了。不再处理。 if (this.ResponseMessage != null) { return; } switch (RequestMessage.MsgType) { case RequestMsgType.DEFAULT: //第三方回调 { if (RequestMessage is IThirdPartyInfoBase) { var thirdPartyInfo = RequestMessage as IThirdPartyInfoBase; switch (thirdPartyInfo.InfoType) { case ThirdPartyInfo.SUITE_TICKET: break; case ThirdPartyInfo.CHANGE_AUTH: break; case ThirdPartyInfo.CANCEL_AUTH: break; default: throw new UnknownRequestMsgTypeException("未知的InfoType请求类型", null); } TextResponseMessage = "success"; //设置文字类型返回 } else { throw new WeixinException("没有找到合适的消息类型。"); } } break; case RequestMsgType.Chat: ResponseMessage = OnChatRequest(RequestMessage as RequestMessageChat); ResponseMessage = null; //不参与上下文记录 break; //以下是普通信息 case RequestMsgType.Text: { var requestMessage = RequestMessage as RequestMessageText; ResponseMessage = OnTextOrEventRequest(requestMessage) ?? OnTextRequest(requestMessage); } break; case RequestMsgType.Location: ResponseMessage = OnLocationRequest(RequestMessage as RequestMessageLocation); break; case RequestMsgType.Image: ResponseMessage = OnImageRequest(RequestMessage as RequestMessageImage); break; case RequestMsgType.Voice: ResponseMessage = OnVoiceRequest(RequestMessage as RequestMessageVoice); break; case RequestMsgType.Video: ResponseMessage = OnVideoRequest(RequestMessage as RequestMessageVideo); break; case RequestMsgType.ShortVideo: ResponseMessage = OnShortVideoRequest(RequestMessage as RequestMessageShortVideo); break; case RequestMsgType.Event: { var requestMessageText = (RequestMessage as IRequestMessageEventBase).ConvertToRequestMessageText(); ResponseMessage = OnTextOrEventRequest(requestMessageText) ?? OnEventRequest(RequestMessage as IRequestMessageEventBase); } break; default: throw new UnknownRequestMsgTypeException("未知的MsgType请求类型", null); } //记录上下文 if (WeixinContextGlobal.UseWeixinContext && ResponseMessage != null) { WeixinContext.InsertMessage(ResponseMessage); } } catch (Exception ex) { throw new MessageHandlerException("MessageHandler中Execute()过程发生错误:" + ex.Message, ex); } finally { OnExecuted(); } }