| @@ -1,5 +1,7 @@ | |||||
| package me.chanjar.weixin.mp.api; | package me.chanjar.weixin.mp.api; | ||||
| import java.util.List; | |||||
| import me.chanjar.weixin.common.exception.WxErrorException; | import me.chanjar.weixin.common.exception.WxErrorException; | ||||
| import me.chanjar.weixin.mp.bean.tag.WxUserTag; | import me.chanjar.weixin.mp.bean.tag.WxUserTag; | ||||
| @@ -13,14 +15,24 @@ public interface WxMpUserTagService { | |||||
| /** | /** | ||||
| * <pre> | * <pre> | ||||
| * 创建标签 | |||||
| * 一个公众号,最多可以创建100个标签。 | |||||
| * 创建标签 | |||||
| * 一个公众号,最多可以创建100个标签。 | |||||
| * 详情请见:<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">用户标签管理</a> | * 详情请见:<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 | * 接口url格式: https://api.weixin.qq.com/cgi-bin/tags/create?access_token=ACCESS_TOKEN | ||||
| * </pre> | * </pre> | ||||
| * | * | ||||
| * @param name 分组名字(30个字符以内) | |||||
| * @param name 标签名字(30个字符以内) | |||||
| */ | */ | ||||
| WxUserTag tagCreate(String name) throws WxErrorException; | WxUserTag tagCreate(String name) throws WxErrorException; | ||||
| /** | |||||
| * <pre> | |||||
| * 获取公众号已创建的标签 | |||||
| * 详情请见:<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/get?access_token=ACCESS_TOKEN | |||||
| * </pre> | |||||
| * | |||||
| */ | |||||
| List<WxUserTag> tagGet() throws WxErrorException; | |||||
| } | } | ||||
| @@ -1,13 +1,16 @@ | |||||
| package me.chanjar.weixin.mp.api.impl; | package me.chanjar.weixin.mp.api.impl; | ||||
| import java.util.List; | |||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| import com.google.gson.JsonObject; | import com.google.gson.JsonObject; | ||||
| import me.chanjar.weixin.common.exception.WxErrorException; | 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.WxMpService; | ||||
| import me.chanjar.weixin.mp.api.WxMpUserTagService; | import me.chanjar.weixin.mp.api.WxMpUserTagService; | ||||
| import me.chanjar.weixin.mp.bean.tag.WxUserTag; | import me.chanjar.weixin.mp.bean.tag.WxUserTag; | ||||
| import org.slf4j.Logger; | |||||
| import org.slf4j.LoggerFactory; | |||||
| /** | /** | ||||
| * | * | ||||
| @@ -15,7 +18,8 @@ import org.slf4j.LoggerFactory; | |||||
| * Created by Binary Wang on 2016/9/2. | * Created by Binary Wang on 2016/9/2. | ||||
| */ | */ | ||||
| public class WxMpUserTagServiceImpl implements WxMpUserTagService { | public class WxMpUserTagServiceImpl implements WxMpUserTagService { | ||||
| protected final Logger log = LoggerFactory.getLogger(WxMpDataCubeServiceImpl.class); | |||||
| protected final Logger log = LoggerFactory | |||||
| .getLogger(WxMpDataCubeServiceImpl.class); | |||||
| private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/tags"; | private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/tags"; | ||||
| private WxMpService wxMpService; | private WxMpService wxMpService; | ||||
| @@ -32,11 +36,19 @@ public class WxMpUserTagServiceImpl implements WxMpUserTagService { | |||||
| groupJson.addProperty("name", name); | groupJson.addProperty("name", name); | ||||
| json.add("tag", groupJson); | json.add("tag", groupJson); | ||||
| String responseContent = this.wxMpService.execute( | |||||
| new SimplePostRequestExecutor(), | |||||
| url, | |||||
| json.toString()); | |||||
| this.log.debug("\nurl:{}\nparams:{}\nresponse:{}",url, name, responseContent); | |||||
| String responseContent = this.wxMpService.post(url, json.toString()); | |||||
| this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, name, | |||||
| responseContent); | |||||
| return WxUserTag.fromJson(responseContent); | return WxUserTag.fromJson(responseContent); | ||||
| } | } | ||||
| @Override | |||||
| public List<WxUserTag> tagGet() throws WxErrorException { | |||||
| String url = API_URL_PREFIX + "/get"; | |||||
| String responseContent = this.wxMpService.get(url, null); | |||||
| this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, "[empty]", | |||||
| responseContent); | |||||
| return WxUserTag.listFromJson(responseContent); | |||||
| } | |||||
| } | } | ||||
| @@ -1,9 +1,12 @@ | |||||
| package me.chanjar.weixin.mp.bean.tag; | package me.chanjar.weixin.mp.bean.tag; | ||||
| import java.util.List; | |||||
| import org.apache.commons.lang3.builder.ToStringBuilder; | import org.apache.commons.lang3.builder.ToStringBuilder; | ||||
| import org.apache.commons.lang3.builder.ToStringStyle; | import org.apache.commons.lang3.builder.ToStringStyle; | ||||
| import com.google.gson.JsonParser; | import com.google.gson.JsonParser; | ||||
| import com.google.gson.reflect.TypeToken; | |||||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | ||||
| @@ -53,7 +56,15 @@ public class WxUserTag { | |||||
| } | } | ||||
| public static WxUserTag fromJson(String json) { | public static WxUserTag fromJson(String json) { | ||||
| return WxMpGsonBuilder.create().fromJson(new JsonParser().parse(json).getAsJsonObject().get("tag"), WxUserTag.class); | |||||
| return WxMpGsonBuilder.create().fromJson( | |||||
| new JsonParser().parse(json).getAsJsonObject().get("tag"), | |||||
| WxUserTag.class); | |||||
| } | |||||
| public static List<WxUserTag> listFromJson(String json) { | |||||
| return WxMpGsonBuilder.create().fromJson( | |||||
| new JsonParser().parse(json).getAsJsonObject().get("tags"), | |||||
| new TypeToken<List<WxUserTag>>(){}.getType()); | |||||
| } | } | ||||
| public String toJson() { | public String toJson() { | ||||
| @@ -3,6 +3,9 @@ package me.chanjar.weixin.mp.api.impl; | |||||
| import com.google.inject.Inject; | import com.google.inject.Inject; | ||||
| import me.chanjar.weixin.mp.api.ApiTestModule; | import me.chanjar.weixin.mp.api.ApiTestModule; | ||||
| import me.chanjar.weixin.mp.bean.tag.WxUserTag; | import me.chanjar.weixin.mp.bean.tag.WxUserTag; | ||||
| import java.util.List; | |||||
| import org.testng.Assert; | import org.testng.Assert; | ||||
| import org.testng.annotations.Guice; | import org.testng.annotations.Guice; | ||||
| import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
| @@ -26,4 +29,11 @@ public class WxMpUserTagServiceImplTest { | |||||
| Assert.assertEquals(tagName, res.getName()); | Assert.assertEquals(tagName, res.getName()); | ||||
| } | } | ||||
| @Test | |||||
| public void testTagGet() throws Exception { | |||||
| List<WxUserTag> res = this.wxService.getUserTagService().tagGet(); | |||||
| System.out.println(res); | |||||
| Assert.assertNotNull(res); | |||||
| } | |||||
| } | } | ||||