com.tmsps.ne4Weixin.api.UserAPI Maven / Gradle / Ivy
The newest version!
package com.tmsps.ne4Weixin.api;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tmsps.ne4Weixin.beans.UserInfo;
import com.tmsps.ne4Weixin.config.WxConfig;
import com.tmsps.ne4Weixin.utils.HttpClient;
/**
*
* @author zhangwei [email protected]
*/
public class UserAPI extends BaseAPI {
private static String GETUSERINFO = BaseURL.concat("/cgi-bin/user/info");
private static String GETUSERLIST = BaseURL.concat("/cgi-bin/user/get");
public UserAPI(WxConfig config) {
super(config);
}
/**
* 获取用户信息
* @param openId
* @return
*/
public UserInfo getUserInfo(String openId) {
Map params = new HashMap();
params.put("access_token", config.getAccessToken());
params.put("openid", openId);
params.put("lang", "zh_CN");
JSONObject result = JSON.parseObject(HttpClient.httpGet(GETUSERINFO, params));
return JSON.parseObject(result.toJSONString(), UserInfo.class);
}
/**
* 获取用户列表
* 相关文档 https://mp.weixin.qq.com/wiki/12/54773ff6da7b8bdc95b7d2667d84b1d4.html
*/
public JSONObject getUserList() {
return getUserList(null);
}
public JSONObject getUserList(String next_openid) {
Map params = new HashMap();
params.put("access_token", config.getAccessToken());
if (next_openid != null && "".equals(next_openid.trim())) {
params.put("next_openid", next_openid);
}
JSONObject result = JSON.parseObject(HttpClient.httpGet(GETUSERLIST, params));
log.debug(result.toString());
return result;
}
/**
* 获取平台的所有用户的 openid 列表
* @return
*/
public List getAllUserList() {
List end = new ArrayList();
int count = 0;
String next_openid = null;
do {
JSONObject userListObj = getUserList(next_openid);
count = userListObj.getIntValue("count");
next_openid = userListObj.getString("next_openid");
if (count > 0) {
JSONArray openids = userListObj.getJSONObject("data")
.getJSONArray("openid");
log.debug(openids.toJSONString());
openids.forEach(e -> {
end.add(e.toString());
});
}
} while (count >= 10000);
return end;
}
}