Skip to content

Commit 214661b

Browse files
committed
增加用户标签修改接口
1 parent 5d95f37 commit 214661b

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,14 @@ public interface WxMpUserTagService {
3535
*/
3636
List<WxUserTag> tagGet() throws WxErrorException;
3737

38+
/**
39+
* <pre>
40+
* 编辑标签
41+
* 详情请见:<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">用户标签管理</a>
42+
* 接口url格式: https://api.weixin.qq.com/cgi-bin/tags/update?access_token=ACCESS_TOKEN
43+
* </pre>
44+
*
45+
*/
46+
Boolean tagUpdate(Integer id, String name) throws WxErrorException;
47+
3848
}

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.google.gson.JsonObject;
99

10+
import me.chanjar.weixin.common.bean.result.WxError;
1011
import me.chanjar.weixin.common.exception.WxErrorException;
1112
import me.chanjar.weixin.mp.api.WxMpService;
1213
import me.chanjar.weixin.mp.api.WxMpUserTagService;
@@ -32,12 +33,12 @@ public WxMpUserTagServiceImpl(WxMpService wxMpService) {
3233
public WxUserTag tagCreate(String name) throws WxErrorException {
3334
String url = API_URL_PREFIX + "/create";
3435
JsonObject json = new JsonObject();
35-
JsonObject groupJson = new JsonObject();
36-
groupJson.addProperty("name", name);
37-
json.add("tag", groupJson);
36+
JsonObject tagJson = new JsonObject();
37+
tagJson.addProperty("name", name);
38+
json.add("tag", tagJson);
3839

3940
String responseContent = this.wxMpService.post(url, json.toString());
40-
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, name,
41+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, json.toString(),
4142
responseContent);
4243
return WxUserTag.fromJson(responseContent);
4344
}
@@ -51,4 +52,20 @@ public List<WxUserTag> tagGet() throws WxErrorException {
5152
responseContent);
5253
return WxUserTag.listFromJson(responseContent);
5354
}
55+
56+
@Override
57+
public Boolean tagUpdate(Integer id, String name) throws WxErrorException {
58+
String url = API_URL_PREFIX + "/update";
59+
60+
JsonObject json = new JsonObject();
61+
JsonObject tagJson = new JsonObject();
62+
tagJson.addProperty("id", id);
63+
tagJson.addProperty("name", name);
64+
json.add("tag", tagJson);
65+
66+
String responseContent = this.wxMpService.post(url, json.toString());
67+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, json.toString(), responseContent);
68+
WxError wxError = WxError.fromJson(responseContent);
69+
return wxError.getErrorCode() == 0;
70+
}
5471
}

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpUserTagServiceImplTest.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3-
import com.google.inject.Inject;
4-
import me.chanjar.weixin.mp.api.ApiTestModule;
5-
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
6-
73
import java.util.List;
84

95
import org.testng.Assert;
106
import org.testng.annotations.Guice;
117
import org.testng.annotations.Test;
128

9+
import com.google.inject.Inject;
10+
11+
import me.chanjar.weixin.mp.api.ApiTestModule;
12+
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
13+
1314
/**
1415
*
1516
* @author binarywang(https://github.com/binarywang)
@@ -21,11 +22,14 @@ public class WxMpUserTagServiceImplTest {
2122
@Inject
2223
protected WxMpServiceImpl wxService;
2324

25+
private Integer tagId;
26+
2427
@Test
2528
public void testTagCreate() throws Exception {
26-
String tagName = "测试标签";
29+
String tagName = "测试标签" + System.currentTimeMillis();
2730
WxUserTag res = this.wxService.getUserTagService().tagCreate(tagName);
2831
System.out.println(res);
32+
this.tagId = res.getId();
2933
Assert.assertEquals(tagName, res.getName());
3034
}
3135

@@ -36,4 +40,12 @@ public void testTagGet() throws Exception {
3640
Assert.assertNotNull(res);
3741
}
3842

43+
@Test(dependsOnMethods = { "testTagCreate" })
44+
public void testTagUpdate() throws Exception {
45+
String tagName = "修改标签" + System.currentTimeMillis();
46+
Boolean res = this.wxService.getUserTagService().tagUpdate(this.tagId, tagName);
47+
System.out.println(res);
48+
Assert.assertTrue(res);
49+
}
50+
3951
}

0 commit comments

Comments
 (0)