Browse Source

修改bug

chenbo 4 years ago
parent
commit
5126481079
2 changed files with 17 additions and 33 deletions
  1. 15 32
      lib/common/weixin_public.class.php
  2. 2 1
      setting.inc.php

+ 15 - 32
lib/common/weixin_public.class.php

@@ -43,46 +43,29 @@ class weixin_public
         exit;
     }
 
-
-    public function getAccessToken()
+    private function getAcessTokenAndOpenId($code)
     {
-        $rs = Baseconfig::getInfoByKey(TOKEN_BASE_KEY);
+        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid
+            ."&secret=".$this->seceret."&code=".$code."&grant_type=authorization_code";
 
-        if (empty($rs) || (time() - $rs["lastupdate"]) > TOKEN_LIMIT_TIME) {
-            $accessToken = self::refreshToken();
-        } else {
-            $accessToken = $rs["value"];
-        }
-        return $accessToken;
+        $jsonInfo = file_get_contents($url);
+        return json_decode($jsonInfo, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
     }
 
-
-
-    public function refreshToken()
+    public function getAllInfo($code)
     {
-        $token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->seceret;
-        $res = file_get_contents($token_access_url); //
-        $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量s
-        $access_token = $result['access_token'];
+        $info = self::getAcessTokenAndOpenId($code);
 
-        $rs = Baseconfig::getInfoByKey(TOKEN_BASE_KEY);
-        if ($rs) {
-            $res = Baseconfig::updateByKey(TOKEN_BASE_KEY, array("value" => $access_token, "lastupdate" => time()));
-        } else {
-            $attrs = array(
-                "key"=>TOKEN_BASE_KEY,
-                "value"=>$access_token,
-                "name"=>"token"
-            );
-            $res = Baseconfig::add($attrs);
-        }
-        if ($res) {
-            return $access_token;
+        $ret = array();
+        if (empty($info['access_token']) || empty($info['openid'])) {
+            return $ret;
         }
-    }
-
-
 
+        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$info['access_token']."&openid=".$info['openid']."&lang=zh_CN";
+        $jsonInfo = file_get_contents($url);
+        $ret = json_decode($jsonInfo, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
+        return $ret;
+    }
 
 
 }

+ 2 - 1
setting.inc.php

@@ -60,6 +60,7 @@ define('APPURI', 'http://fission.xazhima.com/front-page/fission-page.html');
 
 define('BASE_KEY_ACCESS_TOKEN', 'access_token');
 define('BASE_KEY_REFRESH_TOKEN', 'refresh_token');
-
+define('BASE_KEY_ACCESS_TOKEN_TIMEOUT', 6900);     //1小时55分钟
+define('BASE_KEY_REFRESH_TOKEN_TIMEOUT', 2505600);     //29天
 
 ?>