wx-tools 菜单栏相关API

2023-03-22 17:19 更新

包括如下接口:
- createMenu - 创建菜单栏
- deleteMenu - 删除菜单栏
- getMenu - 获取菜单栏
- getMenuCurInfo - 获取当前菜单栏配置信息

3.1.1 createMenu 创建菜单栏

        IService iService = new WxService();
        WxMenu menu = new WxMenu();
        List<WxMenu.WxMenuButton> btnList = new ArrayList<>();


        //飙升功能
        WxMenu.WxMenuButton btn1 = new WxMenu.WxMenuButton();
        btn1.setName("分类");
        List<WxMenu.WxMenuButton> subList = new ArrayList<>();
        WxMenu.WxMenuButton btn1_1 = new WxMenu.WxMenuButton();
        btn1_1.setType(WxConsts.MENU_BUTTON_CLICK);
        btn1_1.setKey(MenuKey.HOT_SONG);
        btn1_1.setName("飙升榜");
        WxMenu.WxMenuButton btn1_2 = new WxMenu.WxMenuButton();
        btn1_2.setType(WxConsts.MENU_BUTTON_CLICK);
        btn1_2.setKey(MenuKey.TOP_500);
        btn1_2.setName("TOP500");
        WxMenu.WxMenuButton btn1_3 = new WxMenu.WxMenuButton();
        btn1_3.setType(WxConsts.MENU_BUTTON_CLICK);
        btn1_3.setKey(MenuKey.NET_HOT_SONG);
        btn1_3.setName("网络红歌");
        WxMenu.WxMenuButton btn1_4 = new WxMenu.WxMenuButton();
        btn1_4.setType(WxConsts.MENU_BUTTON_CLICK);
        btn1_4.setKey(MenuKey.HUAYU_SONG);
        btn1_4.setName("华语新歌");
        WxMenu.WxMenuButton btn1_5 = new WxMenu.WxMenuButton();
        btn1_5.setType(WxConsts.MENU_BUTTON_CLICK);
        btn1_5.setKey(MenuKey.XINAO_SONG);
        btn1_5.setName("洗脑神曲");


        WxMenu.WxMenuButton btn2 = new WxMenu.WxMenuButton();
        btn2.setType(WxConsts.MENU_BUTTON_CLICK);
        btn2.setKey(MenuKey.CHANGE_NEWS);
        btn2.setName("换一组");


        WxMenu.WxMenuButton btn3 = new WxMenu.WxMenuButton();
        btn3.setType(WxConsts.MENU_BUTTON_CLICK);
        btn3.setKey(MenuKey.HELP);
        btn3.setName("帮助");


        subList.addAll(Arrays.asList(btn1_1, btn1_2, btn1_3, btn1_4, btn1_5));
        btn1.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) {
            e.printStackTrace();
        }

  • 如果想创建个性化菜单栏, 写法如下:

        //初始化Menu同上

        
        //设置MatchRule, 例如广东珠海的同学能看到这个菜单栏
        //个性化菜单栏
        WxMenuRule rule = new WxMenuRule();
        rule.setProvince("广东");
        rule.setCity("珠海");
        menu.setMatchrule(rule);

        
        try {
            iService.createMenu(menu, true);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }


3.1.2 deleteMenu 删除菜单栏

try {
    iService.deleteMenu();
} catch (WxErrorException e) {
    e.printStackTrace();
}

  • 如果想删除个性菜单栏,写法如下:
    //如果是删除个性化菜单栏,需要传入MenuID. MenuID是创建个性菜单栏时返回的.
    try {
    iService.deleteMenu("MenuID");
    } catch (WxErrorException e) {
    e.printStackTrace();
    }


3.1.3 getMenu 获取菜单栏

try {
    WxMenuResult result = iService.getMenu();
    System.out.println(result.toString());
} catch (WxErrorException e) {
    e.printStackTrace();
}

3.1.4 getMenuCurInfo 获取现有菜单栏配置

try {
    WxCurMenuInfoResult result = iService.getMenuCurInfo();
    System.out.println(result.toString());
} catch (WxErrorException e) {
    e.printStackTrace();
}
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号