c# 微信開發 《內容回覆或事件觸發》

///驗籤綁定公衆號的時候,有回覆信息的入口
public void ProcessRequest(HttpContext context) { string postString = string.Empty; if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST") { using (Stream stream = HttpContext.Current.Request.InputStream) { Byte[] postBytes = new Byte[stream.Length]; stream.Read(postBytes, 0, (Int32)stream.Length); postString = Encoding.UTF8.GetString(postBytes); } if (!string.IsNullOrEmpty(postString)) { WeiXinService.ResponseMsg(postString, context); } } else { Auth(); //微信接入的測試 } }

  

/// <summary>
        /// 處理回覆消息
        /// </summary>
        /// <param name="postString"></param>
        /// <returns></returns>
        public void ResponseMsg(string postString, HttpContext context)
        {
    //經過信息類型返回相應的處理類
            IHandler handler = HandlerFactory.CreateHandler(postString);
            if (handler != null)
            {
                 handler.HandleRequest(context);
            }
        }

        /// <summary>
        /// 建立處理器
        /// </summary>
        /// <param name="requestXml">請求的xml</param>
        /// <returns>IHandler對象</returns>
        public static IHandler CreateHandler(string requestXml)
        {
            IHandler handler = null;
            if (!string.IsNullOrEmpty(requestXml))
            {
                //解析數據
                XmlDocument doc = new System.Xml.XmlDocument();
                doc.LoadXml(requestXml);
                XmlNode node = doc.SelectSingleNode("/xml/MsgType");
                if (node != null)
                {
                    XmlCDataSection section = node.FirstChild as XmlCDataSection;
                    if (section != null)
                    {
                        string msgType = section.Value;
                        //工廠類
                        switch (msgType)
                        {
                            case "text":
                                handler = new TextHandler(requestXml);
                                break;
                            case "event":
                                handler = new EventHandler(requestXml);
                                break;
                        }
                    }
                }
            }

            return handler;
        }

  

/// <summary>
        /// 事件處理,處理請求
        /// </summary>
        /// <returns></returns>
        public void HandleRequest(HttpContext context)
        {
            string response = string.Empty;
            EventMessage em = EventMessage.LoadFromXml(RequestXml);
            switch (em.Event.ToLower())
            {
                case ("unsubscribe")://取消關注
                    UnSubscribeEventHandler(em, context);
                    break;
                case ("subscribe"): //關注
                    SubscribeEventHandler(em, context);
                    break;
                case "click": //點擊事件
                    ClickEventHandler(em, context);
                    break;
            }
        }

  

點擊事件

/// <summary>
        /// 處理點擊事件
        /// </summary>
        /// <param name="em"></param>
        /// <returns></returns>
        private void ClickEventHandler(EventMessage em, HttpContext context)
        {
            string result = string.Empty;
            if (em != null && em.EventKey != null)
            {
                switch (em.EventKey.ToUpper())
                {
                    case "BTN_HELP":
                        btnHelpClick(em, context);
                        break;
                    case "BTN_QRCODE":
                        btnQrcodeClick(em, context);
                        break;
                }
            }
        }

  

/// <summary>
        /// 
        /// </summary>
        /// <param name="em"></param>
        /// <returns></returns>
        private void btnHelpClick(EventMessage em, HttpContext context)
        {
            //回覆歡迎消息
            TextMessage tm = new TextMessage();
            tm.ToUserName = em.FromUserName;
            tm.FromUserName = em.ToUserName;
            tm.CreateTime = WeiXinCommom.GetNowTime();
            tm.Content = 「回覆內容」;
            string response = TextTemplate.NormalText(tm.ToUserName, tm.FromUserName, tm.Content);
            context.Response.Clear();
            context.Response.Charset = "UTF-8";
            context.Response.Write(response);//回覆消息
            context.Response.End();
        }
相關文章
相關標籤/搜索