提交代码
This commit is contained in:
parent
79380f3d20
commit
a3ec72340a
|
|
@ -171,25 +171,35 @@ class Wx extends MyController
|
||||||
*/
|
*/
|
||||||
public function get_access_token()
|
public function get_access_token()
|
||||||
{
|
{
|
||||||
#信息
|
$redis = new \Redis();
|
||||||
$access_token_info = getConfig('access_token');
|
$redis->connect('127.0.0.1', 6379);
|
||||||
if ($access_token_info && $access_token_info['access_token_time'] > time()) {
|
$redis_key = 'wx_access_token';
|
||||||
$access_token = $access_token_info['access_token'];
|
|
||||||
} else {
|
$access_token_info = $redis->get($redis_key);
|
||||||
|
if ($access_token_info) {
|
||||||
|
$access_token_info = json_decode($access_token_info, true);
|
||||||
|
if ($access_token_info['access_token_time'] > time()) {
|
||||||
|
return $access_token_info['access_token'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . self::$wx_appid . "&secret=" . self::$wx_secret;
|
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . self::$wx_appid . "&secret=" . self::$wx_secret;
|
||||||
$res_access_token = $this->get_curl_data($url);
|
$res_access_token = $this->get_curl_data($url);
|
||||||
if (isset($res_access_token['errcode'])) {
|
if (isset($res_access_token['errcode'])) {
|
||||||
return $this->renderError('获取失败');
|
return $this->renderError('获取失败');
|
||||||
}
|
}
|
||||||
|
|
||||||
$access_token = $res_access_token['access_token'];
|
$access_token = $res_access_token['access_token'];
|
||||||
$expires_in = $res_access_token['expires_in'];
|
$expires_in = $res_access_token['expires_in'];
|
||||||
$access_token_time = time() + 3600;
|
$access_token_time = time() + $expires_in;
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
'access_token' => $access_token,
|
'access_token' => $access_token,
|
||||||
'access_token_time' => $access_token_time,
|
'access_token_time' => $access_token_time,
|
||||||
];
|
];
|
||||||
setConfig('access_token', $data);
|
|
||||||
}
|
$redis->set($redis_key, json_encode($data), $expires_in);
|
||||||
|
|
||||||
return $access_token;
|
return $access_token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user