From a6d73a249d575f99e56bfc5a069f86a9368da08f Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Sat, 3 Mar 2018 17:09:56 +0800 Subject: [PATCH] =?UTF-8?q?#409=20=E5=AE=9E=E7=8E=B0=E4=BC=81=E4=B8=9A?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=9A=84userid=E4=B8=8Eopenid=E4=BA=92?= =?UTF-8?q?=E6=8D=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/cp/api/WxCpUserService.java | 41 +++++++++++++++++++ .../cp/api/impl/WxCpUserServiceImpl.java | 36 ++++++++++++++++ .../cp/api/impl/WxCpUserServiceImplTest.java | 27 +++++++++++- 3 files changed, 102 insertions(+), 2 deletions(-) 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 1487d86160..93333fefe6 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 @@ -4,6 +4,7 @@ import me.chanjar.weixin.cp.bean.WxCpUser; import java.util.List; +import java.util.Map; /** *
@@ -90,6 +91,46 @@ public interface WxCpUserService {
    * @param userId     用户的userid
    * @param inviteTips 推送到微信上的提示语(只有认证号可以使用)。当使用微信推送时,该字段默认为“请关注XXX企业号”,邮件邀请时,该字段无效。
    * @return 1:微信邀请 2.邮件邀请
+   * @deprecated api obsoleted. 邀请关注的功能微信企业号已经下线了,
+   * 详细请参考该链接点击查看 https://qy.weixin.qq.com/cgi-bin/homepagenotify?action=get&id=46
    */
+  @Deprecated
   int invite(String userId, String inviteTips) throws WxErrorException;
+
+  /**
+   * 
+   *  userid转openid.
+   *  该接口使用场景为微信支付、微信红包和企业转账。
+   *
+   * 在使用微信支付的功能时,需要自行将企业微信的userid转成openid。
+   * 在使用微信红包功能时,需要将应用id和userid转成appid和openid才能使用。
+   * 注:需要成员使用微信登录企业微信或者关注微信插件才能转成openid
+   *
+   * 文档地址:https://work.weixin.qq.com/api/doc#11279
+   * 
+ * + * @param userId 企业内的成员id + * @param agentId 非必填,整型,仅用于发红包。其它场景该参数不要填,如微信支付、企业转账、电子发票 + * @return map对象,可能包含以下值: + * - openid 企业微信成员userid对应的openid,若有传参agentid,则是针对该agentid的openid。否则是针对企业微信corpid的openid + * - appid 应用的appid,若请求包中不包含agentid则不返回appid。该appid在使用微信红包时会用到 + */ + Map userId2Openid(String userId, Integer agentId) throws WxErrorException; + + /** + *
+   * openid转userid
+   *
+   * 该接口主要应用于使用微信支付、微信红包和企业转账之后的结果查询。
+   * 开发者需要知道某个结果事件的openid对应企业微信内成员的信息时,可以通过调用该接口进行转换查询。
+   * 权限说明:
+   * 管理组需对openid对应的企业微信成员有查看权限。
+   *
+   * 文档地址:https://work.weixin.qq.com/api/doc#11279
+   * 
+ * + * @param openid 在使用微信支付、微信红包和企业转账之后,返回结果的openid + * @return userid 该openid在企业微信对应的成员userid + */ + String openid2UserId(String openid) 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 dbb5a2f34a..f0249e8e11 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 @@ -1,5 +1,6 @@ package me.chanjar.weixin.cp.api.impl; +import com.google.common.collect.Maps; import com.google.gson.*; import com.google.gson.reflect.TypeToken; import me.chanjar.weixin.common.exception.WxErrorException; @@ -10,6 +11,7 @@ import org.apache.commons.lang3.StringUtils; import java.util.List; +import java.util.Map; /** *
@@ -114,6 +116,7 @@ public List listSimpleByDepartment(Integer departId, Boolean fetchChil
   }
 
   @Override
+  @Deprecated
   public int invite(String userId, String inviteTips) throws WxErrorException {
     String url = "https://qyapi.weixin.qq.com/cgi-bin/invite/send";
     JsonObject jsonObject = new JsonObject();
@@ -125,4 +128,37 @@ public int invite(String userId, String inviteTips) throws WxErrorException {
     JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
     return tmpJsonElement.getAsJsonObject().get("type").getAsInt();
   }
+
+  @Override
+  public Map userId2Openid(String userId, Integer agentId) throws WxErrorException {
+    String url = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid";
+    JsonObject jsonObject = new JsonObject();
+    jsonObject.addProperty("userid", userId);
+    if (agentId != null) {
+      jsonObject.addProperty("agentid", agentId);
+    }
+
+    String responseContent = this.mainService.post(url, jsonObject.toString());
+    JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
+    Map result = Maps.newHashMap();
+    if (tmpJsonElement.getAsJsonObject().get("openid") != null) {
+      result.put("openid", tmpJsonElement.getAsJsonObject().get("openid").getAsString());
+    }
+
+    if (tmpJsonElement.getAsJsonObject().get("appid") != null) {
+      result.put("appid", tmpJsonElement.getAsJsonObject().get("appid").getAsString());
+    }
+
+    return result;
+  }
+
+  @Override
+  public String openid2UserId(String openid) throws WxErrorException {
+    String url = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_userid";
+    JsonObject jsonObject = new JsonObject();
+    jsonObject.addProperty("openid", openid);
+    String responseContent = this.mainService.post(url, jsonObject.toString());
+    JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
+    return tmpJsonElement.getAsJsonObject().get("userid").getAsString();
+  }
 }
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 3a7d3debdd..bbb5dcab2d 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
@@ -6,11 +6,14 @@
 import me.chanjar.weixin.cp.bean.WxCpUser;
 import org.apache.commons.lang3.builder.ToStringBuilder;
 import org.apache.commons.lang3.builder.ToStringStyle;
-import org.testng.annotations.*;
+import org.testng.annotations.Guice;
+import org.testng.annotations.Test;
 
 import java.util.List;
+import java.util.Map;
 
-import static org.testng.Assert.*;
+import static org.testng.Assert.assertNotEquals;
+import static org.testng.Assert.assertNotNull;
 
 /**
  * 
@@ -83,4 +86,24 @@ public void testListSimpleByDepartment() throws Exception {
     }
   }
 
+  @Test
+  @Deprecated
+  public void testInvite() throws Exception {
+    int result = this.wxCpService.getUserService().invite(userId, "");
+    System.out.println(result);
+  }
+
+  @Test
+  public void testUserId2Openid() throws Exception {
+    Map result = this.wxCpService.getUserService().userId2Openid(userId, null);
+    System.out.println(result);
+    assertNotNull(result);
+  }
+
+  @Test
+  public void testOpenid2UserId() throws Exception {
+    String result = this.wxCpService.getUserService().openid2UserId(userId);
+    System.out.println(result);
+    assertNotNull(result);
+  }
 }