diff --git a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/WeixinProxy.java b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/WeixinProxy.java
index c0303c30..d12dcf67 100644
--- a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/WeixinProxy.java
+++ b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/WeixinProxy.java
@@ -1720,6 +1720,24 @@ public class WeixinProxy {
return tagApi.getTagFollowingOpenIds(tagId, nextOpenId);
}
+ /**
+ * 获取标签下粉丝列表 请慎重使用
+ *
+ * @param tagId
+ * 标签ID
+ * @param nextOpenId
+ * 第一个拉取的OPENID,不填默认从头开始拉取
+ * @return 被打标签者信息 包含用户的详细信息
+ * @throws WeixinException
+ * @see com.foxinmy.weixin4j.mp.api.TagApi
+ * @see 获取标签下粉丝列表
+ */
+ public Following getTagFollowing(int tagId, String nextOpenId)
+ throws WeixinException {
+ return tagApi.getTagFollowing(tagId, nextOpenId);
+ }
+
/**
* 获取标签下全部的粉丝列表 请慎重使用
*
@@ -1737,6 +1755,22 @@ public class WeixinProxy {
return tagApi.getAllTagFollowingOpenIds(tagId);
}
+ /**
+ * 获取标签下全部的粉丝列表 请慎重使用
+ *
+ * @param tagId
+ * 标签ID
+ * @return 被打标签者信息 包含用户的详细信息
+ * @throws WeixinException
+ * @see com.foxinmy.weixin4j.mp.api.TagApi
+ * @see #getTagFollowing(int,String)
+ * @see 获取标签下粉丝列表
+ */
+ public List getAllTagFollowing(int tagId) throws WeixinException {
+ return tagApi.getAllTagFollowing(tagId);
+ }
+
/**
* 获取用户身上的标签列表
*
diff --git a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/TagApi.java b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/TagApi.java
index b8bc8f46..dfa0bbd4 100644
--- a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/TagApi.java
+++ b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/TagApi.java
@@ -10,6 +10,7 @@ import com.foxinmy.weixin4j.http.weixin.JsonResult;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.mp.model.Following;
import com.foxinmy.weixin4j.mp.model.Tag;
+import com.foxinmy.weixin4j.mp.model.User;
import com.foxinmy.weixin4j.token.TokenHolder;
/**
@@ -23,9 +24,11 @@ import com.foxinmy.weixin4j.token.TokenHolder;
*/
public class TagApi extends MpApi {
private final TokenHolder tokenHolder;
+ private final UserApi userApi;
public TagApi(TokenHolder tokenHolder) {
this.tokenHolder = tokenHolder;
+ this.userApi = new UserApi(tokenHolder);
}
/**
@@ -45,7 +48,8 @@ public class TagApi extends MpApi {
String.format(tag_create_uri, tokenHolder.getAccessToken()),
String.format("{\"tag\":{\"name\":\"%s\"}}", name));
- return response.getAsJson().getObject("tag", Tag.class);
+ return JSON.parseObject(response.getAsJson().getString("tag"),
+ Tag.class);
}
/**
@@ -79,9 +83,11 @@ public class TagApi extends MpApi {
*/
public JsonResult updateTag(Tag tag) throws WeixinException {
String tag_update_uri = getRequestUri("tag_update_uri");
+ JSONObject obj = new JSONObject();
+ obj.put("tag", tag);
WeixinResponse response = weixinExecutor.post(
String.format(tag_update_uri, tokenHolder.getAccessToken()),
- JSON.toJSONString(tag));
+ obj.toJSONString());
return response.getAsJsonResult();
}
@@ -181,6 +187,35 @@ public class TagApi extends MpApi {
return following;
}
+ /**
+ * 获取标签下粉丝列表 请慎重使用
+ *
+ * @param tagId
+ * 标签ID
+ * @param nextOpenId
+ * 第一个拉取的OPENID,不填默认从头开始拉取
+ * @return 被打标签者信息 包含用户的详细信息
+ * @throws WeixinException
+ * @see 获取标签下粉丝列表
+ */
+ public Following getTagFollowing(int tagId, String nextOpenId)
+ throws WeixinException {
+ Following following = getTagFollowingOpenIds(tagId, nextOpenId);
+ if (following.getCount() > 0) {
+ List users = new ArrayList(following.getCount());
+ for (int i = 1; i <= (int) Math.ceil(following.getCount() / 100d); i++) {
+ users.addAll(userApi.getUsers(following
+ .getOpenIds()
+ .subList((i - 1) * 100,
+ Math.min(i * 100, following.getCount()))
+ .toArray(new String[] {})));
+ }
+ following.setUserList(users);
+ }
+ return following;
+ }
+
/**
* 获取标签下全部的粉丝列表 请慎重使用
*
@@ -208,6 +243,32 @@ public class TagApi extends MpApi {
return openIds;
}
+ /**
+ * 获取标签下全部的粉丝列表 请慎重使用
+ *
+ * @param tagId
+ * 标签ID
+ * @return 被打标签者信息 包含用户的详细信息
+ * @throws WeixinException
+ * @see #getTagFollowing(int,String)
+ * @see 获取标签下粉丝列表
+ */
+ public List getAllTagFollowing(int tagId) throws WeixinException {
+ List userList = new ArrayList();
+ String nextOpenId = null;
+ Following f = null;
+ for (;;) {
+ f = getTagFollowing(tagId, nextOpenId);
+ if (f.getCount() == 0) {
+ break;
+ }
+ userList.addAll(f.getUserList());
+ nextOpenId = f.getNextOpenId();
+ }
+ return userList;
+ }
+
/**
* 获取用户身上的标签列表
*
diff --git a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/UserApi.java b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/UserApi.java
index b9fc2de6..7814c31d 100644
--- a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/UserApi.java
+++ b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/api/UserApi.java
@@ -67,8 +67,8 @@ public class UserApi extends MpApi {
public User getUser(String openId, Lang lang) throws WeixinException {
String user_info_uri = getRequestUri("api_user_info_uri");
Token token = tokenHolder.getToken();
- WeixinResponse response = weixinExecutor
- .get(String.format(user_info_uri, token.getAccessToken(), openId, lang.name()));
+ WeixinResponse response = weixinExecutor.get(String.format(
+ user_info_uri, token.getAccessToken(), openId, lang.name()));
return response.getAsObject(new TypeReference() {
});
@@ -106,21 +106,25 @@ public class UserApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.User
* @throws WeixinException
*/
- public List getUsers(Lang lang, String... openIds) throws WeixinException {
+ public List getUsers(Lang lang, String... openIds)
+ throws WeixinException {
String api_users_info_uri = getRequestUri("api_users_info_uri");
StringBuilder parameter = new StringBuilder();
parameter.append("{\"user_list\": [");
for (String openId : openIds) {
parameter.append("{\"openid\": \"").append(openId).append("\"");
- parameter.append(",\"lang\": \"").append(lang.name()).append("\"").append("},");
+ parameter.append(",\"lang\": \"").append(lang.name()).append("\"")
+ .append("},");
}
parameter.delete(parameter.length() - 1, parameter.length());
parameter.append("]}");
Token token = tokenHolder.getToken();
- WeixinResponse response = weixinExecutor.post(String.format(api_users_info_uri, token.getAccessToken()),
+ WeixinResponse response = weixinExecutor.post(
+ String.format(api_users_info_uri, token.getAccessToken()),
parameter.toString());
- return JSON.parseArray(response.getAsJson().getString("user_info_list"), User.class);
+ return JSON.parseArray(
+ response.getAsJson().getString("user_info_list"), User.class);
}
/**
@@ -144,8 +148,11 @@ public class UserApi extends MpApi {
if (following.getCount() > 0) {
List users = new ArrayList(following.getCount());
for (int i = 1; i <= (int) Math.ceil(following.getCount() / 100d); i++) {
- users.addAll(getUsers(following.getOpenIds()
- .subList((i - 1) * 100, Math.min(i * 100, following.getCount())).toArray(new String[] {})));
+ users.addAll(getUsers(following
+ .getOpenIds()
+ .subList((i - 1) * 100,
+ Math.min(i * 100, following.getCount()))
+ .toArray(new String[] {})));
}
following.setUserList(users);
}
@@ -164,17 +171,20 @@ public class UserApi extends MpApi {
* 获取关注者列表
* @see com.foxinmy.weixin4j.mp.model.Following
*/
- public Following getFollowingOpenIds(String nextOpenId) throws WeixinException {
+ public Following getFollowingOpenIds(String nextOpenId)
+ throws WeixinException {
String following_uri = getRequestUri("following_uri");
Token token = tokenHolder.getToken();
- WeixinResponse response = weixinExecutor
- .get(String.format(following_uri, token.getAccessToken(), nextOpenId == null ? "" : nextOpenId));
+ WeixinResponse response = weixinExecutor.get(String.format(
+ following_uri, token.getAccessToken(), nextOpenId == null ? ""
+ : nextOpenId));
JSONObject result = response.getAsJson();
Following following = JSON.toJavaObject(result, Following.class);
if (following.getCount() > 0) {
- following.setOpenIds(JSON.parseArray(result.getJSONObject("data").getString("openid"), String.class));
+ following.setOpenIds(JSON.parseArray(result.getJSONObject("data")
+ .getString("openid"), String.class));
}
return following;
}
@@ -204,10 +214,10 @@ public class UserApi extends MpApi {
Following f = null;
for (;;) {
f = getFollowing(nextOpenId);
- if (f.getCount() == 0) {
+ userList.addAll(f.getUserList());
+ if (f.getCount() == f.getTotal() || f.getCount() == 0) {
break;
}
- userList.addAll(f.getUserList());
nextOpenId = f.getNextOpenId();
}
return userList;
@@ -233,10 +243,10 @@ public class UserApi extends MpApi {
Following f = null;
for (;;) {
f = getFollowingOpenIds(nextOpenId);
- if (f.getCount() == 0) {
+ openIds.addAll(f.getOpenIds());
+ if (f.getCount() == f.getTotal() || f.getCount() == 0) {
break;
}
- openIds.addAll(f.getOpenIds());
nextOpenId = f.getNextOpenId();
}
return openIds;
@@ -254,13 +264,15 @@ public class UserApi extends MpApi {
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140838&token=&lang=zh_CN">
* 设置用户备注名
*/
- public JsonResult remarkUserName(String openId, String remark) throws WeixinException {
+ public JsonResult remarkUserName(String openId, String remark)
+ throws WeixinException {
String username_remark_uri = getRequestUri("username_remark_uri");
Token token = tokenHolder.getToken();
JSONObject obj = new JSONObject();
obj.put("openid", openId);
obj.put("remark", remark);
- WeixinResponse response = weixinExecutor.post(String.format(username_remark_uri, token.getAccessToken()),
+ WeixinResponse response = weixinExecutor.post(
+ String.format(username_remark_uri, token.getAccessToken()),
obj.toJSONString());
return response.getAsJsonResult();
diff --git a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/model/Tag.java b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/model/Tag.java
index d0514912..7c2bff1e 100644
--- a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/model/Tag.java
+++ b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/model/Tag.java
@@ -31,6 +31,12 @@ public class Tag implements Serializable {
*/
private int count;
+ @JSONCreator
+ public Tag(@JSONField(name = "id") int id,
+ @JSONField(name = "name") String name) {
+ this(id, name, 0);
+ }
+
@JSONCreator
public Tag(@JSONField(name = "id") int id,
@JSONField(name = "name") String name,
diff --git a/weixin4j-mp/src/test/java/com/foxinmy/weixin4j/mp/test/TagTest.java b/weixin4j-mp/src/test/java/com/foxinmy/weixin4j/mp/test/TagTest.java
new file mode 100644
index 00000000..1d996f6c
--- /dev/null
+++ b/weixin4j-mp/src/test/java/com/foxinmy/weixin4j/mp/test/TagTest.java
@@ -0,0 +1,92 @@
+package com.foxinmy.weixin4j.mp.test;
+
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.foxinmy.weixin4j.exception.WeixinException;
+import com.foxinmy.weixin4j.http.weixin.JsonResult;
+import com.foxinmy.weixin4j.mp.api.TagApi;
+import com.foxinmy.weixin4j.mp.model.Tag;
+import com.foxinmy.weixin4j.mp.model.User;
+
+/**
+ * 标签单元测试
+ *
+ * @className TagTest
+ * @author jy
+ * @date 2016年5月2日
+ * @since JDK 1.6
+ * @see
+ */
+public class TagTest extends TokenTest {
+ private TagApi tagApi;
+
+ @Before
+ public void init() {
+ tagApi = new TagApi(tokenHolder);
+ }
+
+ @Test
+ public void create() throws WeixinException {
+ Tag tag = tagApi.createTag("测试三");
+ Assert.assertNotNull(tag);
+ System.out.println(tag);
+ }
+
+ @Test
+ public void list() throws WeixinException {
+ List tags = tagApi.listTags();
+ Assert.assertFalse(tags.isEmpty());
+ }
+
+ @Test
+ public void update() throws WeixinException {
+ JsonResult result = tagApi.updateTag(new Tag(120, "测试12"));
+ System.err.println(result);
+ }
+
+ @Test
+ public void remove() throws WeixinException {
+ JsonResult result = tagApi.deleteTag(134);
+ System.err.print(result);
+ }
+
+ @Test
+ public void batchtagging() throws WeixinException {
+ JsonResult result = tagApi.taggingUsers(120,
+ "owGBft-GyGJuKXBzpkzrfl-RG8TI", "owGBfty5TYNwh-3iUTGtxAHcD310",
+ "owGBftzXEfBml_bYvbrYxE5lE5U8");
+ System.err.println(result);
+ }
+
+ @Test
+ public void batchuntagging() throws WeixinException {
+ JsonResult result = tagApi.taggingUsers(120,
+ "owGBftwS5Yr6xKH_Hb9mGv1nbd3o");
+ System.err.println(result);
+ }
+
+ @Test
+ public void getidlist() throws WeixinException {
+ Integer[] tagIds = tagApi.getUserTags("owGBft-GyGJuKXBzpkzrfl-RG8TI");
+ Assert.assertNotNull(tagIds);
+ System.out.println(tagIds[0]);
+ }
+
+ @Test
+ public void getAllTagFollowing() throws WeixinException {
+ List users = tagApi.getAllTagFollowing(120);
+ Assert.assertNotNull(users);
+ System.out.println(users);
+ }
+
+ @Test
+ public void getAllTagFollowingOpenIds() throws WeixinException {
+ List tags = tagApi.getAllTagFollowingOpenIds(120);
+ Assert.assertNotNull(tags);
+ System.out.println(tags);
+ }
+}