From 22a29c51c072a47c17b1d99b65808fee675b3aeb Mon Sep 17 00:00:00 2001 From: ID_Wangqiang Date: Wed, 19 Apr 2023 22:03:26 +0800 Subject: [PATCH] =?UTF-8?q?:new:=20#2981=20=E3=80=90=E4=BC=81=E4=B8=9A?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E3=80=91=E5=A2=9E=E5=8A=A0=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E9=82=AE=E7=AE=B1=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7Userid?= =?UTF-8?q?=E7=9A=84=E6=8E=A5=E5=8F=A3=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chanjar/weixin/cp/api/WxCpUserService.java | 17 +++++++++++++++++ .../weixin/cp/api/impl/WxCpUserServiceImpl.java | 11 +++++++++++ .../weixin/cp/constant/WxCpApiPathConsts.java | 4 ++++ .../cp/api/impl/WxCpUserServiceImplTest.java | 12 ++++++++++++ 4 files changed, 44 insertions(+) diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpUserService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpUserService.java index 0125db625..b397a121a 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpUserService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpUserService.java @@ -174,6 +174,23 @@ public interface WxCpUserService { */ String getUserId(String mobile) throws WxErrorException; + /** + *
+   *
+   * 通过邮箱获取其所对应的userid。
+   *
+   * 请求方式:POST(HTTPS)
+   * 请求地址:https://qyapi.weixin.qq.com/cgi-bin/user/get_userid_by_email?access_token=ACCESS_TOKEN
+   *
+   * 文档地址:https://developer.work.weixin.qq.com/document/path/95895
+   * 
+ * + * @param email 手机号码。长度为5~32个字节 + * @return userid email对应的成员userid + * @throws WxErrorException . + */ + String getUserIdByEmail(String email,int emailType) throws WxErrorException; + /** * 获取外部联系人详情. *
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImpl.java
index 407418d05..f1556d4e3 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImpl.java
@@ -203,6 +203,17 @@ public class WxCpUserServiceImpl implements WxCpUserService {
     return tmpJson.get("userid").getAsString();
   }
 
+  @Override
+  public String getUserIdByEmail(String email, int emailType) throws WxErrorException {
+    JsonObject jsonObject = new JsonObject();
+    jsonObject.addProperty("email", email);
+    jsonObject.addProperty("email_type", emailType);
+    String url = this.mainService.getWxCpConfigStorage().getApiUrl(GET_USER_ID_BY_EMAIL);
+    String responseContent = this.mainService.post(url, jsonObject.toString());
+    JsonObject tmpJson = GsonParser.parse(responseContent);
+    return tmpJson.get("userid").getAsString();
+  }
+
   @Override
   public WxCpExternalContactInfo getExternalContact(String userId) throws WxErrorException {
     String url = this.mainService.getWxCpConfigStorage().getApiUrl(GET_EXTERNAL_CONTACT + userId);
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java
index d03a52f31..be9f4e064 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java
@@ -965,6 +965,10 @@ public interface WxCpApiPathConsts {
      * The constant GET_USER_ID.
      */
     String GET_USER_ID = "/cgi-bin/user/getuserid";
+    /**
+     * The constant GET_USER_ID_BY_EMAIL.
+     */
+    String GET_USER_ID_BY_EMAIL = "/cgi-bin/user/get_userid_by_email";
     /**
      * The constant GET_EXTERNAL_CONTACT.
      */
diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImplTest.java
index 49b2c8e8d..73282c16f 100644
--- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImplTest.java
+++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImplTest.java
@@ -178,6 +178,18 @@ public class WxCpUserServiceImplTest {
     assertNotNull(result);
   }
 
+  /**
+   * Test get user id by email.
+   *
+   * @throws WxErrorException the wx error exception
+   */
+  @Test
+  public void testGetUserIdByEmail() throws WxErrorException {
+    String result = this.wxCpService.getUserService().getUserIdByEmail("xxx",1);
+    System.out.println(result);
+    assertNotNull(result);
+  }
+
   /**
    * Test get external contact.
    */