work/corpgroup.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 152 CC 11 SCAR 22.1
CC
11
Cov
0%
unionid_to_external_userid
12.0
unionid_to_pending_id
12.0
import_chain_contact
12.0
CRAP 9.1
1 package QQ::weixin::work::corpgroup;
2
3 =encoding utf8
4
5 =head1 Name
6
7 QQ::weixin::work::corpgroup
8
9 =head1 DESCRIPTION
10
11 =cut
12
13 use strict;
14 use base qw(QQ::weixin::work);
15 use Encode;
16 use LWP::UserAgent;
17 use JSON;
18 use utf8;
19
20 our $VERSION = '0.10';
21 our @EXPORT = qw/ unionid_to_external_userid unionid_to_pending_id
22 import_chain_contact getresult get_corp_shared_chain_list /;
23
24 =head1 FUNCTION
25
26 =head2 unionid_to_external_userid(access_token, hash);
27
28 通过unionid和openid查询external_userid
29 最后更新:2023/09/28
30
31 =head2 SYNOPSIS
32
33 L<https://developer.work.weixin.qq.com/document/path/95818>
34
35 =head3 请求说明:
36
37 external_userid的说明
38
39 external_userid是企业微信用于表示企业的外部联系人而设立的id,且满足以下条件
40 假设同一个微信用户属于多个下游企业的外部联系人
41 1.同一上游企业获取到不同企业的外部联系人的external_userid不一致。
42 2.不同一上游企业获取同一个企业的同一个外部联系人的external_userid也不一致。
43
44 =head4 请求包结构体为:
45
46 {
47 "unionid":"xxxxx",
48 "openid":"xxxxx",
49 "corpid":"xxxxx",
50 }
51
52 =head4 参数说明:
53
54 参数 必须 说明
55 access_token 是 调用接口凭证
56 unionid 是 微信客户的unionid
57 openid 是 微信客户的openid
58 corpid 否 需要换取的企业corpid,不填则拉取所有企业
59
60 =head4 权限说明:
61
62 调用该接口的应用必须是上下游共享的应用
63 上游企业须已认证
64 unionid(unionid的主体为绑定了该小程序的微信开放平台账号主体)和openid(即小程序账号主体)的主体需与当前企业的主体一致
65 openid与unionid必须是在同一个小程序获取到的
66 应用需要具有客户联系权限
67 自建应用/代开发应用可调用,第三方应用请查看企业客户微信unionid的升级方案
68 调用频率最大为2万次/小时,24万次/天
69
70 =head3 RETURN 返回结果:
71
72 {
73 "errcode":0,
74 "errmsg":"ok",
75 "external_userid_info":[
76 {
77 "corpid":"AAAAA",
78 "external_userid":"BBBB"
79 },
80 {
81 "corpid":"CCCCC",
82 "external_userid":"DDDDD"
83 }
84 ]
85 }
86
87 =head4 RETURN 参数说明:
88
89 参数 说明
90 errcode 出错返回码,为0表示成功,非0表示调用失败
91 errmsg 对返回码的文本描述内容
92 external_userid_info 该unionid对应的外部联系人信息
93 external_userid_info.corpid 所属企业id
94 external_userid_info.external_userid 外部联系人id
95
96 =cut
97
98 sub unionid_to_external_userid {
99 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
100 my $access_token = $_[0];
101 my $json = $_[1];
102 my $ua = LWP::UserAgent->new;
103 $ua->timeout(30);
104 $ua->env_proxy;
105
106 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/corpgroup/unionid_to_external_userid?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
107 if ($response->is_success) {
108 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
109 }
110
111 }
112 return 0;
113 }
114
115 =head2 unionid_to_pending_id(access_token, hash);
116
117 unionid查询pending_id
118 最后更新:2023/03/30
119
120 =head2 SYNOPSIS
121
122 L<https://developer.work.weixin.qq.com/document/path/97357>
123
124 =head3 请求说明:
125
126 pending_id的说明
127
128 pending_id主要用于关联微信unionid与外部联系人external_userid,可理解为临时外部联系人ID;
129 上游企业可通过此接口将微信unionid转为pending_id,当微信用户成为下游企业客户后,可使用上下游external_userid转pending_id接口将下游external_userid转换为pending_id,建立unionid => pending_id => external_userid的映射关系;
130 pending_id有效期90天,共享应用内唯一。
131
132 =head4 请求包结构体为:
133
134 {
135 "unionid":"UNIONID",
136 "openid":"OPENID"
137 }
138
139 =head4 参数说明:
140
141 参数 必须 说明
142 access_token 是 调用接口凭证
143 unionid 是 微信客户的unionid
144 openid 是 微信客户的openid
145
146 =head4 权限说明:
147
148 1. 调用该接口的应用必须是上下游共享的自建应用或代开发应用
149 2. 应用需要具有客户联系权限
150 3. 当前授权企业必须已认证或已验证;若为代开发应用,服务商必须已认证
151 4. unionid(即微信开放平台账号主体)与openid(即小程序或服务号账号主体)需要认证,且主体名称需与上游企业的主体名称一致(查看由服务商代注册的开放平台账号认证流程)
152 5. openid与unionid必须是在同一个小程序获取到的
153 6. pending_id有效期90天
154
155 =head3 RETURN 返回结果:
156
157 {
158 "errcode":0,
159 "errmsg":"ok",
160 "pending_id":"PENDINGID"
161 }
162
163 =head4 RETURN 参数说明:
164
165 参数 说明
166 errcode 出错返回码,为0表示成功,非0表示调用失败
167 errmsg 对返回码的文本描述内容
168 pending_id unionid和openid对应的pending_id
169
170 =cut
171
172 sub unionid_to_pending_id {
173 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
174 my $access_token = $_[0];
175 my $json = $_[1];
176 my $ua = LWP::UserAgent->new;
177 $ua->timeout(30);
178 $ua->env_proxy;
179
180 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/corpgroup/unionid_to_pending_id?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
181 if ($response->is_success) {
182 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
183 }
184
185 }
186 return 0;
187 }
188
189 =head2 import_chain_contact(access_token, hash);
190
191 批量导入上下游联系人
192 最后更新:2023/11/30
193
194 =head2 SYNOPSIS
195
196 L<https://developer.work.weixin.qq.com/document/path/95821>
197
198 =head3 请求说明:
199
200 提交批量导入上下游联系人任务
201
202 =head4 请求包结构体为:
203
204 {
205 "chain_id":"xxxxxx",
206 "contact_list":[
207 {
208 "corp_name":"飞飞培训学校",
209 "group_path":"华北区/北京市/海淀区",
210 "custom_id":"wof3du51quo5sl1is",
211 "contact_info_list":[
212 {
213 "name":"张三",
214 "identity_type":1,
215 "mobile":"13000000001",
216 "user_custom_id":"100"
217 },
218 {
219 "name":"李四",
220 "identity_type":2,
221 "mobile":"13000000001",
222 "user_custom_id":"100"
223 }
224 ]
225 }
226 ]
227 }
228
229 =head4 参数说明:
230
231 参数 必须 说明
232 access_token 是 调用接口凭证。上游企业应用access_token
233 chain_id 是 上下游id。文件中的联系人将会被导入此上下游中
234 contact_list 是 上下游联系人列表。这些联系人将会被导入此上下游中
235 contact_list.corp_name 是 上下游企业名称。长度为1-32个utf8字符。只能由中文、字母、数字和“ -_()()”六种字符组成
236 contact_list.group_path 否 导入后企业所在分组。分组为空的企业会放在根分组下。仅针对新导入企业生效,不会修改已导入企业的分组。
237 contact_list.custom_id 否 上下游企业自定义 id。长度为0~64 个字节,只能由数字和字母组成
238 contact_list.contact_info_list 是 上下游联系人信息列表
239 contact_list.contact_info_list.name 是 上下游联系人姓名。长度为1~32个utf8字符
240 contact_list.contact_info_list.identity_type 是 联系人身份类型。1:成员,2:负责人。
241 contact_list.contact_info_list.mobile 是 手机号。支持国内、国际手机号(国内手机号直接输入手机号即可,格式示例:“138****0001”;国际手机号必须包含加号以及国家地区码,格式示例:“+85259****45”
242 contact_list.contact_info_list.user_custom_id 否 上下游用户自定义 id。类型为字符串,暂时只支持传入64比特无符号整型,取值范围1到2^64-2,必须是全数字,不得传入前置0,且不能为11位或13位数字。
243
244 =head4 权限说明:
245
246 调用的应用需要满足如下的权限,仅已验证的企业可调用
247
248 应用类型 权限要求
249 自建应用 配置到「上下游- 可调用接口的应用」中
250 注: 从2023年12月1日0点起,不再支持通过系统应用secret调用接口,存量企业暂不受影响 查看详情
251
252 导入任务限制:
253 同时只能存在一个导入任务。导入任务包括通过API提交的任务和从管理后台提交的导入任务。
254
255 =head3 RETURN 返回结果
256
257 {
258 "errcode": 0,
259 "errmsg": "ok",
260 "jobid": "xxxxx"
261 }
262
263 可使用jobid通过获取异步任务结果接口查询任务执行状态及结果
264 当开启了上下游应用回调通知后,任务运行完成时会推送异步任务完成通知
265
266 =head4 RETURN 参数说明
267
268 参数 说明
269 errcode 返回码。仅表示提交任务的结果。任务执行结果需在任务提交成功后调用获取异步任务结果接口查询
270 errmsg 对返回码的文本描述内容
271 jobid 异步任务id,最大长度为64字节
272
273 =cut
274
275 sub import_chain_contact {
276 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
277 my $access_token = $_[0];
278 my $json = $_[1];
279 my $ua = LWP::UserAgent->new;
280 $ua->timeout(30);
281 $ua->env_proxy;
282
283 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/corpgroup/import_chain_contact?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
284 if ($response->is_success) {
285 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
286 }
287
288 }
289 return 0;
290 }
291
292 =head2 getresult(access_token, jobid);
293
294 获取异步任务结果
295 最后更新:2023/11/30
296
297 =head2 SYNOPSIS
298
299 L<https://developer.work.weixin.qq.com/document/path/95823>
300
301 =head3 请求说明:
302
303 =head4 参数说明:
304
305 参数 必须 说明
306 access_token 是 调用接口凭证
307 jobid 是 异步任务id,最大长度为64字节
308
309 =head3 权限说明
310
311 只能查询已经提交过的历史任务
312 调用的应用需要满足如下的权限
313 应用类型 权限要求
314 自建应用 配置到「上下游- 可调用接口的应用」中
315
316 注: 从2023年12月1日0点起,不再支持通过系统应用secret调用接口,存量企业暂不受影响 查看详情
317 并发限制:5
318
319 =head3 RETURN 返回结果
320
321 {
322 "errcode": 0,
323 "errmsg": "ok",
324 "status": 3,
325 "result": {
326 "chain_id": "xxxx",
327 "import_status": 2,
328 "fail_list": [{
329 "corp_name": "飞飞培训学校2入2222",
330 "custom_id": "",
331 "errcode": 670016,
332 "errmsg": "invalid contact identity",
333 "contact_info_list": [{
334 "mobile": "13000000001",
335 "errcode": 670016,
336 "errmsg": "invalid contact identity"
337 }]
338 }]
339 }
340 }
341
342 =head4 RETURN 参数说明
343
344 参数 说明
345 errcode 返回码
346 errmsg 对返回码的文本描述内容
347 status 任务状态,整型,1表示任务开始,2表示任务进行中,3表示任务已完成
348 result 详细的处理结果。当任务完成后此字段有效
349 result.chain_id 上下游id
350 result.import_status 导入状态。1:全部企业导入成功,2:部分企业导入成功,3:全部企业导入失败
351 result.fail_list 导入失败结果列表 。当企业中有联系人导入失败时,本次导入该企业所有联系人的导入都会被阻断。
352 result.fail_list.custom_id 自定义企业id
353 result.fail_list.corp_name 自定义企业名称
354 result.fail_list.errmsg 该企业导入操作的结果错误码
355 result.fail_list.errcode 该企业导入操作的结果错误码描述
356 result.fail_list.contact_info_list 导入失败的联系人结果
357 result.fail_list.contact_info.user_mobile 导入失败的联系人手机号。有此联系人相关的错误时才会返回
358 result.fail_list.contact_info.errcode 导入失败的联系人错误码。有此联系人相关的错误时才会返回
359 result.fail_list.contact_info.errmsg 导入失败的联系人错误码描述。有此联系人相关的错误时才会返回
360
361 =cut
362
363 sub getresult {
364 if ( @_ && $_[0] && $_[1] ) {
365 my $access_token = $_[0];
366 my $jobid = $_[1];
367 my $ua = LWP::UserAgent->new;
368 $ua->timeout(30);
369 $ua->env_proxy;
370
371 my $response = $ua->get("https://qyapi.weixin.qq.com/cgi-bin/corpgroup/getresult?access_token=$access_token&jobid=$jobid");
372 if ($response->is_success) {
373 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
374 }
375
376 }
377 return 0;
378 }
379
380 =head2 get_corp_shared_chain_list(access_token, hash);
381
382 获取下级企业加入的上下游
383 最后更新:2023/11/29
384
385 =head2 SYNOPSIS
386
387 L<https://developer.work.weixin.qq.com/document/path/97442>
388
389 =head3 请求说明:
390
391 上级企业自建应用/代开发应用通过本接口查询下级企业所在上下游
392
393 =head4 请求包结构体为:
394
395 {
396 "corpid":"xxxxx"
397 }
398
399 =head4 参数说明:
400
401 参数 必须 说明
402 access_token 是 调用接口凭证。上游企业应用access_token
403 corpid 否 已加入企业id
404
405 =head4 权限说明:
406
407 调自建应用、代开发应用和「上下游- 可调用接口的应用」可调用,仅可指定应用可见范围内的企业
408
409 =head3 RETURN 返回结果
410
411 {
412 "errcode": 0,
413 "errmsg": "ok",
414 "chains": [
415 {
416 "chain_id": "xxx",
417 "chain_name": "xxx"
418 }
419 ]
420 }
421
422 =head4 RETURN 参数说明
423
424 参数 说明
425 errcode 返回码。仅表示提交任务的结果。任务执行结果需在任务提交成功后调用获取异步任务结果接口查询
426 errmsg 对返回码的文本描述内容
427 chains 上下游列表
428 chains.chain_id 上下游id
429 chains.chain_name 上下游名称
430
431 =cut
432
433 sub get_corp_shared_chain_list {
434 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
435 my $access_token = $_[0];
436 my $json = $_[1];
437 my $ua = LWP::UserAgent->new;
438 $ua->timeout(30);
439 $ua->env_proxy;
440
441 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/corpgroup/get_corp_shared_chain_list?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
442 if ($response->is_success) {
443 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
444 }
445
446 }
447 return 0;
448 }
449
450 1;
451 __END__