Skip to content

Commit 56977a6

Browse files
zhanyan-Ader1yzhanyan
andauthored
🆕 #3405 【企业微信】增加获取已服务的外部联系人的接口
Co-authored-by: zhanyan <[email protected]>
1 parent 3f3c37d commit 56977a6

File tree

5 files changed

+161
-23
lines changed

5 files changed

+161
-23
lines changed

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package me.chanjar.weixin.cp.api;
22

3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.util.Date;
7+
import java.util.List;
38
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
49
import me.chanjar.weixin.common.error.WxErrorException;
510
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
@@ -11,12 +16,6 @@
1116
import me.chanjar.weixin.cp.bean.external.interceptrule.WxCpInterceptRuleInfo;
1217
import me.chanjar.weixin.cp.bean.external.interceptrule.WxCpInterceptRuleList;
1318

14-
import java.io.File;
15-
import java.io.IOException;
16-
import java.io.InputStream;
17-
import java.util.Date;
18-
import java.util.List;
19-
2019
/**
2120
* <pre>
2221
* 外部联系人管理接口,企业微信的外部联系人的接口和通讯录接口已经拆离
@@ -381,6 +380,24 @@ WxCpExternalContactBatchInfo getContactDetailBatch(String[] userIdList, String c
381380
Integer limit)
382381
throws WxErrorException;
383382

383+
/**
384+
* 获取已服务的外部联系人
385+
* <pre>
386+
* 企业可通过此接口获取所有已服务的外部联系人,及其添加人和加入的群聊。
387+
* 外部联系人分为客户和其他外部联系人,如果是客户,接口将返回外部联系人临时ID和externaluserid;如果是其他外部联系人,接口将只返回外部联系人临时ID。
388+
* 请求方式:POST(HTTPS)
389+
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/externalcontact/contact_list?access_token=ACCESS_TOKEN
390+
* 文档地址: https://developer.work.weixin.qq.com/document/path/99434
391+
* </pre>
392+
*
393+
* @param cursor the cursor
394+
* @param limit the limit
395+
* @return 已服务的外部联系人列表
396+
* @throws WxErrorException .
397+
* @apiNote 企业可通过外部联系人临时ID排除重复数据,外部联系人临时ID有效期为4小时。
398+
*/
399+
WxCpExternalContactListInfo getContactList(String cursor, Integer limit) throws WxErrorException;
400+
384401
/**
385402
* 修改客户备注信息.
386403
* <pre>

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
package me.chanjar.weixin.cp.api.impl;
22

3+
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.ExternalContact.*;
4+
35
import com.google.gson.Gson;
46
import com.google.gson.JsonObject;
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.util.Collections;
11+
import java.util.Date;
12+
import java.util.List;
13+
import java.util.UUID;
514
import lombok.NonNull;
615
import lombok.RequiredArgsConstructor;
716
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
@@ -26,16 +35,6 @@
2635
import org.apache.commons.lang3.ArrayUtils;
2736
import org.apache.commons.lang3.StringUtils;
2837

29-
import java.io.File;
30-
import java.io.IOException;
31-
import java.io.InputStream;
32-
import java.util.Collections;
33-
import java.util.Date;
34-
import java.util.List;
35-
import java.util.UUID;
36-
37-
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.ExternalContact.*;
38-
3938
/**
4039
* The type Wx cp external contact service.
4140
*
@@ -215,6 +214,20 @@ public WxCpExternalContactBatchInfo getContactDetailBatch(String[] userIdList, S
215214
return WxCpExternalContactBatchInfo.fromJson(responseContent);
216215
}
217216

217+
@Override
218+
public WxCpExternalContactListInfo getContactList(String cursor, Integer limit) throws WxErrorException {
219+
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(GET_CONTACT_LIST);
220+
JsonObject json = new JsonObject();
221+
if (StringUtils.isNotBlank(cursor)) {
222+
json.addProperty("cursor", cursor);
223+
}
224+
if (limit != null) {
225+
json.addProperty("limit", limit);
226+
}
227+
String responseContent = this.mainService.post(url, json.toString());
228+
return WxCpExternalContactListInfo.fromJson(responseContent);
229+
}
230+
218231
@Override
219232
public void updateRemark(WxCpUpdateRemarkRequest request) throws WxErrorException {
220233
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(UPDATE_REMARK);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package me.chanjar.weixin.cp.bean.external.contact;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import java.io.Serializable;
5+
import java.util.List;
6+
import lombok.Getter;
7+
import lombok.Setter;
8+
import me.chanjar.weixin.cp.api.WxCpService;
9+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
10+
11+
/**
12+
* 获取已服务的外部联系人( <a href="https://developer.work.weixin.qq.com/document/path/99434">参考文档</a>)
13+
* @see WxCpService#getExternalContactService()
14+
* @serial
15+
*/
16+
@Getter
17+
@Setter
18+
public class WxCpExternalContactListInfo implements Serializable {
19+
20+
private static final long serialVersionUID = 7114885886548364396L;
21+
22+
@SerializedName("next_cursor")
23+
private String nextCursor;
24+
25+
@SerializedName("errcode")
26+
private String errcode;
27+
28+
@SerializedName("errmsg")
29+
private String errmsg;
30+
31+
@SerializedName("info_list")
32+
private List<ExternalContactInfo> infoList;
33+
34+
@Getter
35+
@Setter
36+
public static class ExternalContactInfo implements Serializable{
37+
38+
private static final long serialVersionUID = -7400291089462740100L;
39+
40+
/**
41+
* 是否被成员标记为客户
42+
*/
43+
@SerializedName("is_customer")
44+
private Boolean isCustomer;
45+
46+
/**
47+
* 外部联系人临时ID
48+
*/
49+
@SerializedName("tmp_openid")
50+
private String tmpOpenid;
51+
52+
/**
53+
* 外部联系人的externaluserid(如果是客户才返回)
54+
*/
55+
@SerializedName("external_userid")
56+
private String externalUserid;
57+
58+
/**
59+
* 脱敏后的外部联系人昵称(如果是其他外部联系人才返回)
60+
*/
61+
@SerializedName("name")
62+
private String name;
63+
64+
/**
65+
* 添加此外部联系人的企业成员或外部联系人所在群聊的群主userid
66+
*/
67+
@SerializedName("follow_userid")
68+
private String followUserid;
69+
70+
/**
71+
* 外部联系人所在的群聊ID(如果群聊被成员标记为客户群才返回)
72+
*/
73+
@SerializedName("chat_id")
74+
private String chatId;
75+
76+
/**
77+
* 外部联系人所在群聊的群名(如果群聊未被成员标记为客户群才返回)
78+
*/
79+
@SerializedName("chat_name")
80+
private String chatName;
81+
82+
/**
83+
* 外部联系人首次添加/进群的时间
84+
*/
85+
@SerializedName("add_time")
86+
private Long addTime;
87+
}
88+
public static WxCpExternalContactListInfo fromJson(String json) {
89+
return WxCpGsonBuilder.create().fromJson(json, WxCpExternalContactListInfo.class);
90+
}
91+
92+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,9 @@ interface ExternalContact {
11371137
* The constant GET_CONTACT_DETAIL_BATCH.
11381138
*/
11391139
String GET_CONTACT_DETAIL_BATCH = "/cgi-bin/externalcontact/batch/get_by_user?";
1140+
1141+
String GET_CONTACT_LIST = "/cgi-bin/externalcontact/contact_list?";
1142+
11401143
/**
11411144
* The constant UPDATE_REMARK.
11421145
*/

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImplTest.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package me.chanjar.weixin.cp.api.impl;
22

3+
import static org.testng.Assert.assertNotNull;
4+
35
import com.google.common.collect.Lists;
46
import com.google.inject.Inject;
7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.Date;
10+
import java.util.List;
511
import me.chanjar.weixin.common.error.WxErrorException;
612
import me.chanjar.weixin.common.util.XmlUtils;
713
import me.chanjar.weixin.cp.api.ApiTestModule;
@@ -10,6 +16,7 @@
1016
import me.chanjar.weixin.cp.bean.external.*;
1117
import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactBatchInfo;
1218
import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactInfo;
19+
import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactListInfo;
1320
import me.chanjar.weixin.cp.bean.external.msg.Attachment;
1421
import me.chanjar.weixin.cp.bean.external.msg.AttachmentBuilder;
1522
import me.chanjar.weixin.cp.bean.external.msg.Image;
@@ -22,13 +29,6 @@
2229
import org.testng.annotations.Test;
2330
import org.testng.collections.CollectionUtils;
2431

25-
import java.util.ArrayList;
26-
import java.util.Collections;
27-
import java.util.Date;
28-
import java.util.List;
29-
30-
import static org.testng.Assert.assertNotNull;
31-
3232
/**
3333
* The type Wx cp external contact service impl test.
3434
*/
@@ -188,6 +188,19 @@ public void testGetContactDetailBatch() throws WxErrorException {
188188
assertNotNull(result);
189189
}
190190

191+
/**
192+
* Test get contact list.
193+
*
194+
* @throws WxErrorException the wx error exception
195+
*/
196+
@Test
197+
public void testGetContactList() throws WxErrorException {
198+
WxCpExternalContactListInfo result =
199+
this.wxCpService.getExternalContactService().getContactList("", 100);
200+
System.out.println(result);
201+
assertNotNull(result);
202+
}
203+
191204
/**
192205
* Test get corp tag list.
193206
*

0 commit comments

Comments
 (0)