4.1)菜单栏相关API
包括如下接口:
- createMenu - 创建菜单栏
- deleteMenu - 删除菜单栏
- getMenu - 获取菜单栏
- getMenuCurInfo - 获取当前菜单栏配置信息
1. createMenu 创建菜单栏
WxMenu menu = new WxMenu();
List<WxMenuButton> btnList = new ArrayList<>();
//设置CLICK类型的按钮1
WxMenuButton btn1 = new WxMenuButton();
btn1.setType(WxConsts.BUTTON_CLICK);
btn1.setKey("btn1_key");
btn1.setName("CLICK按钮1");
//设置VIEW类型的按钮2
WxMenuButton btn2 = new WxMenuButton();
btn2.setType(WxConsts.BUTTON_VIEW);
btn2.setUrl("http://www.baidu.com");
btn2.setName("VIEW按钮2");
//设置含有子按钮的按钮3
List<WxMenuButton> subList = new ArrayList<>();
//子按钮
WxMenuButton btn3_1 = new WxMenuButton();
btnbtn3_1 .setType(WxConsts.BUTTON_VIEW);
btnbtn3_1 .setUrl("http://www.baidu.com");
btnbtn3_1 .setName("子按钮3_1");
WxMenuButton btn3_2 = new WxMenuButton();
btn3_2.setType(WxConsts.BUTTON_VIEW);
btn3_2.setUrl("http://www.baidu.com");
btn3_2.setName("子按钮3_2");
subList.add(btn3_1);
subList.add(btn3_2);
//把子按钮列表设置进按钮3
WxMenuButton btn3 = new WxMenuButton();
btn3.setName("子按钮3");
btn3.setSub_button(subList);
//将三个按钮设置进btnList
btnList.add(btn1);
btnList.add(btn2);
btnList.add(btn3);
//设置进菜单类
menu.setButton(btnList);
//调用API即可
try {
//参数1--menu ,参数2--是否是个性化定制。如果是个性化菜单栏,需要设置MenuRule
iService.createMenu(menu, false);
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//个性化菜单栏
WxMenuRule rule = new WxMenuRule();
rule.setProvince("广东");
menu.setMatchrule(rule);
try {
//参数1--menu ,参数2--是否是个性化定制。如果是个性化菜单栏,需要设置MenuRule
iService.createMenu(menu, true);
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2. deleteMenu 删除菜单栏
try {
iService.deleteMenu();
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//如果是删除个性化菜单栏,需要传入MenuID
try {
iService.deleteMenu("MenuID");
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
3. getMenu 获取菜单栏
try {
WxMenuResult result = iService.getMenu();
System.out.println(result.toString());
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
4. getMenuCurInfo 获取现有菜单栏配置
try {
WxCurMenuInfoResult result = iService.getMenuCurInfo();
System.out.println(result.toString());
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}