• 公众号自定义菜单(含个性化)


    洛塔服务号回复002获取代码。

    功能说明

    看官方文档说明就知道这里修改了好几次,很多地方语句都不通,比如查询接口中官方写的“本接口与自定义菜单查询接口的不同之处在于,本接口无论公众号的接口是如何设置的,都能查询到接口,而自定义菜单查询接口则仅能查询到使用 API 设置的菜单配置。”【本接口就是自定义菜单查询接口,哪里来的不同之处,感觉是在写查询接口和个性化里面的查询接口之不同】。
    官方文档地址是:https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html
    因为测试过程中涉及到了获取永久素材Id和发布图文消息Id,多写了对应获取方式。需要先登录后台上传一张素材图片,并发布一篇图文消息(发布即可,无需群发)。然后可以使用下述代码获取到,用来绑定到菜单上进行测试。

    获取素材Id
    	// 获取图片永久素材id,用于菜单使用
    	static void testImage() throws Exception {
    		// 先获取access_token,这部分正式环境需要配置定时获取,每天2000次调用限制
    		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret="
    				+ SECRET;
    		String result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		String accessToken = JSON.parseObject(result).getString("access_token");
    		// 获取永久素材
    		url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + accessToken;
    		JSONObject param = new JSONObject();
    		param.put("type", "image");
    		param.put("offset", "0");
    		param.put("count", "20");
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute()
    				.body();
    		System.out.println(result);
    	}
    
    发布图文消息Id
    	// 获取发布文章ID,用于菜单使用
    	static void testArticle() throws Exception {
    		// 先获取access_token,这部分正式环境需要配置定时获取,每天2000次调用限制
    		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret="
    				+ SECRET;
    		String result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		String accessToken = JSON.parseObject(result).getString("access_token");
    		// 获取成功发布的图片列表
    		url = "https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token=" + accessToken;
    		JSONObject param = new JSONObject();
    		param.put("offset", "0");
    		param.put("count", "20");
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute()
    				.body();
    		System.out.println(result);
    	}
    

    自定义菜单

    包括菜单功能的创建、查询、删除。

    创建:会覆盖掉之前已存在的
    查询:如果调用了删除,再查询的时候不是空的,而是删除之前最后一次的
    删除:虽然查询还是有,但是公众号确实已经删除了

    说明:这里的查询只包括创建的菜单,不包括个性化菜单内容,如果需要连同个性化菜单一起返回,可以调用个性化中的查询接口。

    菜单包括诸多类型,测试代码均已列出

    • click:点击事件,后台会收到key值,做对应处理(消息接口部分介绍)
    • view:网址链接
    • miniprogram:小程序,必须是已关联的,后台左侧小程序管理
    • scancode_push:扫码后打开,并推送后台
    • scancode_waitmsg:扫码后提示,并推送后台
    • location_select:给公众号发送位置
    • pic_photo_or_album:拍照或者相册选择图片
    • pic_sysphoto:仅拍照
    • pic_weixin: 仅相册
    • media_id:图片、语音、视频等永久素材
    • view_limited: 已经废弃,之前的图文素材,当前图文已经属于发布的文章
    • article_id:接收到已发布的图文消息
      -article_view_limited:直接打开图片消息的链接

    完整测试代码:

    	// 测试菜单功能
    	static void menu() throws Exception {
    		// 先获取access_token,这部分正式环境需要配置定时获取,每天2000次调用限制
    		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret="
    				+ SECRET;
    		String result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		String accessToken = JSON.parseObject(result).getString("access_token");
    		// 查询自定义菜单
    		// {"is_menu_open":1,"selfmenu_info":{"button":[{"type":"view","name":"科技","url":"https:\/\/maliyun0520148304-10637.126net.cn\/"},{"type":"view","name":"猎企","url":"https:\/\/aliyun0520148304-10637.126net.cn\/"},{"name":"志达货运","sub_button":{"list":[{"type":"view","name":"手机站","url":"https:\/\/maliyun0520148304-10637.126net.cn\/"},{"type":"view","name":"电脑站","url":"https:\/\/aliyun0520148304-10637.126net.cn\/"}]}}]}}
    		url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" + accessToken;
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		// 删除菜单:这里有个bug,删除后调用查询,返回的还是删除之前的菜单,但是公众号确实已经删除掉了
    		url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken;
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		// 查询菜单
    		url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" + accessToken;
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		// 创建菜单
    		url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken;
    		JSONObject param = new JSONObject();
    		JSONArray array = new JSONArray();
    		// 第一个菜单,无子菜单,测试click,点击后后台会收到key值,做对应处理(消息接口部分介绍)
    		JSONObject param1 = new JSONObject();
    		param1.put("type", "click");
    		param1.put("name", "每日一言");
    		param1.put("key", "TODAY_WORD");
    		array.add(param1);
    		// 第二个菜单,有子菜单
    		JSONObject param2 = new JSONObject();
    		param2.put("name", "有子菜单");
    		JSONArray array2 = new JSONArray();
    		JSONObject param21 = new JSONObject(); // 21-测试view
    		param21.put("type", "view");
    		param21.put("name", "链接");
    		param21.put("url", "https://maliyun0520148304-10637.126net.cn/");
    		array2.add(param21);
    		JSONObject param22 = new JSONObject(); // 22-测试小程序:必须是已关联的小程序(后台左侧小程序管理)
    		param22.put("type", "miniprogram");
    		param22.put("name", "小程序");
    		param22.put("url", "https://maliyun0520148304-10637.126net.cn/"); // 如果是老版本客户打不开小程序,将会打卡这个网址
    		param22.put("appid", "wxea2d4a5f6537f71f"); // 必须是已关联小程序的appid
    		param22.put("pagepath", "pages/index/index"); // 进入的小程序页面路径
    		array2.add(param22);
    		JSONObject param23 = new JSONObject(); // 23-扫码1,会显示结果,后台会收到消息(类似view点击)
    		param23.put("type", "scancode_push");
    		param23.put("name", "扫码推事件");
    		param23.put("key", "CODE_PUSH_1");
    		array2.add(param23);
    		JSONObject param24 = new JSONObject(); // 24-扫码2,消息接受中效果,后台会收到消息(类似view点击)
    		param24.put("type", "scancode_waitmsg");
    		param24.put("name", "扫码带提示");
    		param24.put("key", "CODE_PUSH_2");
    		array2.add(param24);
    		JSONObject param25 = new JSONObject(); // 25-发送位置,后台会收到消息(类似view点击)
    		param25.put("type", "location_select");
    		param25.put("name", "位置");
    		param25.put("key", "LOCATION");
    		array2.add(param25);
    		param2.put("sub_button", array2);
    		array.add(param2);
    		// 第三个菜单,有子菜单
    		JSONObject param3 = new JSONObject();
    		param3.put("name", "素材相关");
    		JSONArray array3 = new JSONArray();
    		JSONObject param31 = new JSONObject(); // 31-拍照或者相册
    		param31.put("type", "pic_photo_or_album"); // 这个可以替换为pic_sysphoto(只允许拍照) 或 pic_weixin(只允许相册)
    		param31.put("name", "拍照或相册");
    		param31.put("key", "PICTURE");
    		array3.add(param31);
    		JSONObject param32 = new JSONObject(); // 32-永久素材-图片视频等
    		param32.put("type", "media_id"); // 测试可以先用网址上传个素材获取media_id(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo)
    		param32.put("name", "图片视频等");
    		param32.put("media_id", "6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS");
    		array3.add(param32);
    //		JSONObject param33 = new JSONObject(); //33-永久素材-图文消息-已经被废弃,图文不再算素材,而是发布(发表)
    //		param33.put("type", "view_limited"); // 测试可以先用网址上传个素材获取media_id(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo)
    //		param33.put("name", "图文消息");
    //		param33.put("media_id", "");
    //		array3.add(param33);
    		JSONObject param34 = new JSONObject(); // 34-发布的图文消息
    		param34.put("type", "article_id"); // 测试可以先用网址上传个素材获取media_id(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo)
    		param34.put("name", "图文消息");
    		param34.put("article_id", "wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn");
    		array3.add(param34);
    		JSONObject param35 = new JSONObject(); // 35-发布的图文消息-打开url
    		param35.put("type", "article_view_limited"); // 测试可以先用网址上传个素材获取media_id(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo)
    		param35.put("name", "打开图文");
    		param35.put("article_id", "wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn");
    		array3.add(param35);
    		param3.put("sub_button", array3);
    		array.add(param3);
    		param.put("button", array);
    		System.out.println(param.toString());
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute()
    				.body();
    		System.out.println(result);
    		// 查询菜单
    		// {"is_menu_open":1,"selfmenu_info":{"button":[{"type":"click","name":"每日一言","key":"TODAY_WORD"},{"name":"有子菜单","sub_button":{"list":[{"type":"view","name":"链接","url":"https:\/\/maliyun0520148304-10637.126net.cn\/"},{"type":"miniprogram","name":"小程序","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","appid":"wxea2d4a5f6537f71f","pagepath":"pages\/index\/index"},{"type":"scancode_push","name":"扫码推事件","key":"CODE_PUSH_1"},{"type":"scancode_waitmsg","name":"扫码带提示","key":"CODE_PUSH_2"},{"type":"location_select","name":"位置","key":"LOCATION"}]}},{"name":"素材相关","sub_button":{"list":[{"type":"pic_photo_or_album","name":"拍照或相册","key":"PICTURE"},{"type":"media_id","name":"图片视频等","media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS"},{"type":"article_id","name":"图文消息","article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"},{"type":"article_view_limited","name":"打开图文","article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"}]}}]}}
    		url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" + accessToken;
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    	}
    

    上面代码依次打印的内容为:

    {"access_token":"61_EjyqrtsEAw8jmK8pKf_eV1kkCR-qlnZ9EuftLU_P5apdvdTZPVKq3DLmHTTYJ-yIhliky0UeXW4col0S8_FTDV3Tqx2tXOxjOSkLl2gWmeZ0ApNBLhuHuuIxZSvtqJuHdwMG8g9AKfmTY6GLOIQbAIAUOW","expires_in":7200}
    {"is_menu_open":1,"selfmenu_info":{"button":[{"type":"click","name":"每日一言","key":"TODAY_WORD"},{"name":"有子菜单","sub_button":{"list":[{"type":"view","name":"链接","url":"https:\/\/maliyun0520148304-10637.126net.cn\/"},{"type":"miniprogram","name":"小程序","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","appid":"wxea2d4a5f6537f71f","pagepath":"pages\/index\/index"},{"type":"scancode_push","name":"扫码推事件","key":"CODE_PUSH_1"},{"type":"scancode_waitmsg","name":"扫码带提示","key":"CODE_PUSH_2"},{"type":"location_select","name":"位置","key":"LOCATION"}]}},{"name":"素材相关","sub_button":{"list":[{"type":"pic_photo_or_album","name":"拍照或相册","key":"PICTURE"},{"type":"media_id","name":"图片视频等","media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS"},{"type":"article_id","name":"图文消息","article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"},{"type":"article_view_limited","name":"打开图文","article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"}]}}]}}
    {"errcode":0,"errmsg":"ok"}
    {"is_menu_open":0,"selfmenu_info":{"button":[{"type":"click","name":"每日一言","key":"TODAY_WORD"},{"name":"有子菜单","sub_button":{"list":[{"type":"view","name":"链接","url":"https:\/\/maliyun0520148304-10637.126net.cn\/"},{"type":"miniprogram","name":"小程序","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","appid":"wxea2d4a5f6537f71f","pagepath":"pages\/index\/index"},{"type":"scancode_push","name":"扫码推事件","key":"CODE_PUSH_1"},{"type":"scancode_waitmsg","name":"扫码带提示","key":"CODE_PUSH_2"},{"type":"location_select","name":"位置","key":"LOCATION"}]}},{"name":"素材相关","sub_button":{"list":[{"type":"pic_photo_or_album","name":"拍照或相册","key":"PICTURE"},{"type":"media_id","name":"图片视频等","media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS"},{"type":"article_id","name":"图文消息","article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"},{"type":"article_view_limited","name":"打开图文","article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"}]}}]}}
    {"button":[{"name":"每日一言","type":"click","key":"TODAY_WORD"},{"name":"有子菜单","sub_button":[{"name":"链接","type":"view","url":"https://maliyun0520148304-10637.126net.cn/"},{"pagepath":"pages/index/index","appid":"wxea2d4a5f6537f71f","name":"小程序","type":"miniprogram","url":"https://maliyun0520148304-10637.126net.cn/"},{"name":"扫码推事件","type":"scancode_push","key":"CODE_PUSH_1"},{"name":"扫码带提示","type":"scancode_waitmsg","key":"CODE_PUSH_2"},{"name":"位置","type":"location_select","key":"LOCATION"}]},{"name":"素材相关","sub_button":[{"name":"拍照或相册","type":"pic_photo_or_album","key":"PICTURE"},{"name":"图片视频等","media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS","type":"media_id"},{"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn","name":"图文消息","type":"article_id"},{"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn","name":"打开图文","type":"article_view_limited"}]}]}
    {"errcode":0,"errmsg":"ok"}
    {"is_menu_open":1,"selfmenu_info":{"button":[{"type":"click","name":"每日一言","key":"TODAY_WORD"},{"name":"有子菜单","sub_button":{"list":[{"type":"view","name":"链接","url":"https:\/\/maliyun0520148304-10637.126net.cn\/"},{"type":"miniprogram","name":"小程序","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","appid":"wxea2d4a5f6537f71f","pagepath":"pages\/index\/index"},{"type":"scancode_push","name":"扫码推事件","key":"CODE_PUSH_1"},{"type":"scancode_waitmsg","name":"扫码带提示","key":"CODE_PUSH_2"},{"type":"location_select","name":"位置","key":"LOCATION"}]}},{"name":"素材相关","sub_button":{"list":[{"type":"pic_photo_or_album","name":"拍照或相册","key":"PICTURE"},{"type":"media_id","name":"图片视频等","media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS"},{"type":"article_id","name":"图文消息","article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"},{"type":"article_view_limited","name":"打开图文","article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"}]}}]}}
    

    测试代码中的请求参数嵌套格式不太明朗,提供一个清晰的json结构:

    {
        "is_menu_open":1,
        "selfmenu_info":{
            "button":[
                {
                    "type":"click",
                    "name":"每日一言",
                    "key":"TODAY_WORD"
                },
                {
                    "name":"有子菜单",
                    "sub_button":{
                        "list":[
                            {
                                "type":"view",
                                "name":"链接",
                                "url":"https://maliyun0520148304-10637.126net.cn/"
                            },
                            {
                                "type":"miniprogram",
                                "name":"小程序",
                                "url":"https://maliyun0520148304-10637.126net.cn/",
                                "appid":"wxea2d4a5f6537f71f",
                                "pagepath":"pages/index/index"
                            },
                            {
                                "type":"scancode_push",
                                "name":"扫码推事件",
                                "key":"CODE_PUSH_1"
                            },
                            {
                                "type":"scancode_waitmsg",
                                "name":"扫码带提示",
                                "key":"CODE_PUSH_2"
                            },
                            {
                                "type":"location_select",
                                "name":"位置",
                                "key":"LOCATION"
                            }
                        ]
                    }
                },
                {
                    "name":"素材相关",
                    "sub_button":{
                        "list":[
                            {
                                "type":"pic_photo_or_album",
                                "name":"拍照或相册",
                                "key":"PICTURE"
                            },
                            {
                                "type":"media_id",
                                "name":"图片视频等",
                                "media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS"
                            },
                            {
                                "type":"article_id",
                                "name":"图文消息",
                                "article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"
                            },
                            {
                                "type":"article_view_limited",
                                "name":"打开图文",
                                "article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"
                            }
                        ]
                    }
                }
            ]
        }
    }
    

    个性化菜单

    针对不同人群设置不同的规则,比如不同系统查看到的菜单不相同等。目前支持的个性化规则包括:

    • 客户端系统:client_platform_type
    • 用户标签:tag_id

    下面以不同用户系统查看到菜单不同为例(用户标签的等写到相应部分了再看)。
    先写一个返回菜单请求参数的方法,给予自定义菜单为例,添加规则内容,调整后代码如下(第一个click,安卓系统看到的是Android,苹果系统看到的是iOS):

    	static String getMenuString(int clientPlatformType) {
    		JSONObject param = new JSONObject();
    		JSONArray array = new JSONArray();
    		// 第一个菜单,无子菜单,测试click,点击后后台会收到key值,做对应处理(消息接口部分介绍)
    		JSONObject param1 = new JSONObject();
    		param1.put("type", "click");
    		param1.put("name", clientPlatformType == 1 ? "iOS" : clientPlatformType == 2 ? "Android" : "Others");
    		param1.put("key", "SYSTEM");
    		array.add(param1);
    		// 第二个菜单,有子菜单
    		JSONObject param2 = new JSONObject();
    		param2.put("name", "有子菜单");
    		JSONArray array2 = new JSONArray();
    		JSONObject param21 = new JSONObject(); // 21-测试view
    		param21.put("type", "view");
    		param21.put("name", "链接");
    		param21.put("url", "https://maliyun0520148304-10637.126net.cn/");
    		array2.add(param21);
    		JSONObject param22 = new JSONObject(); // 22-测试小程序:必须是已关联的小程序(后台左侧小程序管理)
    		param22.put("type", "miniprogram");
    		param22.put("name", "小程序");
    		param22.put("url", "https://maliyun0520148304-10637.126net.cn/"); // 如果是老版本客户打不开小程序,将会打卡这个网址
    		param22.put("appid", "wxea2d4a5f6537f71f"); // 必须是已关联小程序的appid
    		param22.put("pagepath", "pages/index/index"); // 进入的小程序页面路径
    		array2.add(param22);
    		JSONObject param23 = new JSONObject(); // 23-扫码1,会显示结果,后台会收到消息(类似view点击)
    		param23.put("type", "scancode_push");
    		param23.put("name", "扫码推事件");
    		param23.put("key", "CODE_PUSH_1");
    		array2.add(param23);
    		JSONObject param24 = new JSONObject(); // 24-扫码2,消息接受中效果,后台会收到消息(类似view点击)
    		param24.put("type", "scancode_waitmsg");
    		param24.put("name", "扫码带提示");
    		param24.put("key", "CODE_PUSH_2");
    		array2.add(param24);
    		JSONObject param25 = new JSONObject(); // 25-发送位置,后台会收到消息(类似view点击)
    		param25.put("type", "location_select");
    		param25.put("name", "位置");
    		param25.put("key", "LOCATION");
    		array2.add(param25);
    		param2.put("sub_button", array2);
    		array.add(param2);
    		// 第三个菜单,有子菜单
    		JSONObject param3 = new JSONObject();
    		param3.put("name", "素材相关");
    		JSONArray array3 = new JSONArray();
    		JSONObject param31 = new JSONObject(); // 31-拍照或者相册
    		param31.put("type", "pic_photo_or_album"); // 这个可以替换为pic_sysphoto(只允许拍照) 或 pic_weixin(只允许相册)
    		param31.put("name", "拍照或相册");
    		param31.put("key", "PICTURE");
    		array3.add(param31);
    		JSONObject param32 = new JSONObject(); // 32-永久素材-图片视频等
    		param32.put("type", "media_id"); // 测试可以先用网址上传个素材获取media_id(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo)
    		param32.put("name", "图片视频等");
    		param32.put("media_id", "6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS");
    		array3.add(param32);
    		JSONObject param34 = new JSONObject(); // 34-发布的图文消息
    		param34.put("type", "article_id"); // 测试可以先用网址上传个素材获取media_id(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo)
    		param34.put("name", "图文消息");
    		param34.put("article_id", "wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn");
    		array3.add(param34);
    		JSONObject param35 = new JSONObject(); // 35-发布的图文消息-打开url
    		param35.put("type", "article_view_limited"); // 测试可以先用网址上传个素材获取media_id(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo)
    		param35.put("name", "打开图文");
    		param35.put("article_id", "wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn");
    		array3.add(param35);
    		param3.put("sub_button", array3);
    		array.add(param3);
    		param.put("button", array);
    		JSONObject matchrule = new JSONObject();
    		matchrule.put("client_platform_type", clientPlatformType);
    		param.put("matchrule", matchrule);
    		return param.toString();
    	}
    

    给予上面的方法,开始生成自定义菜单测试。

    创建:即便生成相同的也不会覆盖,而是都保留,所以如果要编辑,方式就是全部删除,然后重新创建。如果没有创建自定义菜单,那边单独创建个性化菜单会报错。
    删除:个性化是根据menuId删除一条,也可以使用自定义菜单删除,会连同自定义菜单和个性化的一起删掉,然后再单独调用创建即可
    查询:会将自定义菜单和个性化菜单一起返回。
    匹配:可以使用粉丝的openid或者微信号来查询匹配规则。

    测试代码如下:

    	// 个性化菜单
    	static void menu2() throws Exception {
    		// 先获取access_token,这部分正式环境需要配置定时获取,每天2000次调用限制
    		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret="
    				+ SECRET;
    		String result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		String accessToken = JSON.parseObject(result).getString("access_token");
    		// 创建个性化菜单
    		url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=" + accessToken;
    		String req = getMenuString(1); // iOS系统
    		System.out.println(req);
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(req).execute().body();
    		System.out.println(result);
    		req = getMenuString(2); // Android系统
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(req).execute().body();
    		System.out.println(result);
    		req = getMenuString(3); // 其他系统
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(req).execute().body();
    		System.out.println(result);
    		// 查询菜单(含个性化菜单部分)
    		url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + accessToken;
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		// 准备删除其他系统的自定义菜单
    		String menuId = JSON.parseObject(result).getJSONArray("conditionalmenu").getJSONObject(0).getString("menuid");
    		url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token=" + accessToken;
    		JSONObject param = new JSONObject();
    		param.put("menuid", menuId);
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute()
    				.body();
    		System.out.println(result);
    		// 查询菜单(含个性化菜单部分)
    		url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + accessToken;
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		// 测试匹配规则
    		url = "https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token=" + accessToken;
    		param = new JSONObject();
    		param.put("user_id", "fymod1988"); //用户的openid或者微信号
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute()
    				.body();
    		System.out.println(result);
    	}
    

    上面代码依次打印的内容为:

    {"access_token":"61_Ls3Aoee9DMvS1ARmmeIsCaSlPvIwpVhloDn4H1FUeoFxM1YSXMItbgTsDOOyu8VpQAmbeAGxwtKD77TyWC6f54yzxaLfXv9Ls1rlkhRzH_esUHNQQ3RL2kxCNpGOTQMo3Pamx9fM4iRwgHp5CAEaAAASQU","expires_in":7200}
    {"button":[{"name":"iOS","type":"click","key":"SYSTEM"},{"name":"有子菜单","sub_button":[{"name":"链接","type":"view","url":"https://maliyun0520148304-10637.126net.cn/"},{"pagepath":"pages/index/index","appid":"wxea2d4a5f6537f71f","name":"小程序","type":"miniprogram","url":"https://maliyun0520148304-10637.126net.cn/"},{"name":"扫码推事件","type":"scancode_push","key":"CODE_PUSH_1"},{"name":"扫码带提示","type":"scancode_waitmsg","key":"CODE_PUSH_2"},{"name":"位置","type":"location_select","key":"LOCATION"}]},{"name":"素材相关","sub_button":[{"name":"拍照或相册","type":"pic_photo_or_album","key":"PICTURE"},{"name":"图片视频等","media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS","type":"media_id"},{"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn","name":"图文消息","type":"article_id"},{"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn","name":"打开图文","type":"article_view_limited"}]}],"matchrule":{"client_platform_type":1}}
    {"menuid":426993354}
    {"menuid":426993356}
    {"menuid":426993358}
    {"menu":{"button":[{"type":"click","name":"每日一言","key":"TODAY_WORD","sub_button":[]},{"name":"有子菜单","sub_button":[{"type":"view","name":"链接","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[]},{"type":"miniprogram","name":"小程序","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[],"appid":"wxea2d4a5f6537f71f","pagepath":"pages\/index\/index"},{"type":"scancode_push","name":"扫码推事件","key":"CODE_PUSH_1","sub_button":[]},{"type":"scancode_waitmsg","name":"扫码带提示","key":"CODE_PUSH_2","sub_button":[]},{"type":"location_select","name":"位置","key":"LOCATION","sub_button":[]}]},{"name":"素材相关","sub_button":[{"type":"pic_photo_or_album","name":"拍照或相册","key":"PICTURE","sub_button":[]},{"type":"media_id","name":"图片视频等","sub_button":[],"media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS"},{"type":"article_id","name":"图文消息","sub_button":[],"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"},{"type":"article_view_limited","name":"打开图文","sub_button":[],"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"}]}],"menuid":426993284},"conditionalmenu":[{"button":[{"type":"click","name":"Others","key":"SYSTEM","sub_button":[]},{"name":"有子菜单","sub_button":[{"type":"view","name":"链接","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[]},{"type":"miniprogram","name":"小程序","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[],"appid":"wxea2d4a5f6537f71f","pagepath":"pages\/index\/index"},{"type":"scancode_push","name":"扫码推事件","key":"CODE_PUSH_1","sub_button":[]},{"type":"scancode_waitmsg","name":"扫码带提示","key":"CODE_PUSH_2","sub_button":[]},{"type":"location_select","name":"位置","key":"LOCATION","sub_button":[]}]},{"name":"素材相关","sub_button":[{"type":"pic_photo_or_album","name":"拍照或相册","key":"PICTURE","sub_button":[]},{"type":"media_id","name":"图片视频等","sub_button":[],"media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS"},{"type":"article_id","name":"图文消息","sub_button":[],"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"},{"type":"article_view_limited","name":"打开图文","sub_button":[],"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"}]}],"matchrule":{"client_platform_type":"3"},"menuid":426993358},{"button":[{"type":"click","name":"Android","key":"SYSTEM","sub_button":[]},{"name":"有子菜单","sub_button":[{"type":"view","name":"链接","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[]},{"type":"miniprogram","name":"小程序","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[],"appid":"wxea2d4a5f6537f71f","pagepath":"pages\/index\/index"},{"type":"scancode_push","name":"扫码推事件","key":"CODE_PUSH_1","sub_button":[]},{"type":"scancode_waitmsg","name":"扫码带提示","key":"CODE_PUSH_2","sub_button":[]},{"type":"location_select","name":"位置","key":"LOCATION","sub_button":[]}]},{"name":"素材相关","sub_button":[{"type":"pic_photo_or_album","name":"拍照或相册","key":"PICTURE","sub_button":[]},{"type":"media_id","name":"图片视频等","sub_button":[],"media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS"},{"type":"article_id","name":"图文消息","sub_button":[],"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"},{"type":"article_view_limited","name":"打开图文","sub_button":[],"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"}]}],"matchrule":{"client_platform_type":"2"},"menuid":426993356},{"button":[{"type":"click","name":"iOS","key":"SYSTEM","sub_button":[]},{"name":"有子菜单","sub_button":[{"type":"view","name":"链接","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[]},{"type":"miniprogram","name":"小程序","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[],"appid":"wxea2d4a5f6537f71f","pagepath":"pages\/index\/index"},{"type":"scancode_push","name":"扫码推事件","key":"CODE_PUSH_1","sub_button":[]},{"type":"scancode_waitmsg","name":"扫码带提示","key":"CODE_PUSH_2","sub_button":[]},{"type":"location_select","name":"位置","key":"LOCATION","sub_button":[]}]},{"name":"素材相关","sub_button":[{"type":"pic_photo_or_album","name":"拍照或相册","key":"PICTURE","sub_button":[]},{"type":"media_id","name":"图片视频等","sub_button":[],"media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS"},{"type":"article_id","name":"图文消息","sub_button":[],"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"},{"type":"article_view_limited","name":"打开图文","sub_button":[],"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"}]}],"matchrule":{"client_platform_type":"1"},"menuid":426993354}]}
    {"errcode":0,"errmsg":"ok"}
    {"menu":{"button":[{"type":"click","name":"每日一言","key":"TODAY_WORD","sub_button":[]},{"name":"有子菜单","sub_button":[{"type":"view","name":"链接","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[]},{"type":"miniprogram","name":"小程序","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[],"appid":"wxea2d4a5f6537f71f","pagepath":"pages\/index\/index"},{"type":"scancode_push","name":"扫码推事件","key":"CODE_PUSH_1","sub_button":[]},{"type":"scancode_waitmsg","name":"扫码带提示","key":"CODE_PUSH_2","sub_button":[]},{"type":"location_select","name":"位置","key":"LOCATION","sub_button":[]}]},{"name":"素材相关","sub_button":[{"type":"pic_photo_or_album","name":"拍照或相册","key":"PICTURE","sub_button":[]},{"type":"media_id","name":"图片视频等","sub_button":[],"media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS"},{"type":"article_id","name":"图文消息","sub_button":[],"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"},{"type":"article_view_limited","name":"打开图文","sub_button":[],"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"}]}],"menuid":426993284},"conditionalmenu":[{"button":[{"type":"click","name":"Android","key":"SYSTEM","sub_button":[]},{"name":"有子菜单","sub_button":[{"type":"view","name":"链接","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[]},{"type":"miniprogram","name":"小程序","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[],"appid":"wxea2d4a5f6537f71f","pagepath":"pages\/index\/index"},{"type":"scancode_push","name":"扫码推事件","key":"CODE_PUSH_1","sub_button":[]},{"type":"scancode_waitmsg","name":"扫码带提示","key":"CODE_PUSH_2","sub_button":[]},{"type":"location_select","name":"位置","key":"LOCATION","sub_button":[]}]},{"name":"素材相关","sub_button":[{"type":"pic_photo_or_album","name":"拍照或相册","key":"PICTURE","sub_button":[]},{"type":"media_id","name":"图片视频等","sub_button":[],"media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS"},{"type":"article_id","name":"图文消息","sub_button":[],"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"},{"type":"article_view_limited","name":"打开图文","sub_button":[],"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"}]}],"matchrule":{"client_platform_type":"2"},"menuid":426993356},{"button":[{"type":"click","name":"iOS","key":"SYSTEM","sub_button":[]},{"name":"有子菜单","sub_button":[{"type":"view","name":"链接","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[]},{"type":"miniprogram","name":"小程序","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[],"appid":"wxea2d4a5f6537f71f","pagepath":"pages\/index\/index"},{"type":"scancode_push","name":"扫码推事件","key":"CODE_PUSH_1","sub_button":[]},{"type":"scancode_waitmsg","name":"扫码带提示","key":"CODE_PUSH_2","sub_button":[]},{"type":"location_select","name":"位置","key":"LOCATION","sub_button":[]}]},{"name":"素材相关","sub_button":[{"type":"pic_photo_or_album","name":"拍照或相册","key":"PICTURE","sub_button":[]},{"type":"media_id","name":"图片视频等","sub_button":[],"media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS"},{"type":"article_id","name":"图文消息","sub_button":[],"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"},{"type":"article_view_limited","name":"打开图文","sub_button":[],"article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"}]}],"matchrule":{"client_platform_type":"1"},"menuid":426993354}]}
    {"menu":{"button":[{"type":"click","name":"iOS","key":"SYSTEM","sub_button":[]},{"name":"有子菜单","sub_button":[{"type":"view","name":"链接","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[]},{"type":"miniprogram","name":"小程序","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","sub_button":[],"media_id":"","article_id":"","appid":"wxea2d4a5f6537f71f","pagepath":"pages\/index\/index"},{"type":"scancode_push","name":"扫码推事件","key":"CODE_PUSH_1","sub_button":[]},{"type":"scancode_waitmsg","name":"扫码带提示","key":"CODE_PUSH_2","sub_button":[]},{"type":"location_select","name":"位置","key":"LOCATION","sub_button":[]}]},{"name":"素材相关","sub_button":[{"type":"pic_photo_or_album","name":"拍照或相册","key":"PICTURE","sub_button":[]},{"type":"media_id","name":"图片视频等","url":"","sub_button":[],"media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS","article_id":"","appid":"","pagepath":""},{"type":"article_id","name":"图文消息","url":"","sub_button":[],"media_id":"","article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn","appid":"","pagepath":""},{"type":"article_view_limited","name":"打开图文","url":"","sub_button":[],"media_id":"","article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn","appid":"","pagepath":""}]}]}}
    

    事件推送

    很多操作,比如扫码、发送位置等,后台都会收到腾讯的异步消息,可以做相应处理,比如回复给用户内容等。这部分等写到消息了再单独出代码demo。

    完整代码

    package com.lootaa.wechat;
    
    import org.jsoup.Connection.Method;
    import org.jsoup.Jsoup;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    /**
     * 前置条件:公众号后台设置ip白名单
     */
    public class Test002 {
    
    	public static final String APPID = "wx276049d6a7551dca";
    	public static final String SECRET = "cbe109fdf6f399bd72ed3a4afafa21b1";
    
    	/**
    	 * 完整项目源码可关注公众号"lootaayun"(洛塔),回复002获取
    	 */
    	public static void main(String[] args) throws Exception {
    		// 单独分别调用下面两个,获取到菜单需要的素材信息
    //		testImage();
    //		testArticle();
    		// 测试菜单功能(查询、添加、删除)
    		menu();
    		// 个性化菜单
    //		menu2();
    
    	}
    
    	// 获取图片永久素材id,用于菜单使用
    	static void testImage() throws Exception {
    		// 先获取access_token,这部分正式环境需要配置定时获取,每天2000次调用限制
    		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret="
    				+ SECRET;
    		String result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		String accessToken = JSON.parseObject(result).getString("access_token");
    		// 获取永久素材
    		url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + accessToken;
    		JSONObject param = new JSONObject();
    		param.put("type", "image");
    		param.put("offset", "0");
    		param.put("count", "20");
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute()
    				.body();
    		System.out.println(result);
    	}
    
    	// 获取发布文章ID,用于菜单使用
    	static void testArticle() throws Exception {
    		// 先获取access_token,这部分正式环境需要配置定时获取,每天2000次调用限制
    		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret="
    				+ SECRET;
    		String result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		String accessToken = JSON.parseObject(result).getString("access_token");
    		// 获取成功发布的图片列表
    		url = "https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token=" + accessToken;
    		JSONObject param = new JSONObject();
    		param.put("offset", "0");
    		param.put("count", "20");
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute()
    				.body();
    		System.out.println(result);
    	}
    
    	// 测试菜单功能
    	static void menu() throws Exception {
    		// 先获取access_token,这部分正式环境需要配置定时获取,每天2000次调用限制
    		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret="
    				+ SECRET;
    		String result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		String accessToken = JSON.parseObject(result).getString("access_token");
    		// 查询自定义菜单
    		// {"is_menu_open":1,"selfmenu_info":{"button":[{"type":"view","name":"科技","url":"https:\/\/maliyun0520148304-10637.126net.cn\/"},{"type":"view","name":"猎企","url":"https:\/\/aliyun0520148304-10637.126net.cn\/"},{"name":"志达货运","sub_button":{"list":[{"type":"view","name":"手机站","url":"https:\/\/maliyun0520148304-10637.126net.cn\/"},{"type":"view","name":"电脑站","url":"https:\/\/aliyun0520148304-10637.126net.cn\/"}]}}]}}
    		url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" + accessToken;
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		// 删除菜单:这里有个bug,删除后调用查询,返回的还是删除之前的菜单,但是公众号确实已经删除掉了
    		url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken;
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		// 查询菜单
    		url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" + accessToken;
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		// 创建菜单
    		url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken;
    		JSONObject param = new JSONObject();
    		JSONArray array = new JSONArray();
    		// 第一个菜单,无子菜单,测试click,点击后后台会收到key值,做对应处理(消息接口部分介绍)
    		JSONObject param1 = new JSONObject();
    		param1.put("type", "click");
    		param1.put("name", "每日一言");
    		param1.put("key", "TODAY_WORD");
    		array.add(param1);
    		// 第二个菜单,有子菜单
    		JSONObject param2 = new JSONObject();
    		param2.put("name", "有子菜单");
    		JSONArray array2 = new JSONArray();
    		JSONObject param21 = new JSONObject(); // 21-测试view
    		param21.put("type", "view");
    		param21.put("name", "链接");
    		param21.put("url", "https://maliyun0520148304-10637.126net.cn/");
    		array2.add(param21);
    		JSONObject param22 = new JSONObject(); // 22-测试小程序:必须是已关联的小程序(后台左侧小程序管理)
    		param22.put("type", "miniprogram");
    		param22.put("name", "小程序");
    		param22.put("url", "https://maliyun0520148304-10637.126net.cn/"); // 如果是老版本客户打不开小程序,将会打卡这个网址
    		param22.put("appid", "wxea2d4a5f6537f71f"); // 必须是已关联小程序的appid
    		param22.put("pagepath", "pages/index/index"); // 进入的小程序页面路径
    		array2.add(param22);
    		JSONObject param23 = new JSONObject(); // 23-扫码1,会显示结果,后台会收到消息(类似view点击)
    		param23.put("type", "scancode_push");
    		param23.put("name", "扫码推事件");
    		param23.put("key", "CODE_PUSH_1");
    		array2.add(param23);
    		JSONObject param24 = new JSONObject(); // 24-扫码2,消息接受中效果,后台会收到消息(类似view点击)
    		param24.put("type", "scancode_waitmsg");
    		param24.put("name", "扫码带提示");
    		param24.put("key", "CODE_PUSH_2");
    		array2.add(param24);
    		JSONObject param25 = new JSONObject(); // 25-发送位置,后台会收到消息(类似view点击)
    		param25.put("type", "location_select");
    		param25.put("name", "位置");
    		param25.put("key", "LOCATION");
    		array2.add(param25);
    		param2.put("sub_button", array2);
    		array.add(param2);
    		// 第三个菜单,有子菜单
    		JSONObject param3 = new JSONObject();
    		param3.put("name", "素材相关");
    		JSONArray array3 = new JSONArray();
    		JSONObject param31 = new JSONObject(); // 31-拍照或者相册
    		param31.put("type", "pic_photo_or_album"); // 这个可以替换为pic_sysphoto(只允许拍照) 或 pic_weixin(只允许相册)
    		param31.put("name", "拍照或相册");
    		param31.put("key", "PICTURE");
    		array3.add(param31);
    		JSONObject param32 = new JSONObject(); // 32-永久素材-图片视频等
    		param32.put("type", "media_id"); // 测试可以先用网址上传个素材获取media_id(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo)
    		param32.put("name", "图片视频等");
    		param32.put("media_id", "6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS");
    		array3.add(param32);
    //		JSONObject param33 = new JSONObject(); //33-永久素材-图文消息-已经被废弃,图文不再算素材,而是发布(发表)
    //		param33.put("type", "view_limited"); // 测试可以先用网址上传个素材获取media_id(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo)
    //		param33.put("name", "图文消息");
    //		param33.put("media_id", "");
    //		array3.add(param33);
    		JSONObject param34 = new JSONObject(); // 34-发布的图文消息
    		param34.put("type", "article_id"); // 测试可以先用网址上传个素材获取media_id(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo)
    		param34.put("name", "图文消息");
    		param34.put("article_id", "wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn");
    		array3.add(param34);
    		JSONObject param35 = new JSONObject(); // 35-发布的图文消息-打开url
    		param35.put("type", "article_view_limited"); // 测试可以先用网址上传个素材获取media_id(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo)
    		param35.put("name", "打开图文");
    		param35.put("article_id", "wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn");
    		array3.add(param35);
    		param3.put("sub_button", array3);
    		array.add(param3);
    		param.put("button", array);
    		System.out.println(param.toString());
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute()
    				.body();
    		System.out.println(result);
    		// 查询菜单
    		// {"is_menu_open":1,"selfmenu_info":{"button":[{"type":"click","name":"每日一言","key":"TODAY_WORD"},{"name":"有子菜单","sub_button":{"list":[{"type":"view","name":"链接","url":"https:\/\/maliyun0520148304-10637.126net.cn\/"},{"type":"miniprogram","name":"小程序","url":"https:\/\/maliyun0520148304-10637.126net.cn\/","appid":"wxea2d4a5f6537f71f","pagepath":"pages\/index\/index"},{"type":"scancode_push","name":"扫码推事件","key":"CODE_PUSH_1"},{"type":"scancode_waitmsg","name":"扫码带提示","key":"CODE_PUSH_2"},{"type":"location_select","name":"位置","key":"LOCATION"}]}},{"name":"素材相关","sub_button":{"list":[{"type":"pic_photo_or_album","name":"拍照或相册","key":"PICTURE"},{"type":"media_id","name":"图片视频等","media_id":"6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS"},{"type":"article_id","name":"图文消息","article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"},{"type":"article_view_limited","name":"打开图文","article_id":"wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn"}]}}]}}
    		url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" + accessToken;
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    	}
    
    	// 个性化菜单
    	static void menu2() throws Exception {
    		// 先获取access_token,这部分正式环境需要配置定时获取,每天2000次调用限制
    		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret="
    				+ SECRET;
    		String result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		String accessToken = JSON.parseObject(result).getString("access_token");
    		// 创建个性化菜单
    		url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=" + accessToken;
    		String req = getMenuString(1); // iOS系统
    		System.out.println(req);
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(req).execute().body();
    		System.out.println(result);
    		req = getMenuString(2); // Android系统
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(req).execute().body();
    		System.out.println(result);
    		req = getMenuString(3); // 其他系统
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(req).execute().body();
    		System.out.println(result);
    		// 查询菜单(含个性化菜单部分)
    		url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + accessToken;
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		// 准备删除其他系统的自定义菜单
    		String menuId = JSON.parseObject(result).getJSONArray("conditionalmenu").getJSONObject(0).getString("menuid");
    		url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token=" + accessToken;
    		JSONObject param = new JSONObject();
    		param.put("menuid", menuId);
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute()
    				.body();
    		System.out.println(result);
    		// 查询菜单(含个性化菜单部分)
    		url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + accessToken;
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		// 测试匹配规则
    		url = "https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token=" + accessToken;
    		param = new JSONObject();
    		param.put("user_id", "fymod1988"); //用户的openid或者微信号
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute()
    				.body();
    		System.out.println(result);
    	}
    
    	static String getMenuString(int clientPlatformType) {
    		JSONObject param = new JSONObject();
    		JSONArray array = new JSONArray();
    		// 第一个菜单,无子菜单,测试click,点击后后台会收到key值,做对应处理(消息接口部分介绍)
    		JSONObject param1 = new JSONObject();
    		param1.put("type", "click");
    		param1.put("name", clientPlatformType == 1 ? "iOS" : clientPlatformType == 2 ? "Android" : "Others");
    		param1.put("key", "SYSTEM");
    		array.add(param1);
    		// 第二个菜单,有子菜单
    		JSONObject param2 = new JSONObject();
    		param2.put("name", "有子菜单");
    		JSONArray array2 = new JSONArray();
    		JSONObject param21 = new JSONObject(); // 21-测试view
    		param21.put("type", "view");
    		param21.put("name", "链接");
    		param21.put("url", "https://maliyun0520148304-10637.126net.cn/");
    		array2.add(param21);
    		JSONObject param22 = new JSONObject(); // 22-测试小程序:必须是已关联的小程序(后台左侧小程序管理)
    		param22.put("type", "miniprogram");
    		param22.put("name", "小程序");
    		param22.put("url", "https://maliyun0520148304-10637.126net.cn/"); // 如果是老版本客户打不开小程序,将会打卡这个网址
    		param22.put("appid", "wxea2d4a5f6537f71f"); // 必须是已关联小程序的appid
    		param22.put("pagepath", "pages/index/index"); // 进入的小程序页面路径
    		array2.add(param22);
    		JSONObject param23 = new JSONObject(); // 23-扫码1,会显示结果,后台会收到消息(类似view点击)
    		param23.put("type", "scancode_push");
    		param23.put("name", "扫码推事件");
    		param23.put("key", "CODE_PUSH_1");
    		array2.add(param23);
    		JSONObject param24 = new JSONObject(); // 24-扫码2,消息接受中效果,后台会收到消息(类似view点击)
    		param24.put("type", "scancode_waitmsg");
    		param24.put("name", "扫码带提示");
    		param24.put("key", "CODE_PUSH_2");
    		array2.add(param24);
    		JSONObject param25 = new JSONObject(); // 25-发送位置,后台会收到消息(类似view点击)
    		param25.put("type", "location_select");
    		param25.put("name", "位置");
    		param25.put("key", "LOCATION");
    		array2.add(param25);
    		param2.put("sub_button", array2);
    		array.add(param2);
    		// 第三个菜单,有子菜单
    		JSONObject param3 = new JSONObject();
    		param3.put("name", "素材相关");
    		JSONArray array3 = new JSONArray();
    		JSONObject param31 = new JSONObject(); // 31-拍照或者相册
    		param31.put("type", "pic_photo_or_album"); // 这个可以替换为pic_sysphoto(只允许拍照) 或 pic_weixin(只允许相册)
    		param31.put("name", "拍照或相册");
    		param31.put("key", "PICTURE");
    		array3.add(param31);
    		JSONObject param32 = new JSONObject(); // 32-永久素材-图片视频等
    		param32.put("type", "media_id"); // 测试可以先用网址上传个素材获取media_id(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo)
    		param32.put("name", "图片视频等");
    		param32.put("media_id", "6XvVazVT3F4qsVQ-QW23lhyagwQKJCDlX8N72L6s3vWt3wRdr_kyv57ykWM073PS");
    		array3.add(param32);
    		JSONObject param34 = new JSONObject(); // 34-发布的图文消息
    		param34.put("type", "article_id"); // 测试可以先用网址上传个素材获取media_id(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo)
    		param34.put("name", "图文消息");
    		param34.put("article_id", "wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn");
    		array3.add(param34);
    		JSONObject param35 = new JSONObject(); // 35-发布的图文消息-打开url
    		param35.put("type", "article_view_limited"); // 测试可以先用网址上传个素材获取media_id(https://mp.weixin.qq.com/debug/cgi-bin/apiinfo)
    		param35.put("name", "打开图文");
    		param35.put("article_id", "wifOAMVhhYn3p_EEhKK4irU6oY9vHgr9LygDlmVW3dhXNbV-Ut5jiPyhvqzIpQcn");
    		array3.add(param35);
    		param3.put("sub_button", array3);
    		array.add(param3);
    		param.put("button", array);
    		JSONObject matchrule = new JSONObject();
    		matchrule.put("client_platform_type", clientPlatformType);
    		param.put("matchrule", matchrule);
    		return param.toString();
    	}
    
    }
    
    
  • 相关阅读:
    我的前端成长之路:中医药大学毕业的业务女前端修炼之路
    国产大语言模型ChatGLM3本地搭建、使用和功能扩展
    Camera HAL 下的SprdCamera3HWI.cpp 内容介绍
    【JVM】垃圾回收(GC)详解
    Cocos Creator3.8 项目实战(四)巧用九宫格图像拉伸
    VSCode配置用户代码片段+内置变量梳理
    SpringBoot:如何优雅地进行响应数据封装、异常处理?
    java-php-python-科大学生党员之家设计计算机毕业设计
    安装WordPress(个人建站)
    【计算机考研必备常识】24考研你开始准备了吗?
  • 原文地址:https://blog.csdn.net/m0_58095675/article/details/126951906