Skip to content

Commit 2869a08

Browse files
authored
🆕 #2013【企业微信】第三方应用开发增加标签管理的相关接口
1 parent 367bf4e commit 2869a08

File tree

9 files changed

+484
-0
lines changed

9 files changed

+484
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
6+
7+
import java.io.Serializable;
8+
9+
/**
10+
*
11+
* @author zhangq <zhangq002@gmail.com>
12+
* @since 2021-02-14 16:15 16:15
13+
*/
14+
@Data
15+
public class WxCpTpTag implements Serializable {
16+
17+
private static final long serialVersionUID = 581740383760234134L;
18+
19+
@SerializedName("tagid")
20+
private String tagId;
21+
22+
@SerializedName("tagname")
23+
private String tagName;
24+
25+
public static WxCpTpTag deserialize(String json) {
26+
return WxCpGsonBuilder.create().fromJson(json, WxCpTpTag.class);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
4+
5+
/**
6+
* 企业微信第三方开发-增加标签成员成员api响应体
7+
* @author zhangq <zhangq002@gmail.com>
8+
* @since 2021/2/14 16:44
9+
*/
10+
public class WxCpTpTagAddOrRemoveUsersResult extends WxCpTagAddOrRemoveUsersResult {
11+
private static final long serialVersionUID = 3490401800490702052L;
12+
13+
public static WxCpTpTagAddOrRemoveUsersResult deserialize(String json) {
14+
return WxCpGsonBuilder.create().fromJson(json, WxCpTpTagAddOrRemoveUsersResult.class);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
4+
5+
/**
6+
* 获取标签成员接口响应体
7+
* @author zhangq <zhangq002@gmail.com>
8+
* @since 2021/2/14 16:28
9+
*/
10+
public class WxCpTpTagGetResult extends WxCpTagGetResult {
11+
private static final long serialVersionUID = 9051748686315562400L;
12+
13+
public static WxCpTpTagGetResult deserialize(String json) {
14+
return WxCpGsonBuilder.create().fromJson(json, WxCpTpTagGetResult.class);
15+
}
16+
17+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java

+28
Original file line numberDiff line numberDiff line change
@@ -491,4 +491,32 @@ public interface WxCpTpService {
491491
*/
492492
WxJsapiSignature createSuiteJsApiTicketSignature(String url, String authCorpId) throws WxErrorException;
493493

494+
/**
495+
* 使套件accessToken缓存失效
496+
*/
497+
void expireSuiteAccessToken();
498+
499+
/**
500+
* 使机构accessToken缓存失效
501+
* @param authCorpId 机构id
502+
*/
503+
void expireAccessToken(String authCorpId);
504+
505+
/**
506+
* 使机构jsapiticket缓存失效
507+
* @param authCorpId 机构id
508+
*/
509+
void expireAuthCorpJsApiTicket(String authCorpId);
510+
511+
/**
512+
* 使应用jsapiticket失效
513+
* @param authCorpId 机构id
514+
*/
515+
void expireAuthSuiteJsApiTicket(String authCorpId);
516+
517+
/**
518+
* 使供应商accessToken失效
519+
*/
520+
void expireProviderToken();
521+
494522
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package me.chanjar.weixin.cp.tp.service;
2+
3+
import me.chanjar.weixin.common.error.WxErrorException;
4+
import me.chanjar.weixin.cp.bean.WxCpTpTag;
5+
import me.chanjar.weixin.cp.bean.WxCpTpTagAddOrRemoveUsersResult;
6+
import me.chanjar.weixin.cp.bean.WxCpTpTagGetResult;
7+
8+
import java.util.List;
9+
10+
/**
11+
* <pre>
12+
* 企业微信第三方开发-标签相关接口
13+
* </pre>
14+
*
15+
* @author zhangq <zhangq002@gmail.com>
16+
* @since 2021-02-14 16:02
17+
*/
18+
public interface WxCpTpTagService {
19+
/**
20+
* 创建标签.
21+
* <pre>
22+
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token=ACCESS_TOKEN
23+
* 文档地址:https://work.weixin.qq.com/api/doc/90001/90143/90346
24+
* </pre>
25+
*
26+
* @param name 标签名称,长度限制为32个字以内(汉字或英文字母),标签名不可与其他标签重名。
27+
* @param id 标签id,非负整型,指定此参数时新增的标签会生成对应的标签id,不指定时则以目前最大的id自增。
28+
* @return 标签id
29+
* @throws WxErrorException
30+
*/
31+
String create(String name, Integer id) throws WxErrorException;
32+
33+
/**
34+
* 更新标签.
35+
*
36+
* @param tagId 标签id
37+
* @param tagName 标签名
38+
* @throws WxErrorException .
39+
*/
40+
void update(String tagId, String tagName) throws WxErrorException;
41+
42+
/**
43+
* 删除标签.
44+
*
45+
* @param tagId 标签id
46+
* @throws WxErrorException .
47+
*/
48+
void delete(String tagId) throws WxErrorException;
49+
50+
/**
51+
* 获取标签成员
52+
* @param tagId
53+
* @return
54+
* @throws WxErrorException
55+
*/
56+
WxCpTpTagGetResult get(String tagId) throws WxErrorException;
57+
58+
/**
59+
* 增加标签成员.
60+
*
61+
* @param tagId 标签id
62+
* @param userIds 用户ID 列表
63+
* @param partyIds 企业部门ID列表
64+
* @return .
65+
* @throws WxErrorException .
66+
*/
67+
WxCpTpTagAddOrRemoveUsersResult addUsers2Tag(String tagId, List<String> userIds, List<String> partyIds)
68+
throws WxErrorException;
69+
70+
/**
71+
* 移除标签成员.
72+
*
73+
* @param tagId 标签id
74+
* @param userIds 用户id列表
75+
* @param partyIds 企业部门ID列表
76+
* @return .
77+
* @throws WxErrorException .
78+
*/
79+
WxCpTpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List<String> userIds, List<String> partyIds)
80+
throws WxErrorException;
81+
82+
/**
83+
* 获得标签列表.
84+
*
85+
* @return 标签列表
86+
* @throws WxErrorException .
87+
*/
88+
List<WxCpTpTag> listAll() throws WxErrorException;
89+
90+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java

+25
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,31 @@ public WxJsapiSignature createSuiteJsApiTicketSignature(String url, String authC
548548
return doCreateWxJsapiSignature(url, authCorpId, this.getSuiteJsApiTicket(authCorpId));
549549
}
550550

551+
@Override
552+
public void expireSuiteAccessToken() {
553+
this.configStorage.expireSuiteAccessToken();
554+
}
555+
556+
@Override
557+
public void expireAccessToken(String authCorpId) {
558+
this.configStorage.expireAccessToken(authCorpId);
559+
}
560+
561+
@Override
562+
public void expireAuthCorpJsApiTicket(String authCorpId) {
563+
this.configStorage.expireAuthCorpJsApiTicket(authCorpId);
564+
}
565+
566+
@Override
567+
public void expireAuthSuiteJsApiTicket(String authCorpId) {
568+
this.configStorage.expireAuthSuiteJsApiTicket(authCorpId);
569+
}
570+
571+
@Override
572+
public void expireProviderToken() {
573+
this.configStorage.expireProviderToken();
574+
}
575+
551576
private WxJsapiSignature doCreateWxJsapiSignature(String url, String authCorpId, String jsapiTicket) {
552577
long timestamp = System.currentTimeMillis() / 1000;
553578
String noncestr = RandomUtils.getRandomStr();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package me.chanjar.weixin.cp.tp.service.impl;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.JsonPrimitive;
6+
import com.google.gson.reflect.TypeToken;
7+
import lombok.RequiredArgsConstructor;
8+
import me.chanjar.weixin.common.error.WxErrorException;
9+
import me.chanjar.weixin.common.util.json.GsonParser;
10+
import me.chanjar.weixin.cp.bean.WxCpTpTag;
11+
import me.chanjar.weixin.cp.bean.WxCpTpTagAddOrRemoveUsersResult;
12+
import me.chanjar.weixin.cp.bean.WxCpTpTagGetResult;
13+
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
14+
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
15+
import me.chanjar.weixin.cp.tp.service.WxCpTpTagService;
16+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
17+
18+
import java.util.List;
19+
20+
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Tag.*;
21+
22+
/**
23+
* <pre>
24+
* 企业微信第三方开发-标签相关接口,部分照搬了WxCpTagServiceImpl
25+
* </pre>
26+
*
27+
* @author zhangq <zhangq002@gmail.com>
28+
* @since 2021-02-14 16:02
29+
*/
30+
@RequiredArgsConstructor
31+
public class WxCpTpTagServiceImpl implements WxCpTpTagService {
32+
private final WxCpTpService mainService;
33+
34+
@Override
35+
public String create(String name, Integer id) throws WxErrorException {
36+
JsonObject o = new JsonObject();
37+
o.addProperty("tagname", name);
38+
39+
if (id != null) {
40+
o.addProperty("tagid", id);
41+
}
42+
return this.create(o);
43+
}
44+
45+
private String create(JsonObject param) throws WxErrorException {
46+
String url = getWxCpTpConfigStorage().getApiUrl(TAG_CREATE);
47+
String responseContent = this.mainService.post(url, param.toString());
48+
JsonObject jsonObject = GsonParser.parse(responseContent);
49+
return jsonObject.get("tagid").getAsString();
50+
}
51+
52+
@Override
53+
public void update(String tagId, String tagName) throws WxErrorException {
54+
String url = getWxCpTpConfigStorage().getApiUrl(TAG_UPDATE);
55+
JsonObject o = new JsonObject();
56+
o.addProperty("tagid", tagId);
57+
o.addProperty("tagname", tagName);
58+
this.mainService.post(url, o.toString());
59+
}
60+
61+
@Override
62+
public void delete(String tagId) throws WxErrorException {
63+
String url = String.format(getWxCpTpConfigStorage().getApiUrl(TAG_DELETE), tagId);
64+
this.mainService.get(url, null);
65+
}
66+
67+
@Override
68+
public List<WxCpTpTag> listAll() throws WxErrorException {
69+
String url = getWxCpTpConfigStorage().getApiUrl(TAG_LIST);
70+
String responseContent = this.mainService.get(url, null);
71+
JsonObject tmpJson = GsonParser.parse(responseContent);
72+
return WxCpGsonBuilder.create().fromJson(tmpJson.get("taglist"), new TypeToken<List<WxCpTpTag>>() {
73+
// do nothing
74+
}.getType());
75+
}
76+
77+
@Override
78+
public WxCpTpTagGetResult get(String tagId) throws WxErrorException {
79+
if (tagId == null) {
80+
throw new IllegalArgumentException("缺少tagId参数");
81+
}
82+
83+
String url = String.format(getWxCpTpConfigStorage().getApiUrl(TAG_GET), tagId);
84+
String responseContent = this.mainService.get(url, null);
85+
return WxCpTpTagGetResult.deserialize(responseContent);
86+
}
87+
88+
@Override
89+
public WxCpTpTagAddOrRemoveUsersResult addUsers2Tag(String tagId, List<String> userIds, List<String> partyIds)
90+
throws WxErrorException {
91+
String url = getWxCpTpConfigStorage().getApiUrl(TAG_ADD_TAG_USERS);
92+
JsonObject jsonObject = new JsonObject();
93+
jsonObject.addProperty("tagid", tagId);
94+
this.addUserIdsAndPartyIdsToJson(userIds, partyIds, jsonObject);
95+
96+
return WxCpTpTagAddOrRemoveUsersResult.deserialize(this.mainService.post(url, jsonObject.toString()));
97+
}
98+
99+
@Override
100+
public WxCpTpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List<String> userIds, List<String> partyIds)
101+
throws WxErrorException {
102+
String url = getWxCpTpConfigStorage().getApiUrl(TAG_DEL_TAG_USERS);
103+
JsonObject jsonObject = new JsonObject();
104+
jsonObject.addProperty("tagid", tagId);
105+
this.addUserIdsAndPartyIdsToJson(userIds, partyIds, jsonObject);
106+
107+
return WxCpTpTagAddOrRemoveUsersResult.deserialize(this.mainService.post(url, jsonObject.toString()));
108+
}
109+
110+
private void addUserIdsAndPartyIdsToJson(List<String> userIds, List<String> partyIds, JsonObject jsonObject) {
111+
if (userIds != null) {
112+
JsonArray jsonArray = new JsonArray();
113+
for (String userId : userIds) {
114+
jsonArray.add(new JsonPrimitive(userId));
115+
}
116+
jsonObject.add("userlist", jsonArray);
117+
}
118+
119+
if (partyIds != null) {
120+
JsonArray jsonArray = new JsonArray();
121+
for (String userId : partyIds) {
122+
jsonArray.add(new JsonPrimitive(userId));
123+
}
124+
jsonObject.add("partylist", jsonArray);
125+
}
126+
}
127+
128+
@SuppressWarnings("deprecation")
129+
private WxCpTpConfigStorage getWxCpTpConfigStorage() {
130+
return this.mainService.getWxCpTpConfigStorage();
131+
}
132+
}

0 commit comments

Comments
 (0)