我是靠谱客的博主 疯狂自行车,这篇文章主要介绍.Net开发微信公众平台之自定义菜单代码详解,现在分享给大家,希望可以做个参考。

这篇文章主要为大家详细解析了微信公众平台开发之自定义菜单.Net代码,感兴趣的小伙伴们可以参考一下

用户自定义菜单制作时,需要用到access_token,我们直接使用前面讲解的IsExistAccess_Token()函数。我理解的微信公共平台里面菜单分为button和sub_button,即菜单和子菜单,这些菜单都有一个name的属性,类别分为click和view,click类有key属性;而view类有url属性,含有子菜单的菜单没有key属性也没有url属性。这些情况可以从下面的例子看出来。


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public void MyMenu() { string weixin1 = ""; weixin1 = @" { ""button"":[ { ""type"":""click"", ""name"":""你好!"", ""key"":""Hello"" }, { ""type"":""view"", ""name"":""公司简介"", ""url"":""http://www.4ugood.net"" }, { ""name"":""产品介绍"", ""sub_button"":[ { ""type"":""click"", ""name"":""产品1"", ""key"":""P1"" }, { ""type"":""click"", ""name"":""产品2"", ""key"":""P2"" }] }] } "; string access_token = IsExistAccess_Token(); string i = GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token, weixin1); Response.Write(i); }
登录后复制

在你页面的 Page_Load 函数中调用这个MyMenu(),就可以显示出来了。
既然显示出来了,菜单的时间如何出发呢?我们已经了解到了如果类型为view的话,他有url属性,这个不需要处理,点击后会直接跳转到你设定的url的页面,下面我来看看如何触发click吧,按照微信的文档可以用(!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "CLICK")来判断,我把之前的代码改造一下,同时把在GetWxMessage()方法中把EventKey的值附上,wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText;


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
protected void Page_Load(object sender, EventArgs e) { MyMenu(); wxmessage wx = GetWxMessage(); string res = ""; if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "subscribe") { string content = ""; content = "/:rose欢迎北京永杰友信科技有限公司/:rosen直接回复“你好”"; res = sendTextMessage(wx, content); } else if (!string.IsNullOrEmpty(wx.EventName) && wx.EventName.Trim() == "CLICK") { if(wx.EventKey=="Hello") res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!"); if(wx.EventKey=="P1") res = sendTextMessage(wx, "你好,点击了产品1"); if(wx.EventKey=="P2") res = sendTextMessage(wx, "你好,点击了产品2"); } else { if (wx.MsgType == "text" && wx.Content == "你好") { res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!"); } else if (wx.MsgType == "voice") { res = sendTextMessage(wx, wx.Recognition); } else { res = sendTextMessage(wx, "你好,未能识别消息!"); } } Response.Write(res); } private wxmessage GetWxMessage() { wxmessage wx = new wxmessage(); StreamReader str = new StreamReader(Request.InputStream, System.Text.Encoding.UTF8); XmlDocument xml = new XmlDocument(); xml.Load(str); wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText; wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText; wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText; if (wx.MsgType.Trim() == "text") { wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText; } if (wx.MsgType.Trim() == "event") { wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText; wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText; } if (wx.MsgType.Trim() == "voice") { wx.Recognition = xml.SelectSingleNode("xml").SelectSingleNode("Recognition").InnerText; } return wx; } /// <summary> /// 发送文字消息 /// </summary> /// <param name="wx">获取的收发者信息</param> /// <param name="content">内容</param> /// <returns></returns> private string sendTextMessage(wxmessage wx, string content) { string res = string.Format(@"<xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[{3}]]></Content> </xml> ", wx.FromUserName, wx.ToUserName, DateTime.Now, content); return res; }
登录后复制

这样就可以相应你的菜单事件了,我上面的代码写的有很多可以优化的地方,这里主要以简介为主,以后我们会逐渐搭建起一个微信公共平台的.net框架,什么菜单类,消息类等等。

以上就是.Net开发微信公众平台之自定义菜单代码详解的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是疯狂自行车最近收集整理的关于.Net开发微信公众平台之自定义菜单代码详解的全部内容,更多相关内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(108)

评论列表共有 0 条评论

立即
投稿
返回
顶部