如何在微信公众号上添加菜单

         public String getAccessToken(String appId, String appSecret) {
     token = daoFac.getAccessTokenDao().getAccessToken(appId, appSecret);
    /*if (token == null
|| token.getCreateTime() + 7200 * 1000 <= System
.currentTimeMillis()) {*/
String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
+ appId + "&secret=" + appSecret;
String accessToken = null;
try {
URL urlGet = new URL(tokenUrl);
HttpURLConnection http = (HttpURLConnection) urlGet
.openConnection();
http.setRequestMethod("GET"); // 必须是get方式请求
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout",
"30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();


InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String message = new String(jsonBytes, "UTF-8");


JSONObject demoJson = new JSONObject(message);
accessToken = demoJson.getString("access_token");
System.out.println(demoJson);
token = new AccessToken();
token.setCreateTime(System.currentTimeMillis());
token.setAccessToken(accessToken);
token.setAppId(appId);
token.setAppSecret(appSecret);
daoFac.getAccessTokenDao().merge(token);
} catch (Exception e) {
e.printStackTrace();
}
// }
return token.getAccessToken();

}



public String createMenu(DxInvokeMap<String, Object> params) {


// 样例菜单配置
// 此处改为自己想要的结构体,替换即可,可后台配置
String menu = "{\"button\":["
+ "{\"type\":\"view\",\"name\":\"首页\",\"url\":\"http://www.tintop.net/\"},"
+ "{\"type\":\"click\",\"name\":\"红包\",\"key\":\"Pack\"},"
+ "{\"name\":\"日常\","
+ "\"sub_button\":["
+ "{\"type\":\"click\",\"name\":\"会议\",\"key\":\"MEET\"},"
+ "{\"type\":\"click\",\"name\":\"日程\",\"key\":\"MANA\"},"
+ "{\"type\":\"click\",\"name\":\"通讯录\",\"key\":\"TXL\"},"
+ "{\"type\":\"view\",\"name\":\"百度\",\"url\":\"http://www.baidu.com/\"},"
+ "{\"type\":\"view\",\"name\":\"淘宝\",\"url\":\"http://www.taobao.com/\"}"
+ "]" + "}" + "]}";
publicAccount = daoFac.getWxPublicAccountDao().getAccountByAppId(
params.getString("appId"));
if (publicAccount == null) {
return "公众号不存在";
}
String accessToken = getAccessToken(publicAccount.getAppId(),
publicAccount.getAppSecret());
String createMenuUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="
+ accessToken;
try {
URL url = new URL(createMenuUrl);
HttpURLConnection http = (HttpURLConnection) url.openConnection();


http.setRequestMethod("POST");
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();


OutputStream os = http.getOutputStream();
os.write(menu.getBytes("UTF-8"));// 传入参数
os.flush();
os.close();


InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String message = new String(jsonBytes, "UTF-8");
System.out.println(message);
if (message.equals("{\"errcode\":0,\"errmsg\":\"ok\"}")) {
return "创建菜单成功";
} else {
return "创建订单失败: " + message;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "创建菜单 失败";
}




public String deleteMenu(DxInvokeMap<String, Object> params) {


publicAccount = daoFac.getWxPublicAccountDao().getAccountByAppId(
params.getString("appId"));
if (publicAccount == null) {
return "公众号不存在";
}
String accessToken = getAccessToken(publicAccount.getAppId(),
publicAccount.getAppSecret());
String action = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token="
+ accessToken;
try {
URL url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();


http.setRequestMethod("GET");
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
OutputStream os = http.getOutputStream();
os.flush();
os.close();


InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String message = new String(jsonBytes, "UTF-8");
if (message.equals("{\"errcode\":0,\"errmsg\":\"ok\"}")) {
return "删除菜单成功";
} else {
return "删除菜单失败: " + message;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "删除菜单失败";
}



public String getMenu(DxInvokeMap<String, Object>params) {
publicAccount = daoFac.getWxPublicAccountDao().getAccountByAppId(
params.getString("appId"));
if (publicAccount == null) {
return "公众号不存在";
}
String accessToken = getAccessToken(publicAccount.getAppId(),
publicAccount.getAppSecret());
String action = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token="
+ accessToken;
try {
URL url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();


http.setRequestMethod("GET");
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
OutputStream os = http.getOutputStream();
os.flush();
os.close();


InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String message = new String(jsonBytes, "UTF-8");
System.out.println(message);
return message;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "获取菜单失败";
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值