Skip to content

Commit cffd173

Browse files
authored
🆕 #2397【企业微信】帐号ID安全性全面升级相关接口改造以及增加代开发应用external_userid转换的接口
1 parent 6cc0b2d commit cffd173

File tree

5 files changed

+239
-1
lines changed

5 files changed

+239
-1
lines changed

Diff for: weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpExternalContactService.java

+91
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,97 @@ public interface WxCpExternalContactService {
168168
*/
169169
String unionidToExternalUserid(@NotNull String unionid,String openid) throws WxErrorException;
170170

171+
/**
172+
* 代开发应用external_userid转换
173+
* <pre>
174+
*
175+
* 文档地址:https://work.weixin.qq.com/api/doc/90001/90143/95195
176+
*
177+
* 企业同时安装服务商第三方应用以及授权代开发自建应用的时,服务商可使用该接口将代开发应用获取到的外部联系人id跟第三方应用的id进行关联,
178+
* 该接口可将代开发自建应用获取到的external_userid转换为服务商第三方应用的external_userid。
179+
*
180+
* 权限说明:
181+
*
182+
* 该企业授权了该服务商第三方应用,且授权的第三方应用具备“企业客户权限->客户基础信息”权限
183+
* 该客户的跟进人必须在应用的可见范围之内
184+
* 应用需具备“企业客户权限->客户基础信息”权限
185+
* </pre>
186+
*
187+
* @param externalUserid 代开发自建应用获取到的外部联系人ID
188+
* @return 该服务商第三方应用下的企业的外部联系人ID
189+
* @throws WxErrorException .
190+
*/
191+
String toServiceExternalUserid(@NotNull String externalUserid) throws WxErrorException;
192+
193+
/**
194+
* 企业客户微信unionid的升级 - unionid查询external_userid
195+
* <pre>
196+
*
197+
* 文档地址:https://open.work.weixin.qq.com/api/doc/35863#4.2%20unionid%E6%9F%A5%E8%AF%A2external_userid
198+
*
199+
* 当微信用户在微信中使用第三方应用的小程序或公众号时,第三方可将获取到的unionid与openid,调用此接口转换为企业客户external_userid。
200+
* 该接口调用频次有限,每个服务商每小时仅可调用1万次,仅用于微信用户主动使用第三方应用的场景来调用,服务商切不可滥用。
201+
* 同时建议服务商的小程序路径或公众号页面链接带上corpid参数,如此可明确地转换出该企业对应的external_userid,以获得更好的性能。
202+
*
203+
* 权限说明:
204+
*
205+
* 该企业授权了该服务商第三方应用
206+
* 调用频率最大为10000次/小时
207+
* unionid和openid的主体需与服务商的主体一致
208+
* openid与unionid必须是在同一个小程序或同一个公众号获取到的
209+
* </pre>
210+
*
211+
* @param unionid 微信客户的unionid
212+
* @param openid 微信客户的openid
213+
* @param corpid 需要换取的企业corpid,不填则拉取所有企业
214+
* @return 该服务商第三方应用下的企业的外部联系人ID
215+
* @throws WxErrorException .
216+
*/
217+
WxCpExternalUserIdList unionidToExternalUserid3rd(@NotNull String unionid, @NotNull String openid, String corpid) throws WxErrorException;
218+
219+
/**
220+
* 转换external_userid
221+
* <pre>
222+
*
223+
* 文档地址:https://open.work.weixin.qq.com/api/doc/35863#转换external_userid
224+
*
225+
* 对于历史已授权的企业,在2022年3月1号之前,所有接口与回调返回的external_userid仍然为旧的external_userid,
226+
* 从2022年3月1号0点开始,所有输入与返回的external_userid字段,将启用升级后的external_userid。
227+
* 所以服务商需要在此之前完成历史数据的迁移整改
228+
*
229+
* 权限说明:
230+
*
231+
* 该企业授权了该服务商第三方应用
232+
* external_userid对应的跟进人需要在应用可见范围内
233+
* </pre>
234+
*
235+
* @param externalUserIdList 微信客户的unionid
236+
* @return List<String> 新外部联系人id
237+
* @throws WxErrorException .
238+
*/
239+
WxCpNewExternalUserIdList getNewExternalUserId(String[] externalUserIdList) throws WxErrorException;
240+
241+
/**
242+
* 设置迁移完成
243+
* <pre>
244+
*
245+
* 文档地址:https://open.work.weixin.qq.com/api/doc/35863#转换external_userid
246+
*
247+
* 企业授权确认之后,且服务商完成了新旧external_userid的迁移,即可主动将该企业设置为“迁移完成”,
248+
* 设置之后,从该企业获取到的将是新的external_userid。注意,该接口需要使用provider_access_token来调用,
249+
* 对于有多个应用的服务商,可逐个应用进行external_userid的转换,最后再使用provider_access_token调用该接口完成设置。
250+
*
251+
* 权限说明:
252+
*
253+
* 该企业授权了该服务商第三方应用
254+
* </pre>
255+
*
256+
* @param corpid 企业corpid
257+
* @return wx cp base resp
258+
* @throws WxErrorException .
259+
*/
260+
WxCpBaseResp finishExternalUserIdMigration(@NotNull String corpid) throws WxErrorException;
261+
171262
/**
172263
* 客户群opengid转换
173264
* <pre>

Diff for: weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpExternalContactServiceImpl.java

+44-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.ExternalContact.*;
2626

2727
/**
28-
* @author 曹祖鹏 & yuanqixun
28+
* @author 曹祖鹏 & yuanqixun & Mr.Pan
2929
*/
3030
@RequiredArgsConstructor
3131
public class WxCpExternalContactServiceImpl implements WxCpExternalContactService {
@@ -135,6 +135,49 @@ public String unionidToExternalUserid(@NotNull String unionid,String openid) thr
135135
return tmpJson.get("external_userid").getAsString();
136136
}
137137

138+
@Override
139+
public String toServiceExternalUserid(@NotNull String externalUserid) throws WxErrorException {
140+
JsonObject json = new JsonObject();
141+
json.addProperty("external_userid", externalUserid);
142+
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(TO_SERVICE_EXTERNAL_USERID);
143+
String responseContent = this.mainService.post(url, json.toString());
144+
JsonObject tmpJson = GsonParser.parse(responseContent);
145+
return tmpJson.get("external_userid").getAsString();
146+
}
147+
148+
@Override
149+
public WxCpExternalUserIdList unionidToExternalUserid3rd(@NotNull String unionid, @NotNull String openid, String corpid) throws WxErrorException {
150+
JsonObject json = new JsonObject();
151+
json.addProperty("unionid", unionid);
152+
json.addProperty("openid", openid);
153+
if(StringUtils.isNotEmpty(corpid)){
154+
json.addProperty("corpid",corpid);
155+
}
156+
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(UNIONID_TO_EXTERNAL_USERID_3RD);
157+
String responseContent = this.mainService.post(url, json.toString());
158+
return WxCpExternalUserIdList.fromJson(responseContent);
159+
}
160+
161+
@Override
162+
public WxCpNewExternalUserIdList getNewExternalUserId(String[] externalUserIdList) throws WxErrorException {
163+
JsonObject json = new JsonObject();
164+
if (ArrayUtils.isNotEmpty(externalUserIdList)) {
165+
json.add("external_userid_list", new Gson().toJsonTree(externalUserIdList).getAsJsonArray());
166+
}
167+
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(GET_NEW_EXTERNAL_USERID);
168+
String responseContent = this.mainService.post(url, json.toString());
169+
return WxCpNewExternalUserIdList.fromJson(responseContent);
170+
}
171+
172+
@Override
173+
public WxCpBaseResp finishExternalUserIdMigration(@NotNull String corpid) throws WxErrorException {
174+
JsonObject json = new JsonObject();
175+
json.addProperty("corpid", corpid);
176+
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(FINISH_EXTERNAL_USERID_MIGRATION);
177+
String responseContent = this.mainService.post(url, json.toString());
178+
return WxCpBaseResp.fromJson(responseContent);
179+
}
180+
138181
@Override
139182
public String opengidToChatid(@NotNull String opengid) throws WxErrorException {
140183
JsonObject json = new JsonObject();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package me.chanjar.weixin.cp.bean.external;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
7+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
8+
9+
import java.io.Serializable;
10+
import java.util.List;
11+
12+
/**
13+
* 企业客户微信unionid的升级 - 企业客户external_userid列表
14+
*
15+
* @author Mr.Pan
16+
* @date 2021/11/18
17+
*/
18+
@Getter
19+
@Setter
20+
public class WxCpExternalUserIdList extends WxCpBaseResp {
21+
22+
@SerializedName("external_userid_info")
23+
private List<ExternalUserIdInfo> externalUserIdInfo;
24+
25+
@Getter
26+
@Setter
27+
public static class ExternalUserIdInfo implements Serializable {
28+
private static final long serialVersionUID = 8846290993790709261L;
29+
30+
/**
31+
* 所属企业id
32+
*/
33+
@SerializedName("corpid")
34+
private String corpId;
35+
36+
/**
37+
* 外部联系人id
38+
*/
39+
@SerializedName("external_userid")
40+
private String externalUserId;
41+
42+
/**
43+
* 新外部联系人id
44+
*/
45+
@SerializedName("new_external_userid")
46+
private String newExternalUserId;
47+
48+
}
49+
50+
public static WxCpExternalUserIdList fromJson(String json) {
51+
return WxCpGsonBuilder.create().fromJson(json, WxCpExternalUserIdList.class);
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package me.chanjar.weixin.cp.bean.external;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
7+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
8+
9+
import java.io.Serializable;
10+
import java.util.List;
11+
12+
/**
13+
* 企业客户微信unionid的升级 - 企业客户external_userid列表
14+
*
15+
* @author Mr.Pan
16+
* @date 2021/11/18
17+
*/
18+
@Getter
19+
@Setter
20+
public class WxCpNewExternalUserIdList extends WxCpBaseResp {
21+
22+
@SerializedName("items")
23+
private List<NewExternalUserIdInfo> items;
24+
25+
@Getter
26+
@Setter
27+
public static class NewExternalUserIdInfo implements Serializable {
28+
private static final long serialVersionUID = 8846290993790709261L;
29+
30+
/**
31+
* 外部联系人id
32+
*/
33+
@SerializedName("external_userid")
34+
private String externalUserId;
35+
36+
/**
37+
* 新外部联系人id
38+
*/
39+
@SerializedName("new_external_userid")
40+
private String newExternalUserId;
41+
42+
}
43+
44+
public static WxCpNewExternalUserIdList fromJson(String json) {
45+
return WxCpGsonBuilder.create().fromJson(json, WxCpNewExternalUserIdList.class);
46+
}
47+
}

Diff for: weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java

+4
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ interface ExternalContact {
182182
String GET_CONTACT_DETAIL = "/cgi-bin/externalcontact/get?external_userid=";
183183
String CONVERT_TO_OPENID = "/cgi-bin/externalcontact/convert_to_openid";
184184
String UNIONID_TO_EXTERNAL_USERID = "/cgi-bin/externalcontact/unionid_to_external_userid";
185+
String UNIONID_TO_EXTERNAL_USERID_3RD = "/cgi-bin/service/externalcontact/unionid_to_external_userid_3rd";
186+
String GET_NEW_EXTERNAL_USERID = "/cgi-bin/service/externalcontact/get_new_external_userid";
187+
String TO_SERVICE_EXTERNAL_USERID = "/cgi-bin/externalcontact/to_service_external_userid";
188+
String FINISH_EXTERNAL_USERID_MIGRATION = "/cgi-bin/externalcontact/finish_external_userid_migration";
185189
String GET_CONTACT_DETAIL_BATCH = "/cgi-bin/externalcontact/batch/get_by_user?";
186190
String UPDATE_REMARK = "/cgi-bin/externalcontact/remark";
187191
String LIST_EXTERNAL_CONTACT = "/cgi-bin/externalcontact/list?userid=";

0 commit comments

Comments
 (0)