0
点赞
收藏
分享

微信扫一扫

自定义公众号菜单(java版)

幸甚至哉歌以咏志 2022-04-21 阅读 64
  /**
     * 公众号菜单
     * @return
     */
    @GetMapping(value = "/setMenu",produces = "text/html;charset=utf-8")
    public String setMenu() {
        String accessToken = getToken();//上期有提到
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        //一级菜单
        JSONObject menu1 = new JSONObject();
        menu1.put("type", "view");
        menu1.put("name", "知识星球");
        menu1.put("url", "https://www.photosir.com/videoTeaching.html");
        jsonArray.add(menu1);
        //一级菜单
        JSONObject menu2 = new JSONObject();
        menu2.put("type", "miniprogram");
        menu2.put("name", "悟空传图");
        menu2.put("url", "http://mp.weixin.qq.com");
        menu2.put("appid", "公众号appid");
        menu2.put("pagepath", "小程序配置路径");
        jsonArray.add(menu2);
        //一级菜单
        JSONObject menu3 = new JSONObject();
        menu3.put("name", "商业合作");
        JSONArray jsonArray2 = new JSONArray();
        //二级菜单
        JSONObject menu4 = new JSONObject();
        menu4.put("type", "view");
        menu4.put("name", "官网");
        menu4.put("url", "https://www.photosir.com/");
        jsonArray2.add(menu4);
        JSONObject menu5 = new JSONObject();
        menu5.put("type", "view");
        menu5.put("name", "联系我们");
        menu5.put("url", "https://www.photosir.com/contact.html");
        jsonArray2.add(menu5);
        menu3.put("sub_button", jsonArray2);
        jsonArray.add(menu3);
        jsonObject.put("button", jsonArray);
        String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken;
        String result = HttpUtil.postData(url, jsonObject.toJSONString());
        System.out.println(result);
        return "设置完成";
    }
举报

相关推荐

0 条评论