Skip to content

Commit 3ab8a3c

Browse files
committed
增加用户标签查询接口
1 parent 7bf7f28 commit 3ab8a3c

4 files changed

Lines changed: 58 additions & 13 deletions

File tree

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.chanjar.weixin.mp.api;
22

3+
import java.util.List;
4+
35
import me.chanjar.weixin.common.exception.WxErrorException;
46
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
57

@@ -13,14 +15,24 @@ public interface WxMpUserTagService {
1315

1416
/**
1517
* <pre>
16-
* 创建标签
17-
* 一个公众号,最多可以创建100个标签。
18+
* 创建标签
19+
* 一个公众号,最多可以创建100个标签。
1820
* 详情请见:<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">用户标签管理</a>
1921
* 接口url格式: https://api.weixin.qq.com/cgi-bin/tags/create?access_token=ACCESS_TOKEN
2022
* </pre>
2123
*
22-
* @param name 分组名字(30个字符以内)
24+
* @param name 标签名字(30个字符以内)
2325
*/
2426
WxUserTag tagCreate(String name) throws WxErrorException;
2527

28+
/**
29+
* <pre>
30+
* 获取公众号已创建的标签
31+
* 详情请见:<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">用户标签管理</a>
32+
* 接口url格式: https://api.weixin.qq.com/cgi-bin/tags/get?access_token=ACCESS_TOKEN
33+
* </pre>
34+
*
35+
*/
36+
List<WxUserTag> tagGet() throws WxErrorException;
37+
2638
}
Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
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+
38
import com.google.gson.JsonObject;
9+
410
import me.chanjar.weixin.common.exception.WxErrorException;
5-
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
611
import me.chanjar.weixin.mp.api.WxMpService;
712
import me.chanjar.weixin.mp.api.WxMpUserTagService;
813
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
9-
import org.slf4j.Logger;
10-
import org.slf4j.LoggerFactory;
1114

1215
/**
1316
*
1417
* @author binarywang(https://github.com/binarywang)
1518
* Created by Binary Wang on 2016/9/2.
1619
*/
1720
public class WxMpUserTagServiceImpl implements WxMpUserTagService {
18-
protected final Logger log = LoggerFactory.getLogger(WxMpDataCubeServiceImpl.class);
21+
protected final Logger log = LoggerFactory
22+
.getLogger(WxMpDataCubeServiceImpl.class);
1923
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/tags";
2024

2125
private WxMpService wxMpService;
@@ -32,11 +36,19 @@ public WxUserTag tagCreate(String name) throws WxErrorException {
3236
groupJson.addProperty("name", name);
3337
json.add("tag", groupJson);
3438

35-
String responseContent = this.wxMpService.execute(
36-
new SimplePostRequestExecutor(),
37-
url,
38-
json.toString());
39-
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, name, responseContent);
39+
String responseContent = this.wxMpService.post(url, json.toString());
40+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, name,
41+
responseContent);
4042
return WxUserTag.fromJson(responseContent);
4143
}
44+
45+
@Override
46+
public List<WxUserTag> tagGet() throws WxErrorException {
47+
String url = API_URL_PREFIX + "/get";
48+
49+
String responseContent = this.wxMpService.get(url, null);
50+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, "[empty]",
51+
responseContent);
52+
return WxUserTag.listFromJson(responseContent);
53+
}
4254
}

‎weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/tag/WxUserTag.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package me.chanjar.weixin.mp.bean.tag;
22

3+
import java.util.List;
4+
35
import org.apache.commons.lang3.builder.ToStringBuilder;
46
import org.apache.commons.lang3.builder.ToStringStyle;
57

68
import com.google.gson.JsonParser;
9+
import com.google.gson.reflect.TypeToken;
710

811
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
912

@@ -53,7 +56,15 @@ public void setId(Integer id) {
5356
}
5457

5558
public static WxUserTag fromJson(String json) {
56-
return WxMpGsonBuilder.create().fromJson(new JsonParser().parse(json).getAsJsonObject().get("tag"), WxUserTag.class);
59+
return WxMpGsonBuilder.create().fromJson(
60+
new JsonParser().parse(json).getAsJsonObject().get("tag"),
61+
WxUserTag.class);
62+
}
63+
64+
public static List<WxUserTag> listFromJson(String json) {
65+
return WxMpGsonBuilder.create().fromJson(
66+
new JsonParser().parse(json).getAsJsonObject().get("tags"),
67+
new TypeToken<List<WxUserTag>>(){}.getType());
5768
}
5869

5970
public String toJson() {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.google.inject.Inject;
44
import me.chanjar.weixin.mp.api.ApiTestModule;
55
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
6+
7+
import java.util.List;
8+
69
import org.testng.Assert;
710
import org.testng.annotations.Guice;
811
import org.testng.annotations.Test;
@@ -26,4 +29,11 @@ public void testTagCreate() throws Exception {
2629
Assert.assertEquals(tagName, res.getName());
2730
}
2831

32+
@Test
33+
public void testTagGet() throws Exception {
34+
List<WxUserTag> res = this.wxService.getUserTagService().tagGet();
35+
System.out.println(res);
36+
Assert.assertNotNull(res);
37+
}
38+
2939
}

0 commit comments

Comments
 (0)