Skip to content

Commit c5c8628

Browse files
committed
添加获取标签下粉丝列表的接口
1 parent 63c41d4 commit c5c8628

4 files changed

Lines changed: 138 additions & 16 deletions

File tree

‎weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpUserTagService.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package me.chanjar.weixin.mp.api;
22

3-
import java.util.List;
4-
53
import me.chanjar.weixin.common.exception.WxErrorException;
4+
import me.chanjar.weixin.mp.bean.tag.WxTagListUser;
65
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
76

7+
import java.util.List;
8+
89
/**
910
* 用户标签管理相关接口
1011
* Created by Binary Wang on 2016/9/2.
@@ -55,4 +56,14 @@ public interface WxMpUserTagService {
5556
*/
5657
Boolean tagDelete(Integer id) throws WxErrorException;
5758

59+
/**
60+
* <pre>
61+
* 获取标签下粉丝列表
62+
* 详情请见:<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">用户标签管理</a>
63+
* 接口url格式: https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=ACCESS_TOKEN
64+
* </pre>
65+
*
66+
*/
67+
WxTagListUser tagListUser(Integer tagId, String nextOpenid) throws WxErrorException;
68+
5869
}

‎weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserTagServiceImpl.java‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3-
import java.util.List;
4-
5-
import org.slf4j.Logger;
6-
import org.slf4j.LoggerFactory;
7-
83
import com.google.gson.JsonObject;
9-
104
import me.chanjar.weixin.common.bean.result.WxError;
115
import me.chanjar.weixin.common.exception.WxErrorException;
126
import me.chanjar.weixin.mp.api.WxMpService;
137
import me.chanjar.weixin.mp.api.WxMpUserTagService;
8+
import me.chanjar.weixin.mp.bean.tag.WxTagListUser;
149
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
10+
import org.apache.commons.lang3.StringUtils;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
14+
import java.util.List;
1515

1616
/**
1717
*
@@ -92,4 +92,18 @@ public Boolean tagDelete(Integer id) throws WxErrorException {
9292

9393
throw new WxErrorException(wxError);
9494
}
95+
96+
@Override
97+
public WxTagListUser tagListUser(Integer tagId, String nextOpenid) throws WxErrorException {
98+
String url = "https://api.weixin.qq.com/cgi-bin/user/tag/get";
99+
100+
JsonObject json = new JsonObject();
101+
json.addProperty("tagid", tagId);
102+
json.addProperty("next_openid", StringUtils.trimToEmpty(nextOpenid));
103+
104+
String responseContent = this.wxMpService.post(url, json.toString());
105+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, json.toString(),
106+
responseContent);
107+
return WxTagListUser.fromJson(responseContent);
108+
}
95109
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package me.chanjar.weixin.mp.bean.tag;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
5+
import org.apache.commons.lang3.builder.ToStringBuilder;
6+
import org.apache.commons.lang3.builder.ToStringStyle;
7+
8+
import java.util.List;
9+
10+
/**
11+
* 获取标签下粉丝列表的结果对象
12+
* @author binarywang(https://github.com/binarywang)
13+
* Created by Binary Wang on 2016-09-19.
14+
*/
15+
public class WxTagListUser {
16+
17+
public static WxTagListUser fromJson(String json) {
18+
return WxMpGsonBuilder.create().fromJson(json,WxTagListUser.class);
19+
}
20+
21+
public String toJson() {
22+
return WxMpGsonBuilder.create().toJson(this);
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
28+
}
29+
30+
/**
31+
*"count":2,这次获取的粉丝数量
32+
*/
33+
@SerializedName("count")
34+
private Integer count;
35+
36+
/**
37+
*"data" 粉丝列表
38+
*/
39+
@SerializedName("data")
40+
private WxTagListUserData data;
41+
42+
/**
43+
*"next_openid" 拉取列表最后一个用户的openid
44+
*/
45+
@SerializedName("next_openid")
46+
private String nextOpenid;
47+
48+
public Integer getCount() {
49+
return count;
50+
}
51+
52+
public void setCount(Integer count) {
53+
this.count = count;
54+
}
55+
56+
public WxTagListUserData getData() {
57+
return data;
58+
}
59+
60+
public void setData(WxTagListUserData data) {
61+
this.data = data;
62+
}
63+
64+
public String getNextOpenid() {
65+
return nextOpenid;
66+
}
67+
68+
public void setNextOpenid(String nextOpenid) {
69+
this.nextOpenid = nextOpenid;
70+
}
71+
72+
public static class WxTagListUserData {
73+
@Override
74+
public String toString() {
75+
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
76+
}
77+
78+
/**
79+
* openid 列表
80+
*/
81+
@SerializedName("openid")
82+
private List<String> openidList;
83+
84+
public List<String> getOpenidList() {
85+
return openidList;
86+
}
87+
88+
public void setOpenidList(List<String> openidList) {
89+
this.openidList = openidList;
90+
}
91+
}
92+
}

‎weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpUserTagServiceImplTest.java‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3-
import java.util.List;
4-
3+
import com.google.inject.Inject;
4+
import me.chanjar.weixin.mp.api.ApiTestModule;
5+
import me.chanjar.weixin.mp.bean.tag.WxTagListUser;
6+
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
57
import org.testng.Assert;
68
import org.testng.annotations.Guice;
79
import org.testng.annotations.Test;
810

9-
import com.google.inject.Inject;
10-
11-
import me.chanjar.weixin.mp.api.ApiTestModule;
12-
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
11+
import java.util.List;
1312

1413
/**
1514
*
@@ -22,7 +21,7 @@ public class WxMpUserTagServiceImplTest {
2221
@Inject
2322
protected WxMpServiceImpl wxService;
2423

25-
private Integer tagId;
24+
private Integer tagId = 2;
2625

2726
@Test
2827
public void testTagCreate() throws Exception {
@@ -55,4 +54,10 @@ public void testTagDelete() throws Exception {
5554
Assert.assertTrue(res);
5655
}
5756

58-
}
57+
@Test
58+
public void testTagListUser() throws Exception {
59+
WxTagListUser res = this.wxService.getUserTagService().tagListUser(this.tagId, null);
60+
System.out.println(res);
61+
Assert.assertNotNull(res);
62+
}
63+
}

0 commit comments

Comments
 (0)