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

来源:互联网 发布:mysql sql union 编辑:程序博客网 时间:2024/04/30 18:44

http://www.it165.net/pro/html/201404/12070.html

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

view sourceprint?
01.1         public void LookMsgType(string msgType)
02.2         {
03.3
04.4             #region 判断消息类型
05.5             switch (msgType)
06.6             {
07.7                 case 'text':
08.8                     RText mText = new RText();
09.9                     mText = ReadXml.GetModel<RText>(mText, xmlModel);
10.10                     BLLWei.DoText(dbHome, mText);//文本消息
11.11                     break;
12.12                 case 'image':
13.13                     RImg mImg = new RImg();
14.14                     mImg = ReadXml.GetModel<RImg>(mImg, xmlModel);
15.15                     BLLWei.DoImg(dbHome,mImg);//图片
16.16                     break;
17.17                 case 'voice'//声音
18.18                     RVoice mVoice = new RVoice();
19.19                     mVoice = ReadXml.GetModel<RVoice>(mVoice, xmlModel);
20.20                     BLLWei.DoVoice(dbHome,mVoice);
21.21                     break;
22.22
23.23                 case 'video'://视频
24.24                     RVideo mVideo = new RVideo();
25.25                     mVideo = ReadXml.GetModel<RVideo>(mVideo, xmlModel);
26.26                     BLLWei.DoVideo(dbHome, mVideo);
27.27                     break;
28.28
29.29                 case 'location'://地理位置
30.30                     RLocation mLocation = new RLocation();
31.31                     mLocation = ReadXml.GetModel<RLocation>(mLocation, xmlModel);
32.32                     BLLWei.DoLocation(dbHome,mLocation);
33.33                     break;
34.34                 case 'link'://链接
35.35                     RLink mLink = new RLink();
36.36                     mLink = ReadXml.GetModel<RLink>(mLink, xmlModel);
37.37                     BLLWei.DoLink(dbHome,mLink);
38.38                     break;
39.39                 #region 事件
40.40                 case 'event':
41.41
42.42                     switch (ReadXml.ReadModel('Event', xmlModel))
43.43                     {
44.44                         case 'subscribe':
45.45
46.46                             if (ReadXml.ReadModel('EventKey', xmlModel).IndexOf('qrscene_') >= 0)
47.47                             {
48.48                                 RCodeNotSub mNotSub = new RCodeNotSub();
49.49                                 mNotSub = ReadXml.GetModel<RCodeNotSub>(mNotSub, xmlModel);
50.50                                 BLLWei.DoCodeNotSub(dbHome,mNotSub);//未关注的新用户,扫描带参数的二维码关注
51.51                             }
52.52                             else
53.53                             {
54.54                                 RSub mSub = new RSub();
55.55                                 mSub = ReadXml.GetModel<RSub>(mSub, xmlModel);
56.56                                 BLLWei.DoSub(dbHome,mSub);//普通关注
57.57                             }
58.58                             break;
59.59                         case 'unsubscribe':
60.60                             RUnsub mUnSub = new RUnsub ();
61.61                             mUnSub = ReadXml.GetModel<RUnsub>(mUnSub, xmlModel);
62.62                             BLLWei.DoUnSub(dbHome,mUnSub);//取消关注
63.63                             break;
64.64
65.65                         case 'SCAN':
66.66                             RCodeSub mCodeSub = new RCodeSub();
67.67                             mCodeSub = ReadXml.GetModel<RCodeSub>(mCodeSub, xmlModel);
68.68                             BLLWei.DoCodeSub(dbHome,mCodeSub);//已经关注的用户扫描带参数的二维码
69.69                             break;
70.70                         case 'LOCATION'://用户上报地理位置
71.71
72.72                             RSubLocation mSubLoc = new RSubLocation();
73.73                             mSubLoc = ReadXml.GetModel<RSubLocation>(mSubLoc, xmlModel);
74.74
75.75                             BLLWei.DoSubLocation(dbHome, mSubLoc);
76.76                             break;
77.77                         case 'CLICK'://自定义菜单点击
78.78
79.79                             RMenuClick mMenuClk = new RMenuClick();
80.80                             mMenuClk = ReadXml.GetModel<RMenuClick>(mMenuClk, xmlModel);
81.81                             BLLWei.DoMenuClick(dbHome, mMenuClk);
82.82                             break;
83.83                         case 'VIEW'://自定义菜单跳转事件
84.84
85.85                             RMenuView mMenuVw = new RMenuView();
86.86                             mMenuVw = ReadXml.GetModel<RMenuView>(mMenuVw, xmlModel);
87.87                             BLLWei.DoMenuView(dbHome, mMenuVw);
88.88                             break;
89.89                     };
90.90                     break;
91.91                 #endregion
92.92             }
93.93             #endregion
94.94         }

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

    1、消息封装:

      

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

\

举例

-----接收消息:

文本消息RText.cs


加载中...
view sourceprint?
01.1     public class RText
02.2     {
03.3         public string ToUserName { get; set; }// 开发者微信号
04.4         public string FromUserName { get; set; }// 用户号(OpenID)
05.5         public long CreateTime { get; set; }// 创建时间
06.6         public string MsgType { get; set; } //消息类型
07.7         public string Content { get; set; }//内容
08.8         public long MsgId { get; set; }//消息ID
09.9
10.10     }

自定义菜单点击RMenuClick.cs


加载中...
view sourceprint?
01.1     public class RMenuClick
02.2     {
03.3         public string ToUserName { get; set; }// 开发者微信号
04.4         public string FromUserName { get; set; }// 用户号(OpenID)
05.5         public long CreateTime { get; set; }// 创建时间
06.6         public string MsgType { get; set; } //消息类型
07.7
08.8         public string Event { get; set; }//事件类型
09.9         public string EventKey { get; set; }//事件key
10.10        
11.11     }

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

-----发送消息

发送文本消息SText.cs


加载中...
view sourceprint?
01.1     public class SText
02.2     {
03.3
04.4
05.5
06.6         public string ToUserName { get; set; }// 用户号(OpenID)
07.7         public string FromUserName { get; set; }// 开发者微信号
08.8
09.9         public long CreateTime { get; set; }// 创建时间
10.10
11.11         public string MsgType { get { return 'text'; } } //消息类型
12.12
13.13         public string Content { get; set; }//内容
14.14
15.15
16.16     }
SText

发送图文消息SNews.cs


加载中...
view sourceprint?
01.1 namespace DataModel.Send
02.2 {
03.3     public class SNews
04.4     {
05.5         public string ToUserName { get; set; }// 用户号(OpenID)
06.6         public string FromUserName { get; set; }// 开发者微信号
07.7
08.8         public long CreateTime { get; set; }// 创建时间
09.9
10.10         public string MsgType { get { return 'news'; } } //消息类型
11.11
12.12         public int ArticleCount { get; set; }//图文个数
13.13
14.14         public List<ArticlesModel> Articles { get; set; }//图文列表
15.15     }
16.16     public class ArticlesModel //默认第一条大图显示
17.17     {
18.18         public string Title { get; set; }//标题
19.19         public string Description { get; set; }//描述
20.20         public string PicUrl { get; set; }//图片链接 
21.21         public string Url { get; set; }//点击之后跳转的链接
22.22
23.23     }
24.24 }
发送图文消息

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

     2、通过反射给model赋值

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

view sourceprint?
01.1         /// <summary>
02.2         /// 通过反射给接收消息model赋值
03.3         /// </summary>
04.4         /// <typeparam name='T'></typeparam>
05.5         /// <param name='model'></param>
06.6         /// <returns></returns>
07.7         public static T GetModel<T>(T model, Dictionary<string, string> xmlModel) where T : class
08.8         {
09.9             var m = model.GetType();
10.10             foreach (PropertyInfo p in m.GetProperties())
11.11             {
12.12                 string name = p.Name;
13.13                 if (xmlModel.Keys.Contains(name))
14.14                 {
15.15                     string value=xmlModel.Where(x => x.Key == name).FirstOrDefault().Value;
16.16                     p.SetValue(model,
17.17                     string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, p.PropertyType), null);
18.18                 }
19.19             }
20.20             return model;
21.21         }

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

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

0 0