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

时间:2022-02-05 02:32:02

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

概述

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

文档定义

接口调用请求说明

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这个接口即可。