-
Notifications
You must be signed in to change notification settings - Fork 23
/
User.php
385 lines (356 loc) · 13.3 KB
/
User.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
<?php
require_once (dirname(__FILE__) . '/CreateConnection.php');
require_once (dirname(__FILE__) . '/Tools.php');
/**
* 微信企业号
* 成员管理
* @author faith [email protected]
* @createtime 2015-07-23 03:11:41
*/
class User {
private $token;
private $tools;
public function __construct($token) {
$this -> token = $token;
$this -> tools = new Tools();
}
/**
* 设置token
*/
public function setToken($token = NULL) {
$this -> token = $token;
}
/**
* 获取token
*/
public function getToken() {
return $this -> token;
}
/**
* 根据部门ID获取用户列表
* @param $department_id 部门ID
* @param $fetch_child 1/0:是否递归获取子部门下面的成员
* @param $status 0获取全部成员,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加
*/
public function getUserList($department_id = 1, $fetch_child = 0, $status = 0) {
if (intval($department_id) < 1) {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Department_id must be greater than zero!', 'errcode'=>-2, 'userlist'=>array()));
}
$url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist";
$data = array('access_token'=>$this->token, 'department_id'=>$department_id);
if (intval($fetch_child) > -1) {
$data['fetch_child'] = $fetch_child;
}
if (intval($status) > -1) {
$data['status'] = $status;
}
$result = $this->tools->httpRequest($url, $data);
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
return json_encode($result);
}else {
$result['success'] = FALSE;
return json_encode($result);
}
}else {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Query fails!', 'errcode'=>-2, 'userlist'=>array()));
}
}
/**
* 根据部门ID获取用户列表(详情)
* @param $department_id 部门ID
* @param $fetch_child 1/0:是否递归获取子部门下面的成员
* @param $status 0获取全部成员,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加
*/
public function getUserListDetails($department_id = 1, $fetch_child = 0, $status = 0) {
if (intval($department_id) < 1) {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Department_id must be greater than zero!', 'errcode'=>-2, 'userlist'=>array()));
}
$url = "https://qyapi.weixin.qq.com/cgi-bin/user/list";
$data = array('access_token'=>$this->token, 'department_id'=>$department_id);
if (intval($fetch_child) > -1) {
$data['fetch_child'] = $fetch_child;
}
if (intval($status) > -1) {
$data['status'] = $status;
}
$result = $this->tools->httpRequest($url, $data);
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
return json_encode($result);
}else {
$result['success'] = FALSE;
return json_encode($result);
}
}else {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Query fails!', 'errcode'=>-2, 'userlist'=>array()));
}
}
/**
* 根据成员ID获取用户详细信息
* @param $userid 用户ID
*/
public function getUserByID($userid) {
if (empty($userid)) {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Userid is empty!', 'errcode'=>-2));
}
$url = "https://qyapi.weixin.qq.com/cgi-bin/user/get";
$data = array('access_token'=>$this->token, 'userid'=>$userid);
$result = $this->tools->httpRequest($url, $data);
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
$result['userid'] = $userid;
return json_encode($result);
}else {
$result['success'] = FALSE;
return json_encode($result);
}
}else {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Query fails!', 'errcode'=>-2));
}
}
/**
* 根据成员名称获得成员详细信息
* @param $name 成员名称
* @param $fuzzy 是否为模糊查询,默认为模糊查询
*/
public function getUserByName($name, $fuzzy = TRUE) {
if (empty($name)) {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Name is empty!', 'errcode'=>-2));
}
$result = array('success'=>FALSE, 'errmsg'=>'Query fails!', 'errcode'=>-2, 'userlist'=>array());
$userlist = $this->getUserListDetails(1, 1);
$userlist = json_decode($userlist, TRUE);
foreach ($userlist['userlist'] as $item) {
if ($fuzzy) {
if (stristr($item['name'], $name)) {
$result['success'] = TRUE;
$result['errmsg'] = $userlist['errmsg'];
$result['errcode'] = $userlist['errcode'];
$result['userlist'][] = $item;
}
}else {
if ($item['name'] == $name) {
$result['success'] = TRUE;
$result['errmsg'] = $userlist['errmsg'];
$result['errcode'] = $userlist['errcode'];
$result['userlist'][] = $item;
}
}
}
return json_encode($result);
}
/**
* 创建成员
* @param $userid 成员UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字节,必填
* @param $name 成员名称。长度为1~64个字节,必填
* @param $departmentid 成员所属部门id列表。注意,每个部门的直属成员上限为1000个
* @param $mobile 手机号码。企业内必须唯一,mobile/weixinid/email三者不能同时为空
* @param $email 邮箱。长度为0~64个字节。企业内必须唯一,mobile/weixinid/email三者不能同时为空
* @param $weixinid 微信号。企业内必须唯一。(注意:是微信号,不是微信的名字)
* @param $position 职位信息。长度为0~64个字节
* @param $gender 性别。1表示男性,2表示女性
* @param $avatar_mediaid 成员头像的mediaid,通过多媒体接口上传图片获得的mediaid
* @param $extattr 扩展属性。扩展属性需要在WEB管理端创建后才生效,否则忽略未知属性的赋值,以数组形式传入
*/
public function createUser($userid, $name, $departmentid, $mobile = FALSE, $email = FALSE, $weixinid = FALSE, $position = FALSE, $gender = FALSE, $avatar_mediaid = FALSE, $extattr = array()) {
if (empty($userid) || empty($name) || empty($departmentid)) {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Userid or name or departmentid is empty!', 'errcode'=>-2));
}
$url = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token={$this->token}";
$data = array('userid'=>$userid, 'name'=>$name, 'department'=>$departmentid);
if (!empty($mobile)) {
$data['mobile'] = $mobile;
}
if (!empty($email)) {
$data['email'] = $email;
}
if (!empty($weixinid)) {
$data['weixinid'] = $weixinid;
}
if (!empty($position)) {
$data['position'] = $position;
}
if (!empty($gender)) {
$data['gender'] = $gender;
}
if (!empty($avatar_mediaid)) {
$data['avatar_mediaid'] = $avatar_mediaid;
}
if (!empty($extattr) && is_array($extattr)) {
$temp = array();
foreach ($extattr as $key => $value) {
$temp[] = array('name'=>$key, 'value'=>$value);
}
$data['extattr'] = array('attrs'=>$temp);
}
$result = $this->tools->httpRequest($url, $data, 'post');
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
$result['userid'] = $userid;
$result['name'] = $name;
$this->inviteConcern($userid);
return json_encode($result);
}else {
$result['success'] = FALSE;
return json_encode($result);
}
}else {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Create a user fails!', 'errcode'=>-2));
}
}
/**
* 更新成员
* @param $userid 成员UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字节,必填
* @param $name 成员名称。长度为1~64个字节,必填
* @param $departmentid 成员所属部门id列表。注意,每个部门的直属成员上限为1000个
* @param $position 职位信息。长度为0~64个字节
* @param $mobile 手机号码。企业内必须唯一,mobile/weixinid/email三者不能同时为空
* @param $gender 性别。1表示男性,2表示女性
* @param $email 邮箱。长度为0~64个字节。企业内必须唯一,mobile/weixinid/email三者不能同时为空
* @param $weixinid 微信号。企业内必须唯一。(注意:是微信号,不是微信的名字)
* @param $enable 启用/禁用成员。1表示启用成员,0表示禁用成员,默认为1
* @param $avatar_mediaid 成员头像的mediaid,通过多媒体接口上传图片获得的mediaid
* @param $extattr 扩展属性。扩展属性需要在WEB管理端创建后才生效,否则忽略未知属性的赋值,以数组形式传入
*/
public function updateUser($userid, $name = FALSE, $departmentid = FALSE, $position = FALSE, $mobile= FALSE, $gender = FALSE, $email = FALSE, $weixinid = FALSE, $enable = 1, $avatar_mediaid = FALSE, $extattr = array()) {
if (empty($userid)) {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Userid is empty!', 'errcode'=>-2));
}
$url = "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token={$this->token}";
$data = array('userid'=>$userid, 'enable'=>$enable);
if (!empty($name)) {
$data['name'] = $name;
}
if (!empty($departmentid)) {
$data['department'] = $departmentid;
}
if (!empty($position)) {
$data['position'] = $position;
}
if (!empty($mobile)) {
$data['mobile'] = $mobile;
}
if (!empty($gender)) {
$data['gender'] = $gender;
}
if (!empty($email)) {
$data['email'] = $email;
}
if (!empty($weixinid)) {
$data['weixinid'] = $weixinid;
}
if (!empty($avatar_mediaid)) {
$data['avatar_mediaid'] = $avatar_mediaid;
}
if (!empty($extattr) && is_array($extattr)) {
$temp = array();
foreach ($extattr as $key => $value) {
$temp[] = array('name'=>$key, 'value'=>$value);
}
$data['extattr'] = array('attrs'=>$temp);
}
$result = $this->tools->httpRequest($url, $data, 'post');
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
$result['userid'] = $userid;
$this->inviteConcern($userid);
return json_encode($result);
}else {
$result['success'] = FALSE;
return json_encode($result);
}
}else {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Update a user fails!', 'errcode'=>-2));
}
}
/**
* 删除成员
* @param $userid 成员UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字节,必填
*/
public function deleteUser($userid) {
if (empty($userid)) {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Userid is empty!', 'errcode'=>-2));
}
$url = "https://qyapi.weixin.qq.com/cgi-bin/user/delete";
$data = array('access_token'=>$this->token, 'userid'=>$userid);
$result = $this->tools->httpRequest($url, $data);
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
$result['userid'] = $userid;
return json_encode($result);
}else {
$result['success'] = FALSE;
return json_encode($result);
}
}else {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Delete a user fails!', 'errcode'=>-2));
}
}
/**
* 批量删除成员
* @param $useridlist 成员UserID列表。 形如["zhangsan", "lisi"]的一维数组
*/
public function batchDeleteUsers($useridlist) {
if (empty($useridlist)) {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Useridlist is empty!', 'errcode'=>-2));
}else if (!is_array($useridlist)) {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Useridlist not an array!', 'errcode'=>-2));
}
$url = "https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete?access_token={$this->token}";
$data = array('useridlist'=>$useridlist);
$result = $this->tools->httpRequest($url, $data, 'post');
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
return json_encode($result);
}else {
$result['success'] = FALSE;
return json_encode($result);
}
}else {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Batch delete users fails!', 'errcode'=>-2));
}
}
/**
* 邀请成员关注企业号
* 认证号优先使用微信推送邀请关注,如果没有weixinid字段则依次对手机号,邮箱绑定的微信进行推送,全部没有匹配则通过邮件邀请关注。 邮箱字段无效则邀请失败。 非认证号只通过邮件邀请关注。邮箱字段无效则邀请失败。 已关注以及被禁用成员不允许发起邀请关注请求。
* 为避免骚扰成员,企业应遵守以下邀请规则:
* 每月邀请的总人次不超过成员上限的2倍;每7天对同一个成员只能邀请一次。
* @param $userid 成员UserID。对应管理端的帐号
*/
public function inviteConcern($userid) {
if (empty($userid)) {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Invite concern failure!', 'errcode'=>-2));
}
$url = "https://qyapi.weixin.qq.com/cgi-bin/invite/send?access_token={$this->token}";
$data = array('userid'=>$userid);
$result = $this->tools->httpRequest($url, $data, 'post');
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
$result['userid'] = $userid;
if ($result['type'] == 1) {
$result['result'] = '已发出微信邀请';
}else if ($result['type'] == 2) {
$result['result'] = '已发出邮件邀请';
}else {
$result['result'] = '已发出邀请';
}
return json_encode($result);
}else {
$result['success'] = FALSE;
return json_encode($result);
}
}else {
return json_encode(array('success'=>FALSE, 'errmsg'=>'Invite concern failure!', 'errcode'=>-2));
}
}
}
/* End of file */