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

微信开发:使用反射+虚函数的方式分散处理微信不同消息类型

4964人阅读 2019/1/18 10:11 总访问:5182624 评论:0 收藏:0 手机
分类: 移动开发

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

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


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

  1.     public class BaseProcess
  2.     {
  3.         protected string ToUserName = "";
  4.         protected string FromUserName = "";
  5.         protected string MsgType = "";
  6.         /// <summary>
  7.         /// 解析公共参数的方法
  8.         /// </summary>
  9.         /// <param name="root"></param>
  10.         public void DealCommonParameter(XElement root)
  11.         {
  12.             ToUserName = root.Element("ToUserName").Value;
  13.             FromUserName = root.Element("FromUserName").Value;
  14.             MsgType = root.Element("MsgType").Value;
  15.         }
  16.         /// <summary>
  17.         /// 处理微信消息的虚函数
  18.         /// </summary>
  19.         /// <param name="root"></param>
  20.         /// <param name="Response"></param>
  21.         public virtual void Process(XElement root, HttpResponseBase Response)
  22.         {
  23.         }
  24.     }


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


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

  1.     public class EventProcess : BaseProcess
  2.     {
  3.         public override void Process(XElement root, HttpResponseBase Response)
  4.         {
  5.             string Event = root.Element("Event").Value;
  6.             if (Event == "CLICK")
  7.             {
  8.                 string EventKey = root.Element("EventKey").Value;
  9.                 if (EventKey == "wx_menu_net")
  10.                 {
  11.                     Response.Write(WXTools.SendNewsMsg(ToUserName, FromUserName));
  12.                     Response.End();
  13.                 }
  14.                 else if (EventKey == "wx_menu_python")
  15.                 {
  16.                     Response.Write(WXTools.SendTextMsg(ToUserName, FromUserName, "你点击了python"));
  17.                     Response.End();
  18.                 }
  19.                 else
  20.                 {
  21.                     Response.Write(WXTools.SendTextMsg(ToUserName, FromUserName, "你点击了事件"));
  22.                     Response.End();
  23.                 }
  24.             }
  25.         }
  26.     }

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

  1.     /// <summary>
  2.     /// 处理文本消息
  3.     /// </summary>
  4.     public class TextProcess : BaseProcess
  5.     {
  6.         public override void Process(XElement root, HttpResponseBase Response)
  7.         {
  8.             string content = root.Element("Content").Value;
  9.             //关键字回复
  10.             if (content.Contains("姐姐"))
  11.             {
  12.                 Response.Write(WXTools.SendImgMsg(ToUserName, FromUserName, "q6yc_ZO"));
  13.                 Response.End();
  14.             }
  15.             if (content.Contains("视频") || content.Contains("教程") || content.Contains("微信"))
  16.             {
  17.                 Response.Write(WXTools.SendVedioMsg(ToUserName, FromUserName, "q6yc_ZOszJbRSKad74""视频教程""这是一个微信开发视频教程"));
  18.                 Response.End();
  19.             }
  20.             if (content.Contains("图文"))
  21.             {
  22.                 Response.Write(WXTools.SendNewsMsg(ToUserName, FromUserName));
  23.                 Response.End();
  24.             }
  25.             else
  26.             {
  27.                 //记录一下日志
  28.                 LogTools.WriteLog(content, "接收到文本消息");
  29.                 Response.Write(WXTools.SendTextMsg(ToUserName, FromUserName, content));
  30.                 Response.End();
  31.             }
  32.         }
  33.     }

在如处理图片消息的类:

  1.     public class ImageProcess : BaseProcess
  2.     {
  3.         public override void Process(XElement root, HttpResponseBase Response)
  4.         {
  5.             string MediaId = root.Element("MediaId").Value;
  6.             //记录一下日志
  7.             //LogTools.WriteLog(PicUrl, "接收到图片消息");
  8.             Response.Write(WXTools.SendImgMsg(ToUserName, FromUserName, MediaId));
  9.             Response.End();
  10.         }
  11.     }

结构如图:


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

  1. /// <summary>
  2. /// 微信推送统一入口
  3. /// </summary>
  4. public void Index()
  5. {
  6.     //获取微信推送的内容
  7.     StreamReader reader = new StreamReader(Request.InputStream);
  8.     string xml = reader.ReadToEnd();
  9.     if (string.IsNullOrEmpty(xml))
  10.     {
  11.         return;
  12.     }
  13.     //解析xml,获取xml里边的具体值
  14.     XElement root = XElement.Parse(xml);
  15.     //首字母转大写
  16.     string MsgType = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(root.Element("MsgType").Value);
  17.     //根据不同的消息类型,通过反射去找到不同类的处理方法
  18.     //反射创建对象
  19.     object obj = Activator.CreateInstance(Type.GetType("MvcApplication2.Process." + MsgType + "Process"));
  20.     //父类提供公共参数
  21.     obj.GetType().GetMethod("DealCommonParamter").Invoke(obj, new object[] { root });
  22.     //调用具体的处理方法
  23.     obj.GetType().GetMethod("Process").Invoke(obj, new object[] { root, Response });
  24. }

当然公共参数的处理也可以直接封装到一个对象,然后反射调用方法的时候多传一个即可。







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

评价