work/department.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 174 CC 13 SCAR 22.5
CC
13
Cov
0%
create
12.0
update
12.0
delete
12.0
CRAP 9.5
1 package QQ::weixin::work::department;
2
3 =pod
4
5 =encoding utf8
6
7 =head1 Name
8
9 QQ::weixin::work::department
10
11 =head1 DESCRIPTION
12
13 通讯录管理->部门管理
14
15 =cut
16
17 use strict;
18 use base qw(QQ::weixin::work);
19 use Encode;
20 use LWP::UserAgent;
21 use JSON;
22 use utf8;
23
24 our $VERSION = '0.10';
25 our @EXPORT = qw/ create update delete list simplelist get /;
26
27 =head1 FUNCTION
28
29 =head2 create(access_token, hash);
30
31 创建部门
32 最后更新:2023/08/15
33
34 =head3 SYNOPSIS
35
36 L<https://developer.work.weixin.qq.com/document/path/90205>
37
38 =head3 请求说明
39
40 =head4 请求包结构体为:
41
42 {
43 "name": "广州研发中心",
44 "name_en": "RDGZ",
45 "parentid": 1,
46 "order": 1,
47 "id": 2
48 }
49
50 =head4 参数说明
51
52 参数 必须 说明
53 access_token 是 调用接口凭证
54 name 是 部门名称。长度限制为1~32个字符,字符不能包括\:?”<>|
55 name_en 否 英文名称,需要在管理后台开启多语言支持才能生效。长度限制为1~32个字符,字符不能包括\:?”<>|
56 parentid 是 父部门id,32位整型
57 order 否 在父部门中的次序值。order值大的排序靠前。有效的值范围是[0, 2^32)
58 id 否 部门id,32位整型,指定时必须大于1。若不填该参数,将自动生成id
59
60 =head3 权限说明
61
62 第三方仅通讯录应用可以调用。
63
64 注意,部门的最大层级为15层;部门总数不能超过3万个;每个部门下的节点不能超过3万个。建议保证创建的部门和对应部门成员是串行化处理。
65
66 =head3 RETURN 返回结果
67
68 {
69 "errcode": 0,
70 "errmsg": "created",
71 "id": 2
72 }
73
74 =head4 RETURN 参数说明
75
76 参数 说明
77 errcode 返回码
78 errmsg 对返回码的文本描述内容
79 id 创建的部门id
80
81 =cut
82
83 sub create {
84 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
85 my $access_token = $_[0];
86 my $json = $_[1];
87 my $ua = LWP::UserAgent->new;
88 $ua->timeout(30);
89 $ua->env_proxy;
90
91 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
92 if ($response->is_success) {
93 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
94 }
95
96 }
97 return 0;
98 }
99
100 =head2 update(access_token, hash);
101
102 更新部门
103 最后更新:2023/08/15
104
105 =head3 SYNOPSIS
106
107 L<https://developer.work.weixin.qq.com/document/path/90206>
108
109 =head3 请求说明:
110
111 =head4 请求包体(如果非必须的字段未指定,则不更新该字段):
112
113 {
114 "id": 2,
115 "name": "广州研发中心",
116 "name_en": "RDGZ",
117 "parentid": 1,
118 "order": 1
119 }
120
121 =head4 参数说明:
122
123 参数 必须 说明
124 access_token 是 调用接口凭证
125 id 是 部门id
126 name 否 部门名称。长度限制为1~32个字符,字符不能包括\:*?"<>|
127 name_en 否 英文名称,需要在管理后台开启多语言支持才能生效。长度限制为1~32个字符,字符不能包括\:*?"<>|
128 parentid 否 父部门id
129 order 否 在父部门中的次序值。order值大的排序靠前。有效的值范围是[0, 2^32)
130
131 =head3 权限说明
132
133 应用须拥有指定部门的管理权限。如若要移动部门,需要有新父部门的管理权限。
134
135 第三方仅通讯录应用可以调用。
136
137 注意,部门的最大层级为15层;部门总数不能超过3万个;每个部门下的节点不能超过3万个。
138
139 =head3 RETURN 返回结果
140
141 {
142 "errcode": 0,
143 "errmsg": "updated"
144 }
145
146 =head4 RETURN 参数说明
147
148 参数 说明
149 errcode 返回码
150 errmsg 对返回码的文本描述内容
151
152 =cut
153
154 sub update {
155 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
156 my $access_token = $_[0];
157 my $json = $_[1];
158 my $ua = LWP::UserAgent->new;
159 $ua->timeout(30);
160 $ua->env_proxy;
161
162 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
163 if ($response->is_success) {
164 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
165 }
166
167 }
168 return 0;
169 }
170
171 =head2 delete(access_token, id);
172
173 删除部门
174 最后更新:2020/03/30
175
176 =head3 SYNOPSIS
177
178 L<https://developer.work.weixin.qq.com/document/path/90207>
179
180 =head3 请求说明:
181
182 =head4 参数说明:
183
184 参数 必须 说明
185 access_token 是 调用接口凭证
186 id 是 部门id。(注:不能删除根部门;不能删除含有子部门、成员的部门)
187
188 =head4 权限说明
189
190 应用须拥有指定部门的管理权限。
191
192 第三方仅通讯录应用可以调用。
193
194 =head3 RETURN 返回结果
195
196 {
197 "errcode": 0,
198 "errmsg": "deleted"
199 }
200
201 =head4 RETURN 参数说明
202
203 参数 说明
204 errcode 返回码
205 errmsg 对返回码的文本描述内容
206
207 =cut
208
209 sub delete {
210 if ( @_ && $_[0] && $_[1] ) {
211 my $access_token = $_[0];
212 my $id = $_[1];
213 my $ua = LWP::UserAgent->new;
214 $ua->timeout(30);
215 $ua->env_proxy;
216
217 my $response = $ua->get("https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token=$access_token&id=$id");
218 if ($response->is_success) {
219 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
220 }
221
222 }
223 return 0;
224 }
225
226 =head2 list(access_token, id);
227
228 获取部门列表
229 最后更新:2022/08/05
230
231 =head3 SYNOPSIS
232
233 L<https://developer.work.weixin.qq.com/document/path/90208>
234
235 =head3 请求说明:
236
237 注:该接口性能较低,建议换用获取子部门ID列表与获取单个部门详情
238
239 =head3 参数说明:
240
241 参数 必须 说明
242 access_token 是 调用接口凭证
243 id 否 部门id。获取指定部门及其下的子部门。 如果不填,默认获取全量组织架构
244
245 =head3 权限说明
246
247 只能拉取token对应的应用的权限范围内的部门列表
248
249 =head3 RETURN 返回结果
250
251 {
252 "errcode": 0,
253 "errmsg": "ok",
254 "department": [
255 {
256 "id": 2,
257 "name": "广州研发中心",
258 "name_en": "RDGZ",
259 "department_leader":["zhangsan","lisi"],
260 "parentid": 1,
261 "order": 10
262 },
263 {
264 "id": 3,
265 "name": "邮箱产品部",
266 "name_en": "mail",
267 "department_leader":["lisi","wangwu"],
268 "parentid": 2,
269 "order": 40
270 }
271 ]
272 }
273
274 =head4 RETURN 参数说明
275
276 参数 说明
277 errcode 返回码
278 errmsg 对返回码的文本描述内容
279 department 部门列表数据。
280 id 创建的部门id
281 name 部门名称,代开发自建应用需要管理员授权才返回;此字段从2019年12月30日起,对新创建第三方应用不再返回,2020年6月30日起,对所有历史第三方应用不再返回name,返回的name字段使用id代替,后续第三方仅通讯录应用可获取,未返回名称的情况需要通过通讯录展示组件来展示部门名称
282 name_en 英文名称,此字段从2019年12月30日起,对新创建第三方应用不再返回,2020年6月30日起,对所有历史第三方应用不再返回该字段
283 department_leader 部门负责人的UserID;第三方仅通讯录应用可获取
284 parentid 父部门id。根部门为1
285 order 在父部门中的次序值。order值大的排序靠前。值范围是[0, 2^32)
286
287 =cut
288
289 sub list {
290 if ( @_ && $_[0] ) {
291 my $access_token = $_[0];
292 my $id = $_[1] || 1;
293 my $ua = LWP::UserAgent->new;
294 $ua->timeout(30);
295 $ua->env_proxy;
296
297 my $response = $ua->get("https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=$access_token&id=$id");
298 if ($response->is_success) {
299 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
300 }
301
302 }
303 return 0;
304 }
305
306 =head2 simplelist(access_token, id);
307
308 获取子部门ID列表
309 最后更新:2023/09/06
310
311 =head3 SYNOPSIS
312
313 L<https://developer.work.weixin.qq.com/document/path/95350>
314
315 =head3 请求说明:
316
317 =head3 参数说明:
318
319 参数 必须 说明
320 access_token 是 调用接口凭证
321 id 否 部门id。获取指定部门及其下的子部门(以及子部门的子部门等等,递归)。 如果不填,默认获取全量组织架构
322
323 =head3 权限说明
324
325 应用类型 权限说明
326 第三方普通应用 若企业授权了“组织架构信息”权限,可获取企业所有部门id; 若未授权“组织架构信息”权限,只能拉取token对应的应用的权限范围内的部门列表
327 第三方通讯录应用 可获取企业所有部门id
328 代开发自建应用 只能拉取token对应的应用的权限范围内的部门列表
329 普通自建应用 只能拉取token对应的应用的权限范围内的部门列表
330 通讯录同步助手 可获取企业所有部门id
331
332 =head3 RETURN 返回结果
333
334 {
335 "errcode": 0,
336 "errmsg": "ok",
337 "department_id": [
338 {
339 "id": 2,
340 "parentid": 1,
341 "order": 10
342 },
343 {
344 "id": 3,
345 "parentid": 2,
346 "order": 40
347 }
348 ]
349 }
350
351 =head4 RETURN 参数说明
352
353 参数 说明
354 errcode 返回码
355 errmsg 对返回码的文本描述内容
356 department_id 部门列表数据。
357 id 创建的部门id
358 parentid 父部门id。根部门为1。
359 order 在父部门中的次序值。order值大的排序靠前。值范围是[0, 2^32)。
360
361 =cut
362
363 sub simplelist {
364 if ( @_ && $_[0] ) {
365 my $access_token = $_[0];
366 my $id = $_[1] || 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/department/simplelist?access_token=$access_token&id=$id");
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(access_token, id);
381
382 获取单个部门详情
383 最后更新:2023/09/06
384
385 =head3 SYNOPSIS
386
387 L<https://developer.work.weixin.qq.com/document/path/95351>
388
389 =head3 请求说明:
390
391 =head3 参数说明:
392
393 参数 必须 说明
394 access_token 是 调用接口凭证
395 id 是 部门id。
396
397 =head3 权限说明
398
399 应用类型 权限说明
400 第三方普通应用 若企业授权了“组织架构信息”权限,可获取企业所有部门ID、部门负责人、父部门ID; 若未授权“组织架构信息”权限,只能拉取token对应的应用的可见范围内部门详情
401 第三方通讯录应用 可获取企业所有部门详情,部门名字除外
402 代开发自建应用 若企业授权了“组织架构信息”-“部门及父部门ID、部门负责人”权限,可获取企业所有部门id; 若未授权“组织架构信息”-“部门及父部门ID、部门负责人”权限,只能拉取token对应的应用的权限范围内的部门列表
403 普通自建应用 只能拉取token对应的应用的权限范围内的部门详情
404 通讯录同步助手 可获取企业所有部门详情
405
406 =head3 RETURN 返回结果
407
408 {
409 "errcode": 0,
410 "errmsg": "ok",
411 "department":
412 {
413 "id": 2,
414 "name": "广州研发中心",
415 "name_en": "RDGZ",
416 "department_leader":["zhangsan","lisi"],
417 "parentid": 1,
418 "order": 10
419 }
420 }
421
422 =head4 RETURN 参数说明
423
424 参数 说明
425 errcode 返回码
426 errmsg 对返回码的文本描述内容
427 department 部门详情。
428 id 部门id
429 name 部门名称,代开发自建应用需要管理员授权才返回;第三方不可获取,需要通过通讯录展示组件来展示部门名称
430 name_en 部门英文名称,代开发自建应用需要管理员授权才返回;第三方不可获取,需要通过通讯录展示组件来展示部门名称
431 department_leader 部门负责人的UserID,返回在应用可见范围内的部门负责人列表;第三方仅通讯录应用或者授权了“组织架构信息-应用可获取企业的部门组织架构信息-部门负责人”的第三方应用可获取
432 parentid 父部门id。根部门为1。
433 order 在父部门中的次序值。order值大的排序靠前。值范围是[0, 2^32)
434
435 =cut
436
437 sub get {
438 if ( @_ && $_[0] ) {
439 my $access_token = $_[0];
440 my $id = $_[1] || 1;
441 my $ua = LWP::UserAgent->new;
442 $ua->timeout(30);
443 $ua->env_proxy;
444
445 my $response = $ua->get("https://qyapi.weixin.qq.com/cgi-bin/department/get?access_token=$access_token&id=$id");
446 if ($response->is_success) {
447 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
448 }
449
450 }
451 return 0;
452 }
453
454
455 1;
456 __END__