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\Core\Exceptions\InvalidArgumentException; |
31
|
|
|
use EasyWeChat\Server\Guard as ServerGuard; |
32
|
|
|
use EasyWeChat\Support\Collection; |
33
|
|
|
use Pimple\Container; |
34
|
|
|
use Symfony\Component\HttpFoundation\Request; |
35
|
|
|
use Symfony\Component\HttpFoundation\Response; |
36
|
|
|
|
37
|
|
|
class Guard extends ServerGuard |
38
|
|
|
{ |
39
|
|
|
const EVENT_AUTHORIZED = 'authorized'; |
40
|
|
|
const EVENT_UNAUTHORIZED = 'unauthorized'; |
41
|
|
|
const EVENT_UPDATE_AUTHORIZED = 'updateauthorized'; |
42
|
|
|
const EVENT_COMPONENT_VERIFY_TICKET = 'component_verify_ticket'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Container in the scope of the open platform authorization. |
46
|
|
|
* |
47
|
|
|
* @var Container |
48
|
|
|
*/ |
49
|
|
|
protected $container; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sets the container for use of event handlers. |
53
|
|
|
* |
54
|
|
|
* @param Container $container |
55
|
|
|
* |
56
|
|
|
* @see getDefaultHandler() |
57
|
|
|
*/ |
58
|
|
|
public function setContainer(Container $container) |
59
|
|
|
{ |
60
|
|
|
$this->container = $container; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc |
65
|
|
|
*/ |
66
|
|
|
public function serve() |
67
|
|
|
{ |
68
|
|
|
|
69
|
|
|
// If sees the `auth_code` query parameter in the url, that is, |
70
|
|
|
// authorization is successful and it calls back, meanwhile, an |
71
|
|
|
// `authorized` event, which also includes the auth code, is sent |
72
|
|
|
// from WeChat, and that event will be handled. |
73
|
|
|
if ($this->request->get('auth_code')) { |
74
|
|
|
return new Response('success'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$message = $this->getMessage(); |
78
|
|
|
|
79
|
|
|
$this->handleMessage($message); |
80
|
|
|
|
81
|
|
|
// Handle Messages. |
82
|
|
|
if (isset($message['MsgType'])) { |
83
|
|
|
return parent::serve(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return new Response('success'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Return for laravel-wechat. |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function listServe() |
95
|
|
|
{ |
96
|
|
|
$message = $this->getMessage(); |
97
|
|
|
$this->handleMessage($message); |
98
|
|
|
|
99
|
|
|
$message = new Collection($message); |
100
|
|
|
|
101
|
|
|
return [ |
102
|
|
|
$message->get('InfoType'), $message, |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Listen for wechat push event. |
108
|
|
|
* |
109
|
|
|
* @param callable|null $callback |
110
|
|
|
* |
111
|
|
|
* @return mixed |
112
|
|
|
* |
113
|
|
|
* @throws InvalidArgumentException |
114
|
|
|
*/ |
115
|
|
|
public function listen($callback = null) |
116
|
|
|
{ |
117
|
|
|
if ($callback) { |
118
|
|
|
$this->setMessageHandler($callback); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->serve(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @inheritdoc |
126
|
|
|
*/ |
127
|
|
|
protected function handleMessage($message) |
128
|
|
|
{ |
129
|
|
|
if (is_array($message)) { |
130
|
|
|
$message = new Collection($message); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($message->has('MsgType')) { |
134
|
|
|
return parent::handleMessage($message->toArray()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$handler = $this->getDefaultHandler($message->get('InfoType')); |
138
|
|
|
|
139
|
|
|
$result = $handler->handle($message); |
140
|
|
|
|
141
|
|
|
// To be compatible with previous version: merges the auth result while |
142
|
|
|
// keeping the original message. |
143
|
|
|
if (is_array($result) || $result instanceof Collection) { |
144
|
|
|
$message->merge($result); |
145
|
|
|
} else { |
146
|
|
|
if (! empty($result)) { |
147
|
|
|
$message->set('result', $result); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if ($customHandler = $this->getMessageHandler()) { |
152
|
|
|
$customHandler($message); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $result; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Gets the default handler by the info type. |
160
|
|
|
* |
161
|
|
|
* @param $type |
162
|
|
|
* |
163
|
|
|
* @return EventHandlers\EventHandler |
164
|
|
|
* @throws InvalidArgumentException |
165
|
|
|
*/ |
166
|
|
|
protected function getDefaultHandler($type) |
167
|
|
|
{ |
168
|
|
|
$handler = $this->container->offsetGet("open_platform.handlers.{$type}"); |
169
|
|
|
if (! $handler) { |
170
|
|
|
throw new InvalidArgumentException("EventHandler \"$type\" does not exists."); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $handler; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|