|
|
@@ -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;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
}
|