@@ -9,6 +9,10 @@ | |||
package me.chanjar.weixin.common.util.json; | |||
import java.util.List; | |||
import com.google.common.collect.Lists; | |||
import com.google.gson.JsonArray; | |||
import com.google.gson.JsonElement; | |||
import com.google.gson.JsonObject; | |||
@@ -112,4 +116,21 @@ public class GsonHelper { | |||
return r == null ? 0f : r; | |||
} | |||
public static Integer[] getIntArray(JsonObject o, String string) { | |||
JsonArray jsonArray = getAsJsonArray(o.getAsJsonArray(string)); | |||
if (jsonArray == null) { | |||
return null; | |||
} | |||
List<Integer> result = Lists.newArrayList(); | |||
for (int i = 0; i < jsonArray.size(); i++) { | |||
result.add(jsonArray.get(i).getAsInt()); | |||
} | |||
return result.toArray(new Integer[0]); | |||
} | |||
public static JsonArray getAsJsonArray(JsonElement element) { | |||
return element == null ? null : element.getAsJsonArray(); | |||
} | |||
} |
@@ -56,7 +56,8 @@ public class WxMpUserServiceImpl implements WxMpUserService { | |||
@Override | |||
public List<WxMpUser> userInfoList(WxMpUserQuery userQuery) throws WxErrorException { | |||
String url = API_URL_PREFIX + "/info/batchget"; | |||
String responseContent = this.wxMpService.execute(new SimplePostRequestExecutor(), url, userQuery.toJsonString()); | |||
String responseContent = this.wxMpService.post(url, | |||
userQuery.toJsonString()); | |||
return WxMpUser.fromJsonList(responseContent); | |||
} | |||
@@ -24,22 +24,22 @@ public class WxMpUserQuery { | |||
/** | |||
* 语言使用默认(zh_CN) | |||
* | |||
* @param openIdList | |||
* @param openids | |||
*/ | |||
public WxMpUserQuery(List<String> openIdList) { | |||
public WxMpUserQuery(List<String> openids) { | |||
super(); | |||
add(openIdList); | |||
add(openids); | |||
} | |||
/** | |||
* 添加OpenId列表,语言使用默认(zh_CN) | |||
* | |||
* @param openIdList | |||
* @param openids | |||
* @return {@link WxMpUserQuery} | |||
*/ | |||
public WxMpUserQuery add(List<String> openIdList) { | |||
for (String openId : openIdList) { | |||
this.add(openId); | |||
public WxMpUserQuery add(List<String> openids) { | |||
for (String openid : openids) { | |||
this.add(openid); | |||
} | |||
return this; | |||
} | |||
@@ -47,12 +47,12 @@ public class WxMpUserQuery { | |||
/** | |||
* 添加一个OpenId | |||
* | |||
* @param openId | |||
* @param openid | |||
* @param lang 国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语 | |||
* @return {@link WxMpUserQuery} | |||
*/ | |||
public WxMpUserQuery add(String openId, String lang) { | |||
this.queryParamList.add(new WxMpUserQueryParam(openId, lang)); | |||
public WxMpUserQuery add(String openid, String lang) { | |||
this.queryParamList.add(new WxMpUserQueryParam(openid, lang)); | |||
return this; | |||
} | |||
@@ -63,34 +63,34 @@ public class WxMpUserQuery { | |||
* 该方法默认lang = zh_CN | |||
* </pre> | |||
* | |||
* @param openId | |||
* @param openid | |||
* @return {@link WxMpUserQuery} | |||
*/ | |||
public WxMpUserQuery add(String openId) { | |||
this.queryParamList.add(new WxMpUserQueryParam(openId)); | |||
public WxMpUserQuery add(String openid) { | |||
this.queryParamList.add(new WxMpUserQueryParam(openid)); | |||
return this; | |||
} | |||
/** | |||
* 删除指定的OpenId,语言使用默认(zh_CN) | |||
* | |||
* @param openId | |||
* @param openid | |||
* @return {@link WxMpUserQuery} | |||
*/ | |||
public WxMpUserQuery remove(String openId) { | |||
this.queryParamList.remove(new WxMpUserQueryParam(openId)); | |||
public WxMpUserQuery remove(String openid) { | |||
this.queryParamList.remove(new WxMpUserQueryParam(openid)); | |||
return this; | |||
} | |||
/** | |||
* 删除指定的OpenId | |||
* | |||
* @param openId | |||
* @param openid | |||
* @param lang 国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语 | |||
* @return {@link WxMpUserQuery} | |||
*/ | |||
public WxMpUserQuery remove(String openId, String lang) { | |||
this.queryParamList.remove(new WxMpUserQueryParam(openId, lang)); | |||
public WxMpUserQuery remove(String openid, String lang) { | |||
this.queryParamList.remove(new WxMpUserQueryParam(openid, lang)); | |||
return this; | |||
} | |||
@@ -110,9 +110,6 @@ public class WxMpUserQuery { | |||
// 查询参数封装 | |||
public class WxMpUserQueryParam implements Serializable { | |||
/** | |||
* @fields serialVersionUID | |||
*/ | |||
private static final long serialVersionUID = -6863571795702385319L; | |||
private String openid; | |||
private String lang; | |||
@@ -4,6 +4,9 @@ import java.io.Serializable; | |||
import java.lang.reflect.Type; | |||
import java.util.List; | |||
import org.apache.commons.lang3.builder.ToStringBuilder; | |||
import org.apache.commons.lang3.builder.ToStringStyle; | |||
import com.google.gson.Gson; | |||
import com.google.gson.JsonObject; | |||
import com.google.gson.reflect.TypeToken; | |||
@@ -17,91 +20,111 @@ import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
*/ | |||
public class WxMpUser implements Serializable { | |||
/** | |||
* @fields serialVersionUID | |||
*/ | |||
private static final long serialVersionUID = 5788154322646488738L; | |||
protected Boolean subscribe; | |||
protected String openId; | |||
protected String nickname; | |||
protected String sex; | |||
protected String language; | |||
protected String city; | |||
protected String province; | |||
protected String country; | |||
protected String headImgUrl; | |||
protected Long subscribeTime; | |||
protected String unionId; | |||
protected Integer sexId; | |||
protected String remark; | |||
protected Integer groupId; | |||
private Boolean subscribe; | |||
private String openId; | |||
private String nickname; | |||
private String sex; | |||
private String language; | |||
private String city; | |||
private String province; | |||
private String country; | |||
private String headImgUrl; | |||
private Long subscribeTime; | |||
private String unionId; | |||
private Integer sexId; | |||
private String remark; | |||
private Integer groupId; | |||
private Integer[] tagIds; | |||
public Boolean getSubscribe() { | |||
return this.subscribe; | |||
} | |||
public Boolean isSubscribe() { | |||
return this.subscribe; | |||
} | |||
public void setSubscribe(Boolean subscribe) { | |||
this.subscribe = subscribe; | |||
} | |||
public String getOpenId() { | |||
return this.openId; | |||
} | |||
public void setOpenId(String openId) { | |||
this.openId = openId; | |||
} | |||
public String getNickname() { | |||
return this.nickname; | |||
} | |||
public void setNickname(String nickname) { | |||
this.nickname = nickname; | |||
} | |||
public String getSex() { | |||
return this.sex; | |||
} | |||
public void setSex(String sex) { | |||
this.sex = sex; | |||
} | |||
public String getLanguage() { | |||
return this.language; | |||
} | |||
public void setLanguage(String language) { | |||
this.language = language; | |||
} | |||
public String getCity() { | |||
return this.city; | |||
} | |||
public void setCity(String city) { | |||
this.city = city; | |||
} | |||
public String getProvince() { | |||
return this.province; | |||
} | |||
public void setProvince(String province) { | |||
this.province = province; | |||
} | |||
public String getCountry() { | |||
return this.country; | |||
} | |||
public void setCountry(String country) { | |||
this.country = country; | |||
} | |||
public String getHeadImgUrl() { | |||
return this.headImgUrl; | |||
} | |||
public void setHeadImgUrl(String headImgUrl) { | |||
this.headImgUrl = headImgUrl; | |||
} | |||
public Long getSubscribeTime() { | |||
return this.subscribeTime; | |||
} | |||
public void setSubscribeTime(Long subscribeTime) { | |||
this.subscribeTime = subscribeTime; | |||
} | |||
public String getUnionId() { | |||
return this.unionId; | |||
} | |||
public void setUnionId(String unionId) { | |||
this.unionId = unionId; | |||
} | |||
@@ -118,22 +141,34 @@ public class WxMpUser implements Serializable { | |||
public String getRemark() { | |||
return this.remark; | |||
} | |||
public void setRemark(String remark) { | |||
this.remark = remark; | |||
} | |||
public Integer getGroupId() { | |||
return this.groupId; | |||
} | |||
public void setGroupId(Integer groupId) { | |||
this.groupId = groupId; | |||
} | |||
public Integer[] getTagIds() { | |||
return this.tagIds; | |||
} | |||
public void setTagIds(Integer[] tagIds) { | |||
this.tagIds = tagIds; | |||
} | |||
public static WxMpUser fromJson(String json) { | |||
return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpUser.class); | |||
} | |||
public static List<WxMpUser> fromJsonList(String json) { | |||
Type collectionType = new TypeToken<List<WxMpUser>>() {}.getType(); | |||
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); | |||
@@ -141,20 +176,7 @@ public class WxMpUser implements Serializable { | |||
@Override | |||
public String toString() { | |||
return "WxMpUser{" + | |||
"subscribe=" + this.subscribe + | |||
", openId='" + this.openId + '\'' + | |||
", nickname='" + this.nickname + '\'' + | |||
", sex='" + this.sex + '\'' + | |||
", language='" + this.language + '\'' + | |||
", city='" + this.city + '\'' + | |||
", province='" + this.province + '\'' + | |||
", country='" + this.country + '\'' + | |||
", headImgUrl='" + this.headImgUrl + '\'' + | |||
", subscribeTime=" + this.subscribeTime + | |||
", unionId='" + this.unionId + '\'' + | |||
", remark='" + this.remark + '\'' + | |||
", groupId='" + this.groupId + '\'' + | |||
'}'; | |||
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
} | |||
} |
@@ -1,7 +1,3 @@ | |||
/** | |||
* Copyright(c) 2011-2016 by UCredit Inc. | |||
* All Rights Reserved | |||
*/ | |||
package me.chanjar.weixin.mp.bean.store; | |||
import org.apache.commons.lang3.builder.ToStringBuilder; | |||
@@ -8,12 +8,17 @@ | |||
*/ | |||
package me.chanjar.weixin.mp.util.json; | |||
import com.google.gson.*; | |||
import java.lang.reflect.Type; | |||
import com.google.gson.JsonDeserializationContext; | |||
import com.google.gson.JsonDeserializer; | |||
import com.google.gson.JsonElement; | |||
import com.google.gson.JsonObject; | |||
import com.google.gson.JsonParseException; | |||
import me.chanjar.weixin.common.util.json.GsonHelper; | |||
import me.chanjar.weixin.mp.bean.result.WxMpUser; | |||
import java.lang.reflect.Type; | |||
public class WxMpUserGsonAdapter implements JsonDeserializer<WxMpUser> { | |||
@Override | |||
@@ -36,6 +41,7 @@ public class WxMpUserGsonAdapter implements JsonDeserializer<WxMpUser> { | |||
Integer sexId = GsonHelper.getInteger(o, "sex"); | |||
wxMpUser.setRemark(GsonHelper.getString(o, "remark")); | |||
wxMpUser.setGroupId(GsonHelper.getInteger(o, "groupid")); | |||
wxMpUser.setTagIds(GsonHelper.getIntArray(o, "tagid_list")); | |||
wxMpUser.setSexId(sexId); | |||
if(new Integer(1).equals(sexId)) { | |||
wxMpUser.setSex("男"); | |||
@@ -1,7 +1,3 @@ | |||
/** | |||
* Copyright(c) 2011-2016 by UCredit Inc. | |||
* All Rights Reserved | |||
*/ | |||
package me.chanjar.weixin.mp.api; | |||
import org.apache.commons.lang3.builder.ToStringBuilder; | |||
@@ -1,7 +1,3 @@ | |||
/** | |||
* Copyright(c) 2011-2016 by UCredit Inc. | |||
* All Rights Reserved | |||
*/ | |||
package me.chanjar.weixin.mp.api.impl; | |||
import static org.junit.Assert.assertNotNull; | |||
@@ -1,10 +1,11 @@ | |||
package me.chanjar.weixin.mp.api.impl; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
import org.testng.Assert; | |||
import org.testng.annotations.BeforeTest; | |||
import org.testng.annotations.Guice; | |||
import org.testng.annotations.Test; | |||
@@ -28,34 +29,42 @@ import me.chanjar.weixin.mp.bean.result.WxMpUserList; | |||
public class WxMpUserServiceImplTest { | |||
@Inject | |||
protected WxMpServiceImpl wxService; | |||
private WxMpServiceImpl wxService; | |||
private WxXmlMpInMemoryConfigStorage configProvider; | |||
@BeforeTest | |||
public void setup() { | |||
this.configProvider = (WxXmlMpInMemoryConfigStorage) this.wxService | |||
.getWxMpConfigStorage(); | |||
} | |||
public void testUserUpdateRemark() throws WxErrorException { | |||
WxXmlMpInMemoryConfigStorage configProvider = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); | |||
this.wxService.getUserService().userUpdateRemark(configProvider.getOpenid(), "测试备注名"); | |||
this.wxService.getUserService() | |||
.userUpdateRemark(this.configProvider.getOpenid(), "测试备注名"); | |||
} | |||
public void testUserInfo() throws WxErrorException { | |||
WxXmlMpInMemoryConfigStorage configProvider = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); | |||
WxMpUser user = this.wxService.getUserService().userInfo(configProvider.getOpenid(), null); | |||
WxMpUser user = this.wxService.getUserService() | |||
.userInfo(this.configProvider.getOpenid(), null); | |||
Assert.assertNotNull(user); | |||
System.out.println(user); | |||
} | |||
public void testUserInfoList() throws WxErrorException { | |||
WxXmlMpInMemoryConfigStorage configProvider = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); | |||
List<String> openIdList = new ArrayList<>(); | |||
openIdList.add(configProvider.getOpenid()); | |||
List<WxMpUser> userList = this.wxService.getUserService().userInfoList(openIdList); | |||
List<String> openids = new ArrayList<>(); | |||
openids.add(this.configProvider.getOpenid()); | |||
List<WxMpUser> userList = this.wxService.getUserService() | |||
.userInfoList(openids); | |||
Assert.assertEquals(userList.size(), 1); | |||
System.out.println(userList); | |||
System.out.println(userList); | |||
} | |||
public void testUserInfoListByWxMpUserQuery() throws WxErrorException { | |||
WxXmlMpInMemoryConfigStorage configProvider = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); | |||
WxMpUserQuery query = new WxMpUserQuery(); | |||
query.add(configProvider.getOpenid(), "zh_CN"); | |||
List<WxMpUser> userList = this.wxService.getUserService().userInfoList(query); | |||
query.add(this.configProvider.getOpenid(), "zh_CN"); | |||
List<WxMpUser> userList = this.wxService.getUserService() | |||
.userInfoList(query); | |||
Assert.assertEquals(userList.size(), 1); | |||
System.out.println(userList); | |||
} | |||
@@ -68,6 +77,5 @@ public class WxMpUserServiceImplTest { | |||
Assert.assertFalse(wxMpUserList.getOpenIds().size() == -1); | |||
System.out.println(wxMpUserList); | |||
} | |||
} |