Skip to content

Commit ce58afc

Browse files
committed
增加用户标签添加接口
1 parent 9b0ab05 commit ce58afc

6 files changed

Lines changed: 189 additions & 45 deletions

File tree

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,16 @@ public interface WxMpService {
314314
*
315315
* @return WxMpGroupService
316316
*/
317+
317318
WxMpGroupService getGroupService();
318319

320+
/**
321+
* 返回用户标签相关接口的方法实现类,以方便调用个其各种接口
322+
*
323+
* @return WxMpUserTagService
324+
*/
325+
WxMpUserTagService getUserTagService();
326+
319327
/**
320328
* 返回二维码相关接口的方法实现类,以方便调用个其各种接口
321329
*
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package me.chanjar.weixin.mp.api;
2+
3+
import me.chanjar.weixin.common.exception.WxErrorException;
4+
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
5+
6+
/**
7+
* 用户标签管理相关接口
8+
* Created by Binary Wang on 2016/9/2.
9+
* @author binarywang(https://github.com/binarywang)
10+
*
11+
*/
12+
public interface WxMpUserTagService {
13+
14+
/**
15+
* <pre>
16+
* 创建标签
17+
* 一个公众号,最多可以创建100个标签。
18+
* 详情请见:<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">用户标签管理</a>
19+
* 接口url格式: https://api.weixin.qq.com/cgi-bin/tags/create?access_token=ACCESS_TOKEN
20+
* </pre>
21+
*
22+
* @param name 分组名字(30个字符以内)
23+
*/
24+
WxUserTag tagCreate(String name) throws WxErrorException;
25+
26+
}

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

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

3-
import java.io.IOException;
4-
5-
import org.apache.http.HttpHost;
6-
import org.apache.http.client.config.RequestConfig;
7-
import org.apache.http.client.methods.CloseableHttpResponse;
8-
import org.apache.http.client.methods.HttpGet;
9-
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
10-
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
11-
import org.apache.http.impl.client.BasicResponseHandler;
12-
import org.apache.http.impl.client.CloseableHttpClient;
13-
import org.slf4j.Logger;
14-
import org.slf4j.LoggerFactory;
15-
163
import com.google.gson.JsonArray;
174
import com.google.gson.JsonElement;
185
import com.google.gson.JsonObject;
196
import com.google.gson.JsonParser;
20-
217
import me.chanjar.weixin.common.bean.WxAccessToken;
228
import me.chanjar.weixin.common.bean.WxJsapiSignature;
239
import me.chanjar.weixin.common.bean.result.WxError;
@@ -26,37 +12,22 @@
2612
import me.chanjar.weixin.common.session.WxSessionManager;
2713
import me.chanjar.weixin.common.util.RandomUtils;
2814
import me.chanjar.weixin.common.util.crypto.SHA1;
29-
import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder;
30-
import me.chanjar.weixin.common.util.http.DefaultApacheHttpClientBuilder;
31-
import me.chanjar.weixin.common.util.http.RequestExecutor;
32-
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
33-
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
34-
import me.chanjar.weixin.common.util.http.URIUtil;
35-
import me.chanjar.weixin.mp.api.WxMpCardService;
36-
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
37-
import me.chanjar.weixin.mp.api.WxMpDataCubeService;
38-
import me.chanjar.weixin.mp.api.WxMpGroupService;
39-
import me.chanjar.weixin.mp.api.WxMpKefuService;
40-
import me.chanjar.weixin.mp.api.WxMpMaterialService;
41-
import me.chanjar.weixin.mp.api.WxMpMenuService;
42-
import me.chanjar.weixin.mp.api.WxMpPayService;
43-
import me.chanjar.weixin.mp.api.WxMpQrcodeService;
44-
import me.chanjar.weixin.mp.api.WxMpService;
45-
import me.chanjar.weixin.mp.api.WxMpUserService;
46-
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
47-
import me.chanjar.weixin.mp.bean.WxMpIndustry;
48-
import me.chanjar.weixin.mp.bean.WxMpMassGroupMessage;
49-
import me.chanjar.weixin.mp.bean.WxMpMassNews;
50-
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
51-
import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage;
52-
import me.chanjar.weixin.mp.bean.WxMpMassVideo;
53-
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
54-
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
55-
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
56-
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
57-
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
58-
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
59-
import me.chanjar.weixin.mp.bean.result.WxMpUser;
15+
import me.chanjar.weixin.common.util.http.*;
16+
import me.chanjar.weixin.mp.api.*;
17+
import me.chanjar.weixin.mp.bean.*;
18+
import me.chanjar.weixin.mp.bean.result.*;
19+
import org.apache.http.HttpHost;
20+
import org.apache.http.client.config.RequestConfig;
21+
import org.apache.http.client.methods.CloseableHttpResponse;
22+
import org.apache.http.client.methods.HttpGet;
23+
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
24+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
25+
import org.apache.http.impl.client.BasicResponseHandler;
26+
import org.apache.http.impl.client.CloseableHttpClient;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
30+
import java.io.IOException;
6031

6132
public class WxMpServiceImpl implements WxMpService {
6233

@@ -86,6 +57,8 @@ public class WxMpServiceImpl implements WxMpService {
8657

8758
private WxMpGroupService groupService = new WxMpGroupServiceImpl(this);
8859

60+
private WxMpUserTagService tagService = new WxMpUserTagServiceImpl(this);
61+
8962
private WxMpQrcodeService qrCodeService = new WxMpQrcodeServiceImpl(this);
9063

9164
private WxMpCardService cardService = new WxMpCardServiceImpl(this);
@@ -534,6 +507,11 @@ public WxMpGroupService getGroupService() {
534507
return this.groupService;
535508
}
536509

510+
@Override
511+
public WxMpUserTagService getUserTagService() {
512+
return this.tagService;
513+
}
514+
537515
@Override
538516
public WxMpQrcodeService getQrcodeService() {
539517
return this.qrCodeService;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package me.chanjar.weixin.mp.api.impl;
2+
3+
import com.google.gson.JsonObject;
4+
import me.chanjar.weixin.common.exception.WxErrorException;
5+
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
6+
import me.chanjar.weixin.mp.api.WxMpService;
7+
import me.chanjar.weixin.mp.api.WxMpUserTagService;
8+
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
9+
10+
/**
11+
*
12+
* @author binarywang(https://github.com/binarywang)
13+
* Created by Binary Wang on 2016/9/2.
14+
*/
15+
public class WxMpUserTagServiceImpl implements WxMpUserTagService {
16+
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/tags";
17+
18+
private WxMpService wxMpService;
19+
20+
public WxMpUserTagServiceImpl(WxMpService wxMpService) {
21+
this.wxMpService = wxMpService;
22+
}
23+
24+
@Override
25+
public WxUserTag tagCreate(String name) throws WxErrorException {
26+
String url = API_URL_PREFIX + "/create";
27+
JsonObject json = new JsonObject();
28+
JsonObject groupJson = new JsonObject();
29+
groupJson.addProperty("name", name);
30+
json.add("tag", groupJson);
31+
32+
String responseContent = this.wxMpService.execute(
33+
new SimplePostRequestExecutor(),
34+
url,
35+
json.toString());
36+
return WxUserTag.fromJson(responseContent);
37+
}
38+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package me.chanjar.weixin.mp.bean.tag;
2+
3+
import com.google.gson.JsonParser;
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+
/**
9+
* 用户标签对象
10+
* @author binarywang(https://github.com/binarywang)
11+
* Created by Binary Wang on 2016/9/2.
12+
*/
13+
public class WxUserTag {
14+
/**
15+
* id 标签id,由微信分配
16+
*/
17+
private Integer id;
18+
19+
/**
20+
* name 标签名,UTF8编码
21+
*/
22+
private String name;
23+
24+
/**
25+
* count 此标签下粉丝数
26+
*/
27+
private Integer count;
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
public Integer getCount() {
38+
return count;
39+
}
40+
41+
public void setCount(Integer count) {
42+
this.count = count;
43+
}
44+
45+
public Integer getId() {
46+
return id;
47+
}
48+
49+
public void setId(Integer id) {
50+
this.id = id;
51+
}
52+
53+
public static WxUserTag fromJson(String json) {
54+
return WxMpGsonBuilder.create().fromJson(new JsonParser().parse(json).getAsJsonObject().get("tag"), WxUserTag.class);
55+
}
56+
57+
public String toJson() {
58+
return WxMpGsonBuilder.create().toJson(this);
59+
}
60+
61+
@Override
62+
public String toString() {
63+
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
64+
}
65+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package me.chanjar.weixin.mp.api.impl;
2+
3+
import com.google.inject.Inject;
4+
import me.chanjar.weixin.mp.api.ApiTestModule;
5+
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
6+
import org.testng.Assert;
7+
import org.testng.annotations.Guice;
8+
import org.testng.annotations.Test;
9+
10+
/**
11+
*
12+
* @author binarywang(https://github.com/binarywang)
13+
* Created by Binary Wang on 2016/9/2.
14+
*/
15+
@Test
16+
@Guice(modules = ApiTestModule.class)
17+
public class WxMpUserTagServiceImplTest {
18+
@Inject
19+
protected WxMpServiceImpl wxService;
20+
21+
@Test
22+
public void testTagCreate() throws Exception {
23+
String tagName = "测试标签";
24+
WxUserTag res = this.wxService.getUserTagService().tagCreate(tagName);
25+
System.out.println(res);
26+
Assert.assertEquals(tagName, res.getName());
27+
}
28+
29+
}

0 commit comments

Comments
 (0)