mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-08-24 16:18:51 +08:00
添加批量查询用户基本信息功能,没有测试,没有添加超过100验证
This commit is contained in:
parent
af9bfa2156
commit
5525c4fa27
@ -1,6 +1,9 @@
|
|||||||
package me.chanjar.weixin.mp.api;
|
package me.chanjar.weixin.mp.api;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||||
|
import me.chanjar.weixin.mp.bean.WxMpUserQuery;
|
||||||
import me.chanjar.weixin.mp.bean.result.WxMpUser;
|
import me.chanjar.weixin.mp.bean.result.WxMpUser;
|
||||||
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
|
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
|
||||||
|
|
||||||
@ -33,13 +36,33 @@ public interface WxMpUserService {
|
|||||||
*/
|
*/
|
||||||
WxMpUser userInfo(String openid, String lang) throws WxErrorException;
|
WxMpUser userInfo(String openid, String lang) throws WxErrorException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* 获取用户基本信息列表
|
||||||
|
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=批量获取用户基本信息
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param openid 用户openid, lang 使用默认(zh_CN 简体)
|
||||||
|
*/
|
||||||
|
List<WxMpUser> userInfoList(List<String> openidList) throws WxErrorException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* 获取用户基本信息列表
|
||||||
|
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=批量获取用户基本信息
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param userQuery 详细查询参数
|
||||||
|
*/
|
||||||
|
List<WxMpUser> userInfoList(WxMpUserQuery userQuery) throws WxErrorException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
* 获取关注者列表
|
* 获取关注者列表
|
||||||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取关注者列表
|
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取关注者列表
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param next_openid 可选,第一个拉取的OPENID,null为从头开始拉取
|
* @param nextOpenid 可选,第一个拉取的OPENID,null为从头开始拉取
|
||||||
*/
|
*/
|
||||||
WxMpUserList userList(String next_openid) throws WxErrorException;
|
WxMpUserList userList(String nextOpenid) throws WxErrorException;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package me.chanjar.weixin.mp.api.impl;
|
package me.chanjar.weixin.mp.api.impl;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||||
@ -7,6 +9,7 @@ import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
|||||||
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
|
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
|
||||||
import me.chanjar.weixin.mp.api.WxMpService;
|
import me.chanjar.weixin.mp.api.WxMpService;
|
||||||
import me.chanjar.weixin.mp.api.WxMpUserService;
|
import me.chanjar.weixin.mp.api.WxMpUserService;
|
||||||
|
import me.chanjar.weixin.mp.bean.WxMpUserQuery;
|
||||||
import me.chanjar.weixin.mp.bean.result.WxMpUser;
|
import me.chanjar.weixin.mp.bean.result.WxMpUser;
|
||||||
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
|
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
|
||||||
|
|
||||||
@ -45,4 +48,16 @@ public class WxMpUserServiceImpl implements WxMpUserService {
|
|||||||
return WxMpUserList.fromJson(responseContent);
|
return WxMpUserList.fromJson(responseContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<WxMpUser> userInfoList(List<String> openidList) throws WxErrorException {
|
||||||
|
return userInfoList(new WxMpUserQuery(openidList));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<WxMpUser> userInfoList(WxMpUserQuery userQuery) throws WxErrorException {
|
||||||
|
String url = API_URL_PREFIX + "/info/batchget";
|
||||||
|
String responseContent = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, userQuery.toJsonString());
|
||||||
|
return WxMpUser.fromJsonList(responseContent);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,194 @@
|
|||||||
|
package me.chanjar.weixin.mp.bean;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量查询用户信息查询参数 <br>
|
||||||
|
* Created by LiuJunGuang on 2016/8/31.
|
||||||
|
*
|
||||||
|
* @author LiuJunGuang
|
||||||
|
*/
|
||||||
|
public class WxMpUserQuery {
|
||||||
|
private List<WxMpUserQueryParam> queryParamList = new ArrayList<>();
|
||||||
|
|
||||||
|
public WxMpUserQuery() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 语言使用默认(zh_CN)
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
* @param openIdList
|
||||||
|
*/
|
||||||
|
public WxMpUserQuery(List<String> openIdList) {
|
||||||
|
super();
|
||||||
|
add(openIdList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加OpenId列表,语言使用默认(zh_CN)
|
||||||
|
*
|
||||||
|
* @param openIdList
|
||||||
|
* @return {@link WxMpUserQuery}
|
||||||
|
*/
|
||||||
|
public WxMpUserQuery add(List<String> openIdList) {
|
||||||
|
for (String openId : openIdList) {
|
||||||
|
this.add(openId);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加一个OpenId
|
||||||
|
*
|
||||||
|
* @param openId
|
||||||
|
* @param lang 国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语
|
||||||
|
* @return {@link WxMpUserQuery}
|
||||||
|
*/
|
||||||
|
public WxMpUserQuery add(String openId, String lang) {
|
||||||
|
queryParamList.add(new WxMpUserQueryParam(openId, lang));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加一个OpenId到列表中,并返回本对象
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* 该方法默认lang = zh_CN
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param openId
|
||||||
|
* @return {@link WxMpUserQuery}
|
||||||
|
*/
|
||||||
|
public WxMpUserQuery add(String openId) {
|
||||||
|
queryParamList.add(new WxMpUserQueryParam(openId));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除指定的OpenId,语言使用默认(zh_CN)
|
||||||
|
*
|
||||||
|
* @param openId
|
||||||
|
* @return {@link WxMpUserQuery}
|
||||||
|
*/
|
||||||
|
public WxMpUserQuery remove(String openId) {
|
||||||
|
queryParamList.remove(new WxMpUserQueryParam(openId));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除指定的OpenId
|
||||||
|
*
|
||||||
|
* @param openId
|
||||||
|
* @param lang 国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语
|
||||||
|
* @return {@link WxMpUserQuery}
|
||||||
|
*/
|
||||||
|
public WxMpUserQuery remove(String openId, String lang) {
|
||||||
|
queryParamList.remove(new WxMpUserQueryParam(openId, lang));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取查询参数列表
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<WxMpUserQueryParam> getQueryParamList() {
|
||||||
|
return queryParamList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toJsonString() {
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("user_list", queryParamList);
|
||||||
|
return new Gson().toJson(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询参数封装
|
||||||
|
public class WxMpUserQueryParam implements Serializable {
|
||||||
|
/**
|
||||||
|
* @fields serialVersionUID
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -6863571795702385319L;
|
||||||
|
private String openid;
|
||||||
|
private String lang;
|
||||||
|
|
||||||
|
public WxMpUserQueryParam(String openid, String lang) {
|
||||||
|
super();
|
||||||
|
this.openid = openid;
|
||||||
|
this.lang = lang;
|
||||||
|
}
|
||||||
|
|
||||||
|
public WxMpUserQueryParam(String openid) {
|
||||||
|
super();
|
||||||
|
this.openid = openid;
|
||||||
|
this.lang = "zh_CN";
|
||||||
|
}
|
||||||
|
|
||||||
|
public WxMpUserQueryParam() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOpenid() {
|
||||||
|
return openid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOpenid(String openid) {
|
||||||
|
this.openid = openid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLang() {
|
||||||
|
return lang;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLang(String lang) {
|
||||||
|
this.lang = lang;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + getOuterType().hashCode();
|
||||||
|
result = prime * result + ((lang == null) ? 0 : lang.hashCode());
|
||||||
|
result = prime * result + ((openid == null) ? 0 : openid.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
WxMpUserQueryParam other = (WxMpUserQueryParam) obj;
|
||||||
|
if (!getOuterType().equals(other.getOuterType()))
|
||||||
|
return false;
|
||||||
|
if (lang == null) {
|
||||||
|
if (other.lang != null)
|
||||||
|
return false;
|
||||||
|
} else if (!lang.equals(other.lang))
|
||||||
|
return false;
|
||||||
|
if (openid == null) {
|
||||||
|
if (other.openid != null)
|
||||||
|
return false;
|
||||||
|
} else if (!openid.equals(other.openid))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private WxMpUserQuery getOuterType() {
|
||||||
|
return WxMpUserQuery.this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,16 +1,27 @@
|
|||||||
package me.chanjar.weixin.mp.bean.result;
|
package me.chanjar.weixin.mp.bean.result;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信用户信息
|
* 微信用户信息
|
||||||
|
*
|
||||||
* @author chanjarster
|
* @author chanjarster
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class WxMpUser implements Serializable {
|
public class WxMpUser implements Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @fields serialVersionUID
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 5788154322646488738L;
|
||||||
protected Boolean subscribe;
|
protected Boolean subscribe;
|
||||||
protected String openId;
|
protected String openId;
|
||||||
protected String nickname;
|
protected String nickname;
|
||||||
@ -29,69 +40,91 @@ public class WxMpUser implements Serializable {
|
|||||||
public Boolean getSubscribe() {
|
public Boolean getSubscribe() {
|
||||||
return subscribe;
|
return subscribe;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean isSubscribe() {
|
public Boolean isSubscribe() {
|
||||||
return subscribe;
|
return subscribe;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSubscribe(Boolean subscribe) {
|
public void setSubscribe(Boolean subscribe) {
|
||||||
this.subscribe = subscribe;
|
this.subscribe = subscribe;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getOpenId() {
|
public String getOpenId() {
|
||||||
return openId;
|
return openId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOpenId(String openId) {
|
public void setOpenId(String openId) {
|
||||||
this.openId = openId;
|
this.openId = openId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getNickname() {
|
public String getNickname() {
|
||||||
return nickname;
|
return nickname;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNickname(String nickname) {
|
public void setNickname(String nickname) {
|
||||||
this.nickname = nickname;
|
this.nickname = nickname;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSex() {
|
public String getSex() {
|
||||||
return sex;
|
return sex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSex(String sex) {
|
public void setSex(String sex) {
|
||||||
this.sex = sex;
|
this.sex = sex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLanguage() {
|
public String getLanguage() {
|
||||||
return language;
|
return language;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLanguage(String language) {
|
public void setLanguage(String language) {
|
||||||
this.language = language;
|
this.language = language;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCity() {
|
public String getCity() {
|
||||||
return city;
|
return city;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCity(String city) {
|
public void setCity(String city) {
|
||||||
this.city = city;
|
this.city = city;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getProvince() {
|
public String getProvince() {
|
||||||
return province;
|
return province;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setProvince(String province) {
|
public void setProvince(String province) {
|
||||||
this.province = province;
|
this.province = province;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCountry() {
|
public String getCountry() {
|
||||||
return country;
|
return country;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCountry(String country) {
|
public void setCountry(String country) {
|
||||||
this.country = country;
|
this.country = country;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHeadImgUrl() {
|
public String getHeadImgUrl() {
|
||||||
return headImgUrl;
|
return headImgUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHeadImgUrl(String headImgUrl) {
|
public void setHeadImgUrl(String headImgUrl) {
|
||||||
this.headImgUrl = headImgUrl;
|
this.headImgUrl = headImgUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getSubscribeTime() {
|
public Long getSubscribeTime() {
|
||||||
return subscribeTime;
|
return subscribeTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSubscribeTime(Long subscribeTime) {
|
public void setSubscribeTime(Long subscribeTime) {
|
||||||
this.subscribeTime = subscribeTime;
|
this.subscribeTime = subscribeTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUnionId() {
|
public String getUnionId() {
|
||||||
return unionId;
|
return unionId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUnionId(String unionId) {
|
public void setUnionId(String unionId) {
|
||||||
this.unionId = unionId;
|
this.unionId = unionId;
|
||||||
}
|
}
|
||||||
@ -108,12 +141,15 @@ public class WxMpUser implements Serializable {
|
|||||||
public String getRemark() {
|
public String getRemark() {
|
||||||
return remark;
|
return remark;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRemark(String remark) {
|
public void setRemark(String remark) {
|
||||||
this.remark = remark;
|
this.remark = remark;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getGroupId() {
|
public Integer getGroupId() {
|
||||||
return groupId;
|
return groupId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setGroupId(Integer groupId) {
|
public void setGroupId(Integer groupId) {
|
||||||
this.groupId = groupId;
|
this.groupId = groupId;
|
||||||
}
|
}
|
||||||
@ -122,22 +158,18 @@ public class WxMpUser implements Serializable {
|
|||||||
return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpUser.class);
|
return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpUser.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<WxMpUser> fromJsonList(String json) {
|
||||||
|
Type collectionType = new TypeToken<List<WxMpUser>>() {
|
||||||
|
}.getType();
|
||||||
|
Gson gson = WxMpGsonBuilder.INSTANCE.create();
|
||||||
|
JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
|
||||||
|
return gson.fromJson(jsonObject.get("user_info_list"), collectionType);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "WxMpUser{" +
|
return "WxMpUser{" + "subscribe=" + subscribe + ", openId='" + openId + '\'' + ", nickname='" + nickname + '\'' + ", sex='" + sex + '\'' + ", language='" + language + '\''
|
||||||
"subscribe=" + subscribe +
|
+ ", city='" + city + '\'' + ", province='" + province + '\'' + ", country='" + country + '\'' + ", headImgUrl='" + headImgUrl + '\'' + ", subscribeTime=" + subscribeTime
|
||||||
", openId='" + openId + '\'' +
|
+ ", unionId='" + unionId + '\'' + ", remark='" + remark + '\'' + ", groupId='" + groupId + '\'' + '}';
|
||||||
", nickname='" + nickname + '\'' +
|
|
||||||
", sex='" + sex + '\'' +
|
|
||||||
", language='" + language + '\'' +
|
|
||||||
", city='" + city + '\'' +
|
|
||||||
", province='" + province + '\'' +
|
|
||||||
", country='" + country + '\'' +
|
|
||||||
", headImgUrl='" + headImgUrl + '\'' +
|
|
||||||
", subscribeTime=" + subscribeTime +
|
|
||||||
", unionId='" + unionId + '\'' +
|
|
||||||
", remark='" + remark + '\'' +
|
|
||||||
", groupId='" + groupId + '\'' +
|
|
||||||
'}';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user