热门IT资讯网

详细介绍微信公众平台的核心库SDK

发表于:2024-11-28 作者:热门IT资讯网编辑
编辑最后更新 2024年11月28日,今天小编就为大家带来一篇介绍微信公众平台的核心库SDK的文章。小编觉得挺不错的,为此分享给大家做个参考。一起跟随小编过来看看吧。NuGet包地址:由于来的OYMLCN.WeChat存在深度封装,并没有

今天小编就为大家带来一篇介绍微信公众平台的核心库SDK的文章。小编觉得挺不错的,为此分享给大家做个参考。一起跟随小编过来看看吧。

NuGet包地址:

由于来的OYMLCN.WeChat存在深度封装,并没有做完整的测试,对于使用不友好,现已重新构建SDK的接收消息被动回复模块。

现已做到最大程度的简易化及模块化,每个模块都已完成单元测试(根据微信说明文档的示例内容)。

接下来将会重新构建微信接口的调用模块,将会在基本完成后发布。

类JQuery操作的方式将会在所有功能完善并完成测试以后再整合到OYMLCN.WeChat当中。

主要使用方式:

var postModel = this.Request.GetQuery().IsValidRequest(Config);if (postModel == null)return Content("");return Content(new DemoHandler(WeChatRequest.Build(Config, postModel, this.Request.GetBody().ReadToEnd())).Result);
Config是基础接口配置OYMLCN.WeChat.Config的实例,参数依次为【公众平台微信号】、【AppId】、【AppSecret】、【Token】以及可选的【AESKey】。    配置的参数顺序相对于以往版本有变更,若报错无法验证签名有效性则需要检查参数的赋值顺序。Request.GetQuery()是对Request的扩展方法,封装在依赖包OYMLCN.Web当中,主要获取请求附带的参数。若在ASP.Net Web Api当中使用,需要安装兼容扩展包OYMLCN.Web.Api。Request.GetBody()主要是获取请求的正文,相当于Request.Body,为了使代码兼容WebApi而设置的一个方法。ReadToEnd()是Stream的扩展方法,主要是读取Stream中的文本内容。IsValidRequest(Config)是Dictionary的扩展方法,用于验证请求的有效性并获取PostModel参数。验证失败则返回null。当前示例是使用Handler处理的。
public class DemoHandler : OYMLCN.WeChat.MessageHandler{public DemoHandler(OYMLCN.WeChat.WeChatRequest request) : base(request) { }public override OYMLCN.WeChat.WeChatResponse DefaultResponseMessage(OYMLCN.WeChat.WeChatRequest request)    {return OYMLCN.WeChat.WeChatResponse.ResponseText(request, "Success");    }}

若需求简单,可不使用Handler来处理,使用方式如下:

OYMLCN.WeChat.Config config = new OYMLCN.WeChat.Config("微信账号名", "AppId", "AppSecret", "Token", "AESKey");OYMLCN.WeChat.PostModel postModel = this.Request.GetQuery().IsValidRequest(config);string body = this.Request.GetBody().ReadToEnd();OYMLCN.WeChat.WeChatRequest request = OYMLCN.WeChat.WeChatRequest.Build(config, postModel, body);OYMLCN.WeChat.WeChatResponse response = null;if (request.MessageType == OYMLCN.WeChat.WeChatRequestMessageType.Text)    response = OYMLCN.WeChat.WeChatResponse.ResponseText(request, "我是测试");else if (request.MessageType == OYMLCN.WeChat.WeChatRequestMessageType.Event)if (request.EventType == OYMLCN.WeChat.WeChatRequestEventType.Event关注)        response = OYMLCN.WeChat.WeChatResponse.ResponseText(request, "你来晚了");if (response != null)return Content(response.Result);return Content("");

附上单元测试代码,基本用法均已展现。

   1 using System;   2 using System.Collections.Generic;   3 using Microsoft.VisualStudio.TestTools.UnitTesting;   4 using System.Linq;   5    6 namespace OYMLCN.WeChat.Core.Test   7 {   8     [TestClass]   9     public class UnitTest  10     {  11         Config Config = new Config("wxName", "appId", "appSecret", "token", "aes");  12         PostModel PostModel = PostModel.Build(new Dictionary()  13         {  14             {"nonce","1362870167" },  15             {"openid","oOk2XjhrbcHP3tGgzDGAVHppo3Bs" },  16             {"signature","7940891098b505c22f99b0e3708627ec715aa832" },  17             {"timestamp","1496218735" }  18         });  19   20         [TestMethod]  21         public void WeChatRequestTest()  22         {  23             string textMsg = @"  24    25    26  1348831860  27    28    29  1234567890123456  30  ";  31   32             var request = WeChatRequest.Build(Config, PostModel, textMsg);  33             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Text);  34             Assert.AreEqual(request.ToUserName, "toUser");  35             Assert.AreEqual(request.FromUserName, "fromUser");  36             Assert.AreEqual(request.CreateTime, 1348831860);  37             Assert.AreEqual(request.MsgId, 1234567890123456);  38             Assert.AreEqual(request.MessageText.Content, "this is a test");  39   40             string imgMsg = @"  41   42   43 1348831860  44   45   46   47 1234567890123456  48 ";  49             request = WeChatRequest.Build(Config, PostModel, imgMsg);  50             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Image);  51             Assert.AreEqual(request.ToUserName, "toUser");  52             Assert.AreEqual(request.FromUserName, "fromUser");  53             Assert.AreEqual(request.CreateTime, 1348831860);  54             Assert.AreEqual(request.MessageImage.PicUrl, "this is a url");  55             Assert.AreEqual(request.MessageImage.MediaId, "media_id");  56             Assert.AreEqual(request.MsgId, 1234567890123456);  57   58             string voiceMsg = @"  59   60   61 1357290913  62   63   64   65 1234567890123456  66 ";  67             request = WeChatRequest.Build(Config, PostModel, voiceMsg);  68             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Voice);  69             Assert.AreEqual(request.ToUserName, "toUser");  70             Assert.AreEqual(request.FromUserName, "fromUser");  71             Assert.AreEqual(request.CreateTime, 1357290913);  72             Assert.AreEqual(request.MessageVoice.MediaId, "media_id");  73             Assert.AreEqual(request.MessageVoice.Format, "Format");  74             Assert.AreEqual(request.MsgId, 1234567890123456);  75   76             voiceMsg = @"  77   78   79 1357290913  80   81   82   83   84 1234567890123456  85 ";  86             request = WeChatRequest.Build(Config, PostModel, voiceMsg);  87             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Voice);  88             Assert.AreEqual(request.ToUserName, "toUser");  89             Assert.AreEqual(request.FromUserName, "fromUser");  90             Assert.AreEqual(request.CreateTime, 1357290913);  91             Assert.AreEqual(request.MessageVoice.MediaId, "media_id");  92             Assert.AreEqual(request.MessageVoice.Format, "Format");  93             Assert.AreEqual(request.MessageVoice.Recognition, "腾讯微信团队");  94             Assert.AreEqual(request.MsgId, 1234567890123456);  95   96             var videoMsg = @"  97   98   99 1357290913 100  101  102  103 1234567890123456 104 "; 105             request = WeChatRequest.Build(Config, PostModel, videoMsg); 106             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Video); 107             Assert.AreEqual(request.ToUserName, "toUser"); 108             Assert.AreEqual(request.FromUserName, "fromUser"); 109             Assert.AreEqual(request.CreateTime, 1357290913); 110             Assert.AreEqual(request.MessageVideo.MediaId, "media_id"); 111             Assert.AreEqual(request.MessageVideo.ThumbMediaId, "thumb_media_id"); 112             Assert.AreEqual(request.MsgId, 1234567890123456); 113  114             videoMsg = @" 115  116  117 1357290913 118  119  120  121 1234567890123456 122 "; 123             request = WeChatRequest.Build(Config, PostModel, videoMsg); 124             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.ShortVideo); 125             Assert.AreEqual(request.ToUserName, "toUser"); 126             Assert.AreEqual(request.FromUserName, "fromUser"); 127             Assert.AreEqual(request.CreateTime, 1357290913); 128             Assert.AreEqual(request.MessageVideo.MediaId, "media_id"); 129             Assert.AreEqual(request.MessageVideo.ThumbMediaId, "thumb_media_id"); 130             Assert.AreEqual(request.MsgId, 1234567890123456); 131  132             var locationMsg = @" 133  134  135 1351776360 136  137 23.134521 138 113.358803 139 20 140  141 1234567890123456 142 "; 143             request = WeChatRequest.Build(Config, PostModel, locationMsg); 144             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Location); 145             Assert.AreEqual(request.ToUserName, "toUser"); 146             Assert.AreEqual(request.FromUserName, "fromUser"); 147             Assert.AreEqual(request.CreateTime, 1351776360); 148             Assert.AreEqual(request.MessageLocation.Location_X, 23.134521); 149             Assert.AreEqual(request.MessageLocation.Location_Y, 113.358803); 150             Assert.AreEqual(request.MessageLocation.Scale, 20); 151             Assert.AreEqual(request.MessageLocation.Label, "位置信息"); 152             Assert.AreEqual(request.MsgId, 1234567890123456); 153  154             var linkMsg = @" 155  156  157 1351776360 158  159 <![CDATA[公众平台官网链接]]> 160  161  162 1234567890123456 163 "; 164             request = WeChatRequest.Build(Config, PostModel, linkMsg); 165             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Link); 166             Assert.AreEqual(request.ToUserName, "toUser"); 167             Assert.AreEqual(request.FromUserName, "fromUser"); 168             Assert.AreEqual(request.CreateTime, 1351776360); 169             Assert.AreEqual(request.MessageLink.Title, "公众平台官网链接"); 170             Assert.AreEqual(request.MessageLink.Description, "公众平台官网链接"); 171             Assert.AreEqual(request.MessageLink.Url, "url"); 172             Assert.AreEqual(request.MsgId, 1234567890123456); 173  174         } 175  176         [TestMethod] 177         public void WeChatRequestEventTest() 178         { 179             var subscribeEvent = @" 180  181  182 123456789 183  184  185 "; 186             var request = WeChatRequest.Build(Config, PostModel, subscribeEvent); 187             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 188             Assert.AreEqual(request.EventType, WeChatRequestEventType.Event关注); 189             Assert.AreEqual(request.ToUserName, "toUser"); 190             Assert.AreEqual(request.FromUserName, "FromUser"); 191             Assert.AreEqual(request.CreateTime, 123456789); 192             Assert.IsTrue(request.Event关注); 193  194             subscribeEvent = @" 195  196  197 123456789 198  199  200 "; 201             request = WeChatRequest.Build(Config, PostModel, subscribeEvent); 202             Assert.IsTrue(request.Event取消关注); 203  204             subscribeEvent = @" 205  206  207 123456789 208  209  210  211  212 "; 213             request = WeChatRequest.Build(Config, PostModel, subscribeEvent); 214             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 215             Assert.AreEqual(request.EventType, WeChatRequestEventType.Event关注); 216             Assert.AreEqual(request.ToUserName, "toUser"); 217             Assert.AreEqual(request.FromUserName, "FromUser"); 218             Assert.AreEqual(request.CreateTime, 123456789); 219             Assert.AreEqual(request.Event扫描带参数二维码.EventKey, "qrscene_123123"); 220             Assert.AreEqual(request.Event扫描带参数二维码.SceneId, "123123"); 221             Assert.AreEqual(request.Event扫描带参数二维码.Ticket, "TICKET"); 222  223             subscribeEvent = @" 224  225  226 123456789 227  228  229  230  231 "; 232             request = WeChatRequest.Build(Config, PostModel, subscribeEvent); 233             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 234             Assert.AreEqual(request.EventType, WeChatRequestEventType.Event扫描带参数二维码); 235             Assert.AreEqual(request.ToUserName, "toUser"); 236             Assert.AreEqual(request.FromUserName, "FromUser"); 237             Assert.AreEqual(request.CreateTime, 123456789); 238             Assert.AreEqual(request.Event扫描带参数二维码.EventKey, "SCENE_VALUE"); 239             Assert.AreEqual(request.Event扫描带参数二维码.SceneId, "SCENE_VALUE"); 240             Assert.AreEqual(request.Event扫描带参数二维码.Ticket, "TICKET"); 241  242  243             var locationEvent = @" 244  245  246 123456789 247  248  249 23.137466 250 113.352425 251 119.385040 252 "; 253             request = WeChatRequest.Build(Config, PostModel, locationEvent); 254             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 255             Assert.AreEqual(request.EventType, WeChatRequestEventType.Event上报地理位置); 256             Assert.AreEqual(request.ToUserName, "toUser"); 257             Assert.AreEqual(request.FromUserName, "fromUser"); 258             Assert.AreEqual(request.CreateTime, 123456789); 259             Assert.AreEqual(request.Event上报地理位置.Latitude, 23.137466); 260             Assert.AreEqual(request.Event上报地理位置.Longitude, 113.352425); 261             Assert.AreEqual(request.Event上报地理位置.Precision, 119.385040); 262  263             var clickEvent = @" 264  265  266 123456789 267  268  269  270 "; 271             request = WeChatRequest.Build(Config, PostModel, clickEvent); 272             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 273             Assert.AreEqual(request.EventType, WeChatRequestEventType.Event点击自定义菜单); 274             Assert.AreEqual(request.ToUserName, "toUser"); 275             Assert.AreEqual(request.FromUserName, "FromUser"); 276             Assert.AreEqual(request.CreateTime, 123456789); 277             Assert.AreEqual(request.Event点击自定义菜单.EventKey, "EVENTKEY"); 278  279             var linkEvent = @" 280  281  282 123456789 283  284  285  286 123 287 "; 288             request = WeChatRequest.Build(Config, PostModel, linkEvent); 289             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 290             Assert.AreEqual(request.EventType, WeChatRequestEventType.Event点击菜单跳转链接); 291             Assert.AreEqual(request.ToUserName, "toUser"); 292             Assert.AreEqual(request.FromUserName, "FromUser"); 293             Assert.AreEqual(request.CreateTime, 123456789); 294             Assert.AreEqual(request.Event点击菜单跳转链接.Url, "www.qq.com"); 295             Assert.AreEqual(request.Event点击菜单跳转链接.MenuId, 123); 296  297         } 298  299         [TestMethod] 300         public void WeChatRequestPushEventTest() 301         { 302             #region 模板消息发送结果 303             var tempplatePush = @" 304  305  306 1395658920 307  308  309 200163836 310  311 "; 312             var request = WeChatRequest.Build(Config, PostModel, tempplatePush); 313             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 314             Assert.AreEqual(request.EventType, WeChatRequestEventType.Push模板消息发送结果); 315             Assert.AreEqual(request.ToUserName, "gh_7f083739789a"); 316             Assert.AreEqual(request.FromUserName, "oia2TjuEGTNoeX76QEjQNrcURxG8"); 317             Assert.AreEqual(request.CreateTime, 1395658920); 318             Assert.AreEqual(request.Push模板消息发送结果.Status, "success"); 319             Assert.IsTrue(request.Push模板消息发送结果.Success); 320  321             tempplatePush = @" 322  323  324 1395658984 325  326  327 200163840 328  329 "; 330             request = WeChatRequest.Build(Config, PostModel, tempplatePush); 331             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 332             Assert.AreEqual(request.EventType, WeChatRequestEventType.Push模板消息发送结果); 333             Assert.AreEqual(request.ToUserName, "gh_7f083739789a"); 334             Assert.AreEqual(request.FromUserName, "oia2TjuEGTNoeX76QEjQNrcURxG8"); 335             Assert.AreEqual(request.CreateTime, 1395658984); 336             Assert.AreEqual(request.Push模板消息发送结果.Status, "failed:userblock"); 337             Assert.IsFalse(request.Push模板消息发送结果.Success); 338             tempplatePush = @" 339  340  341 1395658984 342  343  344 200163840 345  346 "; 347             request = WeChatRequest.Build(Config, PostModel, tempplatePush); 348             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 349             Assert.AreEqual(request.EventType, WeChatRequestEventType.Push模板消息发送结果); 350             Assert.AreEqual(request.ToUserName, "gh_7f083739789a"); 351             Assert.AreEqual(request.FromUserName, "oia2TjuEGTNoeX76QEjQNrcURxG8"); 352             Assert.AreEqual(request.CreateTime, 1395658984); 353             Assert.AreEqual(request.Push模板消息发送结果.Status, "failed:system failed"); 354             Assert.IsFalse(request.Push模板消息发送结果.Success); 355             #endregion 356  357             #region 群发结果 358             var massResultPush = @" 359  360  361 1481013459 362  363  364 1000001625 365  366 0 367 0 368 0 369 0 370  371 2 372  373  374 1 375 0 376 2 377  378 1 379 1 380 1 381 1 382  383  384 2 385 0 386 2 387  388 1 389 1 390 1 391 1 392  393  394 2 395  396 "; 397             request = WeChatRequest.Build(Config, PostModel, massResultPush); 398             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 399             Assert.AreEqual(request.EventType, WeChatRequestEventType.Push群发结果); 400             Assert.AreEqual(request.ToUserName, "gh_4d00ed8d6399"); 401             Assert.AreEqual(request.FromUserName, "oV5CrjpxgaGXNHIQigzNlgLTnwic"); 402             Assert.AreEqual(request.CreateTime, 1481013459); 403             Assert.AreEqual(request.Push群发结果.MsgID, 1000001625); 404             Assert.AreEqual(request.Push群发结果.Status, "err(30003)"); 405             Assert.IsFalse(request.Push群发结果.Success); 406             Assert.AreEqual(request.Push群发结果.ErrorReason, "原创校验被判定为转载文且用户选择了被判为转载就不群发"); 407             Assert.AreEqual(request.Push群发结果.TotalCount, 0); 408             Assert.AreEqual(request.Push群发结果.FilterCount, 0); 409             Assert.AreEqual(request.Push群发结果.SentCount, 0); 410             Assert.AreEqual(request.Push群发结果.ErrorCount, 0); 411             var checkResult = request.Push群发结果.CopyrightCheckResult; 412             var item = checkResult.First(); 413             Assert.AreEqual(item.ArticleIdx, 1); 414             Assert.AreEqual(item.UserDeclareState, 0); 415             Assert.AreEqual(item.AuditState, 2); 416             Assert.AreEqual(item.OriginalArticleUrl, "Url_1"); 417             Assert.AreEqual(item.OriginalArticleType, 1); 418             Assert.AreEqual(item.CanReprint, true); 419             Assert.AreEqual(item.NeedReplaceContent, true); 420             Assert.AreEqual(item.NeedShowReprintSource, true); 421             item = checkResult.Last(); 422             Assert.AreEqual(item.ArticleIdx, 2); 423             Assert.AreEqual(item.UserDeclareState, 0); 424             Assert.AreEqual(item.AuditState, 2); 425             Assert.AreEqual(item.OriginalArticleUrl, "Url_2"); 426             Assert.AreEqual(item.OriginalArticleType, 1); 427             Assert.AreEqual(item.CanReprint, true); 428             Assert.AreEqual(item.NeedReplaceContent, true); 429             Assert.AreEqual(item.NeedShowReprintSource, true); 430             Assert.AreEqual(request.Push群发结果.CheckState, 2); 431             #endregion 432  433             #region 菜单相关推送 434             var scancodePush = @" 435  436  437 1408090502 438  439  440  441  442  443  444 "; 445             request = WeChatRequest.Build(Config, PostModel, scancodePush); 446             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 447             Assert.AreEqual(request.EventType, WeChatRequestEventType.MenuPush扫码推事件); 448             Assert.AreEqual(request.ToUserName, "gh_e136c6e50636"); 449             Assert.AreEqual(request.FromUserName, "oMgHVjngRipVsoxg6TuX3vz6glDg"); 450             Assert.AreEqual(request.CreateTime, 1408090502); 451             Assert.AreEqual(request.MenuPush扫码推事件.EventKey, "6"); 452             Assert.AreEqual(request.MenuPush扫码推事件.ScanType, "qrcode"); 453             Assert.AreEqual(request.MenuPush扫码推事件.ScanResult, "1"); 454             scancodePush = @" 455  456  457 1408090606 458  459  460  461  462  463  464 "; 465             request = WeChatRequest.Build(Config, PostModel, scancodePush); 466             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 467             Assert.AreEqual(request.EventType, WeChatRequestEventType.MenuPush扫码推等待事件); 468             Assert.AreEqual(request.ToUserName, "gh_e136c6e50636"); 469             Assert.AreEqual(request.FromUserName, "oMgHVjngRipVsoxg6TuX3vz6glDg"); 470             Assert.AreEqual(request.CreateTime, 1408090606); 471             Assert.AreEqual(request.MenuPush扫码推等待事件.EventKey, "6"); 472             Assert.AreEqual(request.MenuPush扫码推等待事件.ScanType, "qrcode"); 473             Assert.AreEqual(request.MenuPush扫码推等待事件.ScanResult, "2"); 474  475             var imagePush = @" 476  477  478 1408090651 479  480  481  482 1 483  484  485  486  487 "; 488             request = WeChatRequest.Build(Config, PostModel, imagePush); 489             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 490             Assert.AreEqual(request.EventType, WeChatRequestEventType.MenuPush系统拍照发图); 491             Assert.AreEqual(request.ToUserName, "gh_e136c6e50636"); 492             Assert.AreEqual(request.FromUserName, "oMgHVjngRipVsoxg6TuX3vz6glDg"); 493             Assert.AreEqual(request.CreateTime, 1408090651); 494             Assert.AreEqual(request.MenuPush系统拍照发图.EventKey, "6"); 495             Assert.AreEqual(request.MenuPush系统拍照发图.Count, 1); 496             CollectionAssert.AreEqual(request.MenuPush系统拍照发图.PicMd5Sum, new string[] { "1b5f7c23b5bf75682a53e7b6d163e185" }); 497             imagePush = @" 498  499  500 1408090816 501  502  503  504 1 505  506  507  508  509 "; 510             request = WeChatRequest.Build(Config, PostModel, imagePush); 511             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 512             Assert.AreEqual(request.EventType, WeChatRequestEventType.MenuPush拍照或者相册发图); 513             Assert.AreEqual(request.ToUserName, "gh_e136c6e50636"); 514             Assert.AreEqual(request.FromUserName, "oMgHVjngRipVsoxg6TuX3vz6glDg"); 515             Assert.AreEqual(request.CreateTime, 1408090816); 516             Assert.AreEqual(request.MenuPush系统拍照发图.EventKey, "6"); 517             Assert.AreEqual(request.MenuPush系统拍照发图.Count, 1); 518             CollectionAssert.AreEqual(request.MenuPush系统拍照发图.PicMd5Sum, new string[] { "5a75aaca956d97be686719218f275c6b" }); 519             imagePush = @" 520  521  522 1408090816 523  524  525  526 1 527  528  529  530  531 "; 532             request = WeChatRequest.Build(Config, PostModel, imagePush); 533             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 534             Assert.AreEqual(request.EventType, WeChatRequestEventType.MenuPush微信相册发图); 535             Assert.AreEqual(request.ToUserName, "gh_e136c6e50636"); 536             Assert.AreEqual(request.FromUserName, "oMgHVjngRipVsoxg6TuX3vz6glDg"); 537             Assert.AreEqual(request.CreateTime, 1408090816); 538             Assert.AreEqual(request.MenuPush系统拍照发图.EventKey, "6"); 539             Assert.AreEqual(request.MenuPush系统拍照发图.Count, 1); 540             CollectionAssert.AreEqual(request.MenuPush系统拍照发图.PicMd5Sum, new string[] { "5a75aaca956d97be686719218f275c6b" }); 541  542             var locationPush = @" 543  544  545 1408091189 546  547  548  549  550  551  552  553  554  555 "; 556             request = WeChatRequest.Build(Config, PostModel, locationPush); 557             Assert.AreEqual(request.MessageType, WeChatRequestMessageType.Event); 558             Assert.AreEqual(request.EventType, WeChatRequestEventType.MenuPush位置选择); 559             Assert.AreEqual(request.ToUserName, "gh_e136c6e50636"); 560             Assert.AreEqual(request.FromUserName, "oMgHVjngRipVsoxg6TuX3vz6glDg"); 561             Assert.AreEqual(request.CreateTime, 1408091189); 562             Assert.AreEqual(request.MenuPush位置选择.EventKey, "6"); 563             Assert.AreEqual(request.MenuPush位置选择.Location_X, 23); 564             Assert.AreEqual(request.MenuPush位置选择.Location_Y, 113); 565             Assert.AreEqual(request.MenuPush位置选择.Scale, 15); 566             Assert.AreEqual(request.MenuPush位置选择.Label, "广州市海珠区客村艺苑路 106号"); 567             Assert.AreEqual(request.MenuPush位置选择.Poiname, ""); 568             #endregion 569         } 570  571         [TestMethod] 572         public void WeChatResponseTest() 573         { 574             var demoMsg = @" 575  576  577 12345678 578 "; 579             var request = WeChatRequest.Build(Config, PostModel, demoMsg); 580             Assert.AreEqual(WeChatResponse.ResponseText(request, "你好").Source, @" 581  582  583 12345678 584  585  586 ".RemoveBr()); 587             Assert.AreEqual(WeChatResponse.ResponseImage(request, "media_id").Source, @" 588  589  590 12345678 591  592  593  594  595 ".RemoveBr()); 596             Assert.AreEqual(WeChatResponse.ResponseVoice(request, "media_id").Source, @" 597  598  599 12345678 600  601  602  603  604 ".RemoveBr()); 605             Assert.AreEqual(WeChatResponse.ResponseVideo(request, "media_id", "title", "description").Source, @" 606  607  608 12345678 609  610  615 ".RemoveBr()); 616             Assert.AreEqual(WeChatResponse.ResponseMusic(request, "media_id", "MUSIC_Url", "TITLE", "DESCRIPTION", "HQ_MUSIC_Url").Source, @" 617  618  619 12345678 620  621  622 <![CDATA[TITLE]]> 623  624  625  626  627  628 ".RemoveBr()); 629  630             Assert.AreEqual(WeChatResponse.ResponseNews(request, 631                 new WeChatResponse.Article("title1", "description1", "picurl", "url"), 632                 new WeChatResponse.Article("title", "description", "picurl", "url") 633                 ).Source, @" 634  635  636 12345678 637  638 2 639  640  641 <![CDATA[title1]]> 642  643  644  645  646  647 <![CDATA[title]]> 648  649  650  651  652  653 ".RemoveBr()); 654             Assert.AreEqual(WeChatResponse.TransferToCustomerService(request).Source, @" 655  656  657 12345678 658  659 ".RemoveBr()); 660             Assert.AreEqual(WeChatResponse.TransferToCustomerService(request, "test1").Source, @" 661  662  663 12345678 664  665  666 ".RemoveBr()); 667         } 668  669         [TestMethod] 670         public void WeChatExtensionTest() 671         { 672             var demoQuery = new Dictionary() 673             { 674                 {"nonce","1362870167"}, 675                 {"openid","oOk2XjhrbcHP3tGgzDGAVHppo3Bs"}, 676                 {"signature","7940891098b505c22f99b0e3708627ec715aa832"}, 677                 {"timestamp","1496218735"}, 678                 {"echostr","demoEchostr"} 679             }; 680             Assert.IsNotNull(demoQuery.IsValidRequest(Config)); 681             Assert.AreEqual(demoQuery.ConfigVerify(Config), "demoEchostr"); 682             demoQuery = new Dictionary(); 683             Assert.IsNull(demoQuery.IsValidRequest(Config)); 684             Assert.AreEqual(demoQuery.ConfigVerify(Config), ""); 685         } 686  687         [TestMethod] 688         public void WeChatHanddlerTest() 689         { 690             string demoMsg = @" 691   692   693  1348831860 694   695   696  1234567890123456 697  "; 698             bool IsOkReponse(HandlerDemo demo, string text) => demo.Result.ToXDocument().SelectValue("Content").Equals(text); 699  700             var request = WeChatRequest.Build(Config, PostModel, demoMsg); 701             var demoHandler = new HandlerDemo(request); 702             Assert.AreEqual(demoHandler.Result, "");//未支持消息,直接回复空字符串 703  704             #region 微信消息 705             demoMsg = @" 706   707   708  1348831860 709   710   711  1234567890123456 712  "; 713             request = WeChatRequest.Build(Config, PostModel, demoMsg); 714             demoHandler = new HandlerDemo(request); 715             Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageText")); 716  717             demoMsg = @" 718  719  720 1348831860 721  722  723  724 1234567890123456 725 "; 726             request = WeChatRequest.Build(Config, PostModel, demoMsg); 727             demoHandler = new HandlerDemo(request); 728             Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageImage")); 729  730             demoMsg = @" 731  732  733 1357290913 734  735  736  737  738 1234567890123456 739 "; 740             request = WeChatRequest.Build(Config, PostModel, demoMsg); 741             demoHandler = new HandlerDemo(request); 742             Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageVoice")); 743  744             demoMsg = @" 745  746  747 1357290913 748  749  750  751 1234567890123456 752 "; 753             request = WeChatRequest.Build(Config, PostModel, demoMsg); 754             demoHandler = new HandlerDemo(request); 755             Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageVideo")); 756  757             demoMsg = @" 758  759  760 1357290913 761  762  763  764 1234567890123456 765 "; 766             request = WeChatRequest.Build(Config, PostModel, demoMsg); 767             demoHandler = new HandlerDemo(request); 768             Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageShortVideo")); 769  770             demoMsg = @" 771  772  773 1351776360 774  775 23.134521 776 113.358803 777 20 778  779 1234567890123456 780 "; 781             request = WeChatRequest.Build(Config, PostModel, demoMsg); 782             demoHandler = new HandlerDemo(request); 783             Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageLocaltion")); 784  785             demoMsg = @" 786  787  788 1351776360 789  790 <![CDATA[公众平台官网链接]]> 791  792  793 1234567890123456 794 "; 795             request = WeChatRequest.Build(Config, PostModel, demoMsg); 796             demoHandler = new HandlerDemo(request); 797             Assert.IsTrue(IsOkReponse(demoHandler, "OnMessageLink")); 798             #endregion 799  800             #region 事件消息 801             demoMsg = @" 802  803  804 123456789 805  806  807 "; 808             request = WeChatRequest.Build(Config, PostModel, demoMsg); 809             demoHandler = new HandlerDemo(request); 810             Assert.IsTrue(IsOkReponse(demoHandler, "OnEvent关注")); 811  812  813             demoMsg = @" 814  815  816 123456789 817  818  819 "; 820             request = WeChatRequest.Build(Config, PostModel, demoMsg); 821             demoHandler = new HandlerDemo(request); 822             Assert.ThrowsException(() => { string result = demoHandler.Result; }, "OnEvent取消关注"); 823  824             demoMsg = @" 825  826  827 123456789 828  829  830  831  832 "; 833             request = WeChatRequest.Build(Config, PostModel, demoMsg); 834             demoHandler = new HandlerDemo(request); 835             Assert.IsTrue(IsOkReponse(demoHandler, "OnEvent关注-扫描带参数二维码")); 836  837             demoMsg = @" 838  839  840 123456789 841  842  843  844  845 "; 846             request = WeChatRequest.Build(Config, PostModel, demoMsg); 847             demoHandler = new HandlerDemo(request); 848             Assert.IsTrue(IsOkReponse(demoHandler, "OnEvent扫描带参数二维码")); 849  850             demoMsg = @" 851  852  853 123456789 854  855  856 23.137466 857 113.352425 858 119.385040 859 "; 860             request = WeChatRequest.Build(Config, PostModel, demoMsg); 861             demoHandler = new HandlerDemo(request); 862             Assert.IsTrue(IsOkReponse(demoHandler, "OnEvent上报地理位置")); 863  864             demoMsg = @" 865  866  867 123456789 868  869  870  871 "; 872             request = WeChatRequest.Build(Config, PostModel, demoMsg); 873             demoHandler = new HandlerDemo(request); 874             Assert.IsTrue(IsOkReponse(demoHandler, "OnEvent点击自定义菜单")); 875  876             demoMsg = @" 877  878  879 123456789 880  881  882  883 123 884 "; 885             request = WeChatRequest.Build(Config, PostModel, demoMsg); 886             demoHandler = new HandlerDemo(request); 887             Assert.ThrowsException(() => { string result = demoHandler.Result; }, "OnEvent点击菜单跳转链接"); 888             #endregion 889  890             #region 微信服务相关 891             demoMsg = @" 892  893  894 1395658920 895  896  897 200163836 898  899 "; 900             request = WeChatRequest.Build(Config, PostModel, demoMsg); 901             demoHandler = new HandlerDemo(request); 902             Assert.ThrowsException(() => { string result = demoHandler.Result; }, "OnPush模板消息发送结果"); 903  904             demoMsg = @" 905  906  907 1481013459 908  909  910 1000001625 911  912 0 913 0 914 0 915 0 916 "; 917             request = WeChatRequest.Build(Config, PostModel, demoMsg); 918             demoHandler = new HandlerDemo(request); 919             Assert.ThrowsException(() => { string result = demoHandler.Result; }, "OnPush群发结果"); 920             #endregion 921  922             #region 菜单相关推送 923             demoMsg = @" 924  925  926 1408090502 927  928  929  930  931  932  933 "; 934             request = WeChatRequest.Build(Config, PostModel, demoMsg); 935             demoHandler = new HandlerDemo(request); 936             Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu扫码推事件")); 937  938             demoMsg = @" 939  940  941 1408090606 942  943  944  945  946  947  948 "; 949             request = WeChatRequest.Build(Config, PostModel, demoMsg); 950             demoHandler = new HandlerDemo(request); 951             Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu扫码推等待事件")); 952  953             demoMsg = @" 954  955  956 1408090651 957  958  959  960 1 961  962  963  964  965 "; 966             request = WeChatRequest.Build(Config, PostModel, demoMsg); 967             demoHandler = new HandlerDemo(request); 968             Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu系统拍照发图")); 969  970             demoMsg = @" 971  972  973 1408090816 974  975  976  977 1 978  979  980  981  982 "; 983             request = WeChatRequest.Build(Config, PostModel, demoMsg); 984             demoHandler = new HandlerDemo(request); 985             Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu拍照或者相册发图")); 986  987             demoMsg = @" 988  989  990 1408090816 991  992  993  994 1 995  996  997  998  999 ";1000             request = WeChatRequest.Build(Config, PostModel, demoMsg);1001             demoHandler = new HandlerDemo(request);1002             Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu微信相册发图"));1003 1004             demoMsg = @"1005 1006 1007 14080911891008 1009 1010 1011 1012 1013 1014 1015 1016 1017 ";1018             request = WeChatRequest.Build(Config, PostModel, demoMsg);1019             demoHandler = new HandlerDemo(request);1020             Assert.IsTrue(IsOkReponse(demoHandler, "OnPushMenu位置选择"));1021             #endregion1022 1023 1024             string FillDemoContent(string text) => $@"1025  1026  1027  13488318601028  1029  1030  12345678901234561031  ";1032             demoMsg = FillDemoContent("123");1033             request = WeChatRequest.Build(Config, PostModel, demoMsg);1034             demoHandler = new HandlerDemo(request);1035             Assert.IsTrue(IsOkReponse(demoHandler, "123"));1036 1037             demoMsg = FillDemoContent("34567");1038             request = WeChatRequest.Build(Config, PostModel, demoMsg);1039             demoHandler = new HandlerDemo(request);1040             Assert.IsTrue(IsOkReponse(demoHandler, "123456"));1041 1042             demoMsg = FillDemoContent("aBc");1043             request = WeChatRequest.Build(Config, PostModel, demoMsg);1044             demoHandler = new HandlerDemo(request);1045             Assert.IsTrue(IsOkReponse(demoHandler, "ABc"));1046 1047             demoMsg = FillDemoContent("BcDeFgh");1048             request = WeChatRequest.Build(Config, PostModel, demoMsg);1049             demoHandler = new HandlerDemo(request);1050             Assert.IsTrue(IsOkReponse(demoHandler, "ABcdEf"));1051 1052 1053             demoMsg = @"1054 1055 1056 1234567891057 1058 1059 1060 ";1061             request = WeChatRequest.Build(Config, PostModel, demoMsg);1062             demoHandler = new HandlerDemo(request);1063             Assert.IsTrue(IsOkReponse(demoHandler, "Event点击自定义菜单Test"));1064 1065             demoMsg = @"1066 1067 1068 1234567891069 1070 1071 1072 1073 ";1074             request = WeChatRequest.Build(Config, PostModel, demoMsg);1075             demoHandler = new HandlerDemo(request);1076             Assert.IsTrue(IsOkReponse(demoHandler, "Event扫描带参数二维码scanId"));1077 1078             demoMsg = @"1079 1080 1081 1234567891082 1083 1084 1085 1086 ";1087             request = WeChatRequest.Build(Config, PostModel, demoMsg);1088             demoHandler = new HandlerDemo(request);1089             Assert.IsTrue(IsOkReponse(demoHandler, "Event扫描带参数二维码scanId"));1090 1091         }1092         public class HandlerDemo : MessageHandler1093         {1094             public HandlerDemo(WeChatRequest request) : base(request)1095             {1096                 AddTextKeyWordHandler(new HandlerRule(HandlerContrast.Equal, "123"), (req, msg) =>1097                  {1098                      return WeChatResponse.ResponseText(req, "123");1099                  });1100                 AddTextKeyWordHandler(new HandlerRule(HandlerContrast.Contain, "456"), (req, msg) =>1101                 {1102                     return WeChatResponse.ResponseText(req, "123456");1103                 });1104                 AddTextKeyWordHandler(new HandlerRule(HandlerContrast.Superficial, "abc"), (req, msg) =>1105                 {1106                     return WeChatResponse.ResponseText(req, "ABc");1107                 });1108                 AddTextKeyWordHandler(new HandlerRule(HandlerContrast.ContainSuperficial, "def"), (req, msg) =>1109                 {1110                     return WeChatResponse.ResponseText(req, "ABcdEf");1111                 });1112 1113                 AddEventMenuClickHandler("test_menu", (req, msg) =>1114                 {1115                     return WeChatResponse.ResponseText(req, "Event点击自定义菜单Test");1116                 });1117                 AddEventScanIdHandler("scanId", (req, msg) =>1118                 {1119                     return WeChatResponse.ResponseText(req, "Event扫描带参数二维码scanId");1120                 });1121             }1122 1123             public override WeChatResponse DefaultResponseMessage(WeChatRequest request)1124             {1125                 return null; //未定义处理的默认方法,不处理则直接返回Null以回复空字符串给微信服务器1126             }1127             public override WeChatResponse OnMessageText(WeChatRequest request, WeChatRequest.WeChatMessageText text)1128             {1129                 return WeChatResponse.ResponseText(request, "OnMessageText");1130             }1131             public override WeChatResponse OnMessageImage(WeChatRequest request, WeChatRequest.WeChatMessageImage image)1132             {1133                 return WeChatResponse.ResponseText(request, "OnMessageImage");1134             }1135             public override WeChatResponse OnMessageVoice(WeChatRequest request, WeChatRequest.WeChatMessageVoice voice)1136             {1137                 return WeChatResponse.ResponseText(request, "OnMessageVoice");1138             }1139             public override WeChatResponse OnMessageLocaltion(WeChatRequest request, WeChatRequest.WeChatMessageLocation location)1140             {1141                 return WeChatResponse.ResponseText(request, "OnMessageLocaltion");1142             }1143             public override WeChatResponse OnMessageVideo(WeChatRequest request, WeChatRequest.WeChatMessageVideo video)1144             {1145                 return WeChatResponse.ResponseText(request, "OnMessageVideo");1146             }1147             public override WeChatResponse OnMessageShortVideo(WeChatRequest request, WeChatRequest.WeChatMessageVideo shortVideo)1148             {1149                 return WeChatResponse.ResponseText(request, "OnMessageShortVideo");1150             }1151             public override WeChatResponse OnMessageLink(WeChatRequest request, WeChatRequest.WeChatMessageLink link)1152             {1153                 return WeChatResponse.ResponseText(request, "OnMessageLink");1154             }1155 1156             public override WeChatResponse OnEvent关注(WeChatRequest request)1157             {1158                 return WeChatResponse.ResponseText(request, "OnEvent关注");1159             }1160             public override WeChatResponse OnEvent关注(WeChatRequest request, WeChatRequest.WeChatEvent扫描带参数二维码 msg)1161             {1162                 return WeChatResponse.ResponseText(request, "OnEvent关注-扫描带参数二维码");1163             }1164             public override void OnEvent取消关注(WeChatRequest request)1165             {1166                 throw new NotImplementedException("OnEvent取消关注");1167             }1168             public override WeChatResponse OnEvent扫描带参数二维码(WeChatRequest request, WeChatRequest.WeChatEvent扫描带参数二维码 msg)1169             {1170                 return WeChatResponse.ResponseText(request, "OnEvent扫描带参数二维码");1171             }1172             public override WeChatResponse OnEvent上报地理位置(WeChatRequest request, WeChatRequest.WeChatEvent上报地理位置 msg)1173             {1174                 return WeChatResponse.ResponseText(request, "OnEvent上报地理位置");1175             }1176             public override WeChatResponse OnEvent点击自定义菜单(WeChatRequest request, WeChatRequest.WeChatEvent点击自定义菜单 msg)1177             {1178                 return WeChatResponse.ResponseText(request, "OnEvent点击自定义菜单");1179             }1180             public override void OnEvent点击菜单跳转链接(WeChatRequest request, WeChatRequest.WeChatEvent点击菜单跳转链接 msg)1181             {1182                 throw new NotImplementedException("OnEvent点击菜单跳转链接");1183             }1184 1185             public override WeChatResponse OnPushMenu位置选择(WeChatRequest request, WeChatRequest.WeChatMenuPush位置选择 msg)1186             {1187                 return WeChatResponse.ResponseText(request, "OnPushMenu位置选择");1188             }1189             public override WeChatResponse OnPushMenu扫码推事件(WeChatRequest request, WeChatRequest.WeChatMenuPush扫码事件 msg)1190             {1191                 return WeChatResponse.ResponseText(request, "OnPushMenu扫码推事件");1192             }1193             public override WeChatResponse OnPushMenu扫码推等待事件(WeChatRequest request, WeChatRequest.WeChatMenuPush扫码事件 msg)1194             {1195                 return WeChatResponse.ResponseText(request, "OnPushMenu扫码推等待事件");1196             }1197             public override WeChatResponse OnPushMenu系统拍照发图(WeChatRequest request, WeChatRequest.WeChatMenuPush拍照发图 msg)1198             {1199                 return WeChatResponse.ResponseText(request, "OnPushMenu系统拍照发图");1200             }1201             public override WeChatResponse OnPushMenu微信相册发图(WeChatRequest request, WeChatRequest.WeChatMenuPush拍照发图 msg)1202             {1203                 return WeChatResponse.ResponseText(request, "OnPushMenu微信相册发图");1204             }1205             public override WeChatResponse OnPushMenu拍照或者相册发图(WeChatRequest request, WeChatRequest.WeChatMenuPush拍照发图 msg)1206             {1207                 return WeChatResponse.ResponseText(request, "OnPushMenu拍照或者相册发图");1208             }1209 1210             public override void OnPush模板消息发送结果(WeChatRequest request, WeChatRequest.WeChatPush模板消息发送结果 msg)1211             {1212                 throw new NotImplementedException("OnPush模板消息发送结果");1213             }1214             public override void OnPush群发结果(WeChatRequest request, WeChatRequest.WeChatPush群发结果 msg)1215             {1216                 throw new NotImplementedException("OnPush群发结果");1217             }1218         }1219 1220     }1221 }
关于微信公众平台的核心库SDK介绍就分享到这里了,希望以上内容可以对大家有一定的参考价值,可以学以致用。如果喜欢本篇文章,不妨把它分享出去让更多的人看到。
0