微信公众号开发(Java)
原创
©著作权归作者所有:来自51CTO博客作者ZBY52031的原创作品,请联系作者获取转载授权,否则将追究法律责任
1.创建菜单栏
//菜单栏
public static void createMenu(){
HashMap<String,Object> hashMap = new LinkedHashMap<>();
List<HashMap> list = new ArrayList<>();
HashMap<String,Object> shoppingBtnMap = new LinkedHashMap<>();
shoppingBtnMap.put("type","view");
shoppingBtnMap.put("name","购物指引");
shoppingBtnMap.put("url","https://www.baidu.com/");
list.add(shoppingBtnMap);
HashMap<String,Object> activeBtnMap = new LinkedHashMap<>();
activeBtnMap.put("name","活动中心");
List<HashMap> erjiList = new ArrayList<>();
HashMap<String,Object> hashMap1 = new LinkedHashMap<>();
hashMap1.put("type","view");
hashMap1.put("name","加入群聊");
hashMap1.put("url","https://www.baidu.com/");
erjiList.add(hashMap1);
hashMap1 = new LinkedHashMap<>();
hashMap1.put("type","view");
hashMap1.put("name","活动细则");
hashMap1.put("url","https://www.baidu.com/");
erjiList.add(hashMap1);
activeBtnMap.put("sub_button",erjiList);
list.add(activeBtnMap);
HashMap<String,Object> relationBtnMap = new LinkedHashMap<>();
relationBtnMap.put("name","联系我们");
erjiList = new ArrayList<>();
hashMap1 = new LinkedHashMap<>();
hashMap1.put("type","view");
hashMap1.put("name","客服");
hashMap1.put("url","https://www.baidu.com/");
erjiList.add(hashMap1);
relationBtnMap.put("sub_button",erjiList);
list.add(relationBtnMap);
hashMap.put("button",list);
LogUtil.sendMsg(JSONObject.toJSONString(hashMap));
System.out.println(JSONObject.toJSONString(hashMap));
String res = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token="+getAccessToken(),JSONObject.toJSONString(hashMap));
LogUtil.sendMsg(res);
System.out.println(res);
}
可能命名不够规范,不要建议哈!将就看一下,重点是容易踩坑点:
a.使用view类型一定要有正确的url
b.数据一定要是json,可以使用本文中的方法转化一下JSONObject.toJSONString(hashMap),JSONObject使用的maven包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
上面创建菜单栏我使用了一个getAccessToken()方法获取token值,内容如下:
//获取token
public static String getAccessToken(){
//如果为空或者过期需重新获取
HashMap<String,Object> hashMap1 = new LinkedHashMap<>();
hashMap1.put("grant_type","client_credential");
hashMap1.put("appid",APPID);
hashMap1.put("secret",SECRET);
String res = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token",hashMap1);
JSONObject jsonObject = JSONObject.parseObject(res);
LogUtil.sendMsg(res);
String accessToken = jsonObject.getString("access_token");
LogUtil.sendMsg(accessToken);
return accessToken;
}
其实一般token存在过期时间,所以可以判断一下是否过期,然后再进行是否需要获取token。我这边有点懒就不判断了,其实主要是我不会判断,好吧!我对不起自己啊!
2.客服回复消息
HashMap<String,Object> textHashMap = new LinkedHashMap<>();
textHashMap.put("touser",fromUserName);
textHashMap.put("msgtype","text");
HashMap<String,String> text= new HashMap<>();
text.put("content","活动火热进行中,扫描下方二维码进群,即可有机会领取价值1300元的奔驰电动汽车和价值300元的母婴玩具大礼包");
textHashMap.put("text",text);
HttpUtil.post("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+getAccessToken(), JSONObject.toJSONString(textHashMap));
以上是文字回复,其实有坑的,一touser其实就是字面上的意思,拆开看一下就知道了,实在不理解参篇文章的fromUserName,二是否要创建客服账号,我这边是没有创建客服号直接调API的,扶额.....有木有写的太简单了。突然看到摸索一天的东西被自己几句话写完了,就特别不爽啊。。。。。注:原谅我扔了一堆代码,其实我看了很多文章都是文字内容,抱歉我看不懂啊,看完了也不会写,所以为了自己可以直接Ctrl+C加Ctrl+V,我就不厚道的扔代码了