1 | <?php |
||
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() |
||
120 | |||
121 | /** |
||
122 | * 设置 http 请求客户端. |
||
123 | * |
||
124 | * @param HttpClient $httpClient |
||
125 | */ |
||
126 | public function setHttpClient($httpClient) |
||
130 | |||
131 | /** |
||
132 | * 获取 http 客户端 |
||
133 | * @return HttpClient |
||
134 | */ |
||
135 | public function getHttpClient() |
||
139 | |||
140 | /** |
||
141 | * 设置事件处理器. |
||
142 | * |
||
143 | * @param DispatcherInterface $eventDispatcher |
||
144 | */ |
||
145 | public function setEventDispatcher($eventDispatcher) |
||
149 | |||
150 | /** |
||
151 | * 获取事件处理器. |
||
152 | * |
||
153 | * @return DispatcherInterface |
||
154 | */ |
||
155 | public function getEventDispatcher() |
||
159 | |||
160 | /** |
||
161 | * 获取所有的群. |
||
162 | * |
||
163 | * @return EntityCollection |
||
164 | */ |
||
165 | public function getGroups() |
||
172 | |||
173 | /** |
||
174 | * 获取群详细信息. |
||
175 | * |
||
176 | * @param Entity\Group $group |
||
177 | * |
||
178 | * @return Entity\GroupDetail |
||
179 | */ |
||
180 | public function getGroupDetail(Entity\Group $group) |
||
187 | |||
188 | /** |
||
189 | * 获取所有讨论组. |
||
190 | * |
||
191 | * @return EntityCollection |
||
192 | */ |
||
193 | public function getDiscusses() |
||
200 | |||
201 | /** |
||
202 | * 获取讨论组详情. |
||
203 | * |
||
204 | * @param Entity\Discuss $discuss |
||
205 | * |
||
206 | * @return Entity\DiscussDetail |
||
207 | */ |
||
208 | public function getDiscussDetail(Entity\Discuss $discuss) |
||
215 | |||
216 | /** |
||
217 | * 获取所有的好友. |
||
218 | * |
||
219 | * @return EntityCollection|Entity\Friend[] |
||
220 | */ |
||
221 | public function getFriends() |
||
228 | |||
229 | /** |
||
230 | * 获取好友的详细信息. |
||
231 | * |
||
232 | * @param Entity\Friend $friend |
||
233 | * |
||
234 | * @return Entity\Profile |
||
235 | */ |
||
236 | public function getFriendDetail(Entity\Friend $friend) |
||
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) |
||
263 | |||
264 | /** |
||
265 | * 获取好友的个性签名. |
||
266 | * |
||
267 | * @param Entity\Friend $friend |
||
268 | * |
||
269 | * @return string |
||
270 | */ |
||
271 | public function getFriendLnick(Entity\Friend $friend) |
||
278 | |||
279 | /** |
||
280 | * 获取好友在线状态 |
||
281 | * |
||
282 | * @return EntityCollection |
||
283 | */ |
||
284 | public function getFriendsOnlineStatus() |
||
291 | |||
292 | /** |
||
293 | * 获取最近的会话. |
||
294 | * |
||
295 | * @return EntityCollection|Entity\Recent[] |
||
296 | */ |
||
297 | public function getRecentList() |
||
304 | |||
305 | /** |
||
306 | * 获取当前登录用户信息. |
||
307 | * |
||
308 | * @return Entity\Profile |
||
309 | */ |
||
310 | public function getCurrentUserInfo() |
||
317 | |||
318 | /** |
||
319 | * 轮询消息, |
||
320 | * client并不会组装信息,只是将接口返回的信息完整抽象并返回 |
||
321 | * 如果需要查询信息对应的数据,如发送人、发送群,请自行获取. |
||
322 | * |
||
323 | * @return ResponseMessage[] |
||
324 | */ |
||
325 | public function pollMessages() |
||
332 | |||
333 | /** |
||
334 | * 获取 Message handler. |
||
335 | * |
||
336 | * @return MessageHandler |
||
337 | */ |
||
338 | public function getMessageHandler() |
||
345 | |||
346 | /** |
||
347 | * 发送消息,包括好友消息,群消息,讨论组消息. |
||
348 | * |
||
349 | * @param RequestMessage $message |
||
350 | * |
||
351 | * @return bool |
||
352 | */ |
||
353 | public function sendMessage(RequestMessage $message) |
||
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 = []) |
||
402 | } |
||
403 |
If you suppress an error, we recommend checking for the error condition explicitly: