1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the docodeit/wechat. |
5
|
|
|
* |
6
|
|
|
* (c) docodeit <[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 JinWeChat\Kernel; |
13
|
|
|
|
14
|
|
|
use JinWeChat\Kernel\Contracts\TokenInterface; |
15
|
|
|
use JinWeChat\Kernel\Exceptions\HttpException; |
16
|
|
|
use JinWeChat\Kernel\Traits\HasHttpRequests; |
17
|
|
|
use JinWeChat\Kernel\Traits\InteractsWithCache; |
18
|
|
|
use Pimple\Container; |
19
|
|
|
use Psr\Http\Message\RequestInterface; |
20
|
|
|
|
21
|
|
|
class Token implements TokenInterface |
22
|
|
|
{ |
23
|
|
|
use HasHttpRequests, InteractsWithCache; |
|
|
|
|
24
|
|
|
|
25
|
|
|
protected $app; |
26
|
|
|
|
27
|
|
|
protected $requestMethod = 'GET'; |
28
|
|
|
|
29
|
|
|
protected $token; |
30
|
|
|
|
31
|
|
|
protected $queryName; |
32
|
|
|
|
33
|
|
|
protected $safeSeconds = 500; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $tokenKey = 'token'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $cachePrefix = 'JinWeChat.kernel.token.'; |
44
|
|
|
|
45
|
|
|
public function __construct(Container $app) |
46
|
|
|
{ |
47
|
|
|
$this->app = $app; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getToken(): array |
51
|
|
|
{ |
52
|
|
|
$cacheKey = $this->getCacheKey(); |
53
|
|
|
$cache = $this->getCache(); |
54
|
|
|
if ($cache->has($cacheKey)) { |
55
|
|
|
return $cache->get($cacheKey); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$token = $this->requestToken($this->getCredentials(), true); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->setToken($token[$this->tokenKey], $token['expires_in'] ?? 57600); |
|
|
|
|
61
|
|
|
|
62
|
|
|
return $token; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function applyToRequest(RequestInterface $request, array $requestOptions = []): RequestInterface |
66
|
|
|
{ |
67
|
|
|
parse_str($request->getUri()->getQuery(), $query); |
68
|
|
|
|
69
|
|
|
$query = http_build_query(array_merge($this->getQuery(), $query)); |
70
|
|
|
|
71
|
|
|
return $request->withUri($request->getUri()->withQuery($query)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function getQuery(): array |
75
|
|
|
{ |
76
|
|
|
return [$this->queryName ?? $this->token => $this->getToken()[$this->token]]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
protected function getCacheKey() |
83
|
|
|
{ |
84
|
|
|
return $this->cachePrefix.$this->app['config']['username']; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function requestToken(array $credentials, $toArray = false) |
88
|
|
|
{ |
89
|
|
|
$response = $this->sendRequest($credentials); |
|
|
|
|
90
|
|
|
$result = json_decode($response->getBody()->getContents(), true); |
91
|
|
|
$formatted = $this->castResponseToType($response, $this->app['config']->get('response_type')); |
|
|
|
|
92
|
|
|
if (preg_match('/token=([\d]+)/i', $result['redirect_url'], $match)) { |
93
|
|
|
$this->token = $match[1]; |
94
|
|
|
} |
95
|
|
|
$this->baseRefer = $result['redirect_url']; |
|
|
|
|
96
|
|
|
|
97
|
|
|
if (empty($result['redirect_url'])) { |
98
|
|
|
throw new HttpException('Request token fail: '.json_encode($result, JSON_UNESCAPED_UNICODE), $response, $formatted); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $toArray ? $result : $formatted; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|