@page @model CustomMenuCreateModel @{ ViewData["Title"] = "自定义菜单创建"; }

@ViewData["Title"]

@Html.Partial("~/Pages/Partials/CodePartail.cshtml", new SupplierWeb.Pages.Partials.CodePartailModel { Index = 1, IsActive = true, Code = () => { return @" var request = new MenuCreateRequest { AccessToken = AccessToken, Buttons = new List { new ClickButton{ Name = ""今日歌曲"", Key = ""V1001_TODAY_MUSIC"", Type = ClickButtonType.click } } }; var response = _clientExecute(request);"; } }) @Html.Partial("~/Pages/Partials/CodePartail.cshtml", new SupplierWeb.Pages.Partials.CodePartailModel { Index = 2, Code = () => { return @" var request = new MenuCreateRequest { AccessToken = AccessToken, Buttons = new List { new ClickButton{ Name = ""我的博客"", Url = ""http://inday.cnblogs.com"", Type = ClickButtonType.view } } }; var response = _clientExecute(request);"; } }) @Html.Partial("~/Pages/Partials/CodePartail.cshtml", new SupplierWeb.Pages.Partials.CodePartailModel { Index = 3, Code = () => { return @" var request = new MenuCreateRequest { AccessToken = AccessToken, Buttons = new List { new ClickButton{ Name = ""扫码推事件"", Key = ""rselfmenu_0_1"", Type = ClickButtonType.scancode_push } } }; var response = _clientExecute(request);"; } }) @Html.Partial("~/Pages/Partials/CodePartail.cshtml", new SupplierWeb.Pages.Partials.CodePartailModel { Index = 4, Code = () => { return @" var request = new MenuCreateRequest { AccessToken = AccessToken, Buttons = new List { new ClickButton{ Name = ""扫码带提示"", Key = ""rselfmenu_0_0"", Type = ClickButtonType.scancode_waitmsg } } }; var response = _clientExecute(request);"; } }) @Html.Partial("~/Pages/Partials/CodePartail.cshtml", new SupplierWeb.Pages.Partials.CodePartailModel { Index = 5, Code = () => { return @" var request = new MenuCreateRequest { AccessToken = AccessToken, Buttons = new List { new ClickButton{ Name = ""系统拍照发图"", Key = ""rselfmenu_1_0"", Type = ClickButtonType.pic_sysphoto } } }; var response = _clientExecute(request);"; }}) @Html.Partial("~/Pages/Partials/CodePartail.cshtml", new SupplierWeb.Pages.Partials.CodePartailModel { Index = 6, Code = () => { return @" var request = new MenuCreateRequest { AccessToken = AccessToken, Buttons = new List { new ClickButton{ Name = ""拍照或者相册发图"", Key = ""rselfmenu_1_1"", Type = ClickButtonType.pic_photo_or_album } } }; var response = _clientExecute(request);"; } }) @Html.Partial("~/Pages/Partials/CodePartail.cshtml", new SupplierWeb.Pages.Partials.CodePartailModel { Index = 7, Code = () => { return @" var request = new MenuCreateRequest { AccessToken = AccessToken, Buttons = new List { new ClickButton{ Name = ""微信相册发图"", Key = ""rselfmenu_1_2"", Type = ClickButtonType.pic_weixin } } }; var response = _clientExecute(request);"; } }) @Html.Partial("~/Pages/Partials/CodePartail.cshtml", new SupplierWeb.Pages.Partials.CodePartailModel { Index = 8, Code = () => { return @" var request = new MenuCreateRequest { AccessToken = AccessToken, Buttons = new List { new ClickButton{ Name = ""发送位置"", Key = ""rselfmenu_2_0"", Type = ClickButtonType.location_select } } }; var response = _clientExecute(request);"; } }) @Html.Partial("~/Pages/Partials/CodePartail.cshtml", new SupplierWeb.Pages.Partials.CodePartailModel { Index = 9, Code = () => { return @" var request = new MenuCreateRequest { AccessToken = AccessToken, Buttons = new List { new ClickButton{ Name = ""图片"", Media_Id = ""MEDIA_ID1"", Type = ClickButtonType.media_id } } }; var response = _clientExecute(request);"; } }) @Html.Partial("~/Pages/Partials/CodePartail.cshtml", new SupplierWeb.Pages.Partials.CodePartailModel { Index = 10, Code = () => { return @" var request = new MenuCreateRequest { AccessToken = AccessToken, Buttons = new List { new ClickButton{ Name = ""图文消息"", Media_Id = ""MEDIA_ID2"", Type = ClickButtonType.view_limited } } }; var response = _clientExecute(request);"; } }) @Html.Partial("~/Pages/Partials/CodePartail.cshtml", new SupplierWeb.Pages.Partials.CodePartailModel { Index = 11, Code = () => { return @" var request = new MenuCreateRequest { AccessToken = AccessToken, Buttons = new List { new ClickButton{ Name = ""小程序"", Url = ""http://mp.weixin.qq.com"", MiniProgramAppId = ""wx286b93c14bbf93aa"", MiniProgramPagePath = ""pages/lunar/index"", Type = ClickButtonType.miniprogram } } }; var response = _clientExecute(request);"; } }) @Html.Partial("~/Pages/Partials/CodePartail.cshtml", new SupplierWeb.Pages.Partials.CodePartailModel { Index = 12, ShowTestButtons = true, Api = "/api/menucreate", Code = () => { return @" var request = new MenuCreateRequest { AccessToken = AccessToken, Buttons = new List { new ClickButton { Type = ClickButtonType.click, Name = ""关注我"", Key = ""Key_Follow_Me"" }, new ClickButton { Type = ClickButtonType.view, Url = ""http://inday.cnblogs.com"", Name = ""我的博客"" }, new ClickButton { Name=""主菜单"", SubButton = new List { new ClickButton { Type = ClickButtonType.scancode_push, Name = ""扫一扫"", Key = ""Event_Scan"" }, new ClickButton { Type = ClickButtonType.pic_sysphoto, Name = ""系统拍照发图"", Key = ""Event_SysPhoto"" }, new ClickButton { Type = ClickButtonType.pic_weixin, Name = ""微信相册发图"", Key = ""Event_Pic_WeiXin"" }, new ClickButton { Type = ClickButtonType.location_select, Name = ""发送位置"", Key = ""Event_Location_Select"" }, new ClickButton { Type = ClickButtonType.miniprogram, Name = ""我的小程序"", Url =""http://inday.cnblogs.com"", MiniProgramAppId = ""wx286b93c14bbf93aa"", MiniProgramPagePath = ""pages/lunar/index"" } } } } }; var response = _clientExecute(request);"; } })
注意

1、自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。

2、一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替。

3、创建自定义菜单后,菜单的刷新策略是,在用户进入公众号会话页或公众号profile页时,如果发现上一次拉取菜单的请求在5分钟以前,就会拉取一下菜单,如果菜单有更新,就会刷新客户端的菜单。测试时可以尝试取消关注公众账号后再次关注,则可以看到创建后的效果。