https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013
* 注意
由于开启的微信服务器配置,微信公众号后台设置的菜单以及自动回复都将失效,此时需要使用代码进行自定义菜单。
自定义菜单能够帮助公众号丰富界面,让用户更好更快地理解公众号的功能。
1、自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。
2、一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替。
3、创建自定义菜单后,菜单的刷新策略是,在用户进入公众号会话页或公众号profile页时,如果发现上一次拉取菜单的请求在5分钟以前,就会拉取一下菜单,如果菜单有更新,就会刷新客户端的菜单。测试时可以尝试取消关注公众账号后再次关注,则可以看到创建后的效果。
用户可以使用相关接口进行相对应的增删改查。
用户也可以根据用户的属性,设置个性化菜单
1、用户标签(开发者的业务需求可以借助用户标签来完成)
2、性别
3、手机操作系统
4、地区(用户在微信客户端设置的地区)
5、语言(用户在微信客户端设置的语言)
package com.lm.action.accessToken;
import com.alibaba.fastjson.JSONObject;
import com.lm.conf.WechatConf;
import com.lm.conf.WechatUrl;
import com.lm.util.HttpConnectionUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 自定义菜单管理
*/
@RestController
public class MenuAction {
/**
* 创建
*/
@GetMapping("/menu/create")
public String create() {
String url = WechatUrl.MENU_CREATE.replaceAll("#ACCESS_TOKEN#", WechatConf.ACCESSTOKEN);
System.out.println(buildMenu());
return HttpConnectionUtil.post(url, buildMenu());
}
/**
* 查询
*/
@GetMapping("/menu/get")
public String get() {
String url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=#ACCESS_TOKEN#";
url = url.replaceAll("#ACCESS_TOKEN#", WechatConf.ACCESSTOKEN);
return HttpConnectionUtil.get(url);
}
/**
* 删除
*/
@GetMapping("/menu/delete")
public String delete() {
String url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=#ACCESS_TOKEN#";
url = url.replaceAll("#ACCESS_TOKEN#", WechatConf.ACCESSTOKEN);
return HttpConnectionUtil.get(url);
}
public String buildMenu() {
List<JSONObject> threeList = new ArrayList<JSONObject>();
JSONObject t1 = new JSONObject();
t1.put("url", "http://www.baidu.com");
t1.put("type", "view");
t1.put("name", "跳转URL");
JSONObject t2 = new JSONObject();
t2.put("key", "dianjishijian");
t2.put("type", "click");
t2.put("name", "点击事件");
JSONObject t3 = new JSONObject();
t3.put("key", "saomatuishijian");
t3.put("type", "scancode_push");
t3.put("name", "扫码推事件");
JSONObject t4 = new JSONObject();
t4.put("key", "saomatuishijianxiaoxi");
t4.put("type", "scancode_waitmsg");
t4.put("name", "扫码弹出消息");
JSONObject t5 = new JSONObject();
t5.put("key", "xitongpaizhao");
t5.put("type", "pic_sysphoto");
t5.put("name", "弹出系统拍照");
threeList.add(t1);
threeList.add(t2);
threeList.add(t3);
threeList.add(t4);
threeList.add(t5);
JSONObject threeJson = new JSONObject();
threeJson.put("name", "测试");
threeJson.put("sub_button", threeList);
//--------------------------------------
List<JSONObject> secondList = new ArrayList<JSONObject>();
JSONObject s1 = new JSONObject();
s1.put("url", "http://www.baidu.com");
s1.put("type", "view");
s1.put("name", "百度1");
JSONObject s2 = new JSONObject();
s2.put("key", "paizhaoxiangce");
s2.put("type", "pic_photo_or_album");
s2.put("name", "弹出拍照相册");
JSONObject s3 = new JSONObject();
s3.put("key", "xiangcefatuqi");
s3.put("type", "pic_weixin");
s3.put("name", "相册发图器");
JSONObject s4 = new JSONObject();
s4.put("key", "diliweizhi");
s4.put("type", "location_select");
s4.put("name", "地理位置");
// JSONObject s5 = new JSONObject();
// s5.put("media_id", "xiafaxiaoxi");
// s5.put("type", "media_id");
// s5.put("name", "下发消息");
secondList.add(s1);
secondList.add(s2);
secondList.add(s3);
secondList.add(s4);
// secondList.add(s5);
JSONObject secondJson = new JSONObject();
secondJson.put("name", "测试");
secondJson.put("sub_button", secondList);
//------------------------------------------
// List<JSONObject> firstList = new ArrayList<JSONObject>();
// JSONObject f1 = new JSONObject();
// f1.put("media_id", "tuwenxiaoxi");
// f1.put("type", "view_limited");
// f1.put("name", "跳转图文消息");
// firstList.add(f1);
// JSONObject firstJson = new JSONObject();
// firstJson.put("name", "测试");
// firstJson.put("sub_button", firstList);
//-------------------------------------------
JSONObject parentJson = new JSONObject();
List<JSONObject> parentList = new ArrayList<JSONObject>();
// parentList.add(firstJson);
parentList.add(secondJson);
parentList.add(threeJson);
parentJson.put("button", parentList);
return JSONObject.toJSONString(parentJson);
}
}
package com.lm.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
public class HttpConnectionUtil {
//get请求
public static String get(String url){
HttpURLConnection conn = null;
BufferedReader rd = null ;
StringBuilder sb = new StringBuilder ();
String line = null ;
String response = null;
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setReadTimeout(20000);
conn.setConnectTimeout(20000);
conn.setUseCaches(false);
conn.connect();
rd = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));
while ((line = rd.readLine()) != null ) {
sb.append(line);
}
response = sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(rd != null){
rd.close();
}
if(conn != null){
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
//post表单请求
public static String post(String url, Map<String, String> form){
HttpURLConnection conn = null;
PrintWriter pw = null ;
BufferedReader rd = null ;
StringBuilder out = new StringBuilder();
StringBuilder sb = new StringBuilder();
String line = null ;
String response = null;
for (String key : form.keySet()) {
if(out.length()!=0){
out.append("&");
}
out.append(key).append("=").append(form.get(key));
}
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setReadTimeout(20000);
conn.setConnectTimeout(20000);
conn.setUseCaches(false);
conn.connect();
pw = new PrintWriter(conn.getOutputStream());
pw.print(out.toString());
pw.flush();
rd = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));
while ((line = rd.readLine()) != null ) {
sb.append(line);
}
response = sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(pw != null){
pw.close();
}
if(rd != null){
rd.close();
}
if(conn != null){
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
//post字符串请求
public static String post(String url, String rawBody){
HttpURLConnection conn = null;
PrintWriter pw = null ;
BufferedReader rd = null ;
StringBuilder sb = new StringBuilder ();
String line = null ;
String response = null;
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setReadTimeout(20000);
conn.setConnectTimeout(20000);
conn.setUseCaches(false);
conn.connect();
pw = new PrintWriter(conn.getOutputStream());
pw.print(rawBody);
pw.flush();
rd = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));
while ((line = rd.readLine()) != null ) {
sb.append(line);
}
response = sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(pw != null){
pw.close();
}
if(rd != null){
rd.close();
}
if(conn != null){
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
}