提交代码
This commit is contained in:
parent
79380f3d20
commit
a3ec72340a
|
|
@ -171,25 +171,35 @@ class Wx extends MyController
|
|||
*/
|
||||
public function get_access_token()
|
||||
{
|
||||
#信息
|
||||
$access_token_info = getConfig('access_token');
|
||||
if ($access_token_info && $access_token_info['access_token_time'] > time()) {
|
||||
$access_token = $access_token_info['access_token'];
|
||||
} else {
|
||||
$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);
|
||||
if (isset($res_access_token['errcode'])) {
|
||||
return $this->renderError('获取失败');
|
||||
$redis = new \Redis();
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
$redis_key = 'wx_access_token';
|
||||
|
||||
$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'];
|
||||
}
|
||||
$access_token = $res_access_token['access_token'];
|
||||
$expires_in = $res_access_token['expires_in'];
|
||||
$access_token_time = time() + 3600;
|
||||
$data = [
|
||||
'access_token' => $access_token,
|
||||
'access_token_time' => $access_token_time,
|
||||
];
|
||||
setConfig('access_token', $data);
|
||||
}
|
||||
|
||||
$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);
|
||||
if (isset($res_access_token['errcode'])) {
|
||||
return $this->renderError('获取失败');
|
||||
}
|
||||
|
||||
$access_token = $res_access_token['access_token'];
|
||||
$expires_in = $res_access_token['expires_in'];
|
||||
$access_token_time = time() + $expires_in;
|
||||
|
||||
$data = [
|
||||
'access_token' => $access_token,
|
||||
'access_token_time' => $access_token_time,
|
||||
];
|
||||
|
||||
$redis->set($redis_key, json_encode($data), $expires_in);
|
||||
|
||||
return $access_token;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user