wx-tools 菜單欄相關(guān)API

2023-03-22 17:19 更新

包括如下接口:
- createMenu - 創(chuàng)建菜單欄
- deleteMenu - 刪除菜單欄
- getMenu - 獲取菜單欄
- getMenuCurInfo - 獲取當(dāng)前菜單欄配置信息

3.1.1 createMenu 創(chuàng)建菜單欄

        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("網(wǎng)絡(luò)紅歌");
        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);


        //將三個(gè)按鈕設(shè)置進(jìn)btnList
        btnList.add(btn1);
        btnList.add(btn2);
        btnList.add(btn3);
        //設(shè)置進(jìn)菜單類
        menu.setButton(btnList);
        //調(diào)用API即可
        try {
            //參數(shù)1--menu  ,參數(shù)2--是否是個(gè)性化定制。如果是個(gè)性化菜單欄,需要設(shè)置MenuRule
            iService.createMenu(menu, false);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }

  • 如果想創(chuàng)建個(gè)性化菜單欄, 寫法如下:

        //初始化Menu同上

        
        //設(shè)置MatchRule, 例如廣東珠海的同學(xué)能看到這個(gè)菜單欄
        //個(gè)性化菜單欄
        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();
}

  • 如果想刪除個(gè)性菜單欄,寫法如下:
    //如果是刪除個(gè)性化菜單欄,需要傳入MenuID. MenuID是創(chuàng)建個(gè)性菜單欄時(shí)返回的.
    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 獲取現(xiàn)有菜單欄配置

try {
    WxCurMenuInfoResult result = iService.getMenuCurInfo();
    System.out.println(result.toString());
} catch (WxErrorException e) {
    e.printStackTrace();
}
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)