Skip to content

Commit bb65189

Browse files
🆕 #2834 【企业微信】新增将代开发应用或第三方应用获取的密文open_userid转换为明文userid的接口
1 parent 8a161cf commit bb65189

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpUserService.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import me.chanjar.weixin.common.error.WxErrorException;
44
import me.chanjar.weixin.cp.bean.WxCpInviteResult;
5+
import me.chanjar.weixin.cp.bean.WxCpOpenUseridToUseridResult;
56
import me.chanjar.weixin.cp.bean.WxCpUser;
67
import me.chanjar.weixin.cp.bean.WxCpUseridToOpenUseridResult;
78
import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactInfo;
@@ -233,6 +234,24 @@ public interface WxCpUserService {
233234
*/
234235
WxCpUseridToOpenUseridResult useridToOpenUserid(ArrayList<String> useridList) throws WxErrorException;
235236

237+
/**
238+
* open_userid转换为userid
239+
* 将代开发应用或第三方应用获取的密文open_userid转换为明文userid
240+
* <pre>
241+
* 文档地址:<a href="https://developer.work.weixin.qq.com/document/path/95884#userid%E8%BD%AC%E6%8D%A2">https://developer.work.weixin.qq.com/document/path/95884#userid%E8%BD%AC%E6%8D%A2</a>
242+
*
243+
* 权限说明:
244+
*
245+
* 需要使用自建应用或基础应用的access_token
246+
* 成员需要同时在access_token和source_agentid所对应应用的可见范围内
247+
* </pre>
248+
* @param openUseridList open_userid列表,最多不超过1000个。必须是source_agentid对应的应用所获取
249+
* @param sourceAgentId 企业授权的代开发自建应用或第三方应用的agentid
250+
* @return the WxCpOpenUseridToUseridResult
251+
* @throws WxErrorException the wx error exception
252+
*/
253+
WxCpOpenUseridToUseridResult openUseridToUserid(List<String> openUseridList, String sourceAgentId) throws WxErrorException;
254+
236255
/**
237256
* 获取成员ID列表
238257
* 获取企业成员的userid与对应的部门ID列表,预计于2022年8月8号发布。若需要获取其他字段,参见「适配建议」。

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import me.chanjar.weixin.cp.api.WxCpService;
1212
import me.chanjar.weixin.cp.api.WxCpUserService;
1313
import me.chanjar.weixin.cp.bean.WxCpInviteResult;
14+
import me.chanjar.weixin.cp.bean.WxCpOpenUseridToUseridResult;
1415
import me.chanjar.weixin.cp.bean.WxCpUser;
1516
import me.chanjar.weixin.cp.bean.WxCpUseridToOpenUseridResult;
1617
import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactInfo;
@@ -240,6 +241,20 @@ public WxCpUseridToOpenUseridResult useridToOpenUserid(ArrayList<String> useridL
240241
return WxCpUseridToOpenUseridResult.fromJson(responseContent);
241242
}
242243

244+
@Override
245+
public WxCpOpenUseridToUseridResult openUseridToUserid(List<String> openUseridList, String sourceAgentId) throws WxErrorException {
246+
JsonObject jsonObject = new JsonObject();
247+
JsonArray jsonArray = new JsonArray();
248+
for (String openUserid : openUseridList) {
249+
jsonArray.add(openUserid);
250+
}
251+
jsonObject.add("open_userid_list", jsonArray);
252+
jsonObject.addProperty("source_agentid", sourceAgentId);
253+
String url = this.mainService.getWxCpConfigStorage().getApiUrl(OPEN_USERID_TO_USERID);
254+
String responseContent = this.mainService.post(url, jsonObject.toString());
255+
return WxCpOpenUseridToUseridResult.fromJson(responseContent);
256+
}
257+
243258
@Override
244259
public WxCpDeptUserResult getUserListId(String cursor, Integer limit) throws WxErrorException {
245260
String apiUrl = this.mainService.getWxCpConfigStorage().getApiUrl(USER_LIST_ID);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
* userid转换
11+
* 将代开发应用或第三方应用获取的密文open_userid转换为明文userid
12+
* 中间对象
13+
* @author yiyingcanfeng
14+
*/
15+
@Data
16+
public class WxCpOpenUseridToUserid implements Serializable {
17+
private static final long serialVersionUID = 1714909184316350423L;
18+
19+
@Override
20+
public String toString() {
21+
return WxCpGsonBuilder.create().toJson(this);
22+
}
23+
24+
/**
25+
* From json wx cp open userid to userid result.
26+
*
27+
* @param json the json
28+
* @return the wx cp open userid to userid result.
29+
*/
30+
public static WxCpOpenUseridToUserid fromJson(String json) {
31+
return WxCpGsonBuilder.create().fromJson(json, WxCpOpenUseridToUserid.class);
32+
}
33+
34+
@SerializedName("userid")
35+
private String userid;
36+
37+
@SerializedName("open_userid")
38+
private String openUserid;
39+
40+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
import java.util.List;
9+
10+
/**
11+
* userid转换
12+
* 将代开发应用或第三方应用获取的密文open_userid转换为明文userid
13+
* @author yiyingcanfeng
14+
*/
15+
@Data
16+
public class WxCpOpenUseridToUseridResult implements Serializable {
17+
private static final long serialVersionUID = 5179329535139861515L;
18+
19+
@Override
20+
public String toString() {
21+
return WxCpGsonBuilder.create().toJson(this);
22+
}
23+
24+
/**
25+
* From json wx cp open userid to userid result.
26+
*
27+
* @param json the json
28+
* @return the wx cp open userid to userid result
29+
*/
30+
public static WxCpOpenUseridToUseridResult fromJson(String json) {
31+
return WxCpGsonBuilder.create().fromJson(json, WxCpOpenUseridToUseridResult.class);
32+
}
33+
34+
@SerializedName("errcode")
35+
private Integer errCode;
36+
37+
@SerializedName("errmsg")
38+
private String errMsg;
39+
40+
@SerializedName("userid_list")
41+
private List<WxCpUseridToOpenUserid> useridList;
42+
43+
@SerializedName("invalid_open_userid_list")
44+
private List<String> invalidOpenUseridList;
45+
46+
47+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,10 @@ interface User {
931931
* The constant USERID_TO_OPEN_USERID.
932932
*/
933933
String USERID_TO_OPEN_USERID = "/cgi-bin/batch/userid_to_openuserid";
934+
/**
935+
* The constant OPEN_USERID_TO_USERID.
936+
*/
937+
String OPEN_USERID_TO_USERID = "/cgi-bin/batch/openuserid_to_userid";
934938

935939
/**
936940
* The constant USER_LIST_ID.

0 commit comments

Comments
 (0)