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 index 1f4277da..864d9cd0 100644 --- 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 @@ -35,4 +35,14 @@ public interface WxMpUserTagService { */ List tagGet() throws WxErrorException; + /** + *
+   * 编辑标签
+   * 详情请见:用户标签管理
+   * 接口url格式: https://api.weixin.qq.com/cgi-bin/tags/update?access_token=ACCESS_TOKEN
+   * 
+ * + */ + Boolean tagUpdate(Integer id, String name) throws WxErrorException; + } 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 index fb934961..18ff67c7 100644 --- 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 @@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory; import com.google.gson.JsonObject; +import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpUserTagService; @@ -32,12 +33,12 @@ public class WxMpUserTagServiceImpl implements WxMpUserTagService { 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); + JsonObject tagJson = new JsonObject(); + tagJson.addProperty("name", name); + json.add("tag", tagJson); String responseContent = this.wxMpService.post(url, json.toString()); - this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, name, + this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, json.toString(), responseContent); return WxUserTag.fromJson(responseContent); } @@ -51,4 +52,20 @@ public class WxMpUserTagServiceImpl implements WxMpUserTagService { responseContent); return WxUserTag.listFromJson(responseContent); } + + @Override + public Boolean tagUpdate(Integer id, String name) throws WxErrorException { + String url = API_URL_PREFIX + "/update"; + + JsonObject json = new JsonObject(); + JsonObject tagJson = new JsonObject(); + tagJson.addProperty("id", id); + tagJson.addProperty("name", name); + json.add("tag", tagJson); + + String responseContent = this.wxMpService.post(url, json.toString()); + this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, json.toString(), responseContent); + WxError wxError = WxError.fromJson(responseContent); + return wxError.getErrorCode() == 0; + } } 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 index ea7799dd..1aab8889 100644 --- 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 @@ -1,15 +1,16 @@ 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 java.util.List; import org.testng.Assert; import org.testng.annotations.Guice; import org.testng.annotations.Test; +import com.google.inject.Inject; + +import me.chanjar.weixin.mp.api.ApiTestModule; +import me.chanjar.weixin.mp.bean.tag.WxUserTag; + /** * * @author binarywang(https://github.com/binarywang) @@ -21,11 +22,14 @@ public class WxMpUserTagServiceImplTest { @Inject protected WxMpServiceImpl wxService; + private Integer tagId; + @Test public void testTagCreate() throws Exception { - String tagName = "测试标签"; + String tagName = "测试标签" + System.currentTimeMillis(); WxUserTag res = this.wxService.getUserTagService().tagCreate(tagName); System.out.println(res); + this.tagId = res.getId(); Assert.assertEquals(tagName, res.getName()); } @@ -36,4 +40,12 @@ public class WxMpUserTagServiceImplTest { Assert.assertNotNull(res); } + @Test(dependsOnMethods = { "testTagCreate" }) + public void testTagUpdate() throws Exception { + String tagName = "修改标签" + System.currentTimeMillis(); + Boolean res = this.wxService.getUserTagService().tagUpdate(this.tagId, tagName); + System.out.println(res); + Assert.assertTrue(res); + } + } \ No newline at end of file