消息推送:
/**
* 消息模板
*
* @param url
* @throws Exception
*/
public boolean sendWxMsg1(String first, String keyword1, String keyword2, String keyword3, String keyword4, String keyword5, String remark, String url, String openId, String template_id) throws Exception {
System.out.println("@nxl-sendWxMsg1- :" + first + "," + keyword1 + "," + keyword2 + "," + keyword3 + "," + keyword4 + "," + keyword5 + "," + remark + "," + url);
String accessToken = getAccessTokenByRedis();
//String accessToken = getAccessToken();
String postUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
JSONObject jsonObject = new JSONObject();
jsonObject.put("touser", openId); // openid
jsonObject.put("template_id", template_id);
if (url != null && url != "") {
TreeMap<String, String> miniprograms = new TreeMap<String, String>();
miniprograms.put("appid", "xxxxxxxx");//小程序的appid
miniprograms.put("pagepath", url);// 注意,这里是支持传参的!!!
jsonObject.put("miniprogram", miniprograms);
}
JSONObject data = new JSONObject();
if (first != "" && first != null) {
JSONObject keyword = new JSONObject();
keyword.put("color", "#173177");
keyword.put("value", first);
data.put("first", keyword);
}
if (keyword1 != "" && keyword1 != null) {
JSONObject keyword = new JSONObject();
keyword.put("color", "#173177");
keyword.put("value", keyword1);
data.put("keyword1", keyword);
}
if (keyword2 != "" && keyword2 != null) {
JSONObject keyword = new JSONObject();
keyword.put("color", "#173177");
keyword.put("value", keyword2);
data.put("keyword2", keyword);
}
if (keyword3 != "" && keyword3 != null) {
JSONObject keyword = new JSONObject();
keyword.put("color", "#173177");
keyword.put("value", keyword3);
data.put("keyword3", keyword);
}
if (keyword4 != "" && keyword4 != null) {
JSONObject keyword = new JSONObject();
keyword.put("color", "#173177");
keyword.put("value", keyword4);
data.put("keyword4", keyword);
}
if (keyword5 != "" && keyword5 != null) {
JSONObject keyword = new JSONObject();
keyword.put("color", "#173177");
keyword.put("value", keyword5);
data.put("keyword5", keyword);
}
if (remark != "" && remark != null) {
JSONObject keyword = new JSONObject();
keyword.put("color", "#173177");
keyword.put("value", remark);
data.put("remark", keyword);
}
jsonObject.put("data", data);
String string = HttpClientUtil.post(postUrl, jsonObject.toJSONString());
JSONObject result = JSON.parseObject(string);
int errcode = result.getIntValue("errcode");
if (errcode == 0) {
// 发送成功
System.out.println("发送成功");
return true;
} else {
// 发送失败
System.out.println("发送失败");
return false;
}
}
获取accessToken:
public static String getAccessToken() throws IOException {
String url = "https://api.weixin.qq.com/cgi-bin/token?";
StringBuilder sb = new StringBuilder();
sb.append(url);
sb.append("&grant_type=client_credential");
sb.append("&appid=");
sb.append("wxxxxxxxx");
sb.append("&secret=");
sb.append("xxxxxxxxxxxx");
url = sb.toString();
String returnVal = HttpClientUtil.get(url);
Map<String, Object> result = HttpClientUtil.parseJSON2Map(returnVal);
String accessToken = (String) result.get("access_token");
return (String) result.get("access_token");
}
获取公众号用户
1.批量获取关注公众号的用户openid
//获取openid 一次最多一万个
public Map<String, Object> getOpenids(String accessToken, String next_openid) throws Exception {
String apenidsUrl = null;
//如果没有next_openid 则是第一次获取
if (next_openid == null || "".equals(next_openid)) {
apenidsUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken;//+"&next_openid="+10
} else {
apenidsUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken + "&next_openid=" + next_openid;
}
System.out.println("apenidsUrl=" + apenidsUrl);
String returnopen = HttpClientUtil.get(apenidsUrl);
Map<String, Object> resultOpen = HttpClientUtil.parseJSON2Map(returnopen);
System.out.println("@nxl -getOpenids result-" + resultOpen.toString());
return resultOpen;
}
2.获取用户信息
//单个获取微信用户
public List<WxUserVo> getWxUser(JSONObject jsonObject, String accessToken) throws Exception {
List<WxUserVo> voList = new LinkedList<>();
JSONArray openids = jsonObject.getJSONArray("openid");
//List<String> openids = data.get("openid");
for (int i = 0; i < openids.size(); i++) {
//循环获取用户信息
String usrUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openids.getString(i) + "&lang=zh_CN";
System.out.println("usrUrl=" + usrUrl);
String returnUsr = HttpClientUtil.get(usrUrl);
Map<String, Object> resultUsr = HttpClientUtil.parseJSON2Map(returnUsr);
//获取vo
WxUserVo uv = new WxUserVo();
// uv.setId(RmIdFactory.requestId("qst_wx_user"));
uv.setOpenId((String) resultUsr.get("openid"));
uv.setUnionId((String) resultUsr.get("unionid"));
uv.setNickName(EmojiUtils.filterName((String) resultUsr.get("nickname")));
System.out.println("WxUser:" + uv.toString());
voList.add(uv);
//wxUserService.insert(uv);
}
return voList;
}
3.批量获取用户信息
/**
* 同时获取多个微信用户 每次最多100个
* @param openides
* @param accessToken
* @return
* @throws Exception
*/
public List<WxUserVo> getWxUsers(List<Map<String, String>> openides, String accessToken) throws Exception {
List<WxUserVo> voList = new LinkedList<>();
HashMap<String, Object> par = new HashMap<>();
par.put("user_list", openides);
String usrUrl = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=" + accessToken;
System.out.println("usrUrl=" + usrUrl);
String returnUsr = HttpClientUtil.post(usrUrl, JSON.toJSONString(par));
Map<String, Object> resultUsr = HttpClientUtil.parseJSON2Map(returnUsr);
//解析返回值获取用户信息
List<Map<String, Object>> infoList = (List<Map<String, Object>>) resultUsr.get("user_info_list");
for (Map<String, Object> map : infoList) {
WxUserVo uv = new WxUserVo();
uv.setOpenId((String) map.get("openid"));
uv.setUnionId((String) map.get("unionid"));
uv.setNickName(EmojiUtils.filterName((String) map.get("nickname")));
voList.add(uv);
}
/*WxUserVo uv=new WxUserVo();
uv.setId(RmIdFactory.requestId("qst_wx_user"));
System.out.println("WxUser:"+uv.toString());*/
return voList;
}
心得:
1.公众号的appid再h5中可以获取openid 小程序的appid不可以再h5中获取openid
2.获取unionid的前提 公众号和小程序绑定 并且在微信开放平台注册 不然开发时无法获取unionid
3.获取unionid的方式 公众号可以统一获取所有用户的openid 然后根据openid获取unionid 小程序无法批量获取 只能在小程序授权登录时和获取openid一块获取unionid
4.小程序获取用户信息 https://jingyan.baidu.com/article/eae0782762cd7d1fec5485cc.html
5.公众号可以根据openid获取用户信息 (微信名称,unionid 头像)