1 | <?php |
||
30 | class AccessToken |
||
31 | { |
||
32 | /** |
||
33 | * App ID. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $appId; |
||
38 | |||
39 | /** |
||
40 | * App secret. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $secret; |
||
45 | |||
46 | /** |
||
47 | * Cache. |
||
48 | * |
||
49 | * @var Cache |
||
50 | */ |
||
51 | protected $cache; |
||
52 | |||
53 | /** |
||
54 | * Http instance. |
||
55 | * |
||
56 | * @var Http |
||
57 | */ |
||
58 | protected $http; |
||
59 | |||
60 | /** |
||
61 | * Query name. |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $queryName = 'access_token'; |
||
66 | |||
67 | /** |
||
68 | * Cache key prefix. |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $prefix = 'easywechat.common.access_token.'; |
||
73 | |||
74 | // API |
||
75 | const API_TOKEN_GET = 'https://api.weixin.qq.com/cgi-bin/token'; |
||
76 | |||
77 | /** |
||
78 | * Constructor. |
||
79 | * |
||
80 | * @param string $appId |
||
81 | * @param string $secret |
||
82 | * @param Doctrine\Common\Cache\Cache $cache |
||
83 | */ |
||
84 | 12 | public function __construct($appId, $secret, Cache $cache = null) |
|
90 | |||
91 | /** |
||
92 | * Get token from WeChat API. |
||
93 | * |
||
94 | * @param bool $forceRefresh |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | 3 | public function getToken($forceRefresh = false) |
|
115 | |||
116 | /** |
||
117 | * Return the app id. |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | 6 | public function getAppId() |
|
125 | |||
126 | /** |
||
127 | * Return the secret. |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | 1 | public function getSecret() |
|
132 | { |
||
133 | 1 | return $this->secret; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * Set cache instance. |
||
138 | * |
||
139 | * @param \Doctrine\Common\Cache\Cache $cache |
||
140 | */ |
||
141 | 1 | public function setCache(Cache $cache) |
|
142 | { |
||
143 | 1 | $this->cache = $cache; |
|
144 | |||
145 | 1 | return $this; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Return the cache manager. |
||
150 | * |
||
151 | * @return \Doctrine\Common\Cache\Cache |
||
152 | */ |
||
153 | 3 | public function getCache() |
|
157 | |||
158 | /** |
||
159 | * Set the query name. |
||
160 | * |
||
161 | * @param string $queryName |
||
162 | * |
||
163 | * @return $this |
||
164 | */ |
||
165 | 1 | public function setQueryName($queryName) |
|
166 | { |
||
167 | 1 | $this->queryName = $queryName; |
|
168 | |||
169 | 1 | return $this; |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * Return the query name. |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | 1 | public function getQueryName() |
|
178 | { |
||
179 | 1 | return $this->queryName; |
|
180 | } |
||
181 | |||
182 | /** |
||
183 | * Return the API request queries. |
||
184 | * |
||
185 | * @return array |
||
186 | */ |
||
187 | 1 | public function getQueryFields() |
|
188 | { |
||
189 | 1 | return [$this->queryName => $this->getToken()]; |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * Get the access token from WeChat server. |
||
194 | * |
||
195 | * @return array|bool |
||
196 | */ |
||
197 | 2 | public function getTokenFromServer() |
|
198 | { |
||
199 | $params = [ |
||
200 | 2 | 'appid' => $this->appId, |
|
201 | 2 | 'secret' => $this->secret, |
|
202 | 2 | 'grant_type' => 'client_credential', |
|
203 | 2 | ]; |
|
204 | |||
205 | 2 | $http = $this->getHttp(); |
|
206 | |||
207 | 2 | $token = $http->parseJSON($http->get(self::API_TOKEN_GET, $params)); |
|
208 | |||
209 | 2 | if (empty($token['access_token'])) { |
|
210 | 1 | throw new HttpException('Request AccessToken fail. response: '.json_encode($token, JSON_UNESCAPED_UNICODE)); |
|
211 | } |
||
212 | |||
213 | 2 | return $token; |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * Return the http instance. |
||
218 | * |
||
219 | * @return \EasyWeChat\Core\Http |
||
220 | */ |
||
221 | 2 | public function getHttp() |
|
225 | |||
226 | /** |
||
227 | * Set the http instance. |
||
228 | * |
||
229 | * @param \EasyWeChat\Core\Http $http |
||
230 | * |
||
231 | * @return $this |
||
232 | */ |
||
233 | 2 | public function setHttp(Http $http) |
|
239 | } |
||
240 |