1. 程式人生 > >微信開發三 使用反射根據訊息型別自動呼叫不同方法

微信開發三 使用反射根據訊息型別自動呼叫不同方法

 使用反射根據訊息型別自動呼叫不同方法              

       微信只會向我們一個地方推送訊息,如果全部邏輯都寫到一起,程式碼會非常多,所以我們可以通過訊息型別,            來實現不同的訊息型別使用不同的處理方法,降低程式碼的耦合性

         先定義一個用於處理請求的基類    

    /// <summary>
    /// 處理微信請求的基類
    /// </summary>
    public class BaseProcess
    {       
        //處理微信請求的虛方法
        public virtual void Process(XElement xl, HttpRequestBase _request, HttpResponseBase _response)
        {

        }
    }

       然後不同的處理方法都去實現這個基類

     例如:處理事件請求的處理方法 (訊息型別為event)   

 /// <summary>
    /// 處理使用者點選的事件處理方法
    /// </summary
    public class EventProcess : BaseProcess
    {
        public override void Process(XElement xl, HttpRequestBase _request, HttpResponseBase _response)
        {
            _response.Write("我是處理事件的請求方法");
            _response.End();
        }

    }

例如:處理使用者傳送的文字訊息方法(訊息型別為text)         

    /// <summary>
    /// 處理使用者傳送的文字訊息方法
    /// </summary>
    public class TextProcess : BaseProcess
    {

        public override void Process(XElement xl, HttpRequestBase _request, HttpResponseBase _response)
        {
            _response.Write("這是處理使用者傳送的文字資訊");
            _response.End();
        }
    }

              

          然後在入口點用反射處理下就行了       

  /// <summary>
        /// 處理微信的請求
        /// </summary>
        public JsonResult Index()
        {
            try
            {
                //讀取微信傳送的xml資料
                StreamReader reader = new StreamReader(Request.InputStream);
                string xmldata = reader.ReadToEnd();
                XElement xl = XElement.Parse(xmldata);

                //取出訊息型別
                string MsgType = xl.Elements().Where(a => a.Name == "MsgType").FirstOrDefault().Value;

                //把微信推送的請求型別首字母轉大寫
                MsgType = FirstToUpper(MsgType);
                //得到需要的型別
                Type needtype = Type.GetType("MvcApplication1.Process." + MsgType + "Process");
                //通過反射呼叫例項化對應的處理類
                BaseProcess process = Activator.CreateInstance(needtype) as BaseProcess;
                //呼叫處理方法
                process.Process(xl, Request, Response);
          
                 
                return Json("請求成功");
            }
            catch (Exception e) 
            {
                //請求失敗進行日誌記錄...略         
                return Json("請求失敗");
            }
        }

         我們把使用者點選的選單事件真正實現以下

 /// <summary>
    /// 處理使用者點選的事件處理方法
    /// </summary
    public class EventProcess : BaseProcess
    {
        public override void Process(XElement xl, HttpRequestBase _request, HttpResponseBase _response)
        {
            //取出訊息型別
            string MsgType = xl.Elements().Where(a => a.Name == "MsgType").FirstOrDefault().Value;

            string ToUserName = xl.Elements().Where(a => a.Name == "ToUserName").FirstOrDefault().Value;
            string FromUserName = xl.Elements().Where(a => a.Name == "FromUserName").FirstOrDefault().Value;

            //取出事件標識
            string EventKey = xl.Elements().Where(a => a.Name == "EventKey").FirstOrDefault().Value;
            string Event = xl.Elements().Where(a => a.Name == "Event").FirstOrDefault().Value;


            //訊息型別為空就沒必要記錄日誌了
            if (!string.IsNullOrEmpty(MsgType))
            {
                AddLog(EventKey, MsgType, Event, ToUserName, FromUserName);
            }

            //使用者推送的是一個事件請求
            ReMsgModel rsm = new ReMsgModel();

            rsm.MsgType = MsgType;
            rsm.Content = "使用者點選了選單按鈕";
            rsm.ToUserName = FromUserName;
            rsm.FromUserName = ToUserName;
            GetTextMesXml(_response, rsm);
        }
    }

        效果如下