获取微信公众号所有订阅用户,并批量获取用户基本信息
public void syncSubscribe() {
String appId = ApiConstants.PUBLIC_ACCOUNT_APP_ID;
List<String> openIdList = getUserList(appId);
List<List<String>> partitionList = org.apache.commons.collections4.ListUtil.partition(openIdList, 100);
for (List<String> list : partitionList) {
R<List<JSONObject>> result = getUserInfoList(appId, list);
if (result.isSuccess()) {
List<LkWxXxx> wxList = new ArrayList<>();
for (JSONObject t : result.getData()) {
if (t.containsKey("unionid")) {
String openid = t.getStr("openid");
String unionid = t.getStr("unionid");
Long subscribeTime = t.getLong("subscribe_time");
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(subscribeTime),
ZoneId.systemDefault());
wxList.add(LkWxXxx.builder()
.openid(openid)
.unionid(unionid)
.subscribeTime(localDateTime)
.build());
}
}
}
}
}
public static List<String> getUserList(String appId) {
List<String> userList = new ArrayList<>();
String nextOpenid = "";
boolean hasMore = true;
while (hasMore) {
String url = String.format("https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s",
getStableAccessToken(appId), nextOpenid);
String result = HttpUtil.get(url);
JSONObject userListObj = JSONUtil.parseObj(result);
if (userListObj.containsKey("data")) {
userList.addAll(userListObj.getJSONObject("data").getJSONArray("openid").toList(String.class));
}
nextOpenid = userListObj.getStr("next_openid");
hasMore = StrUtil.isNotEmpty(nextOpenid);
}
return userList;
}
public static R<List<JSONObject>> getUserInfoList(String appId, List<String> openIdList) {
try {
JSONObject openIdBody = new JSONObject().set("user_list", toMapList(openIdList, t -> new JSONObject().set("openid", t)));
String result = HttpUtil.post(
StrUtil.format("https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token={}", getStableAccessToken(appId)),
JSONUtil.toJsonStr(openIdBody));
JSONObject userInfoBatchGet = JSONUtil.parseObj(result);
if (userInfoBatchGet.containsKey("user_info_list")) {
return R.ok(userInfoBatchGet.getBeanList("user_info_list", JSONObject.class));
}
} catch (Exception e) {
e.printStackTrace();
}
return R.fail();
}
public static <E, T> List<T> toMapList(Collection<E> collection, Function<E, T> function) {
if (CollUtil.isEmpty(collection)) return CollUtil.newArrayList();
return collection
.stream()
.map(function)
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
public static String getStableAccessToken(String appId) {
if (StrUtil.isEmpty(appId)) return "";
String url = "https://api.weixin.qq.com/cgi-bin/token";
Map<String, Object> request = new HashMap<>(3);
request.put("grant_type", "client_credential");
request.put("appid", appId);
request.put("secret", ApiConstants.getAppSecret(appId));
try {
String result = HttpUtil.get(url, request, 30000);
accessToken = JSONUtil.parseObj(result).getStr("access_token", "");
if (StrUtil.isNotEmpty(accessToken)) {
return accessToken;
}
} catch (Exception e) {
log.info("getAccessToken Exception:{}", e.getMessage());
}
return "";
}