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
|
|
|
/** |
13
|
|
|
* Guard.php. |
14
|
|
|
* |
15
|
|
|
* Part of Overtrue\WeChat. |
16
|
|
|
* |
17
|
|
|
* For the full copyright and license information, please view the LICENSE |
18
|
|
|
* file that was distributed with this source code. |
19
|
|
|
* |
20
|
|
|
* @author mingyoung <[email protected]> |
21
|
|
|
* @author lixiao <[email protected]> |
22
|
|
|
* @copyright 2016 |
23
|
|
|
* |
24
|
|
|
* @see https://github.com/overtrue |
25
|
|
|
* @see http://overtrue.me |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace EasyWeChat\OpenPlatform; |
29
|
|
|
|
30
|
|
|
use EasyWeChat\Server\Guard as ServerGuard; |
31
|
|
|
use EasyWeChat\Support\Collection; |
32
|
|
|
use EasyWeChat\Support\Log; |
33
|
|
|
use Symfony\Component\HttpFoundation\Response; |
34
|
|
|
|
35
|
|
|
class Guard extends ServerGuard |
36
|
|
|
{ |
37
|
|
|
const EVENT_AUTHORIZED = 'authorized'; |
38
|
|
|
const EVENT_UNAUTHORIZED = 'unauthorized'; |
39
|
|
|
const EVENT_UPDATE_AUTHORIZED = 'updateauthorized'; |
40
|
|
|
const EVENT_COMPONENT_VERIFY_TICKET = 'component_verify_ticket'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Event handlers. |
44
|
|
|
* |
45
|
|
|
* @var \EasyWeChat\Support\Collection |
46
|
|
|
*/ |
47
|
|
|
protected $handlers; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set handlers. |
51
|
|
|
* |
52
|
|
|
* @param array $handlers |
53
|
|
|
*/ |
54
|
2 |
|
public function setHandlers(array $handlers) |
55
|
|
|
{ |
56
|
2 |
|
$this->handlers = new Collection($handlers); |
57
|
|
|
|
58
|
2 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get handlers. |
63
|
|
|
* |
64
|
|
|
* @return \EasyWeChat\Support\Collection |
65
|
|
|
*/ |
66
|
|
|
public function getHandlers() |
67
|
|
|
{ |
68
|
|
|
return $this->handlers; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get handler. |
73
|
|
|
* |
74
|
|
|
* @param string $type |
75
|
|
|
* |
76
|
|
|
* @return \EasyWeChat\OpenPlatform\EventHandlers\EventHandler|null |
77
|
|
|
*/ |
78
|
1 |
|
public function getHandler($type) |
79
|
|
|
{ |
80
|
1 |
|
return $this->handlers->get($type); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function serve() |
87
|
|
|
{ |
88
|
|
|
$message = $this->getMessage(); |
89
|
|
|
|
90
|
|
|
// Handle Messages. |
91
|
|
|
if (isset($message['MsgType'])) { |
92
|
|
|
return parent::serve(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
Log::debug('OpenPlatform Request received:', [ |
96
|
|
|
'Method' => $this->request->getMethod(), |
97
|
|
|
'URI' => $this->request->getRequestUri(), |
98
|
|
|
'Query' => $this->request->getQueryString(), |
99
|
|
|
'Protocal' => $this->request->server->get('SERVER_PROTOCOL'), |
100
|
|
|
'Content' => $this->request->getContent(), |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
// If sees the `auth_code` query parameter in the url, that is, |
104
|
|
|
// authorization is successful and it calls back, meanwhile, an |
105
|
|
|
// `authorized` event, which also includes the auth code, is sent |
106
|
|
|
// from WeChat, and that event will be handled. |
107
|
|
|
if ($this->request->get('auth_code')) { |
108
|
|
|
return new Response(self::SUCCESS_EMPTY_RESPONSE); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->handleEventMessage($message); |
112
|
|
|
|
113
|
|
|
return new Response(self::SUCCESS_EMPTY_RESPONSE); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Handle event message. |
118
|
|
|
* |
119
|
|
|
* @param array $message |
120
|
|
|
*/ |
121
|
|
|
protected function handleEventMessage(array $message) |
122
|
|
|
{ |
123
|
|
|
Log::debug('OpenPlatform Event Message detail:', $message); |
124
|
|
|
|
125
|
|
|
$message = new Collection($message); |
126
|
|
|
|
127
|
|
|
$infoType = $message->get('InfoType'); |
128
|
|
|
|
129
|
|
|
if ($handler = $this->getHandler($infoType)) { |
130
|
|
|
$handler->handle($message); |
131
|
|
|
} else { |
132
|
|
|
Log::notice("No existing handler for '{$infoType}'."); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($customHandler = $this->getMessageHandler()) { |
136
|
|
|
$customHandler($message); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|