diff --git a/app/common/server/Wx.php b/app/common/server/Wx.php index 1e80997..48cf78c 100755 --- a/app/common/server/Wx.php +++ b/app/common/server/Wx.php @@ -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; }