|
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
|
|
|
namespace EasyWeChat\BasicService\Jssdk; |
|
13
|
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\BaseClient; |
|
15
|
|
|
use EasyWeChat\Kernel\Exceptions\RuntimeException; |
|
16
|
|
|
use EasyWeChat\Kernel\Support; |
|
17
|
|
|
use EasyWeChat\Kernel\Traits\InteractsWithCache; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Client. |
|
21
|
|
|
* |
|
22
|
|
|
* @author overtrue <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class Client extends BaseClient |
|
25
|
|
|
{ |
|
26
|
|
|
use InteractsWithCache; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $ticketEndpoint = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Current URI. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $url; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get config json for jsapi. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $jsApiList |
|
44
|
|
|
* @param bool $debug |
|
45
|
|
|
* @param bool $beta |
|
46
|
|
|
* @param bool $json |
|
47
|
|
|
* @param array $openTagList |
|
48
|
|
|
* |
|
49
|
|
|
* @return array|string |
|
50
|
|
|
* |
|
51
|
|
|
* @throws RuntimeException |
|
52
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
53
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
54
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function buildConfig(array $jsApiList, bool $debug = false, bool $beta = false, bool $json = true, array $openTagList = []) |
|
57
|
|
|
{ |
|
58
|
1 |
|
$config = array_merge(compact('debug', 'beta', 'jsApiList', 'openTagList'), $this->configSignature()); |
|
59
|
|
|
|
|
60
|
1 |
|
return $json ? json_encode($config) : $config; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Return jsapi config as a PHP array. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $apis |
|
67
|
|
|
* @param bool $debug |
|
68
|
|
|
* @param bool $beta |
|
69
|
|
|
* @param array $openTagList |
|
70
|
|
|
* |
|
71
|
|
|
* @return array |
|
72
|
|
|
* |
|
73
|
|
|
* @throws RuntimeException |
|
74
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
75
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
76
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function getConfigArray(array $apis, bool $debug = false, bool $beta = false, array $openTagList = []) |
|
79
|
|
|
{ |
|
80
|
1 |
|
return $this->buildConfig($apis, $debug, $beta, false, $openTagList); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get js ticket. |
|
85
|
|
|
* |
|
86
|
|
|
* @param bool $refresh |
|
87
|
|
|
* @param string $type |
|
88
|
|
|
* |
|
89
|
|
|
* @return array |
|
90
|
|
|
* |
|
91
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
92
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
93
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException |
|
94
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
95
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function getTicket(bool $refresh = false, string $type = 'jsapi'): array |
|
98
|
|
|
{ |
|
99
|
2 |
|
$cacheKey = sprintf('easywechat.basic_service.jssdk.ticket.%s.%s', $type, $this->getAppId()); |
|
100
|
|
|
|
|
101
|
2 |
|
if (!$refresh && $this->getCache()->has($cacheKey)) { |
|
102
|
1 |
|
return $this->getCache()->get($cacheKey); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** @var array<string, mixed> $result */ |
|
106
|
2 |
|
$result = $this->castResponseToType( |
|
107
|
2 |
|
$this->requestRaw($this->ticketEndpoint, 'GET', ['query' => ['type' => $type]]), |
|
108
|
2 |
|
'array' |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
2 |
|
$this->getCache()->set($cacheKey, $result, $result['expires_in'] - 500); |
|
112
|
|
|
|
|
113
|
2 |
|
if (!$this->getCache()->has($cacheKey)) { |
|
114
|
1 |
|
throw new RuntimeException('Failed to cache jssdk ticket.'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
return $result; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Build signature. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string|null $url |
|
124
|
|
|
* @param string|null $nonce |
|
125
|
|
|
* @param int|null $timestamp |
|
126
|
|
|
* |
|
127
|
|
|
* @return array |
|
128
|
|
|
* |
|
129
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
130
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
131
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException |
|
132
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
133
|
|
|
*/ |
|
134
|
1 |
|
protected function configSignature(string $url = null, string $nonce = null, $timestamp = null): array |
|
135
|
|
|
{ |
|
136
|
1 |
|
$url = $url ?: $this->getUrl(); |
|
137
|
1 |
|
$nonce = $nonce ?: Support\Str::quickRandom(10); |
|
138
|
1 |
|
$timestamp = $timestamp ?: time(); |
|
139
|
|
|
|
|
140
|
|
|
return [ |
|
141
|
1 |
|
'appId' => $this->getAppId(), |
|
142
|
1 |
|
'nonceStr' => $nonce, |
|
143
|
1 |
|
'timestamp' => $timestamp, |
|
144
|
1 |
|
'url' => $url, |
|
145
|
1 |
|
'signature' => $this->getTicketSignature($this->getTicket()['ticket'], $nonce, $timestamp, $url), |
|
146
|
|
|
]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Sign the params. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $ticket |
|
153
|
|
|
* @param string $nonce |
|
154
|
|
|
* @param int $timestamp |
|
155
|
|
|
* @param string $url |
|
156
|
|
|
* |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
2 |
|
public function getTicketSignature($ticket, $nonce, $timestamp, $url): string |
|
160
|
|
|
{ |
|
161
|
2 |
|
return sha1(sprintf('jsapi_ticket=%s&noncestr=%s×tamp=%s&url=%s', $ticket, $nonce, $timestamp, $url)); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
1 |
|
public function dictionaryOrderSignature() |
|
168
|
|
|
{ |
|
169
|
1 |
|
$params = func_get_args(); |
|
170
|
|
|
|
|
171
|
1 |
|
sort($params, SORT_STRING); |
|
172
|
|
|
|
|
173
|
1 |
|
return sha1(implode('', $params)); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Set current url. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $url |
|
180
|
|
|
* |
|
181
|
|
|
* @return $this |
|
182
|
|
|
*/ |
|
183
|
2 |
|
public function setUrl(string $url) |
|
184
|
|
|
{ |
|
185
|
2 |
|
$this->url = $url; |
|
186
|
|
|
|
|
187
|
2 |
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Get current url. |
|
192
|
|
|
* |
|
193
|
|
|
* @return string |
|
194
|
|
|
*/ |
|
195
|
2 |
|
public function getUrl(): string |
|
196
|
|
|
{ |
|
197
|
2 |
|
if ($this->url) { |
|
198
|
2 |
|
return $this->url; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
1 |
|
return Support\current_url(); |
|
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @return string |
|
206
|
|
|
*/ |
|
207
|
3 |
|
protected function getAppId() |
|
208
|
|
|
{ |
|
209
|
3 |
|
return $this->app['config']->get('app_id'); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @return string |
|
214
|
|
|
*/ |
|
215
|
|
|
protected function getAgentId() |
|
216
|
|
|
{ |
|
217
|
|
|
return $this->app['config']->get('agent_id'); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|