@@ -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; | |||
/** | |||
* <pre> | |||
* 获取关注者列表 | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取关注者列表 | |||
* </pre> | |||
* @param next_openid 可选,第一个拉取的OPENID,null为从头开始拉取 | |||
* @return | |||
* @throws WxErrorException | |||
*/ | |||
public WxUserList userList(String next_openid) throws WxErrorException; | |||
/** | |||
* 注入 {@link WxConfigStorage} 的实现 | |||
* @param wxConfigProvider | |||
@@ -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); | |||
} | |||
/** | |||
@@ -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<String> openids = new ArrayList<String>(); | |||
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<String> getOpenids() { | |||
return openids; | |||
} | |||
public void setOpenids(List<String> 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); | |||
} | |||
} |
@@ -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() { | |||
@@ -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<WxUserList> { | |||
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; | |||
} | |||
} |
@@ -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); | |||
} | |||
} |