Skip to content

Commit 9780d65

Browse files
committed
添加批量为用户打标签的接口
1 parent 592ac81 commit 9780d65

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎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
@@ -66,4 +66,14 @@ public interface WxMpUserTagService {
6666
*/
6767
WxTagListUser tagListUser(Integer tagId, String nextOpenid) throws WxErrorException;
6868

69+
/**
70+
* <pre>
71+
* 批量为用户打标签
72+
* 详情请见:<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">用户标签管理</a>
73+
* 接口url格式: https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=ACCESS_TOKEN
74+
* </pre>
75+
*
76+
*/
77+
boolean batchTagging(Integer tagId, String[] openids) throws WxErrorException;
78+
6979
}

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

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

3+
import com.google.gson.JsonArray;
34
import com.google.gson.JsonObject;
45
import me.chanjar.weixin.common.bean.result.WxError;
56
import me.chanjar.weixin.common.exception.WxErrorException;
@@ -106,4 +107,27 @@ public WxTagListUser tagListUser(Integer tagId, String nextOpenid) throws WxErro
106107
responseContent);
107108
return WxTagListUser.fromJson(responseContent);
108109
}
110+
111+
@Override
112+
public boolean batchTagging(Integer tagId, String[] openids) throws WxErrorException {
113+
String url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging";
114+
115+
JsonObject json = new JsonObject();
116+
json.addProperty("tagid", tagId);
117+
JsonArray openidArrayJson = new JsonArray();
118+
for (String openid : openids) {
119+
openidArrayJson.add(openid);
120+
}
121+
json.add("openid_list", openidArrayJson);
122+
123+
String responseContent = this.wxMpService.post(url, json.toString());
124+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, json.toString(),
125+
responseContent);
126+
WxError wxError = WxError.fromJson(responseContent);
127+
if (wxError.getErrorCode() == 0) {
128+
return true;
129+
}
130+
131+
throw new WxErrorException(wxError);
132+
}
109133
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,12 @@ public void testTagListUser() throws Exception {
6060
System.out.println(res);
6161
Assert.assertNotNull(res);
6262
}
63+
64+
@Test
65+
public void testBatchTagging() throws Exception {
66+
String[] openids = new String[]{((ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage()).getOpenid()};
67+
boolean res = this.wxService.getUserTagService().batchTagging(this.tagId, openids);
68+
System.out.println(res);
69+
Assert.assertTrue(res);
70+
}
6371
}

0 commit comments

Comments
 (0)