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