增加用户标签添加接口

This commit is contained in:
BinaryWang 2016-09-02 19:21:27 +08:00
parent 9b0ab05b34
commit ce58afc5da
6 changed files with 190 additions and 46 deletions

View File

@ -314,8 +314,16 @@ public interface WxMpService {
*
* @return WxMpGroupService
*/
WxMpGroupService getGroupService();
/**
* 返回用户标签相关接口的方法实现类以方便调用个其各种接口
*
* @return WxMpUserTagService
*/
WxMpUserTagService getUserTagService();
/**
* 返回二维码相关接口的方法实现类以方便调用个其各种接口
*

View File

@ -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 {
/**
* <pre>
* 创建标签
* 一个公众号最多可以创建100个标签
* 详情请见<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">用户标签管理</a>
* 接口url格式 https://api.weixin.qq.com/cgi-bin/tags/create?access_token=ACCESS_TOKEN
* </pre>
*
* @param name 分组名字30个字符以内
*/
WxUserTag tagCreate(String name) throws WxErrorException;
}

View File

@ -1,7 +1,21 @@
package me.chanjar.weixin.mp.api.impl;
import java.io.IOException;
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;
import me.chanjar.weixin.common.exception.WxErrorException;
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.*;
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;
@ -13,50 +27,7 @@ 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;
import me.chanjar.weixin.common.exception.WxErrorException;
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 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;

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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());
}
}