123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Web;
- using Senparc.NeuChar.Context;
- using Senparc.NeuChar.Entities;
- using Senparc.Weixin.MP.Entities;
- using Senparc.Weixin.MP.Entities.Request;
- using Senparc.Weixin.MP.MessageHandlers;
- namespace EMIS.Utility.OnlinePay.WeChat
- {
- public class CustomMessageHandler : MessageHandler<CustomMessageContext>
- {
- public CustomMessageHandler(Stream inputStream, PostModel postModel)
- : base(inputStream, postModel)
- {
- }
- public override IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage)
- {
- var responseMessage = base.CreateResponseMessage<ResponseMessageText>(); //ResponseMessageText也可以是News等其他类型
- responseMessage.Content = "";
- return responseMessage;
- }
- public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage)
- {
- return base.OnEvent_SubscribeRequest(requestMessage);
- }
- public override IResponseMessageBase OnEvent_UnsubscribeRequest(RequestMessageEvent_Unsubscribe requestMessage)
- {
- return base.OnEvent_UnsubscribeRequest(requestMessage);
- }
- }
- public class CustomMessageContext : MessageContext<IRequestMessageBase, IResponseMessageBase>
- {
- public CustomMessageContext()
- {
- base.MessageContextRemoved += CustomMessageContext_MessageContextRemoved;
- }
- /// <summary>
- /// 当上下文过期,被移除时触发的时间
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void CustomMessageContext_MessageContextRemoved(object sender, Senparc.NeuChar.Context.WeixinContextRemovedEventArgs<IRequestMessageBase, IResponseMessageBase> e)
- {
- /* 注意,这个事件不是实时触发的(当然你也可以专门写一个线程监控)
- * 为了提高效率,根据WeixinContext中的算法,这里的过期消息会在过期后下一条请求执行之前被清除
- */
- var messageContext = e.MessageContext as CustomMessageContext;
- if (messageContext == null)
- {
- return;//如果是正常的调用,messageContext不会为null
- }
- //TODO:这里根据需要执行消息过期时候的逻辑,下面的代码仅供参考
- //Log.InfoFormat("{0}的消息上下文已过期",e.OpenId);
- //api.SendMessage(e.OpenId, "由于长时间未搭理客服,您的客服状态已退出!");
- }
- }
- }
|