您系统中的IE浏览器版本过低,不能更好的体验,建议您升级至IE9以上,或者双核浏览器切换到极速核,IT人士,应该做个现代人---http://www.cnblogs.com/mochen/

[c#]asp.net开发微信公众平台(3)微信消息封装及反射赋值

 

     上一篇已经搭建好整体框架,实现了入口的验证,   验证通过后就交给LookMsgType方法处理,LookMsgType方法主要是对微信发来的不同的消息进行分解,不同的类型交给业务逻辑层不同的方法处理,   对不同类型的消息判断,可以用if,也可以用switch   一般来说超过5个的if用switch会更好, 这里贴出LookMsgType方法:

 1         public void LookMsgType(string msgType)
 2         {
 3 
 4             #region 判断消息类型
 5             switch (msgType)
 6             {
 7                 case "text":
 8                     RText mText = new RText();
 9                     mText = ReadXml.GetModel<RText>(mText, xmlModel);
10                     BLLWei.DoText(dbHome, mText);//文本消息
11                     break;
12                 case "image":
13                     RImg mImg = new RImg();
14                     mImg = ReadXml.GetModel<RImg>(mImg, xmlModel);
15                     BLLWei.DoImg(dbHome,mImg);//图片
16                     break;
17                 case "voice": //声音
18                     RVoice mVoice = new RVoice();
19                     mVoice = ReadXml.GetModel<RVoice>(mVoice, xmlModel);
20                     BLLWei.DoVoice(dbHome,mVoice);
21                     break;
22 
23                 case "video"://视频
24                     RVideo mVideo = new RVideo();
25                     mVideo = ReadXml.GetModel<RVideo>(mVideo, xmlModel);
26                     BLLWei.DoVideo(dbHome, mVideo);
27                     break;
28 
29                 case "location"://地理位置
30                     RLocation mLocation = new RLocation();
31                     mLocation = ReadXml.GetModel<RLocation>(mLocation, xmlModel);
32                     BLLWei.DoLocation(dbHome,mLocation);
33                     break;
34                 case "link"://链接
35                     RLink mLink = new RLink();
36                     mLink = ReadXml.GetModel<RLink>(mLink, xmlModel);
37                     BLLWei.DoLink(dbHome,mLink);
38                     break;
39                 #region 事件
40                 case "event":
41 
42                     switch (ReadXml.ReadModel("Event", xmlModel))
43                     {
44                         case "subscribe":
45 
46                             if (ReadXml.ReadModel("EventKey", xmlModel).IndexOf("qrscene_") >= 0)
47                             {
48                                 RCodeNotSub mNotSub = new RCodeNotSub();
49                                 mNotSub = ReadXml.GetModel<RCodeNotSub>(mNotSub, xmlModel);
50                                 BLLWei.DoCodeNotSub(dbHome,mNotSub);//未关注的新用户,扫描带参数的二维码关注
51                             }
52                             else
53                             {
54                                 RSub mSub = new RSub();
55                                 mSub = ReadXml.GetModel<RSub>(mSub, xmlModel);
56                                 BLLWei.DoSub(dbHome,mSub);//普通关注
57                             }
58                             break;
59                         case "unsubscribe":
60                             RUnsub mUnSub = new RUnsub ();
61                             mUnSub = ReadXml.GetModel<RUnsub>(mUnSub, xmlModel);
62                             BLLWei.DoUnSub(dbHome,mUnSub);//取消关注
63                             break;
64 
65                         case "SCAN":
66                             RCodeSub mCodeSub = new RCodeSub();
67                             mCodeSub = ReadXml.GetModel<RCodeSub>(mCodeSub, xmlModel);
68                             BLLWei.DoCodeSub(dbHome,mCodeSub);//已经关注的用户扫描带参数的二维码
69                             break;
70                         case "LOCATION"://用户上报地理位置
71 
72                             RSubLocation mSubLoc = new RSubLocation();
73                             mSubLoc = ReadXml.GetModel<RSubLocation>(mSubLoc, xmlModel);
74 
75                             BLLWei.DoSubLocation(dbHome, mSubLoc);
76                             break;
77                         case "CLICK"://自定义菜单点击
78 
79                             RMenuClick mMenuClk = new RMenuClick();
80                             mMenuClk = ReadXml.GetModel<RMenuClick>(mMenuClk, xmlModel);
81                             BLLWei.DoMenuClick(dbHome, mMenuClk);
82                             break;
83                         case "VIEW"://自定义菜单跳转事件
84 
85                             RMenuView mMenuVw = new RMenuView();
86                             mMenuVw = ReadXml.GetModel<RMenuView>(mMenuVw, xmlModel);
87                             BLLWei.DoMenuView(dbHome, mMenuVw);
88                             break;
89                     };
90                     break;
91                 #endregion
92             }
93             #endregion
94         }

 

外层switch判断msgtype,   在event类型时,再次switch判断具体的事件类型(关注、取消关注、自定义菜单事件等),  至此所有的微信发来的消息都有处理了,在上面代码中用到消息模型以及ReadXml.GetModel方法给模型赋值, 赋值之后传递给业务逻辑层对应的方法处理,  下面写出消息封装和给模型赋值的方法。

    1、消息封装:

      

           对所有微信发来的消息进行封装, 在datamodel中建一个Receive文件夹和一个send文件夹,在其中分别建立对应消息的类,完成之后,完整的datamodel类库如下图:

 

举例

-----接收消息:

文本消息RText.cs

 1     public class RText
 2     {
 3         public string ToUserName { get; set; }// 开发者微信号
 4         public string FromUserName { get; set; }// 用户号(OpenID)
 5         public long CreateTime { get; set; }// 创建时间
 6         public string MsgType { get; set; } //消息类型
 7         public string Content { get; set; }//内容
 8         public long MsgId { get; set; }//消息ID
 9 
10     }
View Code

自定义菜单点击RMenuClick.cs

 1     public class RMenuClick
 2     {
 3         public string ToUserName { get; set; }// 开发者微信号
 4         public string FromUserName { get; set; }// 用户号(OpenID)
 5         public long CreateTime { get; set; }// 创建时间
 6         public string MsgType { get; set; } //消息类型
 7 
 8         public string Event { get; set; }//事件类型
 9         public string EventKey { get; set; }//事件key
10         
11     }
View Code

其他也都类似,不一一列举。

-----发送消息

 

发送文本消息SText.cs

 1     public class SText
 2     {
 3 
 4 
 5 
 6         public string ToUserName { get; set; }// 用户号(OpenID)
 7         public string FromUserName { get; set; }// 开发者微信号
 8 
 9         public long CreateTime { get; set; }// 创建时间
10 
11         public string MsgType { get { return "text"; } } //消息类型
12 
13         public string Content { get; set; }//内容
14 
15 
16     }
SText

发送图文消息SNews.cs

 1 namespace DataModel.Send
 2 {
 3     public class SNews
 4     {
 5         public string ToUserName { get; set; }// 用户号(OpenID)
 6         public string FromUserName { get; set; }// 开发者微信号
 7 
 8         public long CreateTime { get; set; }// 创建时间
 9 
10         public string MsgType { get { return "news"; } } //消息类型
11 
12         public int ArticleCount { get; set; }//图文个数
13 
14         public List<ArticlesModel> Articles { get; set; }//图文列表
15     }
16     public class ArticlesModel //默认第一条大图显示
17     {
18         public string Title { get; set; }//标题
19         public string Description { get; set; }//描述
20         public string PicUrl { get; set; }//图片链接  
21         public string Url { get; set; }//点击之后跳转的链接
22 
23     }
24 }
发送图文消息

在发送图文消息中,因为回复给微信的图文消息中,具体的图文内容是多条(最多可以10条),所以单独会有ArticlesModel。    后面文章会写出图文消息的发送。

 

 

 

     2、通过反射给model赋值

 

       在上篇文章写的入口处,已经有了解析xml的方法,现在封装了消息,通常的做法,是每次用到对应的model就手动写代码赋值,  而我这里LookMsgType方法中所有给消息赋值时全用的ReadXml.GetModel这同一个方法,  这里用的就是反射,方法如下:

 1         /// <summary>
 2         /// 通过反射给接收消息model赋值
 3         /// </summary>
 4         /// <typeparam name="T"></typeparam>
 5         /// <param name="model"></param>
 6         /// <returns></returns>
 7         public static T GetModel<T>(T model, Dictionary<string, string> xmlModel) where T : class
 8         {
 9             var m = model.GetType();
10             foreach (PropertyInfo p in m.GetProperties())
11             {
12                 string name = p.Name;
13                 if (xmlModel.Keys.Contains(name))
14                 {
15                     string value=xmlModel.Where(x => x.Key == name).FirstOrDefault().Value;
16                     p.SetValue(model,
17                     string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, p.PropertyType), null); 
18                 }
19             }
20             return model;
21         }

 

T model 就是要使用的消息类,   xmlmodel是在入口处传递进来的解析的微信发来的xml信息,  这样,就不需要每次手动写代码赋值了。

 

     好了,此篇实现了lookmsgtype方法, 实现了消息封装和反射赋值, 接下去就是到了业务逻辑层中的处理和具体实现了...

 

 

posted @ 2014-04-14 10:25  沫尘  阅读(7594)  评论(7编辑  收藏  举报