diff --git a/src/main/java/chanjarster/weixin/api/WxService.java b/src/main/java/chanjarster/weixin/api/WxService.java index c4900afe1..5d2dcf67b 100644 --- a/src/main/java/chanjarster/weixin/api/WxService.java +++ b/src/main/java/chanjarster/weixin/api/WxService.java @@ -16,6 +16,7 @@ import chanjarster.weixin.bean.result.WxMassSendResult; import chanjarster.weixin.bean.result.WxMassUploadResult; import chanjarster.weixin.bean.result.WxMediaUploadResult; import chanjarster.weixin.bean.result.WxUser; +import chanjarster.weixin.bean.result.WxUserList; import chanjarster.weixin.exception.WxErrorException; /** @@ -258,6 +259,17 @@ public interface WxService { */ public WxUser userInfo(String openid, String lang) throws WxErrorException; + /** + *
+   * 获取关注者列表
+   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取关注者列表
+   * 
+ * @param next_openid 可选,第一个拉取的OPENID,null为从头开始拉取 + * @return + * @throws WxErrorException + */ + public WxUserList userList(String next_openid) throws WxErrorException; + /** * 注入 {@link WxConfigStorage} 的实现 * @param wxConfigProvider diff --git a/src/main/java/chanjarster/weixin/api/WxServiceImpl.java b/src/main/java/chanjarster/weixin/api/WxServiceImpl.java index e1d2abb0c..7f4d89162 100644 --- a/src/main/java/chanjarster/weixin/api/WxServiceImpl.java +++ b/src/main/java/chanjarster/weixin/api/WxServiceImpl.java @@ -32,6 +32,7 @@ import chanjarster.weixin.bean.result.WxMassSendResult; import chanjarster.weixin.bean.result.WxMassUploadResult; import chanjarster.weixin.bean.result.WxMediaUploadResult; import chanjarster.weixin.bean.result.WxUser; +import chanjarster.weixin.bean.result.WxUserList; import chanjarster.weixin.exception.WxErrorException; import chanjarster.weixin.util.fs.FileUtil; import chanjarster.weixin.util.http.MediaDownloadRequestExecutor; @@ -42,7 +43,6 @@ import chanjarster.weixin.util.http.SimplePostRequestExecutor; import chanjarster.weixin.util.json.GsonHelper; import chanjarster.weixin.util.json.WxGsonBuilder; -import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.internal.Streams; @@ -254,7 +254,13 @@ public class WxServiceImpl implements WxService { String url = "https://api.weixin.qq.com/cgi-bin/user/info"; lang = lang == null ? "zh_CN" : lang; String responseContent = execute(new SimpleGetRequestExecutor(), url, "openid=" + openid + "&lang=" + lang); - ;return WxUser.fromJson(responseContent); + return WxUser.fromJson(responseContent); + } + + public WxUserList userList(String next_openid) throws WxErrorException { + String url = "https://api.weixin.qq.com/cgi-bin/user/get"; + String responseContent = execute(new SimpleGetRequestExecutor(), url, next_openid == null ? null : "next_openid=" + next_openid); + return WxUserList.fromJson(responseContent); } /** diff --git a/src/main/java/chanjarster/weixin/bean/result/WxUserList.java b/src/main/java/chanjarster/weixin/bean/result/WxUserList.java new file mode 100644 index 000000000..9232e77e8 --- /dev/null +++ b/src/main/java/chanjarster/weixin/bean/result/WxUserList.java @@ -0,0 +1,48 @@ +package chanjarster.weixin.bean.result; + +import java.util.ArrayList; +import java.util.List; + +import chanjarster.weixin.util.json.WxGsonBuilder; + + +/** + * 关注者列表 + * @author chanjarster + * + */ +public class WxUserList { + + protected int total = -1; + protected int count = -1; + protected List openids = new ArrayList(); + protected String next_openid; + public int getTotal() { + return total; + } + public void setTotal(int total) { + this.total = total; + } + public int getCount() { + return count; + } + public void setCount(int count) { + this.count = count; + } + public List getOpenids() { + return openids; + } + public void setOpenids(List openids) { + this.openids = openids; + } + public String getNext_openid() { + return next_openid; + } + public void setNext_openid(String next_openid) { + this.next_openid = next_openid; + } + + public static WxUserList fromJson(String json) { + return WxGsonBuilder.INSTANCE.create().fromJson(json, WxUserList.class); + } +} diff --git a/src/main/java/chanjarster/weixin/util/json/WxGsonBuilder.java b/src/main/java/chanjarster/weixin/util/json/WxGsonBuilder.java index a46823fd0..7040d00f0 100644 --- a/src/main/java/chanjarster/weixin/util/json/WxGsonBuilder.java +++ b/src/main/java/chanjarster/weixin/util/json/WxGsonBuilder.java @@ -7,6 +7,7 @@ import chanjarster.weixin.bean.WxMassNews; import chanjarster.weixin.bean.WxMassOpenIdsMessage; import chanjarster.weixin.bean.WxMenu; import chanjarster.weixin.bean.result.WxUser; +import chanjarster.weixin.bean.result.WxUserList; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -24,6 +25,7 @@ public class WxGsonBuilder { INSTANCE.registerTypeAdapter(WxMassOpenIdsMessage.class, new WxMassOpenIdsMessageGsonAdapter()); INSTANCE.registerTypeAdapter(WxGroup.class, new WxGroupGsonAdapter()); INSTANCE.registerTypeAdapter(WxUser.class, new WxUserGsonAdapter()); + INSTANCE.registerTypeAdapter(WxUserList.class, new WxUserListGsonAdapter()); } public static Gson create() { diff --git a/src/main/java/chanjarster/weixin/util/json/WxUserListGsonAdapter.java b/src/main/java/chanjarster/weixin/util/json/WxUserListGsonAdapter.java new file mode 100644 index 000000000..7fdbb9308 --- /dev/null +++ b/src/main/java/chanjarster/weixin/util/json/WxUserListGsonAdapter.java @@ -0,0 +1,42 @@ +/* + * KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved. + * + * This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended + * only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction + * arose from modification of the original source, or other redistribution of this source + * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. + */ +package chanjarster.weixin.util.json; + +import java.lang.reflect.Type; + +import chanjarster.weixin.bean.result.WxUserList; + +import com.google.gson.JsonArray; +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; + +/** + * + * @author qianjia + * + */ +public class WxUserListGsonAdapter implements JsonDeserializer { + + public WxUserList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + JsonObject o = json.getAsJsonObject(); + WxUserList wxUserList = new WxUserList(); + wxUserList.setTotal(GsonHelper.getInteger(o, "total")); + wxUserList.setCount(GsonHelper.getInteger(o, "count")); + wxUserList.setNext_openid(GsonHelper.getString(o, "next_openid")); + JsonArray data = o.get("data").getAsJsonObject().get("openid").getAsJsonArray(); + for (int i = 0; i < data.size(); i++) { + wxUserList.getOpenids().add(GsonHelper.getAsString(data.get(i))); + } + return wxUserList; + } + +} \ No newline at end of file diff --git a/src/test/java/chanjarster/weixin/api/WxUserAPITest.java b/src/test/java/chanjarster/weixin/api/WxUserAPITest.java index c57335663..1381f158e 100644 --- a/src/test/java/chanjarster/weixin/api/WxUserAPITest.java +++ b/src/test/java/chanjarster/weixin/api/WxUserAPITest.java @@ -6,6 +6,7 @@ import org.testng.annotations.Test; import chanjarster.weixin.api.ApiTestModule.WxXmlConfigStorage; import chanjarster.weixin.bean.result.WxUser; +import chanjarster.weixin.bean.result.WxUserList; import chanjarster.weixin.exception.WxErrorException; import chanjarster.weixin.util.json.WxGsonBuilder; @@ -35,4 +36,12 @@ public class WxUserAPITest { System.out.println(WxGsonBuilder.INSTANCE.create().toJson(user)); } + public void testUserList() throws WxErrorException { + WxUserList wxUserList = wxService.userList(null); + Assert.assertNotNull(wxUserList); + Assert.assertFalse(wxUserList.getCount() == -1); + Assert.assertFalse(wxUserList.getTotal() == -1); + Assert.assertFalse(wxUserList.getOpenids().size() == -1); + } + }