Skip to content

Commit

Permalink
🎨 #2811 【公众号】用户管理增加可一次性拉取所有用户数据的接口
Browse files Browse the repository at this point in the history
  • Loading branch information
Cocowwy authored Sep 13, 2022
1 parent 310e1d9 commit 286596f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ public interface WxMpUserService {
*/
WxMpUserList userList(String nextOpenid) throws WxErrorException;

/**
* <pre>
* 获取用户列表(全部)
* 公众号可通过本接口来获取帐号的关注者列表,
* 关注者列表由一串OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的)组成。
* @see #userList(java.lang.String) 的增强,内部进行了多次数据拉取的汇总
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140840&token=&lang=zh_CN
* http请求方式: GET(请使用https协议)
* 接口地址:https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
* </pre>
*/
WxMpUserList userList() throws WxErrorException;

/**
* <pre>
* 微信公众号主体变更迁移用户 openid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -52,6 +53,25 @@ public WxMpUserList userList(String nextOpenid) throws WxErrorException {
return WxMpUserList.fromJson(responseContent);
}

@Override
public WxMpUserList userList() throws WxErrorException {
String responseContent = this.wxMpService.get(USER_GET_URL, null);
WxMpUserList mergeList = new WxMpUserList();

WxMpUserList wxMpUserList = WxMpUserList.fromJson(responseContent);
mergeList.getOpenids().addAll(wxMpUserList.getOpenids());
mergeList.setCount(wxMpUserList.getCount());
mergeList.setTotal(wxMpUserList.getTotal());

while (StringUtils.isNotEmpty(wxMpUserList.getNextOpenid())) {
WxMpUserList nextReqUserList = userList(wxMpUserList.getNextOpenid());
mergeList.getOpenids().addAll(nextReqUserList.getOpenids());
mergeList.setCount(mergeList.getCount() + nextReqUserList.getCount());
wxMpUserList = nextReqUserList;
}
return mergeList;
}

@Override
public List<WxMpChangeOpenid> changeOpenid(String fromAppid, List<String> openidList) throws WxErrorException {
Map<String, Object> map = new HashMap<>(2);
Expand Down

0 comments on commit 286596f

Please sign in to comment.