国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

微信公眾平臺開發之自定義菜單.Net代碼解析

2024-07-10 12:46:58
字體:
來源:轉載
供稿:網友

用戶自定義菜單制作時,需要用到access_token,我們直接使用前面講解的IsExistAccess_Token()函數。我理解的微信公共平臺里面菜單分為button和sub_button,即菜單和子菜單,這些菜單都有一個name的屬性,類別分為click和view,click類有key屬性;而view類有url屬性,含有子菜單的菜單沒有key屬性也沒有url屬性。這些情況可以從下面的例子看出來。

 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;  

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歡迎北京永杰友信科技有限公司/:rose/n直接回復“你好”";    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;  }            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 两当县| 巴林右旗| 那曲县| 泰顺县| 平遥县| 九台市| 安岳县| 舞钢市| 比如县| 丹寨县| 涪陵区| 英吉沙县| 沐川县| 成都市| 尚志市| 开平市| 保德县| 神木县| 靖边县| 柘城县| 广平县| 张掖市| 花莲市| 衡东县| 安阳县| 巩留县| 麻栗坡县| 泗水县| 厦门市| 南汇区| 台北县| 宁波市| 保山市| 新田县| 同仁县| 贞丰县| 土默特右旗| 渭南市| 葫芦岛市| 石门县| 新巴尔虎左旗|