Skip to content

Commit

Permalink
#409 实现企业微信的userid与openid互换接口
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Mar 3, 2018
1 parent 0b7c1da commit a6d73a2
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.chanjar.weixin.cp.bean.WxCpUser;

import java.util.List;
import java.util.Map;

/**
* <pre>
Expand Down Expand Up @@ -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;

/**
* <pre>
* userid转openid.
* 该接口使用场景为微信支付、微信红包和企业转账。
*
* 在使用微信支付的功能时,需要自行将企业微信的userid转成openid。
* 在使用微信红包功能时,需要将应用id和userid转成appid和openid才能使用。
* 注:需要成员使用微信登录企业微信或者关注微信插件才能转成openid
*
* 文档地址:https://work.weixin.qq.com/api/doc#11279
* </pre>
*
* @param userId 企业内的成员id
* @param agentId 非必填,整型,仅用于发红包。其它场景该参数不要填,如微信支付、企业转账、电子发票
* @return map对象,可能包含以下值:
* - openid 企业微信成员userid对应的openid,若有传参agentid,则是针对该agentid的openid。否则是针对企业微信corpid的openid
* - appid 应用的appid,若请求包中不包含agentid则不返回appid。该appid在使用微信红包时会用到
*/
Map<String, String> userId2Openid(String userId, Integer agentId) throws WxErrorException;

/**
* <pre>
* openid转userid
*
* 该接口主要应用于使用微信支付、微信红包和企业转账之后的结果查询。
* 开发者需要知道某个结果事件的openid对应企业微信内成员的信息时,可以通过调用该接口进行转换查询。
* 权限说明:
* 管理组需对openid对应的企业微信成员有查看权限。
*
* 文档地址:https://work.weixin.qq.com/api/doc#11279
* </pre>
*
* @param openid 在使用微信支付、微信红包和企业转账之后,返回结果的openid
* @return userid 该openid在企业微信对应的成员userid
*/
String openid2UserId(String openid) throws WxErrorException;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,6 +11,7 @@
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
* <pre>
Expand Down Expand Up @@ -114,6 +116,7 @@ public List<WxCpUser> 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();
Expand All @@ -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<String, String> 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<String, String> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* <pre>
Expand Down Expand Up @@ -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<String, String> 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);
}
}

0 comments on commit a6d73a2

Please sign in to comment.