获取微信公众号所有订阅用户,并批量获取用户基本信息

获取微信公众号所有订阅用户,并批量获取用户基本信息

public void syncSubscribe() {
	String appId = ApiConstants.PUBLIC_ACCOUNT_APP_ID;
	// 所有订阅用户
	List<String> openIdList = getUserList(appId);
	// 一个批次只能查100条数据,将List按100的长度进行拆分
	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());
				}
			}
			// TODO doShings
		}
	}
}
	
 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); //hutool工具类
		JSONObject userListObj = JSONUtil.parseObj(result); //hutool工具类
		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());
}
/**
* 获取公众号普通的AccessToken
*/
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 "";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值