一日一技:微信開(kāi)發(fā)自定義菜單
概述
自定義菜單能夠幫助公眾號(hào)豐富界面,讓用戶(hù)更好更快地理解公眾號(hào)的功能。
文檔定義
接口調(diào)用請(qǐng)求說(shuō)明
http請(qǐng)求方式:POST(請(qǐng)使用https協(xié)議) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
click和view的請(qǐng)求示例
- {
- "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"
- }]
- }]
- }
實(shí)現(xiàn)方式
1、定義菜單json文檔
- {
- "button": [
- {
- "name": "運(yùn)營(yíng)提升",
- "sub_button": [
- {
- "type": "view",
- "name": "免費(fèi)試用",
- "url": "https://acc.bqool.cn/freetrial"
- },
- {
- "type": "view",
- "name": "新 AI調(diào)價(jià)",
- "url": "https://www.bqool.cn/products/repricing-central/"
- },
- {
- "type": "view",
- "name": "熱 賣(mài)家酷",
- "url": "https://www.bqool.cn/bigcentral/"
- },
- {
- "type": "view",
- "name": "熱 選品酷",
- "url": "https://www.bqool.cn/products/chrome-extension/"
- },
- {
- "type": "view",
- "name": "更多產(chǎn)品..",
- "url": "https://www.bqool.cn/"
- }
- ]
- },
- {
- "name": "酷仔部落",
- "sub_button": [
- {
- "type": "view",
- "name": "限時(shí)福利",
- "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": "教學(xué)視頻",
- "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": "權(quán)益中心",
- "sub_button": [
- {
- "type": "view",
- "name": "簽到抽獎(jiǎng)",
- "url": "http://u135069.s.24hwpro.com/web/game/game_id/1220189859"
- },
- {
- "type": "view",
- "name": "商務(wù)合作",
- "url": "https://h5.eqxiul.com/ls/EyOfmUXk"
- },
- {
- "type": "view",
- "name": "電商服務(wù)",
- "url": "https://www.bqool.cn/seller-services/"
- },
- {
- "type": "view",
- "name": "聯(lián)系酷仔",
- "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提交數(shù)據(jù)到微信服務(wù)器
- 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);
- }
備注:一般情況下,不管是創(chuàng)建或者修改菜單,使用 "cgi-bin/menu/create這個(gè)接口即可。