一日一技:微信開發自定義菜單
作者:conan5566
自定義菜單能夠幫助公眾號豐富界面,讓用戶更好更快地理解公眾號的功能。一般情況下,不管是創建或者修改菜單,使用 "cgi-bin/menu/create這個接口即可。
概述
自定義菜單能夠幫助公眾號豐富界面,讓用戶更好更快地理解公眾號的功能。
文檔定義
接口調用請求說明
http請求方式:POST(請使用https協議) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
click和view的請求示例
- {
- "button":[
- {
- "type":"click",
- "name":"今日歌曲",
- "key":"V1001_TODAY_MUSIC"
- },
- {
- "name":"菜單",
- "sub_button":[
- {
- "type":"view",
- "name":"搜索",
- "url":"http://www.soso.com/"
- },
- {
- "type":"miniprogram",
- "name":"wxa",
- "url":"http://mp.weixin.qq.com",
- "appid":"wx286b93c14bbf93aa",
- "pagepath":"pages/lunar/index"
- },
- {
- "type":"click",
- "name":"贊一下我們",
- "key":"V1001_GOOD"
- }]
- }]
- }
實現方式
1、定義菜單json文檔
- {
- "button": [
- {
- "name": "運營提升",
- "sub_button": [
- {
- "type": "view",
- "name": "免費試用",
- "url": "https://acc.bqool.cn/freetrial"
- },
- {
- "type": "view",
- "name": "新 AI調價",
- "url": "https://www.bqool.cn/products/repricing-central/"
- },
- {
- "type": "view",
- "name": "熱 賣家酷",
- "url": "https://www.bqool.cn/bigcentral/"
- },
- {
- "type": "view",
- "name": "熱 選品酷",
- "url": "https://www.bqool.cn/products/chrome-extension/"
- },
- {
- "type": "view",
- "name": "更多產品..",
- "url": "https://www.bqool.cn/"
- }
- ]
- },
- {
- "name": "酷仔部落",
- "sub_button": [
- {
- "type": "view",
- "name": "限時福利",
- "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"
- },
- {
- "type": "view",
- "name": "成功案例",
- "url": "https://www.bqool.cn/user-stories/"
- },
- {
- "type": "view",
- "name": "教學視頻",
- "url": "https://www.bqool.cn/video-tutorial/"
- },
- {
- "type": "view",
- "name": "部落文章",
- "url": "https://mp.weixin.qq.com/mp/homepage?__biz=MzIzMjc1NTY3MQ==&hid=1&sn=db4536a338f0e128ce2e5db089f381e1&scene=18"
- }
- ]
- },
- {
- "name": "權益中心",
- "sub_button": [
- {
- "type": "view",
- "name": "簽到抽獎",
- "url": "http://u135069.s.24hwpro.com/web/game/game_id/1220189859"
- },
- {
- "type": "view",
- "name": "商務合作",
- "url": "https://h5.eqxiul.com/ls/EyOfmUXk"
- },
- {
- "type": "view",
- "name": "電商服務",
- "url": "https://www.bqool.cn/seller-services/"
- },
- {
- "type": "view",
- "name": "聯系酷仔",
- "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"
- }
- ]
- }
- ]
- }
2、讀取json文件
- /// <summary>
- /// 自定義菜單
- /// </summary>
- /// <param name="OPENID"></param>
- /// <param name="template_id"></param>
- /// <returns></returns>
- [BQoolException]
- [HttpPost]
- public ApiResult CustomMenus(string CustomMenusPwd)
- {
- if (!string.IsNullOrWhiteSpace(CustomMenusPwd) && CustomMenusPwd.Trim() == MvcTools.GetAppSetting("WeixinCustomMenus"))
- {
- CustomMenusParam param = new CustomMenusParam() { Account = "sys", UpdateUser = "sys" };
- param.Jsonstr = WeiXinHelper.ReadAccess(HttpRuntime.AppDomainAppPath.ToString() + "/App_Data/WeChat/Custom_Menus.json");
- ApiResult result = _weChatAlertsService.CustomMenus(param, AppId, Appsecret);
- return result;
- }
- else
- {
- return new ApiResult() { Success = false, Code = ApiResultCode.InvalidError, ErrorMessage = ApiResultCode.ErrorMessages[ApiResultCode.InvalidError] };
- }
- }
3、post提交數據到微信服務器
- private static Tuple<WxBaseInfo, string, string> CustomMenus(string AppId, string Appsecret, string jsonstr, bool isResh)
- {
- var accessToken = TryGetAccessToken(AppId, Appsecret, isResh);
- string url = APIConfig.Custom_Menus(accessToken);
- string str = APIConfig.CreatePostHttpResponse(url, jsonstr);
- _logger.Debug("自定義菜單\r\n" + "輸入:" + url + "\r\n" + jsonstr + "\r\n輸出:" + str);
- return new Tuple<WxBaseInfo, string, string>(JsonConvert.DeserializeObject<WxBaseInfo>(str), url, jsonstr);
- }
- public static string WEIXIN_URL = "https://api.weixin.qq.com/";
- public static string Custom_Menus(string token)
- {
- return String.Format(WEIXIN_URL + "cgi-bin/menu/create?access_token={0}", token);
- }
備注:一般情況下,不管是創建或者修改菜單,使用 "cgi-bin/menu/create這個接口即可。
責任編輯:武曉燕
來源:
UP技術控