MessageHandler.php
5.76 KB
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
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2016/12/14
* Time: 23:08
*/
namespace Hanson\Vbot\Core;
use Closure;
use Hanson\Vbot\Message\Entity\Emoticon;
use Hanson\Vbot\Message\Entity\Image;
use Hanson\Vbot\Message\Entity\Message;
use Hanson\Vbot\Message\Entity\Text;
use Hanson\Vbot\Message\Entity\Video;
use Hanson\Vbot\Support\Console;
use Hanson\Vbot\Support\System;
class MessageHandler
{
/**
* @var MessageHandler
*/
static $instance = null;
private $handler;
private $customHandler;
private $exitHandler;
private $exceptionHandler;
private $onceHandler;
private $sync;
private $messageFactory;
public function __construct()
{
$this->sync = new Sync();
$this->messageFactory = new MessageFactory();
}
/**
* 设置单例模式
*
* @return MessageHandler
*/
public static function getInstance()
{
if(static::$instance === null){
static::$instance = new MessageHandler();
}
return static::$instance;
}
/**
* 消息处理器
*
* @param Closure $closure
* @throws \Exception
*/
public function setMessageHandler(Closure $closure)
{
if(!$closure instanceof Closure){
throw new \Exception('message handler must be a closure!');
}
$this->handler = $closure;
}
/**
* 自定义处理器
*
* @param Closure $closure
* @throws \Exception
*/
public function setCustomHandler(Closure $closure)
{
if(!$closure instanceof Closure){
throw new \Exception('custom handler must be a closure!');
}
$this->customHandler = $closure;
}
/**
* 退出处理器
*
* @param Closure $closure
* @throws \Exception
*/
public function setExitHandler(Closure $closure)
{
if(!$closure instanceof Closure){
throw new \Exception('exit handler must be a closure!');
}
$this->exitHandler = $closure;
}
/**
* 异常处理器
*
* @param Closure $closure
* @throws \Exception
*/
public function setExceptionHandler(Closure $closure)
{
if(!$closure instanceof Closure){
throw new \Exception('exit handler must be a closure!');
}
$this->exceptionHandler = $closure;
}
/**
* 执行一次的处理器
*
* @param Closure $closure
* @throws \Exception
*/
public function setOnceHandler(Closure $closure)
{
if(!$closure instanceof Closure){
throw new \Exception('exit handler must be a closure!');
}
$this->onceHandler = $closure;
}
/**
* 轮询消息API接口
*/
public function listen()
{
if($this->onceHandler instanceof Closure){
call_user_func_array($this->onceHandler, []);
}
while (true){
if($this->customHandler instanceof Closure){
call_user_func_array($this->customHandler, []);
}
$time = time();
list($retCode, $selector) = $this->sync->checkSync();
if(in_array($retCode, ['1100', '1101'])){ # 微信客户端上登出或者其他设备登录
Console::log('微信客户端正常退出');
if($this->exitHandler){
call_user_func_array($this->exitHandler, []);
}
break;
}elseif ($retCode == 0){
$this->handlerMessage($selector);
}else{
Console::log('微信客户端异常退出');
if($this->exceptionHandler){
call_user_func_array($this->exitHandler, []);
}
break;
}
$this->sync->checkTime($time);
}
Console::log('程序结束');
}
/**
* 处理消息
*
* @param $selector
*/
private function handlerMessage($selector)
{
if($selector === 0){
return;
}
$message = $this->sync->sync();
if(count($message['ModContactList']) > 0){
foreach ($message['ModContactList'] as $contact) {
group()->put($contact['UserName'], $contact);
}
}
if($message['AddMsgList']){
foreach ($message['AddMsgList'] as $msg) {
$content = $this->messageFactory->make($msg);
if($content){
$this->addToMessageCollection($content);
if($this->handler){
$reply = call_user_func_array($this->handler, [$content]);
if($reply){
if($reply instanceof Image){
Image::sendByMsgId($content->from['UserName'], $reply->msg['MsgId']);
}elseif($reply instanceof Video){
Video::sendByMsgId($content->from['UserName'], $reply->msg['MsgId']);
}elseif($reply instanceof Emoticon){
Emoticon::sendByMsgId($content->from['UserName'], $reply->msg['MsgId']);
}else{
Text::send($content->from['UserName'], $reply);
}
}
}
}
}
}
}
/**
* @param $message Message
*/
private function addToMessageCollection($message)
{
message()->put($message->msg['MsgId'], $message);
if(server()->config['debug']) {
$file = fopen(System::getPath() . 'message.json', 'a');
fwrite($file, json_encode($message) . PHP_EOL);
fclose($file);
}
}
}