RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:24小时服务
你可能遇到了下面的问题
关闭右侧工具栏
微信公众号开发之创建自定义菜单

厦门网优介绍微信公众号开发之怎样创建自定义菜单,介绍情况,请看本文讲解。
微信菜单创建调用接口
public static String menu_create_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN"; 
菜单的创建需要传入access_token作为参数,还没获取access_token的小伙伴可参照微信公众号开发之获取access_token

在这里创建最基本的菜单实例:
public class Menu { 
    private Button[] button;    //一级菜单数组,个数应为1~3个 
    public Button[] getButton() { 
        return button; 
    } 
    public void setButton(Button[] button) { 
        this.button = button; 
    } 

public class Button {
 
    private String name;

    public String getName() {
 
       return name;

    }

    public void setName(String name) {

        this.name = name;
 
   }
}

public class SubButton extends Button{

    private Button[] sub_button;

    public Button[] getSub_button() {
 
       return sub_button;

    }

    public void setSub_button(Button[] sub_button) {
 
       this.sub_button = sub_button;

    }
}

public class ClickButton extends Button{
 
   private String type;

    private String key;

    public String getKey() {

        return key;

    }

    public void setKey(String key) {

        this.key = key;

    }

    public String getType() {
 
       return type;

    }

    public void setType(String type) {
 
       this.type = type;

    }
}

public class ViewButton extends Button{
 
   private String type;

    private String url;

    public String getUrl() {
 
       return url;
 
   }
 
   public void setUrl(String url) {
 
       this.url = url;

    }
 
   public String getType() {
 
       return type;

    }
 
   public void setType(String type) {
 
       this.type = type;
 
   }
}

组装菜单数据
/**
 * 描述:菜单管理器类 </br>
 */

public class MenuManager {

    /**
 
    * 组装菜单数据

     * @return

     */
 
   public static Menu getMenu() {
 
       Menu menu = new Menu();

        ViewButton button11 = new ViewButton();

        button11.setName("如何使用");

        button11.setType("view");
 
       button11.setUrl(WXConstants.BASE_SERVER + "/wechat/toshopstep");

        ViewButton button12 = new ViewButton();
 
       button12.setName("联系我们");
 
       button12.setType("view");
 
       button12.setUrl(WXConstants.BASE_SERVER + "/wechat/tocontentus");

        SubButton button1 = new SubButton();

        button1.setName("关于我们");
 
       button1.setSub_button(new Button[]{button11,button12});
       
        ViewButton button31 = new ViewButton();

        button31.setName("领取福利");
 
       button31.setType("view");

        button31.setUrl(WXConstants.BASE_SERVER + "/activity/xdou");

        ViewButton button32 = new ViewButton();
 
       button32.setName("人脸识别");

        button32.setType("view");
 
       button32.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + WXConstants.APPID + "&redirect_uri=" + WXConstants.BASE_SERVER + "/wechat/check&response_type=code&scope=snsapi_base&state=BINDFACE#wechat_redirect");
        SubButton button3 = new SubButton();

        button3.setName("豆便利");
 
       button3.setSub_button(new Button[]{button31,button32});
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;menu.setButton(new Button[]{button1,button3});
 
       return menu;
 
   }
}

创建菜单 (httpRequest方法参照微信公众号开发之获取access_token)
    /**

     * 创建菜单

     * @param menu 菜单实例
 
    * @param accessToken 有效的access_token
 
    * @return 0表示成功,其他值表示失败

     */
 
   public static int createMenu(Menu menu, String accessToken) {
 
       int result = 0;
 
       // 拼装创建菜单的url
 
       String url = WXConstants.menu_create_url.replace("ACCESS_TOKEN", accessToken);
        // 将菜单对象转换成json字符串

        String jsonMenu = JSONObject.fromObject(menu).toString();

        // 调用接口创建菜单

        JSONObject jsonObject = httpRequest(url, "POST", jsonMenu);

        if (null != jsonObject) {

            if (0 != jsonObject.getInt("errcode")) {
 
               result = jsonObject.getInt("errcode");

                log.error("创建菜单失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));

            }
 
       }
 
       return result;

    }

main方法测试:
&nbsp;&nbsp;&nbsp;&nbsp;private static Logger log = LoggerFactory.getLogger(MenuManager.class);

    public static void main(String[] args) {
 
       // 通过缓存获取access_token

        Map<String,Object> map = AccessTokenCache.getInstance().getAcessTokenAndExpiresIn(WXConstants.APPID, WXConstants.APPSECRET);

        String accessToken = (String)map.get("access_token");

        if (!accessToken.equals("")) {
 
           // 调用接口创建菜单

            int result = WeiXinUtil.createMenu(getMenu(), accessToken);

            // 判断菜单创建结果

            if (0 == result)
 
               log.info("菜单创建成功!");
 
           else
 
               log.info("菜单创建失败,错误码:" + result);

        }

    }

试结果:

 


当然只是测试的话,还有更简单的方式,就是通过微信公众平台的测试接口https://mp.weixin.qq.com/debug

先获取access_token

然后复制获取到的access_token,再创建菜单(菜单是以json封装的,具体可参考微信公众平台)

返回如下结果,则说明创建成功