Skip to content

Commit 22a29c5

Browse files
authored
🆕 #2981 【企业微信】增加根据邮箱获取用户Userid的接口方法
1 parent 087870e commit 22a29c5

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ public interface WxCpUserService {
174174
*/
175175
String getUserId(String mobile) throws WxErrorException;
176176

177+
/**
178+
* <pre>
179+
*
180+
* 通过邮箱获取其所对应的userid。
181+
*
182+
* 请求方式:POST(HTTPS)
183+
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/user/get_userid_by_email?access_token=ACCESS_TOKEN
184+
*
185+
* 文档地址:https://developer.work.weixin.qq.com/document/path/95895
186+
* </pre>
187+
*
188+
* @param email 手机号码。长度为5~32个字节
189+
* @return userid email对应的成员userid
190+
* @throws WxErrorException .
191+
*/
192+
String getUserIdByEmail(String email,int emailType) throws WxErrorException;
193+
177194
/**
178195
* 获取外部联系人详情.
179196
* <pre>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ public String getUserId(String mobile) throws WxErrorException {
203203
return tmpJson.get("userid").getAsString();
204204
}
205205

206+
@Override
207+
public String getUserIdByEmail(String email, int emailType) throws WxErrorException {
208+
JsonObject jsonObject = new JsonObject();
209+
jsonObject.addProperty("email", email);
210+
jsonObject.addProperty("email_type", emailType);
211+
String url = this.mainService.getWxCpConfigStorage().getApiUrl(GET_USER_ID_BY_EMAIL);
212+
String responseContent = this.mainService.post(url, jsonObject.toString());
213+
JsonObject tmpJson = GsonParser.parse(responseContent);
214+
return tmpJson.get("userid").getAsString();
215+
}
216+
206217
@Override
207218
public WxCpExternalContactInfo getExternalContact(String userId) throws WxErrorException {
208219
String url = this.mainService.getWxCpConfigStorage().getApiUrl(GET_EXTERNAL_CONTACT + userId);

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
@@ -965,6 +965,10 @@ interface User {
965965
* The constant GET_USER_ID.
966966
*/
967967
String GET_USER_ID = "/cgi-bin/user/getuserid";
968+
/**
969+
* The constant GET_USER_ID_BY_EMAIL.
970+
*/
971+
String GET_USER_ID_BY_EMAIL = "/cgi-bin/user/get_userid_by_email";
968972
/**
969973
* The constant GET_EXTERNAL_CONTACT.
970974
*/

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ public void testGetUserId() throws WxErrorException {
178178
assertNotNull(result);
179179
}
180180

181+
/**
182+
* Test get user id by email.
183+
*
184+
* @throws WxErrorException the wx error exception
185+
*/
186+
@Test
187+
public void testGetUserIdByEmail() throws WxErrorException {
188+
String result = this.wxCpService.getUserService().getUserIdByEmail("xxx",1);
189+
System.out.println(result);
190+
assertNotNull(result);
191+
}
192+
181193
/**
182194
* Test get external contact.
183195
*/

0 commit comments

Comments
 (0)