1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the slince/smartqq package. |
4
|
|
|
* |
5
|
|
|
* (c) Slince <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Slince\SmartQQ; |
12
|
|
|
|
13
|
|
|
use GuzzleHttp\Client as HttpClient; |
14
|
|
|
use GuzzleHttp\Psr7\Request as HttpRequest; |
15
|
|
|
use GuzzleHttp\Psr7\Response as HttpResponse; |
16
|
|
|
use Slince\EventDispatcher\Dispatcher; |
17
|
|
|
use Slince\EventDispatcher\DispatcherInterface; |
18
|
|
|
use Slince\SmartQQ\Entity; |
19
|
|
|
use Slince\SmartQQ\Exception\InvalidArgumentException; |
20
|
|
|
use Slince\SmartQQ\Message\Request\FriendMessage; |
21
|
|
|
use Slince\SmartQQ\Message\Request\GroupMessage; |
22
|
|
|
use Slince\SmartQQ\Message\Request\Message as RequestMessage; |
23
|
|
|
use Slince\SmartQQ\Message\Response\Message as ResponseMessage; |
24
|
|
|
use Slince\SmartQQ\Request; |
25
|
|
|
|
26
|
|
|
class Client |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Credential |
30
|
|
|
*/ |
31
|
|
|
protected $credential; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var HttpClient |
35
|
|
|
*/ |
36
|
|
|
protected $httpClient; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var DispatcherInterface |
40
|
|
|
*/ |
41
|
|
|
protected $eventDispatcher; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var MessageHandler |
45
|
|
|
*/ |
46
|
|
|
protected $messageHandler; |
47
|
|
|
|
48
|
|
|
public function __construct( |
49
|
|
|
Credential $credential = null, |
50
|
|
|
HttpClient $httpClient = null, |
51
|
|
|
DispatcherInterface $eventDispatcher = null |
52
|
|
|
) |
53
|
|
|
{ |
54
|
|
|
if (!is_null($credential)) { |
55
|
|
|
$this->setCredential($credential); |
56
|
|
|
} |
57
|
|
|
if (is_null($httpClient)) { |
58
|
|
|
$httpClient = new HttpClient([ |
59
|
|
|
'verify' => false, |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
if (is_null($eventDispatcher)) { |
63
|
|
|
$eventDispatcher = new Dispatcher(); |
64
|
|
|
} |
65
|
|
|
$this->httpClient = $httpClient; |
66
|
|
|
$this->eventDispatcher = $eventDispatcher; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* 开启登录流程自行获取凭证 |
71
|
|
|
* |
72
|
|
|
* @param string|callable $qrCallback 二维码图片位置|或者自定义处理器 |
73
|
|
|
* |
74
|
|
|
* @return Credential |
75
|
|
|
*/ |
76
|
|
|
public function login($qrCallback) |
77
|
|
|
{ |
78
|
|
|
$resolver = new CredentialResolver($this); |
79
|
|
|
|
80
|
|
|
// 兼容二维码位置传参 |
81
|
|
|
if (is_string($qrCallback)) { |
82
|
|
|
$qrCallback = function ($qrcode) use($qrCallback){ |
83
|
|
|
file_put_contents($qrCallback, $qrcode); |
84
|
|
|
}; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// 进行授权流程,确认授权 |
88
|
|
|
$credential = $resolver->resolve($qrCallback); |
89
|
|
|
$this->setCredential($credential); |
90
|
|
|
|
91
|
|
|
//获取在线状态避免103 |
92
|
|
|
$this->getFriendsOnlineStatus(); |
93
|
|
|
|
94
|
|
|
return $this->credential; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* 设置凭据. |
99
|
|
|
* |
100
|
|
|
* @param Credential $credential |
101
|
|
|
*/ |
102
|
|
|
public function setCredential(Credential $credential) |
103
|
|
|
{ |
104
|
|
|
$this->credential = $credential; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* 获取正在使用的凭据. |
109
|
|
|
* |
110
|
|
|
* @return Credential |
111
|
|
|
*/ |
112
|
|
|
public function getCredential() |
113
|
|
|
{ |
114
|
|
|
if (!$this->credential) { |
115
|
|
|
throw new InvalidArgumentException('Please login first or set a credential'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->credential; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* 设置 http 请求客户端. |
123
|
|
|
* |
124
|
|
|
* @param HttpClient $httpClient |
125
|
|
|
*/ |
126
|
|
|
public function setHttpClient($httpClient) |
127
|
|
|
{ |
128
|
|
|
$this->httpClient = $httpClient; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* 获取 http 客户端 |
133
|
|
|
* @return HttpClient |
134
|
|
|
*/ |
135
|
|
|
public function getHttpClient() |
136
|
|
|
{ |
137
|
|
|
return $this->httpClient; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* 设置事件处理器. |
142
|
|
|
* |
143
|
|
|
* @param DispatcherInterface $eventDispatcher |
144
|
|
|
*/ |
145
|
|
|
public function setEventDispatcher($eventDispatcher) |
146
|
|
|
{ |
147
|
|
|
$this->eventDispatcher = $eventDispatcher; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* 获取事件处理器. |
152
|
|
|
* |
153
|
|
|
* @return DispatcherInterface |
154
|
|
|
*/ |
155
|
|
|
public function getEventDispatcher() |
156
|
|
|
{ |
157
|
|
|
return $this->eventDispatcher; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* 获取所有的群. |
162
|
|
|
* |
163
|
|
|
* @return EntityCollection |
164
|
|
|
*/ |
165
|
|
|
public function getGroups() |
166
|
|
|
{ |
167
|
|
|
$request = new Request\GetGroupsRequest($this->getCredential()); |
168
|
|
|
$response = $this->sendRequest($request); |
169
|
|
|
|
170
|
|
|
return Request\GetGroupsRequest::parseResponse($response); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* 获取群详细信息. |
175
|
|
|
* |
176
|
|
|
* @param Entity\Group $group |
177
|
|
|
* |
178
|
|
|
* @return Entity\GroupDetail |
179
|
|
|
*/ |
180
|
|
|
public function getGroupDetail(Entity\Group $group) |
181
|
|
|
{ |
182
|
|
|
$request = new Request\GetGroupDetailRequest($group, $this->getCredential()); |
183
|
|
|
$response = $this->sendRequest($request); |
184
|
|
|
|
185
|
|
|
return Request\GetGroupDetailRequest::parseResponse($response); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* 获取所有讨论组. |
190
|
|
|
* |
191
|
|
|
* @return EntityCollection |
192
|
|
|
*/ |
193
|
|
|
public function getDiscusses() |
194
|
|
|
{ |
195
|
|
|
$request = new Request\GetDiscussesRequest($this->getCredential()); |
196
|
|
|
$response = $this->sendRequest($request); |
197
|
|
|
|
198
|
|
|
return Request\GetDiscussesRequest::parseResponse($response); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* 获取讨论组详情. |
203
|
|
|
* |
204
|
|
|
* @param Entity\Discuss $discuss |
205
|
|
|
* |
206
|
|
|
* @return Entity\DiscussDetail |
207
|
|
|
*/ |
208
|
|
|
public function getDiscussDetail(Entity\Discuss $discuss) |
209
|
|
|
{ |
210
|
|
|
$request = new Request\GetDiscussDetailRequest($discuss, $this->getCredential()); |
211
|
|
|
$response = $this->sendRequest($request); |
212
|
|
|
|
213
|
|
|
return Request\GetDiscussDetailRequest::parseResponse($response); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* 获取所有的好友. |
218
|
|
|
* |
219
|
|
|
* @return EntityCollection|Entity\Friend[] |
220
|
|
|
*/ |
221
|
|
|
public function getFriends() |
222
|
|
|
{ |
223
|
|
|
$request = new Request\GetFriendsRequest($this->getCredential()); |
224
|
|
|
$response = $this->sendRequest($request); |
225
|
|
|
|
226
|
|
|
return Request\GetFriendsRequest::parseResponse($response); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* 获取好友的详细信息. |
231
|
|
|
* |
232
|
|
|
* @param Entity\Friend $friend |
233
|
|
|
* |
234
|
|
|
* @return Entity\Profile |
235
|
|
|
*/ |
236
|
|
|
public function getFriendDetail(Entity\Friend $friend) |
237
|
|
|
{ |
238
|
|
|
$request = new Request\GetFriendDetailRequest($friend, $this->getCredential()); |
239
|
|
|
$response = $this->sendRequest($request); |
240
|
|
|
|
241
|
|
|
return Request\GetFriendDetailRequest::parseResponse($response); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* 获取好友的QQ号. |
246
|
|
|
* |
247
|
|
|
* @param Entity\Friend $friend |
248
|
|
|
* |
249
|
|
|
* @return int |
250
|
|
|
* @deprecated 此接口 Smartqq 官方已经不再提供 |
251
|
|
|
*/ |
252
|
|
|
public function getFriendQQ(Entity\Friend $friend) |
253
|
|
|
{ |
254
|
|
|
@trigger_error('The api is not supported now',E_USER_DEPRECATED); |
|
|
|
|
255
|
|
|
|
256
|
|
|
$request = new Request\GetQQRequest($friend, $this->getCredential()); |
257
|
|
|
$response = $this->sendRequest($request); |
258
|
|
|
$qq = Request\GetQQRequest::parseResponse($response); |
259
|
|
|
$friend->setQq($qq); |
260
|
|
|
|
261
|
|
|
return $qq; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* 获取好友的个性签名. |
266
|
|
|
* |
267
|
|
|
* @param Entity\Friend $friend |
268
|
|
|
* |
269
|
|
|
* @return string |
270
|
|
|
*/ |
271
|
|
|
public function getFriendLnick(Entity\Friend $friend) |
272
|
|
|
{ |
273
|
|
|
$request = new Request\GetLnickRequest($friend, $this->getCredential()); |
274
|
|
|
$response = $this->sendRequest($request); |
275
|
|
|
|
276
|
|
|
return Request\GetLnickRequest::parseResponse($response, $friend); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* 获取好友在线状态 |
281
|
|
|
* |
282
|
|
|
* @return EntityCollection |
283
|
|
|
*/ |
284
|
|
|
public function getFriendsOnlineStatus() |
285
|
|
|
{ |
286
|
|
|
$request = new Request\GetFriendsOnlineStatusRequest($this->getCredential()); |
287
|
|
|
$response = $this->sendRequest($request); |
288
|
|
|
|
289
|
|
|
return Request\GetFriendsOnlineStatusRequest::parseResponse($response); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* 获取最近的会话. |
294
|
|
|
* |
295
|
|
|
* @return EntityCollection|Entity\Recent[] |
296
|
|
|
*/ |
297
|
|
|
public function getRecentList() |
298
|
|
|
{ |
299
|
|
|
$request = new Request\GetRecentListRequest($this->getCredential()); |
300
|
|
|
$response = $this->sendRequest($request); |
301
|
|
|
|
302
|
|
|
return Request\GetRecentListRequest::parseResponse($response); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* 获取当前登录用户信息. |
307
|
|
|
* |
308
|
|
|
* @return Entity\Profile |
309
|
|
|
*/ |
310
|
|
|
public function getCurrentUserInfo() |
311
|
|
|
{ |
312
|
|
|
$request = new Request\GetCurrentUserRequest(); |
313
|
|
|
$response = $this->sendRequest($request); |
314
|
|
|
|
315
|
|
|
return Request\GetCurrentUserRequest::parseResponse($response); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* 轮询消息, |
320
|
|
|
* client并不会组装信息,只是将接口返回的信息完整抽象并返回 |
321
|
|
|
* 如果需要查询信息对应的数据,如发送人、发送群,请自行获取. |
322
|
|
|
* |
323
|
|
|
* @return ResponseMessage[] |
324
|
|
|
*/ |
325
|
|
|
public function pollMessages() |
326
|
|
|
{ |
327
|
|
|
$request = new Request\PollMessagesRequest($this->getCredential()); |
328
|
|
|
$response = $this->sendRequest($request); |
329
|
|
|
|
330
|
|
|
return Request\PollMessagesRequest::parseResponse($response); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* 获取 Message handler. |
335
|
|
|
* |
336
|
|
|
* @return MessageHandler |
337
|
|
|
*/ |
338
|
|
|
public function getMessageHandler() |
339
|
|
|
{ |
340
|
|
|
if (null !== $this->messageHandler) { |
341
|
|
|
return $this->messageHandler; |
342
|
|
|
} |
343
|
|
|
return $this->messageHandler = new MessageHandler($this); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* 发送消息,包括好友消息,群消息,讨论组消息. |
348
|
|
|
* |
349
|
|
|
* @param RequestMessage $message |
350
|
|
|
* |
351
|
|
|
* @return bool |
352
|
|
|
*/ |
353
|
|
|
public function sendMessage(RequestMessage $message) |
354
|
|
|
{ |
355
|
|
|
if ($message instanceof FriendMessage) { |
356
|
|
|
$request = new Request\SendFriendMessageRequest($message, $this->getCredential()); |
357
|
|
|
} elseif ($message instanceof GroupMessage) { |
358
|
|
|
$request = new Request\SendGroupMessageRequest($message, $this->getCredential()); |
359
|
|
|
} else { |
360
|
|
|
$request = new Request\SendDiscusMessageRequest($message, $this->getCredential()); |
|
|
|
|
361
|
|
|
} |
362
|
|
|
$response = $this->sendRequest($request); |
363
|
|
|
|
364
|
|
|
return Request\SendMessageRequest::parseResponse($response); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* 发送请求 |
370
|
|
|
* |
371
|
|
|
* @param Request\RequestInterface $request |
372
|
|
|
* @param array $options |
373
|
|
|
* |
374
|
|
|
* @return HttpResponse |
375
|
|
|
*/ |
376
|
|
|
public function sendRequest(Request\RequestInterface $request, array $options = []) |
377
|
|
|
{ |
378
|
|
|
// cookies 必须启用. |
379
|
|
|
if (!isset($options['cookies']) && $this->credential) { |
380
|
|
|
$options['cookies'] = $this->credential->getCookies(); |
381
|
|
|
} |
382
|
|
|
if ($parameters = $request->getParameters()) { |
383
|
|
|
if (Request\RequestInterface::REQUEST_METHOD_GET == $request->getMethod()) { |
384
|
|
|
$options['query'] = $parameters; |
385
|
|
|
} else { |
386
|
|
|
$options['form_params'] = $parameters; |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
//如果有referer需要伪造该信息 |
390
|
|
|
if ($referer = $request->getReferer()) { |
391
|
|
|
$options['headers'] = [ |
392
|
|
|
'Referer' => $referer, |
393
|
|
|
]; |
394
|
|
|
} |
395
|
|
|
$response = $this->httpClient->send( |
396
|
|
|
new HttpRequest($request->getMethod(), $request->getUri()), |
397
|
|
|
$options |
398
|
|
|
); |
399
|
|
|
|
400
|
|
|
return $response; |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: