-
Notifications
You must be signed in to change notification settings - Fork 158
/
QyWechat.php
868 lines (813 loc) · 24.8 KB
/
QyWechat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
<?php
namespace callmez\wechat\sdk;
use callmez\wechat\sdk\components\BaseWechat;
/**
* 微信企业号操作SDK
* @package calmez\wechat\sdk
*/
class QyWechat extends BaseWechat
{
/**
* 微信接口基本地址
*/
const WECHAT_BASE_URL = 'https://qyapi.weixin.qq.com';
/**
* 数据缓存前缀
* @var string
*/
public $cachePrefix = 'cache_wechat_sdk_qy';
/**
* 企业号的唯一标识
* @var string
*/
public $corpId;
/**
* 管理组凭证密钥
* @var string
*/
public $secret;
/**
* 增加微信基本链接
* @inheritdoc
*/
protected function httpBuildQuery($url, array $options)
{
if (stripos($url, 'http://') === false && stripos($url, 'https://') === false) {
$url = self::WECHAT_BASE_URL . $url;
}
return parent::httpBuildQuery($url, $options);
}
/* =================== 建立连接 =================== */
/**
* access token获取
*/
const WECHAT_ACCESS_TOKEN_PREFIX = '/cgi-bin/token';
/**
* 请求服务器access_token
* @return array|false
*/
protected function requestAccessToken()
{
$result = $this->httpGet(self::WECHAT_ACCESS_TOKEN_PREFIX, [
'corpid' => $this->corpId,
'corpsecret' => $this->secret
]);
return isset($result['access_token']) ? $result : false;
}
/**
* 获取微信服务器IP地址
*/
const WECHAT_IP_PREFIX = '/cgi-bin/getcallbackip';
/**
* 获取微信服务器IP地址
* @return array|bool
* @throws \yii\web\HttpException
*/
public function getIp()
{
$result = $this->httpGet(self::WECHAT_IP_PREFIX, [
'access_token' => $this->getAccessToken()
]);
return isset($result['ip_list']) ? $result['ip_list'] : false;
}
/* =================== 管理通讯录 =================== */
/**
* 二次验证
*/
const WECHAT_USER_AUTH_SUCCESS_PREFIX = '/cgi-bin/user/authsucc';
/**
* 二次验证
* @param $userId
* @return bool
* @throws \yii\web\HttpException
*/
public function userAuthSuccess($userId)
{
$result = $this->httpGet(self::WECHAT_USER_AUTH_SUCCESS_PREFIX, [
'access_token' => $this->getAccessToken(),
'userid' => $userId
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok';
}
/**
* 创建部门
*/
const WECHAT_DEPARTMENT_CREATE_PREFIX = '/cgi-bin/department/create';
/**
* 创建部门
* @param array $data
* @return bool
* @throws \yii\web\HttpException
*/
public function createDepartment(array $data)
{
$result = $this->httpRaw(self::WECHAT_DEPARTMENT_CREATE_PREFIX, $data, [
'access_token' => $this->getAccessToken()
]);
return isset($result['errcode']) && !$result['errcode'] ? $result['id'] : false;
}
/**
* 创建部门
*/
const WECHAT_DEPARTMENT_UPDATE_PREFIX = '/cgi-bin/department/update';
/**
* 创建部门
* @param array $data
* @return bool
* @throws \yii\web\HttpException
*/
public function updateDepartment(array $data)
{
$result = $this->httpRaw(self::WECHAT_DEPARTMENT_CREATE_PREFIX, $data, [
'access_token' => $this->getAccessToken()
]);
return isset($result['errcode']) && !$result['errcode'];
}
/**
* 删除部门
*/
const WECHAT_DEPARTMENT_DELETE_PREFIX = '/cgi-bin/department/delete';
/**
* 删除部门
* @param $id
* @return bool
* @throws \yii\web\HttpException
*/
public function deleteDepartment($id)
{
$result = $this->httpGet(self::WECHAT_DEPARTMENT_DELETE_PREFIX, [
'access_token' => $this->getAccessToken(),
'id' => $id
]);
return isset($result['errcode']) && !$result['errcode'];
}
/**
* 获取部门列表
*/
const WECHAT_DEPARTMENT_LIST = '/cgi-bin/department/list';
/**
* 获取部门列表
* @param null $id 部门id。获取指定部门id下的子部门
* @return bool
* @throws \yii\web\HttpException
*/
public function getDepartmentList($id = null)
{
$result = $this->httpGet(self::WECHAT_DEPARTMENT_DELETE_PREFIX, [
'access_token' => $this->getAccessToken(),
] + ($id === null ? [] : [
'id' => $id
]));
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result['department'] : false;
}
/**
* 创建成员
*/
const WECHAT_USER_CREATE_PREFIX = '/cgi-bin/user/create';
/**
* 创建成员
* @param array $data
* @return bool
* @throws \yii\web\HttpException
*/
public function createUser(array $data)
{
$result = $this->httpRaw(self::WECHAT_USER_CREATE_PREFIX, $data, [
'access_token' => $this->getAccessToken()
]);
return isset($result['errcode']) && !$result['errcode'];
}
/**
* 创建成员
*/
const WECHAT_USER_UPDATE_PREFIX = '/cgi-bin/user/create';
/**
* 创建成员
* @param array $data
* @return bool
* @throws \yii\web\HttpException
*/
public function updateUser(array $data)
{
$result = $this->httpRaw(self::WECHAT_USER_UPDATE_PREFIX, $data, [
'access_token' => $this->getAccessToken()
]);
return isset($result['errcode']) && !$result['errcode'];
}
/**
* 删除成员
*/
const WECHAT_USER_DELETE_PREFIX = '/cgi-bin/user/delete';
/**
* 删除成员
* @param $userId
* @return bool
* @throws \yii\web\HttpException
*/
public function deleteUser($userId)
{
$result = $this->httpGet(self::WECHAT_USER_DELETE_PREFIX, [
'access_token' => $this->getAccessToken(),
'userid' => $userId
]);
return isset($result['errcode']) && !$result['errcode'];
}
/**
* 批量删除成员
*/
const WECHAT_USER_BATCH_DELETE_PREFIX = '/cgi-bin/user/batchdelete';
/**
* 批量删除成员
* @param array $userIdList
* @return bool
* @throws \yii\web\HttpException
*/
public function batchDeleteUser(array $userIdList)
{
$result = $this->httpRaw(self::WECHAT_USER_BATCH_DELETE_PREFIX, [
'useridlist' => $userIdList
], [
'access_token' => $this->getAccessToken()
]);
return isset($result['errcode']) && !$result['errcode'];
}
/**
* 获取部门成员(详情)
*/
const WECHAT_USER_GET_PREFIX = '/cgi-bin/user/get';
/**
* 获取部门成员(详情)
* @param $userId
* @return bool|mixed
* @throws \yii\web\HttpException
*/
public function getUser($userId)
{
$result = $this->httpGet(self::WECHAT_USER_GET_PREFIX, [
'access_token' => $this->getAccessToken(),
'userid' => $userId
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result : false;
}
/**
* 获取部门成员
*/
const WECHAT_DEPARTMENT_USER_LIST_GET_PREFIX = '/cgi-bin/user/simplelist';
/**
* 获取部门成员
* @param $departmentId
* @param int $fetchChild
* @param int $status
* @return bool|mixed
* @throws \yii\web\HttpException
*/
public function getDepartmentUserList($departmentId, $fetchChild = 0, $status = 0)
{
$result = $this->httpGet(self::WECHAT_DEPARTMENT_USER_LIST_GET_PREFIX, [
'access_token' => $this->getAccessToken(),
'department_id' => $departmentId,
'fetch_child' => $fetchChild,
'status' => $status,
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result['userlist'] : false;
}
/**
* 获取部门成员(详情)
*/
const WECHAT_DEPARTMENT_USERS_INFO_LIST_GET_PREFIX = '/cgi-bin/user/list';
/**
* 获取部门成员(详情)
* @param $departmentId
* @param int $fetchChild
* @param int $status
* @return bool|mixed
* @throws \yii\web\HttpException
*/
public function getDepartmentUserInfoList($departmentId, $fetchChild = 0, $status = 0)
{
$result = $this->httpGet(self::WECHAT_DEPARTMENT_USERS_INFO_LIST_GET_PREFIX, [
'access_token' => $this->getAccessToken(),
'department_id' => $departmentId,
'fetch_child' => $fetchChild,
'status' => $status,
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result['userlist'] : false;
}
/**
* 邀请成员关注
*/
const WECHAT_USER_INVITE_PREFIX = '/cgi-bin/invite/send';
/**
* 邀请成员关注
* @param $userId
* @return bool
* @throws \yii\web\HttpException
*/
public function inviteUser($userId)
{
$result = $this->httpRaw(self::WECHAT_USER_INVITE_PREFIX, [
'userid' => $userId
], [
'access_token' => $this->getAccessToken(),
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result['type'] : false;
}
/**
* 创建标签
*/
const WECHAT_TAG_CREATE_PREFIX = '/cgi-bin/tag/create';
/**
* 创建标签
* @param $tagName
* @return int|bool
* @throws \yii\web\HttpException
*/
public function createTag($tagName)
{
$result = $this->httpRaw(self::WECHAT_TAG_CREATE_PREFIX, [
'tagname' => $tagName
], [
'access_token' => $this->getAccessToken()
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result['tagid'] : false;
}
/**
* 更新标签名字
*/
const WECHAT_TAG_NAME_UPDATE_PREFIX = '/cgi-bin/tag/update';
/**
* 更新标签名字
* @param $tagId
* @param $tagName
* @return bool
* @throws \yii\web\HttpException
*/
public function updateTagName($tagId, $tagName)
{
$result = $this->httpRaw(self::WECHAT_TAG_CREATE_PREFIX, [
'tagid' => $tagId,
'tagname' => $tagName
], [
'access_token' => $this->getAccessToken()
]);
return isset($result['errcode']) && !$result['errcode'];
}
/**
* 删除标签
*/
const WECHAT_TAG_DELETE_PREFIX = '/cgi-bin/tag/delete';
/**
* 删除标签
* @param $tagId
* @return bool
* @throws \yii\web\HttpException
*/
public function deleteTag($tagId)
{
$result = $this->httpGet(self::WECHAT_TAG_DELETE_PREFIX, [
'access_token' => $this->getAccessToken(),
'tagid' => $tagId
]);
return isset($result['errcode']) && !$result['errcode'];
}
/**
* 获取标签成员
*/
const WECHAT_TAG_USER_LIST_GET_PREFIX = '/cgi-bin/tag/get';
/**
* 获取标签成员
* @param $tagId
* @return bool|mixed
* @throws \yii\web\HttpException
*/
public function getTagUserList($tagId)
{
$result = $this->httpGet(self::WECHAT_TAG_USER_LIST_GET_PREFIX, [
'access_token' => $this->getAccessToken(),
'tagid' => $tagId
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result : false;
}
/**
* 增加标签成员
*/
const WECHAT_TAG_USERS_ADD_PREFIX = '/cgi-bin/tag/addtagusers';
/**
* 增加标签成员
* @param array $data
* @return bool
* @throws \yii\web\HttpException
*/
public function addTagUsers(array $data)
{
$result = $this->httpRaw(self::WECHAT_TAG_USERS_ADD_PREFIX, $data, [
'access_token' => $this->getAccessToken(),
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok';
}
/**
* 删除标签成员
*/
const WECHAT_TAG_USERS_DELETE_PREFIX = '/cgi-bin/tag/deltagusers';
/**
* 删除标签成员
* @param array $data
* @return bool
* @throws \yii\web\HttpException
*/
public function deleteTagUsers(array $data)
{
$result = $this->httpRaw(self::WECHAT_TAG_USERS_DELETE_PREFIX, $data, [
'access_token' => $this->getAccessToken(),
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok';
}
/**
* 获取标签列表
*/
const WECHAT_TAG_LIST_GET_PREFIX = '/cgi-bin/tag/list';
/**
* 获取标签列表
* @return bool|mixed
* @throws \yii\web\HttpException
*/
public function getTagList()
{
$result = $this->httpGet(self::WECHAT_TAG_LIST_GET_PREFIX, [
'access_token' => $this->getAccessToken()
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result['taglist'] : false;
}
/**
* 邀请成员关注
*/
const WECHAT_USER_BATCH_INVITE_PREFIX = '/cgi-bin/batch/inviteuser';
/**
* 邀请成员关注
* @param array $data
* @return bool
* @throws \yii\web\HttpException
*/
public function batchInviteUser(array $data)
{
$result = $this->httpRaw(self::WECHAT_TAG_USERS_DELETE_PREFIX, $data, [
'access_token' => $this->getAccessToken()
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result['jobid'] : false;
}
/**
* 增量更新成员
*/
const WECHAT_USER_BATCH_SYNC_PREFIX = '/cgi-bin/batch/syncuser';
/**
* 增量更新成员
* @param array $data
* @return bool
* @throws \yii\web\HttpException
*/
public function batchSyncUser(array $data)
{
$result = $this->httpRaw(self::WECHAT_USER_BATCH_SYNC_PREFIX, $data, [
'access_token' => $this->getAccessToken()
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result['jobid'] : false;
}
/**
* 全量覆盖成员
*/
const WECHAT_USER_BATCH_REPLACE_PREFIX = '/cgi-bin/batch/replaceuser';
/**
* 全量覆盖成员
* @param array $data
* @return bool
* @throws \yii\web\HttpException
*/
public function batchReplaceUser(array $data)
{
$result = $this->httpRaw(self::WECHAT_USER_BATCH_REPLACE_PREFIX, $data, [
'access_token' => $this->getAccessToken(),
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result['jobid'] : false;
}
/**
* 全量覆盖部门
*/
const WECHAT_PARTY_BATCH_REPLACE_PREFIX = '/cgi-bin/batch/replaceparty';
/**
* 全量覆盖部门
* @param array $data
* @return bool
* @throws \yii\web\HttpException
*/
public function batchReplaceParty(array $data)
{
$result = $this->httpRaw(self::WECHAT_PARTY_BATCH_REPLACE_PREFIX, $data, [
'access_token' => $this->getAccessToken()
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result['jobid'] : false;
}
/**
* 获取异步任务结果
*/
const WECHAT_BATCH_RESULT_GET_PREFIX = '/cgi-bin/batch/getresult';
/**
* 获取异步任务结果
* @param $jobId
* @return bool|mixed
* @throws \yii\web\HttpException
*/
public function getBatchResult($jobId)
{
$result = $this->httpGet(self::WECHAT_BATCH_RESULT_GET_PREFIX, [
'access_token' => $this->getAccessToken(),
'jobid' => $jobId
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result : false;
}
/* =================== 管理多媒体文件 =================== */
/**
* 上传媒体文件
*/
const WECHAT_MEDIA_UPLOAD_PREFIX = '/cgi-bin/media/upload';
/**
* 上传媒体文件
* @param $mediaPath
* @param $type
* @return bool|mixed
* @throws \yii\web\HttpException
*/
public function updateMedia($mediaPath, $type)
{
$result = $this->httpPost(self::WECHAT_MEDIA_UPLOAD_PREFIX, [
'media' => $this->uploadFile($mediaPath)
], [
'access_token' => $this->getAccessToken(),
'type' => $type
]);
return isset($result['media_id']) ? $result : false;
}
/**
* 获取媒体文件
*/
const WECHAT_MEDIA_GET_PREFIX = '/cgi-bin/media/get';
/**
* 获取媒体文件
* @param $mediaId
* @return bool|string
* @throws \yii\web\HttpException
*/
public function getMedia($mediaId)
{
$result = $this->httpGet(self::WECHAT_MEDIA_GET_PREFIX, [
'access_token' => $this->getAccessToken(),
'media_id' => $mediaId
]);
return !isset($result['errcode']) ? $result : false;
}
/* =================== 管理企业号应用 =================== */
/**
* 获取企业号应用
*/
const WECHAT_AGENT_GET_PREFIX = '/cgi-bin/agent/get';
/**
* 获取企业号应用
* @param $agentId
* @return bool|mixed
* @throws \yii\web\HttpException
*/
public function getAgent($agentId)
{
$result = $this->httpGet(self::WECHAT_AGENT_GET_PREFIX, [
'access_token' => $this->getAccessToken(),
'agent_id' => $agentId
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result : false;
}
/**
* 设置企业号应用
*/
const WECHAT_AGENT_SET_PREFIX = '/cgi-bin/agent/set';
/**
* 设置企业号应用
* @param array $data
* @return bool
* @throws \yii\web\HttpException
*/
public function setAgent(array $data)
{
$result = $this->httpRaw(self::WECHAT_AGENT_SET_PREFIX, $data, [
'access_token' => $this->getAccessToken()
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok';
}
/**
* 获取应用概况列表
*/
const WECHAT_AGENT_LIST_GET_PREFIX = '/cgi-bin/agent/list';
/**
* 获取应用概况列表
* @return bool
* @throws \yii\web\HttpException
*/
public function getAgentList()
{
$result = $this->httpGet(self::WECHAT_AGENT_SET_PREFIX, [
'access_token' => $this->getAccessToken()
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result['agentlist'] : false;
}
/* =================== 发送消息 =================== */
/**
* 发送消息
*/
const WECHAT_MESSAGE_SEND_PREFIX = '/cgi-bin/message/send';
/**
* 发送消息
* @param array $data
* @return bool
* @throws \yii\web\HttpException
*/
public function sendMessage(array $data)
{
$result = $this->httpRaw(self::WECHAT_CUSTOM_MESSAGE_SEND_PREFIX, $data, [
'access_token' => $this->getAccessToken()
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok' ? $result : false;
}
/* =================== 自定义菜单 =================== */
/**
* 创建应用菜单
*/
const WECHAT_MENU_CREATE_PREFIX = '/cgi-bin/menu/create';
/**
* 创建应用菜单
* @param $agentId
* @param array $data
* @return bool
* @throws \yii\web\HttpException
*/
public function createMenu($agentId, array $data)
{
$result = $this->httpRaw(self::WECHAT_MENU_CREATE_PREFIX, $data, [
'access_token' => $this->getAccessToken(),
'agentid' => $agentId
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok';
}
/**
* 删除菜单
*/
const WECHAT_MENU_DELETE_PREFIX = '/cgi-bin/menu/delete';
/**
* 删除菜单
* @param $agentId
* @return bool
* @throws \yii\web\HttpException
*/
public function deleteMenu($agentId)
{
$result = $this->httpGet(self::WECHAT_MENU_DELETE_PREFIX, [
'access_token' => $this->getAccessToken(),
'agentid' => $agentId
]);
return isset($result['errmsg']) && $result['errmsg'] == 'ok';
}
/**
* 获取菜单列表
*/
const WECHAT_MENU_GET_PREFIX = '/cgi-bin/menu/get';
/**
* 获取菜单列表
* @param $agentId
* @return bool
* @throws \yii\web\HttpException
*/
public function getMenu($agentId)
{
$result = $this->httpGet(self::WECHAT_MENU_GET_PREFIX, [
'access_token' => $this->getAccessToken(),
'agentid' => $agentId
]);
return isset($result['menu']['button']) ? $result['menu']['button'] : false;
}
/* =================== OAuth2验证接口 =================== */
/**
* 企业获取code
*/
const WECHAT_OAUTH2_AUTHORIZE_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize';
/**
* 企业获取code:第
* 通过此函数生成授权url
* @param $redirectUrl 授权后重定向的回调链接地址,请使用urlencode对链接进行处理
* @param string $state 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值
* @param string $scope 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),
* snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)
* @return string
*/
public function getOauth2AuthorizeUrl($redirectUrl, $state = 'authorize', $scope = 'snsapi_base')
{
return $this->httpBuildQuery(self::WECHAT_OAUTH2_AUTHORIZE_URL, [
'appid' => $this->corpId,
'redirect_uri' => $redirectUrl,
'response_type' => 'code',
'scope' => $scope,
'state' => $state,
]) . '#wechat_redirect';
}
/**
* 根据code获取成员信息
*/
const WECHAT_USER_IFNO_GET_PREFIX = '/cgi-bin/user/getuserinfo';
/**
* 根据code获取成员信息
* @param $agentId
* @param $code
* @return bool|mixed
* @throws \yii\web\HttpException
*/
public function getUserInfo($agentId, $code)
{
$result = $this->httpGet(self::WECHAT_USER_IFNO_GET_PREFIX, [
'access_token' => $this->getAccessToken(),
'code' => $code,
'agentid' => $agentId
]);
return !isset($result['errcode']) ? $result : false;
}
/* =================== 微信JS接口 =================== */
/**
* js api ticket 获取
*/
const WECHAT_JS_API_TICKET_PREFIX = '/cgi-bin/get_jsapi_ticket';
/**
* 请求服务器jsapi_ticket
* @return array
*/
protected function requestJsApiTicket()
{
return $this->httpGet(self::WECHAT_JS_API_TICKET_PREFIX, [
'access_token' => $this->getAccessToken(),
]);
}
/**
* 生成js 必需的config
* 只需在视图文件输出JS代码:
* wx.config(<?= json_encode($wehcat->jsApiConfig()) ?>); // 默认全权限
* wx.config(<?= json_encode($wehcat->jsApiConfig([ // 只允许使用分享到朋友圈功能
* 'jsApiList' => [
* 'onMenuShareTimeline'
* ]
* ])) ?>);
* @param array $config
* @return array
* @throws HttpException
*/
public function jsApiConfig(array $config = [])
{
$data = [
'jsapi_ticket' => $this->getJsApiTicket(),
'noncestr' => Yii::$app->security->generateRandomString(16),
'timestamp' => $_SERVER['REQUEST_TIME'],
'url' => explode('#', Yii::$app->request->getAbsoluteUrl())[0]
];
return array_merge([
'debug' => YII_DEBUG,
'appId' => $this->corpId,
'timestamp' => $data['timestamp'],
'nonceStr' => $data['noncestr'],
'signature' => sha1(urldecode(http_build_query($data))),
'jsApiList' => [
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'startRecord',
'stopRecord',
'onVoiceRecordEnd',
'playVoice',
'pauseVoice',
'stopVoice',
'onVoicePlayEnd',
'uploadVoice',
'downloadVoice',
'chooseImage',
'previewImage',
'uploadImage',
'downloadImage',
'translateVoice',
'getNetworkType',
'openLocation',
'getLocation',
'hideOptionMenu',
'showOptionMenu',
'hideMenuItems',
'showMenuItems',
'hideAllNonBaseMenuItem',
'showAllNonBaseMenuItem',
'closeWindow',
'scanQRCode'
]
], $config);
}
/* =================== 第三方应用授权 =================== */
}