微信公众平台C#开发系列(七):创建自定义菜单

微信公众平台开发
262
补充展位
Pages_Weblog_Get#bd30f646-d665-4673-a3f6-ad3c00fd0b00
文章摘要
此内容由人工摘要内容,并由AI根据文章内容进行润色
暂无内容

c#调用接口为微信公众号自定义设置菜单

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

接口文档地址:

https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html

接着上一篇:微信公众平台C#开发系列(六):网络检测

部分使用类方法详情 请参考系列四 ,本文不再重复赘述

根据文档我们先定义一个CreatingCustomDefinedMenuResponse类继承 ErrorMessage 用于解析接口结果

ErrorMessage 公共类参考 : 微信公众平台C#开发系列(四):获取access token凭证

namespace YuanTK.WeiXin.Wrapper
{
    /// <summary>
    /// 自定义菜单创建接口返回值
    /// </summary>
    public class CreatingCustomDefinedMenuResponse : ErrorMessage
    {
    }
}

定义一个CreatingCustomDefinedMenu方法类继承WeiXinRequest 和接口IWeiXin 和CreatingCustomDefinedMenuRequest 请求参数类


namespace YuanTK.WeiXin
{

    /// <summary>
    /// 一级菜单按钮
    /// </summary>
    public class Button
    {
        /// <summary>
        /// 菜单的响应动作类型,view表示网页类型,click表示点击类型,miniprogram表示小程序类型
        /// </summary>
        public string type { get; set; }
        /// <summary>
        /// 菜单标题,不超过16个字节,子菜单不超过60个字节
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 菜单KEY值,用于消息接口推送,不超过128字节
        /// click等点击类型必须
        /// </summary>
        public string key { get; set; }
        /// <summary>
        /// 二级菜单数组,个数应为1~5个
        /// </summary>
        public List<Sub_Button> sub_button { get; set; }
    }

    /// <summary>
    /// 二级菜单按钮
    /// </summary>
    public class Sub_Button
    {
        /// <summary>
        /// 菜单的响应动作类型,view表示网页类型,click表示点击类型,miniprogram表示小程序类型
        /// </summary>
        public string type { get; set; }
        /// <summary>
        /// 菜单标题,不超过16个字节,子菜单不超过60个字节
        /// </summary>
        public string name { get; set; }

        /// <summary>
        /// 网页 链接,用户点击菜单可打开链接,不超过1024字节。 type为miniprogram时,不支持小程序的老版本客户端将打开本url。
        /// </summary>
        public string url { get; set; }

        /// <summary>
        /// miniprogram类型必须	小程序的appid(仅认证公众号可配置)
        /// </summary>
        public string appid { get; set; }

        /// <summary>
        /// miniprogram类型必须	小程序的页面路径
        /// </summary>
        public string pagepath { get; set; }

        /// <summary>
        /// 菜单KEY值,用于消息接口推送,不超过128字节
        /// click等点击类型必须
        /// </summary>
        public string key { get; set; }
    }

    /// <summary>
    /// 创建自定义菜单请求参数
    /// </summary>
    public class CreatingCustomDefinedMenuRequest
    {
        /// <summary>
        /// 一级菜单数组,个数应为1 ~3个
        /// </summary>
        public List<Button> button { get; set; }
    }

    /// <summary>
    /// 创建自定义菜单
    /// </summary>
    public class CreatingCustomDefinedMenu : WeiXinRequest<CreatingCustomDefinedMenuResponse>, IWeiXin<CreatingCustomDefinedMenuResponse>
    {
        String api;
        CreatingCustomDefinedMenuRequest _RequestData;

        public CreatingCustomDefinedMenu(CreatingCustomDefinedMenuRequest RequestData, string access_token)
        {
            this.api = $"https://api.weixin.qq.com/cgi-bin/menu/create?access_token={access_token}";
            this._RequestData = RequestData;
        }

        /// <summary>
        /// 调用接口获取结果
        /// </summary>
        /// <returns></returns>
        public CreatingCustomDefinedMenuResponse GetResponse()
        {
            if (this._RequestData is null)
            {
                throw new Exception("请求参数不能为空");
            }
            var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
            string poststr = Newtonsoft.Json.JsonConvert.SerializeObject(this._RequestData, Formatting.Indented, jSetting);
            return this.Post(api, poststr);
        }
    }
}

调用代码例子

     string access_token = token.access_token;

            CreatingCustomDefinedMenuRequest request = new CreatingCustomDefinedMenuRequest();

            //创建第一个一级菜单
            Button bt1 = new Button()
            {
                type = "click",
                key = "V1001_TODAY_MUSIC",
                name = "今日歌曲"
            };

            //创建第二个一级菜单
            Button bt2 = new Button()
            {
                name = "菜单",
                sub_button = new List<Sub_Button>()
                {
                       new Sub_Button()
                       {
                            type="view",
                            name="搜索",
                            url="http://www.yuantk.com/"
                       }
                       //,new Sub_Button()
                       //{
                       //    type="miniprogram",
                       //    name="wxa",
                       //    url="http://mp.weixin.qq.com/",
                       //    appid="wx286b93c14bbf93aa",
                       //    pagepath="pages/lunar/index"
                       //}
                }
            };

            //创建第三个一级菜单
            Button bt3 = new Button()
            {
                type = "click",
                key = "V1001_GOOD",
                name = "赞一下我们"
            };

            request.button = new List<Button>() { bt1, bt2, bt3 };

            IWeiXin<CreatingCustomDefinedMenuResponse> api = new CreatingCustomDefinedMenu(request, access_token);
            CreatingCustomDefinedMenuResponse response = api.GetResponse();

调用成功返回的结果(因为没有小程序权限所以小程序菜单就注释了)

c#调用接口为微信公众号自定义设置菜单

补充展位 Pages_Weblog_Get#0
补充展位 Pages_Weblog_Get#1