Skip to content

Commit 505cdaf

Browse files
committed
#705 企业微信增加获取外部联系人详情接口
1 parent d5d106f commit 505cdaf

File tree

8 files changed

+329
-41
lines changed

8 files changed

+329
-41
lines changed

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package me.chanjar.weixin.cp.api;
22

3+
import java.util.List;
4+
import java.util.Map;
5+
36
import me.chanjar.weixin.common.error.WxErrorException;
47
import me.chanjar.weixin.cp.bean.WxCpInviteResult;
58
import me.chanjar.weixin.cp.bean.WxCpUser;
6-
7-
import java.util.List;
8-
import java.util.Map;
9+
import me.chanjar.weixin.cp.bean.WxCpUserExternalContactInfo;
910

1011
/**
1112
* <pre>
@@ -18,7 +19,7 @@
1819
public interface WxCpUserService {
1920
/**
2021
* <pre>
21-
* 用在二次验证的时候
22+
* 用在二次验证的时候.
2223
* 企业在员工验证成功后,调用本方法告诉企业号平台该员工关注成功。
2324
* </pre>
2425
*
@@ -28,7 +29,7 @@ public interface WxCpUserService {
2829

2930
/**
3031
* <pre>
31-
* 获取部门成员(详情)
32+
* 获取部门成员(详情).
3233
*
3334
* http://qydev.weixin.qq.com/wiki/index.php?title=管理成员#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E6.88.90.E5.91.98.28.E8.AF.A6.E6.83.85.29
3435
* </pre>
@@ -41,7 +42,7 @@ public interface WxCpUserService {
4142

4243
/**
4344
* <pre>
44-
* 获取部门成员
45+
* 获取部门成员.
4546
*
4647
* http://qydev.weixin.qq.com/wiki/index.php?title=管理成员#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E6.88.90.E5.91.98
4748
* </pre>
@@ -53,22 +54,22 @@ public interface WxCpUserService {
5354
List<WxCpUser> listSimpleByDepartment(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException;
5455

5556
/**
56-
* 新建用户
57+
* 新建用户.
5758
*
5859
* @param user 用户对象
5960
*/
6061
void create(WxCpUser user) throws WxErrorException;
6162

6263
/**
63-
* 更新用户
64+
* 更新用户.
6465
*
6566
* @param user 用户对象
6667
*/
6768
void update(WxCpUser user) throws WxErrorException;
6869

6970
/**
7071
* <pre>
71-
* 删除用户/批量删除成员
72+
* 删除用户/批量删除成员.
7273
* http://qydev.weixin.qq.com/wiki/index.php?title=管理成员#.E6.89.B9.E9.87.8F.E5.88.A0.E9.99.A4.E6.88.90.E5.91.98
7374
* </pre>
7475
*
@@ -77,15 +78,15 @@ public interface WxCpUserService {
7778
void delete(String... userIds) throws WxErrorException;
7879

7980
/**
80-
* 获取用户
81+
* 获取用户.
8182
*
8283
* @param userid 用户id
8384
*/
8485
WxCpUser getById(String userid) throws WxErrorException;
8586

8687
/**
8788
* <pre>
88-
* 邀请成员
89+
* 邀请成员.
8990
* 企业可通过接口批量邀请成员使用企业微信,邀请后将通过短信或邮件下发通知。
9091
* 请求方式:POST(HTTPS)
9192
* 请求地址: https://qyapi.weixin.qq.com/cgi-bin/batch/invite?access_token=ACCESS_TOKEN
@@ -113,14 +114,14 @@ public interface WxCpUserService {
113114
* @param userId 企业内的成员id
114115
* @param agentId 非必填,整型,仅用于发红包。其它场景该参数不要填,如微信支付、企业转账、电子发票
115116
* @return map对象,可能包含以下值:
116-
* - openid 企业微信成员userid对应的openid,若有传参agentid,则是针对该agentid的openid。否则是针对企业微信corpid的openid
117-
* - appid 应用的appid,若请求包中不包含agentid则不返回appid。该appid在使用微信红包时会用到
117+
* - openid 企业微信成员userid对应的openid,若有传参agentid,则是针对该agentid的openid。否则是针对企业微信corpid的openid
118+
* - appid 应用的appid,若请求包中不包含agentid则不返回appid。该appid在使用微信红包时会用到
118119
*/
119120
Map<String, String> userId2Openid(String userId, Integer agentId) throws WxErrorException;
120121

121122
/**
122123
* <pre>
123-
* openid转userid
124+
* openid转userid.
124125
*
125126
* 该接口主要应用于使用微信支付、微信红包和企业转账之后的结果查询。
126127
* 开发者需要知道某个结果事件的openid对应企业微信内成员的信息时,可以通过调用该接口进行转换查询。
@@ -134,4 +135,17 @@ public interface WxCpUserService {
134135
* @return userid 该openid在企业微信对应的成员userid
135136
*/
136137
String openid2UserId(String openid) throws WxErrorException;
138+
139+
/**
140+
* 获取外部联系人详情.
141+
* <pre>
142+
* 企业可通过此接口,根据外部联系人的userid,拉取外部联系人详情。权限说明:
143+
* 企业需要使用外部联系人管理secret所获取的accesstoken来调用
144+
* 第三方应用需拥有“企业客户”权限。
145+
* 第三方应用调用时,返回的跟进人follow_user仅包含应用可见范围之内的成员。
146+
* </pre>
147+
*
148+
* @param userId 外部联系人的userid
149+
*/
150+
WxCpUserExternalContactInfo getExternalContact(String userId) throws WxErrorException;
137151
}

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

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

3+
import java.util.List;
4+
import java.util.Map;
5+
36
import com.google.common.collect.Maps;
4-
import com.google.gson.*;
7+
import com.google.gson.JsonArray;
8+
import com.google.gson.JsonElement;
9+
import com.google.gson.JsonObject;
10+
import com.google.gson.JsonParser;
11+
import com.google.gson.JsonPrimitive;
512
import com.google.gson.reflect.TypeToken;
613
import me.chanjar.weixin.common.error.WxErrorException;
714
import me.chanjar.weixin.cp.api.WxCpService;
815
import me.chanjar.weixin.cp.api.WxCpUserService;
916
import me.chanjar.weixin.cp.bean.WxCpInviteResult;
1017
import me.chanjar.weixin.cp.bean.WxCpUser;
18+
import me.chanjar.weixin.cp.bean.WxCpUserExternalContactInfo;
1119
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
1220

13-
import java.util.List;
14-
import java.util.Map;
15-
1621
/**
1722
* <pre>
1823
* Created by BinaryWang on 2017/6/24.
@@ -178,4 +183,11 @@ public String openid2UserId(String openid) throws WxErrorException {
178183
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
179184
return tmpJsonElement.getAsJsonObject().get("userid").getAsString();
180185
}
186+
187+
@Override
188+
public WxCpUserExternalContactInfo getExternalContact(String userId) throws WxErrorException {
189+
String url = "https://qyapi.weixin.qq.com/cgi-bin/crm/get_external_contact?external_userid=" + userId;
190+
String responseContent = this.mainService.get(url, null);
191+
return WxCpUserExternalContactInfo.fromJson(responseContent);
192+
}
181193
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpUser.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public class WxCpUser implements Serializable {
3939
/**
4040
* 成员对外信息.
4141
*/
42-
private List<ExternalAttr> externalAttrs = new ArrayList<>();
42+
private List<ExternalAttribute> externalAttrs = new ArrayList<>();
4343

44-
public void addExternalAttr(ExternalAttr externalAttr) {
44+
public void addExternalAttr(ExternalAttribute externalAttr) {
4545
this.externalAttrs.add(externalAttr);
4646
}
4747

@@ -68,7 +68,7 @@ public static class Attr {
6868
@Builder
6969
@NoArgsConstructor
7070
@AllArgsConstructor
71-
public static class ExternalAttr {
71+
public static class ExternalAttribute {
7272
/**
7373
* 属性类型: 0-本文 1-网页 2-小程序.
7474
*/
@@ -99,5 +99,4 @@ public static class ExternalAttr {
9999
*/
100100
private String pagePath;
101101
}
102-
103102
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
import java.util.List;
4+
5+
import com.google.gson.annotations.SerializedName;
6+
import lombok.AllArgsConstructor;
7+
import lombok.Builder;
8+
import lombok.Data;
9+
import lombok.Getter;
10+
import lombok.NoArgsConstructor;
11+
import lombok.Setter;
12+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
13+
14+
/**
15+
* <pre>
16+
* 外部联系人详情
17+
* Created by Binary Wang on 2018/9/16.
18+
* 参考文档:https://work.weixin.qq.com/api/doc#13878
19+
* </pre>
20+
*
21+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
22+
*/
23+
@Getter
24+
@Setter
25+
public class WxCpUserExternalContactInfo {
26+
@SerializedName("external_contact")
27+
private ExternalContact externalContact;
28+
29+
@SerializedName("follow_user")
30+
private List<FollowedUser> followedUsers;
31+
32+
@Getter
33+
@Setter
34+
public static class ExternalContact {
35+
@SerializedName("external_userid")
36+
private String externalUserId;
37+
38+
@SerializedName("position")
39+
private String position;
40+
41+
@SerializedName("name")
42+
private String name;
43+
44+
@SerializedName("avatar")
45+
private String avatar;
46+
47+
@SerializedName("corp_name")
48+
private String corpName;
49+
50+
@SerializedName("corp_full_name")
51+
private String corpFullName;
52+
53+
@SerializedName("type")
54+
private Integer type;
55+
56+
@SerializedName("gender")
57+
private Integer gender;
58+
59+
@SerializedName("unionid")
60+
private String unionId;
61+
62+
@SerializedName("external_profile")
63+
private ExternalProfile externalProfile;
64+
}
65+
66+
@Setter
67+
@Getter
68+
public static class ExternalProfile {
69+
@SerializedName("external_attr")
70+
private List<ExternalAttribute> externalAttrs;
71+
}
72+
73+
@Data
74+
@Builder
75+
@NoArgsConstructor
76+
@AllArgsConstructor
77+
public static class ExternalAttribute {
78+
@Setter
79+
@Getter
80+
public static class Text {
81+
private String value;
82+
}
83+
84+
@Setter
85+
@Getter
86+
public static class Web {
87+
private String title;
88+
private String url;
89+
}
90+
91+
@Setter
92+
@Getter
93+
public static class MiniProgram {
94+
@SerializedName("pagepath")
95+
private String pagePath;
96+
private String appid;
97+
private String title;
98+
}
99+
100+
private int type;
101+
102+
private String name;
103+
104+
private Text text;
105+
106+
private Web web;
107+
108+
@SerializedName("miniprogram")
109+
private MiniProgram miniProgram;
110+
}
111+
112+
@Setter
113+
@Getter
114+
public static class FollowedUser {
115+
@SerializedName("userid")
116+
private String userId;
117+
private String remark;
118+
private String description;
119+
@SerializedName("createtime")
120+
private Long createTime;
121+
}
122+
123+
public static WxCpUserExternalContactInfo fromJson(String json) {
124+
return WxCpGsonBuilder.create().fromJson(json, WxCpUserExternalContactInfo.class);
125+
}
126+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpUserGsonAdapter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationC
8282
switch (type) {
8383
case 0: {
8484
user.getExternalAttrs()
85-
.add(WxCpUser.ExternalAttr.builder()
85+
.add(WxCpUser.ExternalAttribute.builder()
8686
.type(type)
8787
.name(name)
8888
.value(GsonHelper.getString(element.getAsJsonObject().get("text").getAsJsonObject(), "value"))
@@ -93,7 +93,7 @@ public WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationC
9393
case 1: {
9494
final JsonObject web = element.getAsJsonObject().get("web").getAsJsonObject();
9595
user.getExternalAttrs()
96-
.add(WxCpUser.ExternalAttr.builder()
96+
.add(WxCpUser.ExternalAttribute.builder()
9797
.type(type)
9898
.name(name)
9999
.url(GsonHelper.getString(web, "url"))
@@ -105,7 +105,7 @@ public WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationC
105105
case 2: {
106106
final JsonObject miniprogram = element.getAsJsonObject().get("miniprogram").getAsJsonObject();
107107
user.getExternalAttrs()
108-
.add(WxCpUser.ExternalAttr.builder()
108+
.add(WxCpUser.ExternalAttribute.builder()
109109
.type(type)
110110
.name(name)
111111
.appid(GsonHelper.getString(miniprogram, "appid"))
@@ -196,7 +196,7 @@ public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationCon
196196

197197
if (user.getExternalAttrs().size() > 0) {
198198
JsonArray attrsJsonArray = new JsonArray();
199-
for (WxCpUser.ExternalAttr attr : user.getExternalAttrs()) {
199+
for (WxCpUser.ExternalAttribute attr : user.getExternalAttrs()) {
200200
JsonObject attrJson = new JsonObject();
201201
attrJson.addProperty("type",attr.getType());
202202
attrJson.addProperty("name", attr.getName());

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

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

3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import org.apache.commons.lang3.builder.ToStringBuilder;
7+
import org.apache.commons.lang3.builder.ToStringStyle;
8+
import org.testng.annotations.*;
9+
310
import com.google.common.collect.Lists;
411
import com.google.inject.Inject;
12+
import me.chanjar.weixin.common.error.WxErrorException;
513
import me.chanjar.weixin.cp.api.ApiTestModule;
614
import me.chanjar.weixin.cp.api.WxCpService;
715
import me.chanjar.weixin.cp.bean.Gender;
816
import me.chanjar.weixin.cp.bean.WxCpInviteResult;
917
import me.chanjar.weixin.cp.bean.WxCpUser;
10-
import org.apache.commons.lang3.builder.ToStringBuilder;
11-
import org.apache.commons.lang3.builder.ToStringStyle;
12-
import org.testng.annotations.Guice;
13-
import org.testng.annotations.Test;
18+
import me.chanjar.weixin.cp.bean.WxCpUserExternalContactInfo;
1419

15-
import java.util.List;
16-
import java.util.Map;
17-
18-
import static org.testng.Assert.assertNotEquals;
19-
import static org.testng.Assert.assertNotNull;
20+
import static org.testng.Assert.*;
2021

2122
/**
2223
* <pre>
@@ -109,4 +110,11 @@ public void testOpenid2UserId() throws Exception {
109110
System.out.println(result);
110111
assertNotNull(result);
111112
}
113+
114+
@Test
115+
public void testGetExternalContact() throws WxErrorException {
116+
WxCpUserExternalContactInfo result = this.wxCpService.getUserService().getExternalContact(userId);
117+
System.out.println(result);
118+
assertNotNull(result);
119+
}
112120
}

0 commit comments

Comments
 (0)