自定义菜单
最后更新于:2022-04-01 07:05:57
开发微信企业号可以通过程序自定义菜单,只需要调用相关的接口就可以实现。
其实这个菜单也就是微信底部的菜单,目前自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替。其实创建菜单也很简单。
但有一点需要说明,如果有子菜单,那么这个菜单就不会向后端发送事件。例如:我定义了三个一级菜单一个click,两个view。
**如果没有子菜单,则点击click类型的菜单时,后主动向后端发送上报菜单事件,如果有则不会发送上报菜单事件。**
如果没有子菜单,则点击view类型的菜单时,回主动向后端发送点击菜单跳转链接的事件。并且会打开对应的网页,如果有则不会发送点击菜单跳转链接的事件,也不会打开对应的网页。
**也就是,如果有子菜单,则这个菜单,就是点击单纯的显示子菜单,不会有其他的动作了。**
例如:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-14_569757dd818c1.jpg)
核心代码菜单相关:
~~~
public enum MenuTypeEnum
{
click = 1,
view = 2,
scancode_push = 3,
scancode_waitmsg = 4,
pic_sysphoto = 5,
pic_photo_or_album = 6,
pic_weixin = 7,
location_select = 8
};
public abstract class SubButton
{
/// <summary>
/// 菜单的响应动作类型,目前有click、view两种类型 scancode_push 扫码推事件scancode_waitmsg 扫码推事件且弹出“消息接收中”提示框
/// </summary>
public string type { get; protected set; }
/// <summary>
/// 菜单标题,不超过16个字节,子菜单不超过40个字节
/// </summary>
public string name { get; set; }
public List<SubButton> sub_button { get; set; }
public virtual bool HasError()
{
if (string.IsNullOrEmpty(this.name))
{
LogInfo.Error("菜单名称为空");
return true;
}
if (string.IsNullOrEmpty(this.type))
{
LogInfo.Error("菜单类型为空");
return true;
}
if (sub_button!=null&&sub_button.Count > 0)
{
foreach (SubButton bt in sub_button)
{
if (bt.HasError())
{
return true;
}
}
}
return false;
}
public static SubButton CreateSubButton(MenuTypeEnum type,string name,string key,string url)
{
SubButton subButton = null;
string menuTypeText = GetMenuTypeText(type);
switch (type)
{
case MenuTypeEnum.view:
subButton = new SubViewButton(menuTypeText,name, url);
break;
case MenuTypeEnum.click:
case MenuTypeEnum.scancode_push:
case MenuTypeEnum.scancode_waitmsg:
case MenuTypeEnum.pic_sysphoto:
case MenuTypeEnum.pic_photo_or_album:
case MenuTypeEnum.pic_weixin:
case MenuTypeEnum.location_select:
subButton = new SubClickButton(menuTypeText, name, key);
break;
default:
throw new Exception("type=" + type + ",此类型的SubButton没有实现");
}
return subButton;
}
public static MenuTypeEnum GetMenuType(string type)
{
MenuTypeEnum text = MenuTypeEnum.click;
switch (type)
{
case "click":
text = MenuTypeEnum.click;
break;
case "view":
text = MenuTypeEnum.view ;
break;
case "scancode_push":
text =MenuTypeEnum.scancode_push ;
break;
case "scancode_waitmsg":
text = MenuTypeEnum.scancode_waitmsg ;
break;
case "pic_sysphoto" :
text = MenuTypeEnum.pic_sysphoto;
break;
case "pic_photo_or_album":
text = MenuTypeEnum.pic_photo_or_album ;
break;
case "pic_weixin":
text = MenuTypeEnum.pic_weixin ;
break;
case "location_select":
text =MenuTypeEnum.location_select ;
break;
default:
throw new Exception("type=" + type + ",此类型的MenuTypeEnum没有找到");
}
return text;
}
public static string GetMenuTypeText(MenuTypeEnum type)
{
string text = "";
switch (type)
{
case MenuTypeEnum.click:
text = "click";
break;
case MenuTypeEnum.view:
text = "view";
break;
case MenuTypeEnum.scancode_push:
text = "scancode_push";
break;
case MenuTypeEnum.scancode_waitmsg:
text = "scancode_waitmsg";
break;
case MenuTypeEnum.pic_sysphoto:
text = "pic_sysphoto";
break;
case MenuTypeEnum.pic_photo_or_album:
text = "pic_photo_or_album";
break;
case MenuTypeEnum.pic_weixin:
text = "pic_weixin";
break;
case MenuTypeEnum.location_select:
text = "location_select";
break;
default:
throw new Exception("type=" + type + ",此类型的MenuTypeEnum没有实现");
}
return text;
}
}
public class SubClickButton : SubButton
{
public SubClickButton(string type,string name,string key)
{
this.type = type;
this.name = name;
this.key = key;
}
/// <summary>
/// 菜单KEY值,用于消息接口推送,不超过128字节
/// </summary>
public string key { get; set; }
public override bool HasError()
{
if (string.IsNullOrEmpty(this.key))
{
LogInfo.Error("菜单key为空");
return true;
}
return base.HasError();
}
}
public class SubViewButton : SubButton
{
public SubViewButton(string type, string name, string url)
{
this.type = type;
this.name = name;
this.url = url;
}
/// <summary>
/// 成员点击view类型按钮后,微信客户端将会打开开发者在按钮中填写的网页URL,可与网页授权获取成员基本信息接口结合,获得成员基本信息。
/// </summary>
public string url { get; set; }
public override bool HasError()
{
if (string.IsNullOrEmpty(this.url))
{
LogInfo.Error("菜单url为空");
return true;
}
return base.HasError();
}
}
~~~
~~~
/// <summary>
/// 自定义菜单
/// </summary>
public class Menu
{
public List<SubButton> button { get; set; }
public virtual bool HasError()
{
if (button.Count >= 4)
{
LogInfo.Error("最多包括3个一级菜单");
return true;
}
foreach(SubButton bt in button)
{
if (bt.HasError())
{
return true;
}
}
return false;
}
}
~~~
添加修改,删除菜单
~~~
public class BLLMenu
{
/// <summary>
///
/// </summary>
/// <param name="info"></param>
/// <param name="agentid">企业应用的id,整型。可在应用的设置页面查看</param>
/// <returns></returns>
public static bool Create(Menu info, int agentid)
{
if (info.HasError())
{
return false;
}
string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token={0}&agentid={1}";
var url = string.Format(urlFormat, BLLAccessToken.GetAccessToken(), agentid);
WebUtils wut = new WebUtils();
var postData = Tools.ToJsonString<Menu>(info);
//数据不用加密发送
LogInfo.Info("创建应用菜单消息: " + postData);
string sendResult = wut.DoPost(url, postData);
ReturnResult tempAccessTokenjson = Tools.JsonStringToObj<ReturnResult>(sendResult);
if (tempAccessTokenjson.HasError())
{
LogInfo.Error("发送创建应用菜单返回错误: " + Tools.ToJsonString<ReturnResult>(tempAccessTokenjson));
return false;
}
return true;
}
/// <summary>
///
/// </summary>
/// <param name="agentid">企业应用的id,整型。可在应用的设置页面查看</param>
/// <returns></returns>
public static bool DelAll(int agentid)
{
string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/menu/delete?access_token={0}&agentid={1}";
var url = string.Format(urlFormat, BLLAccessToken.GetAccessToken(), agentid);
WebUtils wut = new WebUtils();
//数据不用加密发送
LogInfo.Info("发送删除菜单消息: " + url);
string sendResult = wut.DoGet(url);
ReturnResult tempAccessTokenjson = Tools.JsonStringToObj<ReturnResult>(sendResult);
if (tempAccessTokenjson.HasError())
{
LogInfo.Error("发送删除菜单返回错误: " + Tools.ToJsonString<ReturnResult>(tempAccessTokenjson));
return false;
}
return true;
}
public static bool GetAll(int agentid)
{
string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?access_token={0}&agentid={1}";
var url = string.Format(urlFormat, BLLAccessToken.GetAccessToken(), agentid);
WebUtils wut = new WebUtils();
//数据不用加密发送
LogInfo.Info("发送获取菜单列表消息: " + url);
string sendResult = wut.DoGet(url);
MenuListResult tempAccessTokenjson = Tools.JsonStringToObj<MenuListResult>(sendResult);
if (tempAccessTokenjson.HasError())
{
LogInfo.Error("发送获取菜单列表返回错误: " + Tools.ToJsonString<MenuListResult>(tempAccessTokenjson));
return false;
}
return true;
}
}
~~~
测试代码
~~~
private void button7_Click(object sender, EventArgs e)
{
///测试添加
ConmonWeixin.MenuInfo.Menu info = new ConmonWeixin.MenuInfo.Menu();
SubButton subbt1 = SubButton.CreateSubButton(MenuTypeEnum.click, "Click1", "Click1", "");
subbt1.sub_button = new List<SubButton>();
SubButton bt11 = SubButton.CreateSubButton(MenuTypeEnum.scancode_push, "codePush2", "CancodePushButton11", "");
SubButton bt12 = SubButton.CreateSubButton(MenuTypeEnum.scancode_waitmsg, "codeWaitmsg2", "CancodeWaitmsgButton12", "");
SubButton bt13 = SubButton.CreateSubButton(MenuTypeEnum.click, "Click12", "Click12", "");
SubButton bt14 = SubButton.CreateSubButton(MenuTypeEnum.view, "V2级", "V22", "https://www.baidu.com");
subbt1.sub_button.Add(bt11);
subbt1.sub_button.Add(bt12);
subbt1.sub_button.Add(bt13);
subbt1.sub_button.Add(bt14);
SubButton subbt2 = SubButton.CreateSubButton(MenuTypeEnum.view, "V1级", "", "www.baidu.com");
subbt2.sub_button = new List<SubButton>();
SubButton bt21 = SubButton.CreateSubButton(MenuTypeEnum.pic_sysphoto, "PicSysphoto", "PicSysphotoButton21", "");
SubButton bt22 = SubButton.CreateSubButton(MenuTypeEnum.pic_photo_or_album, "photoalbum2", "PicSysphotoButton22", "");
subbt2.sub_button.Add(bt21);
subbt2.sub_button.Add(bt22);
SubButton subbt3 = SubButton.CreateSubButton(MenuTypeEnum.view, "V1级", "", "http://hlogin.html");
subbt3.sub_button = new List<SubButton>();
SubButton bt31 = SubButton.CreateSubButton(MenuTypeEnum.pic_weixin, "pic_weixin2", "Subpic_weixinButton31", "");
SubButton bt32 = SubButton.CreateSubButton(MenuTypeEnum.location_select, "location_selec", "location_selec32", "");
subbt3.sub_button.Add(bt31);
subbt3.sub_button.Add(bt32);
info.button = new List<SubButton>();
info.button.Add(subbt1);
info.button.Add(subbt2);
info.button.Add(subbt3);
BLLMenu.Create(info,7);
// Menu info, int agentid
}
private void button8_Click(object sender, EventArgs e)
{
//测试删除
BLLMenu.DelAll(7);
}
~~~
[创建应用菜单官方文档](http://qydev.weixin.qq.com/wiki/index.php?title=%E5%88%9B%E5%BB%BA%E5%BA%94%E7%94%A8%E8%8F%9C%E5%8D%95)
[菜单接收事件官方文档](http://qydev.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E6%94%B6%E4%BA%8B%E4%BB%B6)