/// <summary>
        ///     将调用转到合适的插件并由找到的插件执行处理。
        ///     <para>如果没有任何插件接收此次调用,将返回null。</para>
        /// </summary>
        /// <param name="context">如果为null,则自动从请求中获取数据</param>
        /// <returns></returns>
        public static ResponseMessage Invoke(InvokerContext context = null)
        {
            if (context == null)
            {
                context = ParseFromContext();
            }
            var msg = context.Message; //接收的消息
            var ctx = new PluginContext
            {
                ReceiveMessage = msg,
                Seller         = Obtainer == null ? null : Obtainer.Obtain(msg.ToUserName),
                UserContext    = UserContext.Get(msg.ToUserName, msg.FromUserName)
            };

            //需要验证签名
            if (RequireCheck && !EntrySign.ParseFromContext().Check(ctx.Seller == null ? null : ctx.Seller.Token))
            {
                return(null);
            }

            var plugin = Plugins.FirstOrDefault(p => p.CanProcess(ctx));
            var rep    = plugin == null ? null : plugin.Execute(ctx); //传递PluginContext

            //触发事件
            if (Invoked != null)
            {
                Invoked(ctx, rep);
            }

            return(rep);
        }
Exemple #2
0
 /// <summary>
 /// 放在接收数据的页面即可 实现WxMsgHandler类的消息
 /// </summary>
 public static void StartAcceptRequest()
 {
     if (!ReceiveMessage.IsRgisterMessageHandler)
     {
         ReceiveMessage.ResisterHandler(new WxMsgHandler());
     }
     if (ReceiveMessage.IsRgisterMessageHandler)
     {
         var sign = EntrySign.ParseFromContext();
         //验签名
         if (sign.Check())
         {
             //如果是GET请求就返回echostr数据
             if (EntrySign.IsEntryCheck())
             {
                 sign.Response();
             }
             else
             {
                 //其他请求交给注册的实现接口IMessageHandler的类完成
                 var msg = ReceiveMessage.ParseFromContext();
                 var rep = msg.Process();
                 rep.Response();
             }
         }
     }
 }