Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【企业微信】为第三方企业开发添加标签管理的功能 #2013

Merged
merged 4 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package me.chanjar.weixin.cp.bean;

import com.google.gson.annotations.SerializedName;
import lombok.Data;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

import java.io.Serializable;

/**
*
* @author zhangq <[email protected]>
* @since 2021-02-14 16:15 16:15
*/
@Data
public class WxCpTpTag implements Serializable {

private static final long serialVersionUID = 581740383760234134L;

@SerializedName("tagid")
private String tagId;

@SerializedName("tagname")
private String tagName;

public static WxCpTpTag deserialize(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpTpTag.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.chanjar.weixin.cp.bean;

import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

/**
* 企业微信第三方开发-增加标签成员成员api响应体
* @author zhangq <[email protected]>
* @since 2021/2/14 16:44
*/
public class WxCpTpTagAddOrRemoveUsersResult extends WxCpTagAddOrRemoveUsersResult {
private static final long serialVersionUID = 3490401800490702052L;

public static WxCpTpTagAddOrRemoveUsersResult deserialize(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpTpTagAddOrRemoveUsersResult.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.chanjar.weixin.cp.bean;

import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

/**
* 获取标签成员接口响应体
* @author zhangq <[email protected]>
* @since 2021/2/14 16:28
*/
public class WxCpTpTagGetResult extends WxCpTagGetResult {
private static final long serialVersionUID = 9051748686315562400L;

public static WxCpTpTagGetResult deserialize(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpTpTagGetResult.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,32 @@ public interface WxCpTpService {
*/
WxJsapiSignature createSuiteJsApiTicketSignature(String url, String authCorpId) throws WxErrorException;

/**
* 使套件accessToken缓存失效
*/
void expireSuiteAccessToken();

/**
* 使机构accessToken缓存失效
* @param authCorpId 机构id
*/
void expireAccessToken(String authCorpId);

/**
* 使机构jsapiticket缓存失效
* @param authCorpId 机构id
*/
void expireAuthCorpJsApiTicket(String authCorpId);

/**
* 使应用jsapiticket失效
* @param authCorpId 机构id
*/
void expireAuthSuiteJsApiTicket(String authCorpId);

/**
* 使供应商accessToken失效
*/
void expireProviderToken();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package me.chanjar.weixin.cp.tp.service;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpTpTag;
import me.chanjar.weixin.cp.bean.WxCpTpTagAddOrRemoveUsersResult;
import me.chanjar.weixin.cp.bean.WxCpTpTagGetResult;

import java.util.List;

/**
* <pre>
* 企业微信第三方开发-标签相关接口
* </pre>
*
* @author zhangq <[email protected]>
* @since 2021-02-14 16:02
*/
public interface WxCpTpTagService {
/**
* 创建标签.
* <pre>
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token=ACCESS_TOKEN
* 文档地址:https://work.weixin.qq.com/api/doc/90001/90143/90346
* </pre>
*
* @param name 标签名称,长度限制为32个字以内(汉字或英文字母),标签名不可与其他标签重名。
* @param id 标签id,非负整型,指定此参数时新增的标签会生成对应的标签id,不指定时则以目前最大的id自增。
* @return 标签id
* @throws WxErrorException
*/
String create(String name, Integer id) throws WxErrorException;

/**
* 更新标签.
*
* @param tagId 标签id
* @param tagName 标签名
* @throws WxErrorException .
*/
void update(String tagId, String tagName) throws WxErrorException;

/**
* 删除标签.
*
* @param tagId 标签id
* @throws WxErrorException .
*/
void delete(String tagId) throws WxErrorException;

/**
* 获取标签成员
* @param tagId
* @return
* @throws WxErrorException
*/
WxCpTpTagGetResult get(String tagId) throws WxErrorException;

/**
* 增加标签成员.
*
* @param tagId 标签id
* @param userIds 用户ID 列表
* @param partyIds 企业部门ID列表
* @return .
* @throws WxErrorException .
*/
WxCpTpTagAddOrRemoveUsersResult addUsers2Tag(String tagId, List<String> userIds, List<String> partyIds)
throws WxErrorException;

/**
* 移除标签成员.
*
* @param tagId 标签id
* @param userIds 用户id列表
* @param partyIds 企业部门ID列表
* @return .
* @throws WxErrorException .
*/
WxCpTpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List<String> userIds, List<String> partyIds)
throws WxErrorException;

/**
* 获得标签列表.
*
* @return 标签列表
* @throws WxErrorException .
*/
List<WxCpTpTag> listAll() throws WxErrorException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,31 @@ public WxJsapiSignature createSuiteJsApiTicketSignature(String url, String authC
return doCreateWxJsapiSignature(url, authCorpId, this.getSuiteJsApiTicket(authCorpId));
}

@Override
public void expireSuiteAccessToken() {
this.configStorage.expireSuiteAccessToken();
}

@Override
public void expireAccessToken(String authCorpId) {
this.configStorage.expireAccessToken(authCorpId);
}

@Override
public void expireAuthCorpJsApiTicket(String authCorpId) {
this.configStorage.expireAuthCorpJsApiTicket(authCorpId);
}

@Override
public void expireAuthSuiteJsApiTicket(String authCorpId) {
this.configStorage.expireAuthSuiteJsApiTicket(authCorpId);
}

@Override
public void expireProviderToken() {
this.configStorage.expireProviderToken();
}

private WxJsapiSignature doCreateWxJsapiSignature(String url, String authCorpId, String jsapiTicket) {
long timestamp = System.currentTimeMillis() / 1000;
String noncestr = RandomUtils.getRandomStr();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package me.chanjar.weixin.cp.tp.service.impl;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.cp.bean.WxCpTpTag;
import me.chanjar.weixin.cp.bean.WxCpTpTagAddOrRemoveUsersResult;
import me.chanjar.weixin.cp.bean.WxCpTpTagGetResult;
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
import me.chanjar.weixin.cp.tp.service.WxCpTpTagService;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

import java.util.List;

import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Tag.*;

/**
* <pre>
* 企业微信第三方开发-标签相关接口,部分照搬了WxCpTagServiceImpl
* </pre>
*
* @author zhangq <[email protected]>
* @since 2021-02-14 16:02
*/
@RequiredArgsConstructor
public class WxCpTpTagServiceImpl implements WxCpTpTagService {
private final WxCpTpService mainService;

@Override
public String create(String name, Integer id) throws WxErrorException {
JsonObject o = new JsonObject();
o.addProperty("tagname", name);

if (id != null) {
o.addProperty("tagid", id);
}
return this.create(o);
}

private String create(JsonObject param) throws WxErrorException {
String url = getWxCpTpConfigStorage().getApiUrl(TAG_CREATE);
String responseContent = this.mainService.post(url, param.toString());
JsonObject jsonObject = GsonParser.parse(responseContent);
return jsonObject.get("tagid").getAsString();
}

@Override
public void update(String tagId, String tagName) throws WxErrorException {
String url = getWxCpTpConfigStorage().getApiUrl(TAG_UPDATE);
JsonObject o = new JsonObject();
o.addProperty("tagid", tagId);
o.addProperty("tagname", tagName);
this.mainService.post(url, o.toString());
}

@Override
public void delete(String tagId) throws WxErrorException {
String url = String.format(getWxCpTpConfigStorage().getApiUrl(TAG_DELETE), tagId);
this.mainService.get(url, null);
}

@Override
public List<WxCpTpTag> listAll() throws WxErrorException {
String url = getWxCpTpConfigStorage().getApiUrl(TAG_LIST);
String responseContent = this.mainService.get(url, null);
JsonObject tmpJson = GsonParser.parse(responseContent);
return WxCpGsonBuilder.create().fromJson(tmpJson.get("taglist"), new TypeToken<List<WxCpTpTag>>() {
// do nothing
}.getType());
}

@Override
public WxCpTpTagGetResult get(String tagId) throws WxErrorException {
if (tagId == null) {
throw new IllegalArgumentException("缺少tagId参数");
}

String url = String.format(getWxCpTpConfigStorage().getApiUrl(TAG_GET), tagId);
String responseContent = this.mainService.get(url, null);
return WxCpTpTagGetResult.deserialize(responseContent);
}

@Override
public WxCpTpTagAddOrRemoveUsersResult addUsers2Tag(String tagId, List<String> userIds, List<String> partyIds)
throws WxErrorException {
String url = getWxCpTpConfigStorage().getApiUrl(TAG_ADD_TAG_USERS);
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("tagid", tagId);
this.addUserIdsAndPartyIdsToJson(userIds, partyIds, jsonObject);

return WxCpTpTagAddOrRemoveUsersResult.deserialize(this.mainService.post(url, jsonObject.toString()));
}

@Override
public WxCpTpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List<String> userIds, List<String> partyIds)
throws WxErrorException {
String url = getWxCpTpConfigStorage().getApiUrl(TAG_DEL_TAG_USERS);
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("tagid", tagId);
this.addUserIdsAndPartyIdsToJson(userIds, partyIds, jsonObject);

return WxCpTpTagAddOrRemoveUsersResult.deserialize(this.mainService.post(url, jsonObject.toString()));
}

private void addUserIdsAndPartyIdsToJson(List<String> userIds, List<String> partyIds, JsonObject jsonObject) {
if (userIds != null) {
JsonArray jsonArray = new JsonArray();
for (String userId : userIds) {
jsonArray.add(new JsonPrimitive(userId));
}
jsonObject.add("userlist", jsonArray);
}

if (partyIds != null) {
JsonArray jsonArray = new JsonArray();
for (String userId : partyIds) {
jsonArray.add(new JsonPrimitive(userId));
}
jsonObject.add("partylist", jsonArray);
}
}

@SuppressWarnings("deprecation")
private WxCpTpConfigStorage getWxCpTpConfigStorage() {
return this.mainService.getWxCpTpConfigStorage();
}
}
Loading