@@ -1,6 +1,9 @@ | |||
package me.chanjar.weixin.mp.api; | |||
import java.util.List; | |||
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.WxMpUserList; | |||
@@ -29,17 +32,37 @@ public interface WxMpUserService { | |||
* </pre> | |||
* | |||
* @param openid 用户openid | |||
* @param lang 语言,zh_CN 简体(默认),zh_TW 繁体,en 英语 | |||
* @param lang 语言,zh_CN 简体(默认),zh_TW 繁体,en 英语 | |||
*/ | |||
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> | |||
* 获取关注者列表 | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取关注者列表 | |||
* </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; | |||
import java.util.List; | |||
import com.google.gson.JsonObject; | |||
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.mp.api.WxMpService; | |||
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.WxMpUserList; | |||
@@ -45,4 +48,16 @@ public class WxMpUserServiceImpl implements WxMpUserService { | |||
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,8 +1,14 @@ | |||
package me.chanjar.weixin.mp.bean.result; | |||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
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; | |||
/** | |||
* 微信用户信息 | |||
@@ -11,6 +17,10 @@ import java.io.Serializable; | |||
*/ | |||
public class WxMpUser implements Serializable { | |||
/** | |||
* @fields serialVersionUID | |||
*/ | |||
private static final long serialVersionUID = 5788154322646488738L; | |||
protected Boolean subscribe; | |||
protected String openId; | |||
protected String nickname; | |||
@@ -122,6 +132,13 @@ public class WxMpUser implements Serializable { | |||
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 | |||
public String toString() { | |||
return "WxMpUser{" + | |||