应无所住,而生其心
排名
1
文章
860
粉丝
112
评论
163
net core webapi post传递参数
庸人 : 确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

微信开发三 使用反射根据消息类型自动调用不同方法

6556人阅读 2019/1/16 21:56 总访问:5182059 评论:1 收藏:0 手机
分类: 微信


微信只会向我们一个地方推送消息,如果全部逻辑都写到一起,代码会非常多。

所以我们可以考虑通过消息类型,来实现不同的消息类型调用不同的处理方法,降低代码的耦合性,

类似这种需求反射就是一种比较科学的方式。


先定义一个用于处理请求的基类       

  1.     /// <summary>
  2.     /// 处理微信请求的基类
  3.     /// </summary>
  4.     public class BaseProcess
  5.     {       
  6.         //处理微信请求的虚方法
  7.         public virtual void Process(XElement xl, HttpRequestBase _request, HttpResponseBase _response)
  8.         {
  9.  
  10.         }
  11.     }

 

然后不同的处理方法都去实现这个基类

例如:处理事件请求的处理方法 (消息类型为event)   

  1.  /// <summary>
  2.     /// 处理用户点击的事件处理方法
  3.     /// </summary
  4.     public class EventProcess : BaseProcess
  5.     {
  6.         public override void Process(XElement xl, HttpRequestBase _request, HttpResponseBase _response)
  7.         {
  8.             _response.Write("我是处理事件的请求方法");
  9.             _response.End();
  10.         }
  11.     }

如:处理用户发送的文本消息方法(消息类型为text)      

  1.     /// <summary>
  2.     /// 处理用户发送的文本消息方法
  3.     /// </summary>
  4.     public class TextProcess : BaseProcess
  5.     {
  6.  
  7.         public override void Process(XElement xl, HttpRequestBase _request, HttpResponseBase _response)
  8.         {
  9.             _response.Write("这是处理用户发送的文本信息");
  10.             _response.End();
  11.         }
  12.     }

如图:


然后在入口点用反射处理下就行了     

  1.   /// <summary>
  2.         /// 处理微信的请求
  3.         /// </summary>
  4.         public JsonResult Index()
  5.         {
  6.             try
  7.             {
  8.                 //读取微信发送的xml数据
  9.                 StreamReader reader = new StreamReader(Request.InputStream);
  10.                 string xmldata = reader.ReadToEnd();
  11.                 XElement xl = XElement.Parse(xmldata);
  12.  
  13.                 //取出消息类型
  14.                 string MsgType = xl.Elements().Where(a => a.Name == "MsgType").FirstOrDefault().Value;
  15.  
  16.                 //把微信推送的请求类型首字母转大写
  17.                 MsgType = FirstToUpper(MsgType);
  18.                 //得到需要的类型
  19.                 Type needtype = Type.GetType("MvcApplication1.Process." + MsgType + "Process");
  20.                 //通过反射调用实例化对应的处理类
  21.                 BaseProcess process = Activator.CreateInstance(needtype) as BaseProcess;
  22.                 //调用处理方法
  23.                 process.Process(xl, Request, Response);
  24.           
  25.                  
  26.                 return Json("请求成功");
  27.             }
  28.             catch (Exception e) 
  29.             {
  30.                 //请求失败进行日志记录...略         
  31.                 return Json("请求失败");
  32.             }
  33.         }

我们把用户点击的菜单事件真正实现以下    

  1.  /// <summary>
  2.     /// 处理用户点击的事件处理方法
  3.     /// </summary
  4.     public class EventProcess : BaseProcess
  5.     {
  6.         public override void Process(XElement xl, HttpRequestBase _request, HttpResponseBase _response)
  7.         {
  8.             //取出消息类型
  9.             string MsgType = xl.Elements().Where(a => a.Name == "MsgType").FirstOrDefault().Value;
  10.  
  11.             string ToUserName = xl.Elements().Where(a => a.Name == "ToUserName").FirstOrDefault().Value;
  12.             string FromUserName = xl.Elements().Where(a => a.Name == "FromUserName").FirstOrDefault().Value;
  13.  
  14.             //取出事件标识
  15.             string EventKey = xl.Elements().Where(a => a.Name == "EventKey").FirstOrDefault().Value;
  16.             string Event = xl.Elements().Where(a => a.Name == "Event").FirstOrDefault().Value;
  17.  
  18.  
  19.             //消息类型为空就没必要记录日志了
  20.             if (!string.IsNullOrEmpty(MsgType))
  21.             {
  22.                 AddLog(EventKey, MsgType, Event, ToUserName, FromUserName);
  23.             }
  24.  
  25.             //用户推送的是一个事件请求
  26.             ReMsgModel rsm = new ReMsgModel();
  27.  
  28.             rsm.MsgType = MsgType;
  29.             rsm.Content = "用户点击了菜单按钮";
  30.             rsm.ToUserName = FromUserName;
  31.             rsm.FromUserName = ToUserName;
  32.             GetTextMesXml(_response, rsm);
  33.         }
  34.     }

效果如下



欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价

zhouyou

2019/9/3 12:07:05

都是答复大方打算范德萨的v