用户信息添加新增的tagid_list属性

This commit is contained in:
BinaryWang
2016-09-28 13:19:36 +08:00
parent aabdc8e4b0
commit 742c74d850
9 changed files with 131 additions and 88 deletions

View File

@@ -9,6 +9,10 @@
package me.chanjar.weixin.common.util.json; 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.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@@ -112,4 +116,21 @@ public class GsonHelper {
return r == null ? 0f : r; 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();
}
} }

View File

@@ -56,7 +56,8 @@ public class WxMpUserServiceImpl implements WxMpUserService {
@Override @Override
public List<WxMpUser> userInfoList(WxMpUserQuery userQuery) throws WxErrorException { public List<WxMpUser> userInfoList(WxMpUserQuery userQuery) throws WxErrorException {
String url = API_URL_PREFIX + "/info/batchget"; 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); return WxMpUser.fromJsonList(responseContent);
} }

View File

@@ -24,22 +24,22 @@ public class WxMpUserQuery {
/** /**
* 语言使用默认(zh_CN) * 语言使用默认(zh_CN)
* *
* @param openIdList * @param openids
*/ */
public WxMpUserQuery(List<String> openIdList) { public WxMpUserQuery(List<String> openids) {
super(); super();
add(openIdList); add(openids);
} }
/** /**
* 添加OpenId列表语言使用默认(zh_CN) * 添加OpenId列表语言使用默认(zh_CN)
* *
* @param openIdList * @param openids
* @return {@link WxMpUserQuery} * @return {@link WxMpUserQuery}
*/ */
public WxMpUserQuery add(List<String> openIdList) { public WxMpUserQuery add(List<String> openids) {
for (String openId : openIdList) { for (String openid : openids) {
this.add(openId); this.add(openid);
} }
return this; return this;
} }
@@ -47,12 +47,12 @@ public class WxMpUserQuery {
/** /**
* 添加一个OpenId * 添加一个OpenId
* *
* @param openId * @param openid
* @param lang 国家地区语言版本zh_CN 简体zh_TW 繁体en 英语 * @param lang 国家地区语言版本zh_CN 简体zh_TW 繁体en 英语
* @return {@link WxMpUserQuery} * @return {@link WxMpUserQuery}
*/ */
public WxMpUserQuery add(String openId, String lang) { public WxMpUserQuery add(String openid, String lang) {
this.queryParamList.add(new WxMpUserQueryParam(openId, lang)); this.queryParamList.add(new WxMpUserQueryParam(openid, lang));
return this; return this;
} }
@@ -63,34 +63,34 @@ public class WxMpUserQuery {
* 该方法默认lang = zh_CN * 该方法默认lang = zh_CN
* </pre> * </pre>
* *
* @param openId * @param openid
* @return {@link WxMpUserQuery} * @return {@link WxMpUserQuery}
*/ */
public WxMpUserQuery add(String openId) { public WxMpUserQuery add(String openid) {
this.queryParamList.add(new WxMpUserQueryParam(openId)); this.queryParamList.add(new WxMpUserQueryParam(openid));
return this; return this;
} }
/** /**
* 删除指定的OpenId语言使用默认(zh_CN) * 删除指定的OpenId语言使用默认(zh_CN)
* *
* @param openId * @param openid
* @return {@link WxMpUserQuery} * @return {@link WxMpUserQuery}
*/ */
public WxMpUserQuery remove(String openId) { public WxMpUserQuery remove(String openid) {
this.queryParamList.remove(new WxMpUserQueryParam(openId)); this.queryParamList.remove(new WxMpUserQueryParam(openid));
return this; return this;
} }
/** /**
* 删除指定的OpenId * 删除指定的OpenId
* *
* @param openId * @param openid
* @param lang 国家地区语言版本zh_CN 简体zh_TW 繁体en 英语 * @param lang 国家地区语言版本zh_CN 简体zh_TW 繁体en 英语
* @return {@link WxMpUserQuery} * @return {@link WxMpUserQuery}
*/ */
public WxMpUserQuery remove(String openId, String lang) { public WxMpUserQuery remove(String openid, String lang) {
this.queryParamList.remove(new WxMpUserQueryParam(openId, lang)); this.queryParamList.remove(new WxMpUserQueryParam(openid, lang));
return this; return this;
} }
@@ -110,9 +110,6 @@ public class WxMpUserQuery {
// 查询参数封装 // 查询参数封装
public class WxMpUserQueryParam implements Serializable { public class WxMpUserQueryParam implements Serializable {
/**
* @fields serialVersionUID
*/
private static final long serialVersionUID = -6863571795702385319L; private static final long serialVersionUID = -6863571795702385319L;
private String openid; private String openid;
private String lang; private String lang;

View File

@@ -4,6 +4,9 @@ import java.io.Serializable;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.List; 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.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
@@ -17,91 +20,111 @@ import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
*/ */
public class WxMpUser implements Serializable { public class WxMpUser implements Serializable {
/**
* @fields serialVersionUID
*/
private static final long serialVersionUID = 5788154322646488738L; private static final long serialVersionUID = 5788154322646488738L;
protected Boolean subscribe; private Boolean subscribe;
protected String openId; private String openId;
protected String nickname; private String nickname;
protected String sex; private String sex;
protected String language; private String language;
protected String city; private String city;
protected String province; private String province;
protected String country; private String country;
protected String headImgUrl; private String headImgUrl;
protected Long subscribeTime; private Long subscribeTime;
protected String unionId; private String unionId;
protected Integer sexId; private Integer sexId;
protected String remark; private String remark;
protected Integer groupId; private Integer groupId;
private Integer[] tagIds;
public Boolean getSubscribe() { public Boolean getSubscribe() {
return this.subscribe; return this.subscribe;
} }
public Boolean isSubscribe() { public Boolean isSubscribe() {
return this.subscribe; return this.subscribe;
} }
public void setSubscribe(Boolean subscribe) { public void setSubscribe(Boolean subscribe) {
this.subscribe = subscribe; this.subscribe = subscribe;
} }
public String getOpenId() { public String getOpenId() {
return this.openId; return this.openId;
} }
public void setOpenId(String openId) { public void setOpenId(String openId) {
this.openId = openId; this.openId = openId;
} }
public String getNickname() { public String getNickname() {
return this.nickname; return this.nickname;
} }
public void setNickname(String nickname) { public void setNickname(String nickname) {
this.nickname = nickname; this.nickname = nickname;
} }
public String getSex() { public String getSex() {
return this.sex; return this.sex;
} }
public void setSex(String sex) { public void setSex(String sex) {
this.sex = sex; this.sex = sex;
} }
public String getLanguage() { public String getLanguage() {
return this.language; return this.language;
} }
public void setLanguage(String language) { public void setLanguage(String language) {
this.language = language; this.language = language;
} }
public String getCity() { public String getCity() {
return this.city; return this.city;
} }
public void setCity(String city) { public void setCity(String city) {
this.city = city; this.city = city;
} }
public String getProvince() { public String getProvince() {
return this.province; return this.province;
} }
public void setProvince(String province) { public void setProvince(String province) {
this.province = province; this.province = province;
} }
public String getCountry() { public String getCountry() {
return this.country; return this.country;
} }
public void setCountry(String country) { public void setCountry(String country) {
this.country = country; this.country = country;
} }
public String getHeadImgUrl() { public String getHeadImgUrl() {
return this.headImgUrl; return this.headImgUrl;
} }
public void setHeadImgUrl(String headImgUrl) { public void setHeadImgUrl(String headImgUrl) {
this.headImgUrl = headImgUrl; this.headImgUrl = headImgUrl;
} }
public Long getSubscribeTime() { public Long getSubscribeTime() {
return this.subscribeTime; return this.subscribeTime;
} }
public void setSubscribeTime(Long subscribeTime) { public void setSubscribeTime(Long subscribeTime) {
this.subscribeTime = subscribeTime; this.subscribeTime = subscribeTime;
} }
public String getUnionId() { public String getUnionId() {
return this.unionId; return this.unionId;
} }
public void setUnionId(String unionId) { public void setUnionId(String unionId) {
this.unionId = unionId; this.unionId = unionId;
} }
@@ -118,22 +141,34 @@ public class WxMpUser implements Serializable {
public String getRemark() { public String getRemark() {
return this.remark; return this.remark;
} }
public void setRemark(String remark) { public void setRemark(String remark) {
this.remark = remark; this.remark = remark;
} }
public Integer getGroupId() { public Integer getGroupId() {
return this.groupId; return this.groupId;
} }
public void setGroupId(Integer groupId) { public void setGroupId(Integer groupId) {
this.groupId = groupId; this.groupId = groupId;
} }
public Integer[] getTagIds() {
return this.tagIds;
}
public void setTagIds(Integer[] tagIds) {
this.tagIds = tagIds;
}
public static WxMpUser fromJson(String json) { public static WxMpUser fromJson(String json) {
return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpUser.class); return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpUser.class);
} }
public static List<WxMpUser> fromJsonList(String json) { 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(); Gson gson = WxMpGsonBuilder.INSTANCE.create();
JsonObject jsonObject = gson.fromJson(json, JsonObject.class); JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
return gson.fromJson(jsonObject.get("user_info_list"), collectionType); return gson.fromJson(jsonObject.get("user_info_list"), collectionType);
@@ -141,20 +176,7 @@ public class WxMpUser implements Serializable {
@Override @Override
public String toString() { public String toString() {
return "WxMpUser{" + return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
"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 + '\'' +
'}';
} }
} }

View File

@@ -1,7 +1,3 @@
/**
* Copyright(c) 2011-2016 by UCredit Inc.
* All Rights Reserved
*/
package me.chanjar.weixin.mp.bean.store; package me.chanjar.weixin.mp.bean.store;
import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringBuilder;

View File

@@ -8,12 +8,17 @@
*/ */
package me.chanjar.weixin.mp.util.json; 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.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.result.WxMpUser; import me.chanjar.weixin.mp.bean.result.WxMpUser;
import java.lang.reflect.Type;
public class WxMpUserGsonAdapter implements JsonDeserializer<WxMpUser> { public class WxMpUserGsonAdapter implements JsonDeserializer<WxMpUser> {
@Override @Override
@@ -36,6 +41,7 @@ public class WxMpUserGsonAdapter implements JsonDeserializer<WxMpUser> {
Integer sexId = GsonHelper.getInteger(o, "sex"); Integer sexId = GsonHelper.getInteger(o, "sex");
wxMpUser.setRemark(GsonHelper.getString(o, "remark")); wxMpUser.setRemark(GsonHelper.getString(o, "remark"));
wxMpUser.setGroupId(GsonHelper.getInteger(o, "groupid")); wxMpUser.setGroupId(GsonHelper.getInteger(o, "groupid"));
wxMpUser.setTagIds(GsonHelper.getIntArray(o, "tagid_list"));
wxMpUser.setSexId(sexId); wxMpUser.setSexId(sexId);
if(new Integer(1).equals(sexId)) { if(new Integer(1).equals(sexId)) {
wxMpUser.setSex(""); wxMpUser.setSex("");

View File

@@ -1,7 +1,3 @@
/**
* Copyright(c) 2011-2016 by UCredit Inc.
* All Rights Reserved
*/
package me.chanjar.weixin.mp.api; package me.chanjar.weixin.mp.api;
import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringBuilder;

View File

@@ -1,7 +1,3 @@
/**
* Copyright(c) 2011-2016 by UCredit Inc.
* All Rights Reserved
*/
package me.chanjar.weixin.mp.api.impl; package me.chanjar.weixin.mp.api.impl;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;

View File

@@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Guice; import org.testng.annotations.Guice;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@@ -28,34 +29,42 @@ import me.chanjar.weixin.mp.bean.result.WxMpUserList;
public class WxMpUserServiceImplTest { public class WxMpUserServiceImplTest {
@Inject @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 { public void testUserUpdateRemark() throws WxErrorException {
WxXmlMpInMemoryConfigStorage configProvider = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); this.wxService.getUserService()
this.wxService.getUserService().userUpdateRemark(configProvider.getOpenid(), "测试备注名"); .userUpdateRemark(this.configProvider.getOpenid(), "测试备注名");
} }
public void testUserInfo() throws WxErrorException { public void testUserInfo() throws WxErrorException {
WxXmlMpInMemoryConfigStorage configProvider = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); WxMpUser user = this.wxService.getUserService()
WxMpUser user = this.wxService.getUserService().userInfo(configProvider.getOpenid(), null); .userInfo(this.configProvider.getOpenid(), null);
Assert.assertNotNull(user); Assert.assertNotNull(user);
System.out.println(user); System.out.println(user);
} }
public void testUserInfoList() throws WxErrorException { public void testUserInfoList() throws WxErrorException {
WxXmlMpInMemoryConfigStorage configProvider = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); List<String> openids = new ArrayList<>();
List<String> openIdList = new ArrayList<>(); openids.add(this.configProvider.getOpenid());
openIdList.add(configProvider.getOpenid()); List<WxMpUser> userList = this.wxService.getUserService()
List<WxMpUser> userList = this.wxService.getUserService().userInfoList(openIdList); .userInfoList(openids);
Assert.assertEquals(userList.size(), 1); Assert.assertEquals(userList.size(), 1);
System.out.println(userList); System.out.println(userList);
} }
public void testUserInfoListByWxMpUserQuery() throws WxErrorException { public void testUserInfoListByWxMpUserQuery() throws WxErrorException {
WxXmlMpInMemoryConfigStorage configProvider = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage();
WxMpUserQuery query = new WxMpUserQuery(); WxMpUserQuery query = new WxMpUserQuery();
query.add(configProvider.getOpenid(), "zh_CN"); query.add(this.configProvider.getOpenid(), "zh_CN");
List<WxMpUser> userList = this.wxService.getUserService().userInfoList(query); List<WxMpUser> userList = this.wxService.getUserService()
.userInfoList(query);
Assert.assertEquals(userList.size(), 1); Assert.assertEquals(userList.size(), 1);
System.out.println(userList); System.out.println(userList);
} }
@@ -69,5 +78,4 @@ public class WxMpUserServiceImplTest {
System.out.println(wxMpUserList); System.out.println(wxMpUserList);
} }
} }