Skip to content

Commit 7033b1d

Browse files
committed
#1144 企业微信模块增加通过手机号获取userid的接口
1 parent 3a1fae6 commit 7033b1d

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,23 @@ public interface WxCpUserService {
137137
*/
138138
String openid2UserId(String openid) throws WxErrorException;
139139

140+
/**
141+
* <pre>
142+
*
143+
* 通过手机号获取其所对应的userid。
144+
*
145+
* 请求方式:POST(HTTPS)
146+
* 请求地址:https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token=ACCESS_TOKEN
147+
*
148+
* 文档地址:https://work.weixin.qq.com/api/doc#90001/90143/91693
149+
* </pre>
150+
*
151+
* @param mobile 手机号码。长度为5~32个字节
152+
* @return userid mobile对应的成员userid
153+
* @throws WxErrorException .
154+
*/
155+
String getUserId(String mobile) throws WxErrorException;
156+
140157
/**
141158
* 获取外部联系人详情.
142159
* <pre>
@@ -147,6 +164,8 @@ public interface WxCpUserService {
147164
* </pre>
148165
*
149166
* @param userId 外部联系人的userid
167+
* @return 联系人详情
168+
* @throws WxErrorException .
150169
*/
151170
WxCpUserExternalContactInfo getExternalContact(String userId) throws WxErrorException;
152171

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ public String openid2UserId(String openid) throws WxErrorException {
180180
return tmpJsonElement.getAsJsonObject().get("userid").getAsString();
181181
}
182182

183+
@Override
184+
public String getUserId(String mobile) throws WxErrorException {
185+
JsonObject jsonObject = new JsonObject();
186+
jsonObject.addProperty("mobile", mobile);
187+
String url = this.mainService.getWxCpConfigStorage().getApiUrl(GET_USER_ID);
188+
String responseContent = this.mainService.post(url, jsonObject.toString());
189+
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
190+
return tmpJsonElement.getAsJsonObject().get("userid").getAsString();
191+
}
192+
183193
@Override
184194
public WxCpUserExternalContactInfo getExternalContact(String userId) throws WxErrorException {
185195
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public static class User {
101101
public static final String BATCH_INVITE = "/cgi-bin/batch/invite";
102102
public static final String USER_CONVERT_TO_OPENID = "/cgi-bin/user/convert_to_openid";
103103
public static final String USER_CONVERT_TO_USERID = "/cgi-bin/user/convert_to_userid";
104+
public static final String GET_USER_ID = "/cgi-bin/user/getuserid";
104105
public static final String GET_EXTERNAL_CONTACT = "/cgi-bin/crm/get_external_contact?external_userid=";
105106
}
106107

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,14 @@ public void testOpenid2UserId() throws Exception {
112112
}
113113

114114

115+
@Test
116+
public void testGetUserId() throws WxErrorException {
117+
String result = this.wxCpService.getUserService().getUserId("xxx");
118+
System.out.println(result);
119+
assertNotNull(result);
120+
}
121+
122+
@Test
123+
public void testGetExternalContact() {
124+
}
115125
}

0 commit comments

Comments
 (0)