1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/wechat. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace EasyWeChat\Work\User; |
13
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\BaseClient; |
15
|
|
|
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Client. |
19
|
|
|
* |
20
|
|
|
* @author mingyoung <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Client extends BaseClient |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Create a user. |
26
|
|
|
* |
27
|
|
|
* @param array $data |
28
|
|
|
* |
29
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
30
|
|
|
*/ |
31
|
1 |
|
public function create(array $data) |
32
|
|
|
{ |
33
|
1 |
|
return $this->httpPostJson('cgi-bin/user/create', $data); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Update an exist user. |
38
|
|
|
* |
39
|
|
|
* @param string $id |
40
|
|
|
* @param array $data |
41
|
|
|
* |
42
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
43
|
|
|
*/ |
44
|
1 |
|
public function update(string $id, array $data) |
45
|
|
|
{ |
46
|
1 |
|
return $this->httpPostJson('cgi-bin/user/update', array_merge(['userid' => $id], $data)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Delete a user. |
51
|
|
|
* |
52
|
|
|
* @param string|array $userId |
53
|
|
|
* |
54
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
55
|
|
|
*/ |
56
|
1 |
|
public function delete($userId) |
57
|
|
|
{ |
58
|
1 |
|
if (is_array($userId)) { |
59
|
1 |
|
return $this->batchDelete($userId); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return $this->httpGet('cgi-bin/user/delete', ['userid' => $userId]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Batch delete users. |
67
|
|
|
* |
68
|
|
|
* @param array $userIds |
69
|
|
|
* |
70
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
71
|
|
|
*/ |
72
|
1 |
|
public function batchDelete(array $userIds) |
73
|
|
|
{ |
74
|
1 |
|
return $this->httpPostJson('cgi-bin/user/batchdelete', ['useridlist' => $userIds]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get user. |
79
|
|
|
* |
80
|
|
|
* @param string $userId |
81
|
|
|
* |
82
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
83
|
|
|
*/ |
84
|
1 |
|
public function get(string $userId) |
85
|
|
|
{ |
86
|
1 |
|
return $this->httpGet('cgi-bin/user/get', ['userid' => $userId]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get simple user list. |
91
|
|
|
* |
92
|
|
|
* @param int $departmentId |
93
|
|
|
* @param bool $fetchChild |
94
|
|
|
* |
95
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
96
|
|
|
*/ |
97
|
1 |
|
public function getDepartmentUsers(int $departmentId, bool $fetchChild = false) |
98
|
|
|
{ |
99
|
|
|
$params = [ |
100
|
1 |
|
'department_id' => $departmentId, |
101
|
1 |
|
'fetch_child' => (int) $fetchChild, |
102
|
|
|
]; |
103
|
|
|
|
104
|
1 |
|
return $this->httpGet('cgi-bin/user/simplelist', $params); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get user list. |
109
|
|
|
* |
110
|
|
|
* @param int $departmentId |
111
|
|
|
* @param bool $fetchChild |
112
|
|
|
* |
113
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
114
|
|
|
*/ |
115
|
1 |
|
public function getDetailedDepartmentUsers(int $departmentId, bool $fetchChild = false) |
116
|
|
|
{ |
117
|
|
|
$params = [ |
118
|
1 |
|
'department_id' => $departmentId, |
119
|
1 |
|
'fetch_child' => (int) $fetchChild, |
120
|
|
|
]; |
121
|
|
|
|
122
|
1 |
|
return $this->httpGet('cgi-bin/user/list', $params); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Convert userId to openid. |
127
|
|
|
* |
128
|
|
|
* @param string $userId |
129
|
|
|
* @param int|null $agentId |
130
|
|
|
* |
131
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
132
|
|
|
*/ |
133
|
1 |
|
public function userIdToOpenid(string $userId, int $agentId = null) |
134
|
|
|
{ |
135
|
|
|
$params = [ |
136
|
1 |
|
'userid' => $userId, |
137
|
1 |
|
'agentid' => $agentId, |
138
|
|
|
]; |
139
|
|
|
|
140
|
1 |
|
return $this->httpPostJson('cgi-bin/user/convert_to_openid', $params); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Convert openid to userId. |
145
|
|
|
* |
146
|
|
|
* @param string $openid |
147
|
|
|
* |
148
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
149
|
|
|
*/ |
150
|
1 |
|
public function openidToUserId(string $openid) |
151
|
|
|
{ |
152
|
|
|
$params = [ |
153
|
1 |
|
'openid' => $openid, |
154
|
|
|
]; |
155
|
|
|
|
156
|
1 |
|
return $this->httpPostJson('cgi-bin/user/convert_to_userid', $params); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Convert mobile to userId. |
161
|
|
|
* |
162
|
|
|
* @param string $mobile |
163
|
|
|
* |
164
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
165
|
|
|
* |
166
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
167
|
|
|
*/ |
168
|
1 |
|
public function mobileToUserId(string $mobile) |
169
|
|
|
{ |
170
|
|
|
$params = [ |
171
|
1 |
|
'mobile' => $mobile, |
172
|
|
|
]; |
173
|
|
|
|
174
|
1 |
|
return $this->httpPostJson('cgi-bin/user/getuserid', $params); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $userId |
179
|
|
|
* |
180
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
181
|
|
|
*/ |
182
|
1 |
|
public function accept(string $userId) |
183
|
|
|
{ |
184
|
|
|
$params = [ |
185
|
1 |
|
'userid' => $userId, |
186
|
|
|
]; |
187
|
|
|
|
188
|
1 |
|
return $this->httpGet('cgi-bin/user/authsucc', $params); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Batch invite users. |
193
|
|
|
* |
194
|
|
|
* @param array $params |
195
|
|
|
* |
196
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
197
|
|
|
* |
198
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
199
|
|
|
*/ |
200
|
1 |
|
public function invite(array $params) |
201
|
|
|
{ |
202
|
1 |
|
return $this->httpPostJson('cgi-bin/batch/invite', $params); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get invitation QR code. |
207
|
|
|
* |
208
|
|
|
* @param int $sizeType |
209
|
|
|
* |
210
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
211
|
|
|
* |
212
|
|
|
* @throws InvalidArgumentException |
213
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
214
|
|
|
*/ |
215
|
1 |
|
public function getInvitationQrCode(int $sizeType = 1) |
216
|
|
|
{ |
217
|
1 |
|
if (!\in_array($sizeType, [1, 2, 3, 4])) { |
218
|
1 |
|
throw new InvalidArgumentException('The sizeType must be 1, 2, 3, 4.'); |
219
|
|
|
} |
220
|
|
|
|
221
|
1 |
|
return $this->httpGet('cgi-bin/corp/get_join_qrcode', ['size_type' => $sizeType]); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|