diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java index 7acd2342..18df437f 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java @@ -314,8 +314,16 @@ public interface WxMpService { * * @return WxMpGroupService */ + WxMpGroupService getGroupService(); + /** + * 返回用户标签相关接口的方法实现类,以方便调用个其各种接口 + * + * @return WxMpUserTagService + */ + WxMpUserTagService getUserTagService(); + /** * 返回二维码相关接口的方法实现类,以方便调用个其各种接口 * diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpUserTagService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpUserTagService.java new file mode 100644 index 00000000..c909f9ce --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpUserTagService.java @@ -0,0 +1,26 @@ +package me.chanjar.weixin.mp.api; + +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.mp.bean.tag.WxUserTag; + +/** + * 用户标签管理相关接口 + * Created by Binary Wang on 2016/9/2. + * @author binarywang(https://github.com/binarywang) + * + */ +public interface WxMpUserTagService { + + /** + *
+   *   创建标签
+   *   一个公众号,最多可以创建100个标签。
+   * 详情请见:用户标签管理
+   * 接口url格式: https://api.weixin.qq.com/cgi-bin/tags/create?access_token=ACCESS_TOKEN
+   * 
+ * + * @param name 分组名字(30个字符以内) + */ + WxUserTag tagCreate(String name) throws WxErrorException; + +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java index c69f2672..cbbd1264 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java @@ -1,23 +1,9 @@ package me.chanjar.weixin.mp.api.impl; -import java.io.IOException; - -import org.apache.http.HttpHost; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.conn.ssl.DefaultHostnameVerifier; -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.impl.client.BasicResponseHandler; -import org.apache.http.impl.client.CloseableHttpClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; - import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.WxJsapiSignature; import me.chanjar.weixin.common.bean.result.WxError; @@ -26,37 +12,22 @@ import me.chanjar.weixin.common.session.StandardSessionManager; import me.chanjar.weixin.common.session.WxSessionManager; import me.chanjar.weixin.common.util.RandomUtils; import me.chanjar.weixin.common.util.crypto.SHA1; -import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder; -import me.chanjar.weixin.common.util.http.DefaultApacheHttpClientBuilder; -import me.chanjar.weixin.common.util.http.RequestExecutor; -import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; -import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; -import me.chanjar.weixin.common.util.http.URIUtil; -import me.chanjar.weixin.mp.api.WxMpCardService; -import me.chanjar.weixin.mp.api.WxMpConfigStorage; -import me.chanjar.weixin.mp.api.WxMpDataCubeService; -import me.chanjar.weixin.mp.api.WxMpGroupService; -import me.chanjar.weixin.mp.api.WxMpKefuService; -import me.chanjar.weixin.mp.api.WxMpMaterialService; -import me.chanjar.weixin.mp.api.WxMpMenuService; -import me.chanjar.weixin.mp.api.WxMpPayService; -import me.chanjar.weixin.mp.api.WxMpQrcodeService; -import me.chanjar.weixin.mp.api.WxMpService; -import me.chanjar.weixin.mp.api.WxMpUserService; -import me.chanjar.weixin.mp.bean.WxMpCustomMessage; -import me.chanjar.weixin.mp.bean.WxMpIndustry; -import me.chanjar.weixin.mp.bean.WxMpMassGroupMessage; -import me.chanjar.weixin.mp.bean.WxMpMassNews; -import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage; -import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage; -import me.chanjar.weixin.mp.bean.WxMpMassVideo; -import me.chanjar.weixin.mp.bean.WxMpSemanticQuery; -import me.chanjar.weixin.mp.bean.WxMpTemplateMessage; -import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult; -import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult; -import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; -import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult; -import me.chanjar.weixin.mp.bean.result.WxMpUser; +import me.chanjar.weixin.common.util.http.*; +import me.chanjar.weixin.mp.api.*; +import me.chanjar.weixin.mp.bean.*; +import me.chanjar.weixin.mp.bean.result.*; +import org.apache.http.HttpHost; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.conn.ssl.DefaultHostnameVerifier; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.impl.client.BasicResponseHandler; +import org.apache.http.impl.client.CloseableHttpClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; public class WxMpServiceImpl implements WxMpService { @@ -86,6 +57,8 @@ public class WxMpServiceImpl implements WxMpService { private WxMpGroupService groupService = new WxMpGroupServiceImpl(this); + private WxMpUserTagService tagService = new WxMpUserTagServiceImpl(this); + private WxMpQrcodeService qrCodeService = new WxMpQrcodeServiceImpl(this); private WxMpCardService cardService = new WxMpCardServiceImpl(this); @@ -534,6 +507,11 @@ public class WxMpServiceImpl implements WxMpService { return this.groupService; } + @Override + public WxMpUserTagService getUserTagService() { + return this.tagService; + } + @Override public WxMpQrcodeService getQrcodeService() { return this.qrCodeService; diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserTagServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserTagServiceImpl.java new file mode 100644 index 00000000..1566f602 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserTagServiceImpl.java @@ -0,0 +1,38 @@ +package me.chanjar.weixin.mp.api.impl; + +import com.google.gson.JsonObject; +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; +import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.api.WxMpUserTagService; +import me.chanjar.weixin.mp.bean.tag.WxUserTag; + +/** + * + * @author binarywang(https://github.com/binarywang) + * Created by Binary Wang on 2016/9/2. + */ +public class WxMpUserTagServiceImpl implements WxMpUserTagService { + private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/tags"; + + private WxMpService wxMpService; + + public WxMpUserTagServiceImpl(WxMpService wxMpService) { + this.wxMpService = wxMpService; + } + + @Override + public WxUserTag tagCreate(String name) throws WxErrorException { + String url = API_URL_PREFIX + "/create"; + JsonObject json = new JsonObject(); + JsonObject groupJson = new JsonObject(); + groupJson.addProperty("name", name); + json.add("tag", groupJson); + + String responseContent = this.wxMpService.execute( + new SimplePostRequestExecutor(), + url, + json.toString()); + return WxUserTag.fromJson(responseContent); + } +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/tag/WxUserTag.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/tag/WxUserTag.java new file mode 100644 index 00000000..79d28f20 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/tag/WxUserTag.java @@ -0,0 +1,65 @@ +package me.chanjar.weixin.mp.bean.tag; + +import com.google.gson.JsonParser; +import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + +/** + * 用户标签对象 + * @author binarywang(https://github.com/binarywang) + * Created by Binary Wang on 2016/9/2. + */ +public class WxUserTag { + /** + * id 标签id,由微信分配 + */ + private Integer id; + + /** + * name 标签名,UTF8编码 + */ + private String name; + + /** + * count 此标签下粉丝数 + */ + private Integer count; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getCount() { + return count; + } + + public void setCount(Integer count) { + this.count = count; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public static WxUserTag fromJson(String json) { + return WxMpGsonBuilder.create().fromJson(new JsonParser().parse(json).getAsJsonObject().get("tag"), WxUserTag.class); + } + + public String toJson() { + return WxMpGsonBuilder.create().toJson(this); + } + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); + } +} diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpUserTagServiceImplTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpUserTagServiceImplTest.java new file mode 100644 index 00000000..af8125bf --- /dev/null +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpUserTagServiceImplTest.java @@ -0,0 +1,29 @@ +package me.chanjar.weixin.mp.api.impl; + +import com.google.inject.Inject; +import me.chanjar.weixin.mp.api.ApiTestModule; +import me.chanjar.weixin.mp.bean.tag.WxUserTag; +import org.testng.Assert; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + +/** + * + * @author binarywang(https://github.com/binarywang) + * Created by Binary Wang on 2016/9/2. + */ +@Test +@Guice(modules = ApiTestModule.class) +public class WxMpUserTagServiceImplTest { + @Inject + protected WxMpServiceImpl wxService; + + @Test + public void testTagCreate() throws Exception { + String tagName = "测试标签"; + WxUserTag res = this.wxService.getUserTagService().tagCreate(tagName); + System.out.println(res); + Assert.assertEquals(tagName, res.getName()); + } + +} \ No newline at end of file