public function getAccessToken($force = false)
{
$time = time();
// 为了更精确控制.取当前时间计算
if ($this->_accessToken === null || $this->_accessToken['expire'] < $time || $force) {
$result = $this->_accessToken === null && !$force ? $this->getCache('access_token', false) : false;
if ($result === false) {
if (!($result = $this->requestAccessToken())) {
throw new HttpException(500, 'Fail to get access_token from wechat server.');
}
$result['expire'] = $time + $result['expires_in'];
$this->trigger(self::EVENT_AFTER_ACCESS_TOKEN_UPDATE, new Event(['data' => $result]));
$this->setCache('access_token', $result, $result['expires_in']);
}
$this->setAccessToken($result);
}
return $this->_accessToken['access_token'];
}