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\Support; |
16
|
|
|
use EasyWeChat\Kernel\Traits\InteractsWithCache; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Client. |
20
|
|
|
* |
21
|
|
|
* @author overtrue <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Client extends BaseClient |
24
|
|
|
{ |
25
|
|
|
use InteractsWithCache; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
const API_GET_TICKET = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Current URI. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $url; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Ticket cache prefix. |
41
|
|
|
*/ |
42
|
|
|
const TICKET_CACHE_PREFIX = 'easywechat.jsapi_ticket.'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get config json for jsapi. |
46
|
|
|
* |
47
|
|
|
* @param array $jsApiList |
48
|
|
|
* @param bool $debug |
49
|
|
|
* @param bool $beta |
50
|
|
|
* @param bool $json |
51
|
|
|
* |
52
|
|
|
* @return array|string |
53
|
|
|
*/ |
54
|
|
|
public function buildConfig(array $jsApiList, bool $debug = false, bool $beta = false, bool $json = true) |
55
|
|
|
{ |
56
|
|
|
$config = array_merge(compact('debug', 'beta', 'jsApiList'), $this->signature()); |
57
|
|
|
|
58
|
|
|
return $json ? json_encode($config) : $config; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Return jsapi config as a PHP array. |
63
|
|
|
* |
64
|
|
|
* @param array $apis |
65
|
|
|
* @param bool $debug |
66
|
|
|
* @param bool $beta |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function getConfigArray(array $apis, bool $debug = false, bool $beta = false) |
71
|
|
|
{ |
72
|
|
|
return $this->buildConfig($apis, $debug, $beta, false); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get js ticket. |
77
|
|
|
* |
78
|
|
|
* @param bool $refresh |
79
|
|
|
* @param string $type |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function getTicket(bool $refresh = false, string $type = 'jsapi'): array |
84
|
|
|
{ |
85
|
|
|
$cacheKey = self::TICKET_CACHE_PREFIX.$this->getAppId(); |
86
|
|
|
|
87
|
|
|
if (!$refresh && $this->getCache()->has($cacheKey)) { |
88
|
|
|
return $this->getCache()->get($cacheKey); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$result = $this->resolveResponse( |
92
|
|
|
$this->requestRaw(static::API_GET_TICKET, 'GET', ['query' => ['type' => $type]]), |
93
|
|
|
'array' |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$this->getCache()->set($cacheKey, $result, $result['expires_in'] - 500); |
97
|
|
|
|
98
|
|
|
return $result; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Build signature. |
103
|
|
|
* |
104
|
|
|
* @param string|null $url |
105
|
|
|
* @param string|null $nonce |
106
|
|
|
* @param int|null $timestamp |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
protected function signature(string $url = null, string $nonce = null, $timestamp = null): array |
111
|
|
|
{ |
112
|
|
|
$url = $url ?: $this->getUrl(); |
113
|
|
|
$nonce = $nonce ?: Support\Str::quickRandom(10); |
114
|
|
|
$timestamp = $timestamp ?: time(); |
115
|
|
|
|
116
|
|
|
return [ |
117
|
|
|
'appId' => $this->getAppId(), |
118
|
|
|
'nonceStr' => $nonce, |
119
|
|
|
'timestamp' => $timestamp, |
120
|
|
|
'url' => $url, |
121
|
|
|
'signature' => $this->getTicketSignature($this->getTicket()['ticket'], $nonce, $timestamp, $url), |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Sign the params. |
127
|
|
|
* |
128
|
|
|
* @param string $ticket |
129
|
|
|
* @param string $nonce |
130
|
|
|
* @param int $timestamp |
131
|
|
|
* @param string $url |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function getTicketSignature($ticket, $nonce, $timestamp, $url): string |
136
|
|
|
{ |
137
|
|
|
return sha1("jsapi_ticket={$ticket}&noncestr={$nonce}×tamp={$timestamp}&url={$url}"); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set current url. |
142
|
|
|
* |
143
|
|
|
* @param string $url |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function setUrl(string $url) |
148
|
|
|
{ |
149
|
|
|
$this->url = $url; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get current url. |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function getUrl(): string |
160
|
|
|
{ |
161
|
|
|
if ($this->url) { |
162
|
|
|
return $this->url; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return Support\current_url(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
protected function getAppId() |
172
|
|
|
{ |
173
|
|
|
return $this->app['config']->get('app_id'); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|