Maison > php教程 > php手册 > le corps du texte

《C#微信开发系列(2)-自定义菜单管理》

WBOY
Libérer: 2016-07-06 13:30:35
original
1038 Les gens l'ont consulté

2.0自定义菜单管理 ①接口说明: 微信服务号聊天窗口下面的菜单项(有的公众号有启用有的则没有),这个可以在编辑模式简单配置,也可以在开发模式代码配置。微信公众平台开发者文档:微信公众号开发平台创建自定义菜单,可以看到创建菜单的一些注意事项,下

2.0自定义菜单管理

 

①接口说明:

微信服务号聊天窗口下面的菜单项(有的公众号有启用有的则没有),这个可以在编辑模式简单配置,也可以在开发模式代码配置。微信公众平台开发者文档:微信公众号开发平台创建自定义菜单,可以看到创建菜单的一些注意事项,下面的使用网页调试工具调试该接口,只是调试接口是否可以正常调用,并不是直接创建菜单,根据微信自定义菜单管理文档菜单事件可分为两种:

click: 用户点击click类型按钮后,微信服务器会通过消息接口推送消息类型为event 的结构给开发者(参考消息接口指南),并且带上按钮中开发者填写的key值,开发者可以通过自定义的key值与用户进行交互。

view: 用户点击view类型按钮后,微信客户端将会打开开发者在按钮中填写的url值 (即网页链接),达到打开网页的目的,建议与网页授权获取用户基本信息接口结合,获得用户的登入个人信息。

设置菜单JSON格式(可根据需要进行修改):

<span style="color: #000000;">{
     </span><span style="color: #800000;">"</span><span style="color: #800000;">button</span><span style="color: #800000;">"</span><span style="color: #000000;">:[
     {    
          </span><span style="color: #800000;">"</span><span style="color: #800000;">type</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">click</span><span style="color: #800000;">"</span><span style="color: #000000;">,
          </span><span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">今日歌曲</span><span style="color: #800000;">"</span><span style="color: #000000;">,
          </span><span style="color: #800000;">"</span><span style="color: #800000;">key</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">V1001_TODAY_MUSIC</span><span style="color: #800000;">"</span><span style="color: #000000;">
      },
      {
           </span><span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">菜单</span><span style="color: #800000;">"</span><span style="color: #000000;">,
           </span><span style="color: #800000;">"</span><span style="color: #800000;">sub_button</span><span style="color: #800000;">"</span><span style="color: #000000;">:[
           {    
               </span><span style="color: #800000;">"</span><span style="color: #800000;">type</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">view</span><span style="color: #800000;">"</span><span style="color: #000000;">,
               </span><span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">搜索</span><span style="color: #800000;">"</span><span style="color: #000000;">,
               </span><span style="color: #800000;">"</span><span style="color: #800000;">url</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">http://www.soso.com/</span><span style="color: #800000;">"</span><span style="color: #000000;">
            },
            {
               </span><span style="color: #800000;">"</span><span style="color: #800000;">type</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">view</span><span style="color: #800000;">"</span><span style="color: #000000;">,
               </span><span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">视频</span><span style="color: #800000;">"</span><span style="color: #000000;">,
               </span><span style="color: #800000;">"</span><span style="color: #800000;">url</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">http://v.QQ.com/</span><span style="color: #800000;">"</span><span style="color: #000000;">
            },
            {
               </span><span style="color: #800000;">"</span><span style="color: #800000;">type</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">click</span><span style="color: #800000;">"</span><span style="color: #000000;">,
               </span><span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">赞一下我们</span><span style="color: #800000;">"</span><span style="color: #000000;">,
               </span><span style="color: #800000;">"</span><span style="color: #800000;">key</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">V1001_GOOD</span><span style="color: #800000;">"</span><span style="color: #000000;">
            }]
       }]
 }</span>
Copier après la connexion

配置菜单的注意点:

1、自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。

2、一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替。

3、创建自定义菜单后,由于微信客户端缓存,需要24小时微信客户端才会展现出来。测试时可以尝试取消关注公众账号后再次关注,则可以看到创建后的效果。

 接口参数配置:

 

②请求接口:

 

接口调用请求说明:

http请求方式:POST(请使用https协议)

https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN

 

POST请求接口调用接口凭据ACCESS_TOKEN的获取方式可参见文档(获取ACCESS_TOKEN文档)使用POST方式带上ACCESS_TOKEN以及菜单JSON请求接口,具体实现代码如下:

 

<span style="color: #008080;"> 1</span> <span style="color: #808080;">///</span> <span style="color: #808080;"><summary></summary></span>
<span style="color: #008080;"> 2</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 创建菜单事件
</span><span style="color: #008080;"> 3</span> <span style="color: #808080;">///</span> <span style="color: #808080;"></span>
<span style="color: #008080;"> 4</span> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="sender"></span>
<span style="color: #008080;"> 5</span> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="e"></span>
<span style="color: #008080;"> 6</span> <span style="color: #0000ff;">PRotected</span> <span style="color: #0000ff;">void</span> creayeMenu_Click(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, EventArgs e)
</span><span style="color: #008080;"> 7</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 8    </span>FileStream fs1 = <span style="color: #0000ff;">new</span> FileStream(Server.MapPath(<span style="color: #800000;">"</span><span style="color: #800000;">~/</span><span style="color: #800000;">"</span>) + <span style="color: #800000;">"</span><span style="color: #800000;">\\menuInfo.txt</span><span style="color: #800000;">"</span><span style="color: #000000;">, FileMode.Open);
</span><span style="color: #008080;"> 9    </span>StreamReader sr = <span style="color: #0000ff;">new</span> StreamReader(fs1, Encoding.GetEncoding(<span style="color: #800000;">"</span><span style="color: #800000;">GBK</span><span style="color: #800000;">"</span><span style="color: #000000;">));
</span><span style="color: #008080;">10    </span><span style="color: #0000ff;">string</span> menu =<span style="color: #000000;"> sr.ReadToEnd();
</span><span style="color: #008080;">11    </span><span style="color: #000000;">sr.Close();
</span><span style="color: #008080;">12    </span><span style="color: #000000;">fs1.Close();
</span><span style="color: #008080;">13    <span style="color: #000000;">CreateMenu</span></span>(<span style="color: #800000;">"</span><span style="color: #800000;">https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN</span><span style="color: #800000;">"</span><span style="color: #000000;">, menu);
</span><span style="color: #008080;">14</span> }
Copier après la connexion

 

<span style="color: #008080;"> 1</span> <span style="color: #808080;">///</span> <span style="color: #808080;"><summary></summary></span>
<span style="color: #008080;"> 2</span> <span style="color: #808080;">///</span><span style="color: #008000;"> 创建公众号菜单
</span><span style="color: #008080;"> 3</span> <span style="color: #808080;">///</span> <span style="color: #808080;"></span>
<span style="color: #008080;"> 4</span> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="posturl"></span><span style="color: #008000;">URL</span><span style="color: #808080;"></span>
<span style="color: #008080;"> 5</span> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="postData"></span><span style="color: #008000;">菜单JSON数据</span><span style="color: #808080;"></span>
<span style="color: #008080;"> 6</span> <span style="color: #808080;">///</span> <span style="color: #808080;"><returns></returns></span>
<span style="color: #008080;"> 7</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> CreateMenu(<span style="color: #0000ff;">string</span> posturl, <span style="color: #0000ff;">string</span><span style="color: #000000;"> postData)
</span><span style="color: #008080;"> 8</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 9    </span>Stream outstream = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
</span><span style="color: #008080;">10    </span>Stream instream = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
</span><span style="color: #008080;">11    </span>StreamReader sr = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
</span><span style="color: #008080;">12    </span>HttpWebResponse response = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
</span><span style="color: #008080;">13    </span>HttpWebRequest request = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
</span><span style="color: #008080;">14    </span>Encoding encoding =<span style="color: #000000;"> Encoding.UTF8;
</span><span style="color: #008080;">15    </span><span style="color: #0000ff;">byte</span>[] data =<span style="color: #000000;"> encoding.GetBytes(postData);
</span><span style="color: #008080;">16</span> 
<span style="color: #008080;">17    </span>request = WebRequest.Create(posturl) <span style="color: #0000ff;">as</span><span style="color: #000000;"> HttpWebRequest;
</span><span style="color: #008080;">18    </span>CookieContainer cookieContainer = <span style="color: #0000ff;">new</span><span style="color: #000000;"> CookieContainer();
</span><span style="color: #008080;">19    </span>request.CookieContainer =<span style="color: #000000;"> cookieContainer;
</span><span style="color: #008080;">20</span>       request.AllowAutoRedirect = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;">21</span>       request.Method = <span style="color: #800000;">"</span><span style="color: #800000;">POST</span><span style="color: #800000;">"</span><span style="color: #000000;">;
</span><span style="color: #008080;">22</span>       request.ContentType = <span style="color: #800000;">"</span><span style="color: #800000;">application/x-www-form-urlencoded</span><span style="color: #800000;">"</span><span style="color: #000000;">;
</span><span style="color: #008080;">23</span>       request.ContentLength =<span style="color: #000000;"> data.Length;
</span><span style="color: #008080;">24</span>       outstream =<span style="color: #000000;"> request.GetRequestStream();
</span><span style="color: #008080;">25</span>       outstream.Write(data, <span style="color: #800080;">0</span><span style="color: #000000;">, data.Length);
</span><span style="color: #008080;">26</span> <span style="color: #000000;">      outstream.Close();
</span><span style="color: #008080;">27</span>       response = request.GetResponse() <span style="color: #0000ff;">as</span><span style="color: #000000;"> HttpWebResponse;
</span><span style="color: #008080;">28</span>       instream =<span style="color: #000000;"> response.GetResponseStream();
</span><span style="color: #008080;">29</span>       sr = <span style="color: #0000ff;">new</span><span style="color: #000000;"> StreamReader(instream, encoding);
</span><span style="color: #008080;">30</span>       <span style="color: #0000ff;">string</span> content =<span style="color: #000000;"> sr.ReadToEnd();
</span><span style="color: #008080;">31</span> <span style="color: #000000;">      Context.Response.Write(content);
</span><span style="color: #008080;">32</span> }
Copier après la connexion

接口返回结果:

正确时返回的JSON数据包格式如下:

{"errcode":0,"errmsg":"ok"}

 

错误时的返回JSON数据包如下(示例为无效菜单名长度):

{"errcode":40018,"errmsg":"invalid button name size"}

 


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal