@@ -14,8 +14,6 @@ class Wechat {
1414
1515 //access_token
1616 private $ access_token = '' ;
17-
18- private $ Cache = null ;
1917
2018 //授权回调地址
2119 private $ redirect_uri = '' ;
@@ -49,9 +47,6 @@ public function __construct($config){
4947 $ this ->redirect_uri = $ config ['redirect_uri ' ];
5048 $ this ->access_token = $ this ->get_access_token ();
5149
52- require_once 'FileCache/src/FileCache.php ' ;
53- $ this ->Cache = new FileCache ();
54-
5550 }
5651
5752 /**
@@ -67,7 +62,8 @@ public function return_access_token(){
6762 */
6863 private function get_access_token (){
6964
70- $ data = $ this ->Cache ('access_token_data ' );
65+ $ data = $ this ->getCacheAccessToken ();
66+
7167 if ($ data ){
7268 //从缓存中取
7369 $ access_token = $ data ['access_token ' ];
@@ -85,7 +81,7 @@ private function get_access_token(){
8581 $ data = $ this ->return_data ($ data );
8682 if ($ data ){
8783 $ access_token = $ data ['access_token ' ];
88- $ this ->Cache ( ' access_token_data ' , $ data , $ data [ ' expires_in ' ] - 100 );
84+ $ this ->setCacheAccessToken ( $ data );
8985 return $ access_token ;
9086 }else {
9187 return false ;
@@ -229,7 +225,7 @@ public function get_auth_user($openid , $access_token , $lang = 'zh_CN'){
229225 */
230226 public function return_data ($ data ){
231227
232- if ($ data ['errcode ' ]){
228+ if (isset ( $ data ['errcode ' ]) ){
233229 $ this ->error = $ data ['errmsg ' ];
234230 return false ;
235231 }else {
@@ -265,7 +261,7 @@ public function create_url($url , $param){
265261 * @return mixed|boolean
266262 */
267263 public function http_get ($ url , $ data_type ='json ' ) {
268-
264+
269265 $ cl = curl_init ();
270266 if (stripos ($ url , 'https:// ' ) !== FALSE ) {
271267 curl_setopt ($ cl , CURLOPT_SSL_VERIFYPEER , FALSE );
@@ -329,37 +325,36 @@ public function http_post($url, $fields, $data_type='json') {
329325 }
330326 }
331327
332- /**
333- * 设置缓存
334- * @param unknown $key
335- * @param unknown $value
336- * @param number $expire
337- */
338- public function cache ($ key , $ value = null , $ expire = 3600 ){
339-
340- $ cache = $ this ->Cache ;
328+ public function getCacheAccessToken (){
341329
342- if ($ value ){
343- $ result = $ cache ->set ($ key , $ value , $ expire );
344- return $ result ;
330+ $ file = dirname (__FILE__ ).'/access_token.json ' ;
331+ $ accessTokenData = file_get_contents ($ file );
332+ if (!$ accessTokenData ){
333+ return false ;
345334 }else {
346- $ value = $ cache ->get ($ key );
347- return $ value ;
335+ $ accessTokenData = $ accessTokenData ? json_decode ($ accessTokenData , true ) : '' ;
336+ if ($ accessTokenData && isset ($ accessTokenData ['access_token ' ])){
337+ if (isset ($ accessTokenData ['time_out ' ]) && $ accessTokenData ['time_out ' ] > time ()){
338+ return $ accessTokenData ;
339+ }else {
340+ return false ;
341+ }
342+
343+ }else {
344+ return false ;
345+ }
348346 }
349-
350347 }
351348
352- public function cacheClear ( $ key = null ){
349+ public function setCacheAccessToken ( $ data ){
353350
354- $ cache = $ this ->Cache ;
355- if ($ key ){
356- $ have = $ cache ->isHave ($ key );
357- if ($ have ){
358- $ cache ->delete ($ key );
359- }
360- }else {
361- $ cache ->flush ();
362- }
351+ $ file = dirname (__FILE__ ).'/access_token.json ' ;
352+
353+ $ data ['expires_in ' ] = $ data ['expires_in ' ] - 100 ;
354+ $ data ['time_out ' ] = time () + $ data ['expires_in ' ];
355+ $ accessTokenData = json_encode ($ data , true );
356+
357+ file_put_contents ($ file , $ accessTokenData );
363358 }
364359
365360}
0 commit comments