Skip to content

Commit 7e21f46

Browse files
committed
添加获取用户身上的标签列表的接口
1 parent 6e32028 commit 7e21f46

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,15 @@ public interface WxMpUserTagService {
8686
*/
8787
boolean batchUntagging(Integer tagId, String[] openids) throws WxErrorException;
8888

89+
90+
/**
91+
* <pre>
92+
* 获取用户身上的标签列表
93+
* 详情请见:<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">用户标签管理</a>
94+
* 接口url格式: https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token=ACCESS_TOKEN
95+
* </pre>
96+
*
97+
*/
98+
List<Integer> userTagList(String openid) throws WxErrorException;
99+
89100
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import com.google.gson.JsonArray;
44
import com.google.gson.JsonObject;
5+
import com.google.gson.JsonParser;
6+
import com.google.gson.reflect.TypeToken;
57
import me.chanjar.weixin.common.bean.result.WxError;
68
import me.chanjar.weixin.common.exception.WxErrorException;
79
import me.chanjar.weixin.mp.api.WxMpService;
810
import me.chanjar.weixin.mp.api.WxMpUserTagService;
911
import me.chanjar.weixin.mp.bean.tag.WxTagListUser;
1012
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
13+
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
1114
import org.apache.commons.lang3.StringUtils;
1215
import org.slf4j.Logger;
1316
import org.slf4j.LoggerFactory;
@@ -153,4 +156,21 @@ public boolean batchUntagging(Integer tagId, String[] openids) throws WxErrorExc
153156

154157
throw new WxErrorException(wxError);
155158
}
159+
160+
@Override
161+
public List<Integer> userTagList(String openid) throws WxErrorException {
162+
String url = "https://api.weixin.qq.com/cgi-bin/tags/getidlist";
163+
164+
JsonObject json = new JsonObject();
165+
json.addProperty("openid", openid);
166+
167+
String responseContent = this.wxMpService.post(url, json.toString());
168+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, json.toString(),
169+
responseContent);
170+
171+
return WxMpGsonBuilder.create().fromJson(
172+
new JsonParser().parse(responseContent).getAsJsonObject().get("tagid_list"),
173+
new TypeToken<List<Integer>>() {
174+
}.getType());
175+
}
156176
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,12 @@ public void testBatchUntagging() throws Exception {
6868
System.out.println(res);
6969
Assert.assertTrue(res);
7070
}
71+
72+
@Test
73+
public void testUserTagList() throws Exception {
74+
List<Integer> res = this.wxService.getUserTagService().userTagList(
75+
((ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage()).getOpenid());
76+
System.out.println(res);
77+
Assert.assertNotNull(res);
78+
}
7179
}

0 commit comments

Comments
 (0)