一日一技:微信开发自定义菜单

开发 前端
自定义菜单能够帮助公众号丰富界面,让用户更好更快地理解公众号的功能。一般情况下,不管是创建或者修改菜单,使用 "cgi-bin/menu/create这个接口即可。

 [[385026]]

概述

自定义菜单能够帮助公众号丰富界面,让用户更好更快地理解公众号的功能。

文档定义

接口调用请求说明

http请求方式:POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN

click和view的请求示例

  1.      "button":[ 
  2.      {   
  3.           "type":"click"
  4.           "name":"今日歌曲"
  5.           "key":"V1001_TODAY_MUSIC" 
  6.       }, 
  7.       { 
  8.            "name":"菜单"
  9.            "sub_button":[ 
  10.            {   
  11.                "type":"view"
  12.                "name":"搜索"
  13.                "url":"http://www.soso.com/" 
  14.             }, 
  15.             { 
  16.                  "type":"miniprogram"
  17.                  "name":"wxa"
  18.                  "url":"http://mp.weixin.qq.com"
  19.                  "appid":"wx286b93c14bbf93aa"
  20.                  "pagepath":"pages/lunar/index" 
  21.              }, 
  22.             { 
  23.                "type":"click"
  24.                "name":"赞一下我们"
  25.                "key":"V1001_GOOD" 
  26.             }] 
  27.        }] 
  28.  } 

实现方式

1、定义菜单json文档

  1.   "button": [ 
  2.  
  3.     { 
  4.       "name""运营提升"
  5.       "sub_button": [ 
  6.         { 
  7.           "type""view"
  8.           "name""免费试用"
  9.           "url""https://acc.bqool.cn/freetrial" 
  10.         }, 
  11.         { 
  12.           "type""view"
  13.           "name""新 AI调价"
  14.           "url""https://www.bqool.cn/products/repricing-central/" 
  15.         }, 
  16.         { 
  17.           "type""view"
  18.           "name""热 卖家酷"
  19.           "url""https://www.bqool.cn/bigcentral/" 
  20.         }, 
  21.  
  22.         { 
  23.           "type""view"
  24.           "name""热 选品酷"
  25.           "url""https://www.bqool.cn/products/chrome-extension/" 
  26.         }, 
  27.         { 
  28.           "type""view"
  29.           "name""更多产品.."
  30.           "url""https://www.bqool.cn/" 
  31.         } 
  32.       ] 
  33.     }, 
  34.     { 
  35.       "name""酷仔部落"
  36.       "sub_button": [ 
  37.         { 
  38.           "type""view"
  39.           "name""限时福利"
  40.           "url""https://mp.weixin.qq.com/mp/video?__biz=MzIzMjc1NTY3MQ==&mid=100007240&sn=04973c15f924fc78a78fec50cd75d706&vid=wxv_1500216251938308102&idx=1&vidsn=a27a0d1d9503802f9f9095534d4d2507&fromid=1&xtrack=1&scene=0&subscene=10000&clicktime=1600054770&enterid=1600054770#wechat_redirect" 
  41.         }, 
  42.         { 
  43.           "type""view"
  44.           "name""成功案例"
  45.           "url""https://www.bqool.cn/user-stories/" 
  46.         }, 
  47.         { 
  48.           "type""view"
  49.           "name""教学视频"
  50.           "url""https://www.bqool.cn/video-tutorial/" 
  51.         }, 
  52.         { 
  53.           "type""view"
  54.           "name""部落文章"
  55.           "url""https://mp.weixin.qq.com/mp/homepage?__biz=MzIzMjc1NTY3MQ==&hid=1&sn=db4536a338f0e128ce2e5db089f381e1&scene=18" 
  56.         } 
  57.  
  58.       ] 
  59.     }, 
  60.     { 
  61.       "name""权益中心"
  62.       "sub_button": [ 
  63.         { 
  64.           "type""view"
  65.           "name""签到抽奖"
  66.           "url""http://u135069.s.24hwpro.com/web/game/game_id/1220189859" 
  67.         }, 
  68.         { 
  69.           "type""view"
  70.           "name""商务合作"
  71.           "url""https://h5.eqxiul.com/ls/EyOfmUXk" 
  72.         }, 
  73.         { 
  74.           "type""view"
  75.           "name""电商服务"
  76.           "url""https://www.bqool.cn/seller-services/" 
  77.         }, 
  78.         { 
  79.           "type""view"
  80.           "name""联系酷仔"
  81.           "url""https://admin.qidian.qq.com/template/blue/mp/menu/qr-code-jump.html?linkType=0&env=ol&kfuin=2852161981&fid=95&key=71d05b6c5e0a545f0be22ec831f9d7af&cate=1&type=16&ftype=1&_type=wpa&qidian=true" 
  82.         } 
  83.       ] 
  84.     } 
  85.   ] 

2、读取json文件

  1. /// <summary> 
  2.         /// 自定义菜单 
  3.         /// </summary> 
  4.         /// <param name="OPENID"></param> 
  5.         /// <param name="template_id"></param> 
  6.         /// <returns></returns
  7.         [BQoolException] 
  8.         [HttpPost] 
  9.         public ApiResult CustomMenus(string CustomMenusPwd) 
  10.         { 
  11.             if (!string.IsNullOrWhiteSpace(CustomMenusPwd) && CustomMenusPwd.Trim() == MvcTools.GetAppSetting("WeixinCustomMenus")) 
  12.             { 
  13.                 CustomMenusParam param = new CustomMenusParam() { Account = "sys", UpdateUser = "sys" }; 
  14.                 param.Jsonstr = WeiXinHelper.ReadAccess(HttpRuntime.AppDomainAppPath.ToString() + "/App_Data/WeChat/Custom_Menus.json"); 
  15.                 ApiResult result = _weChatAlertsService.CustomMenus(param, AppId, Appsecret); 
  16.                 return result; 
  17.             } 
  18.             else 
  19.             { 
  20.                 return new ApiResult() { Success = false, Code = ApiResultCode.InvalidError, ErrorMessage = ApiResultCode.ErrorMessages[ApiResultCode.InvalidError] }; 
  21.             } 
  22.         } 

3、post提交数据到微信服务器

  1. private static Tuple<WxBaseInfo, string, string> CustomMenus(string AppId, string Appsecret, string jsonstr, bool isResh) 
  2.         { 
  3.             var accessToken = TryGetAccessToken(AppId, Appsecret, isResh); 
  4.             string url = APIConfig.Custom_Menus(accessToken); 
  5.             string str = APIConfig.CreatePostHttpResponse(url, jsonstr); 
  6.             _logger.Debug("自定义菜单\r\n" + "输入:" + url + "\r\n" + jsonstr + "\r\n输出:" + str); 
  7.             return new Tuple<WxBaseInfo, string, string>(JsonConvert.DeserializeObject<WxBaseInfo>(str), url, jsonstr); 
  8.         } 
  1. public static string WEIXIN_URL = "https://api.weixin.qq.com/"
  2.  
  3.         public static string Custom_Menus(string token) 
  4.         { 
  5.             return String.Format(WEIXIN_URL + "cgi-bin/menu/create?access_token={0}", token); 
  6.         } 

备注:一般情况下,不管是创建或者修改菜单,使用 "cgi-bin/menu/create这个接口即可。

 

责任编辑:武晓燕 来源: UP技术控
相关推荐

2021-10-15 21:08:31

PandasExcel对象

2021-04-27 22:15:02

Selenium浏览器爬虫

2021-04-12 21:19:01

PythonMakefile项目

2021-04-05 14:47:55

Python多线程事件监控

2021-03-12 21:19:15

Python链式调用

2021-09-13 20:38:47

Python链式调用

2023-10-28 12:14:35

爬虫JavaScriptObject

2022-03-12 20:38:14

网页Python测试

2022-06-28 09:31:44

LinuxmacOS系统

2021-04-19 23:29:44

MakefilemacOSLinux

2021-10-03 20:08:29

HTTP2Scrapy

2021-05-08 19:33:51

移除字符零宽

2021-07-27 21:32:57

Python 延迟调用

2020-12-11 06:30:00

工具分组DataFrame

2021-02-14 22:22:18

格式图片 HTTP

2024-02-20 22:13:48

Python项目Java

2020-05-19 13:55:38

Python加密密码

2021-11-12 05:00:43

装饰器代码功能

2023-07-07 09:04:21

JSON信息微信

2022-03-07 09:14:04

Selenium鼠标元素
点赞
收藏

51CTO技术栈公众号