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