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
|
|
|
/** |
13
|
|
|
* AuthorizerToken.php. |
14
|
|
|
* |
15
|
|
|
* Part of Overtrue\WeChat. |
16
|
|
|
* |
17
|
|
|
* For the full copyright and license information, please view the LICENSE |
18
|
|
|
* file that was distributed with this source code. |
19
|
|
|
* |
20
|
|
|
* @author lixiao <[email protected]> |
21
|
|
|
* @copyright 2016 |
22
|
|
|
* |
23
|
|
|
* @see https://github.com/overtrue |
24
|
|
|
* @see http://overtrue.me |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace EasyWeChat\OpenPlatform; |
28
|
|
|
|
29
|
|
|
// Don't change the alias name please. I met the issue "name already in use" |
30
|
|
|
// when used in Laravel project, not sure what is causing it, this is quick |
31
|
|
|
// solution. |
32
|
|
|
use EasyWeChat\Core\AccessToken as BaseAccessToken; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Class AuthorizerToken |
36
|
|
|
* |
37
|
|
|
* AuthorizerToken is responsible for the access token of the authorizer, |
38
|
|
|
* the complexity is that this access token also requires the refresh token |
39
|
|
|
* of the authorizer which is acquired by the open platform authorization |
40
|
|
|
* process. |
41
|
|
|
* |
42
|
|
|
* This completely overrides the original AccessToken. |
43
|
|
|
* |
44
|
|
|
* @package EasyWeChat\OpenPlatform |
45
|
|
|
*/ |
46
|
|
|
class AuthorizerToken extends BaseAccessToken |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* Handles authorization. |
50
|
|
|
* |
51
|
|
|
* @var Authorization |
52
|
|
|
*/ |
53
|
|
|
protected $authorization; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* AuthorizerAccessToken constructor. |
57
|
|
|
* |
58
|
|
|
* @param string $appId |
59
|
|
|
* @param Authorization $authorization |
60
|
|
|
*/ |
61
|
|
|
public function __construct($appId, Authorization $authorization) |
62
|
|
|
{ |
63
|
|
|
parent::__construct($appId, null); |
64
|
|
|
|
65
|
|
|
$this->authorization = $authorization; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get token from WeChat API. |
70
|
|
|
* |
71
|
|
|
* @param bool $forceRefresh |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getToken($forceRefresh = false) |
76
|
|
|
{ |
77
|
|
|
$cached = $this->authorization->getAuthorizerAccessToken(); |
78
|
|
|
|
79
|
|
|
if ($forceRefresh || empty($cached)) { |
80
|
|
|
return $this->authorization->handleAuthorizerAccessToken(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $cached; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|