Esempio n. 1
0
 /**
  * 获取 Ticket(从 API 获取)
  */
 protected function _getTicket($type)
 {
     $request = Http::get(self::TICKET_URL, array('query' => array('access_token' => $this->accessToken->getAccessToken(), 'type' => $type)));
     $response = $request->json();
     if (array_key_exists('ticket', $response) && array_key_exists('expires_in', $response)) {
         return $response;
     }
     throw new \Exception($response['errcode'] . ': ' . $response['errmsg']);
 }
Esempio n. 2
0
 /**
  * 获取 AccessToken(从 API 获取)
  */
 protected function _getAccessToken()
 {
     $request = Http::get(self::ACCESS_TOKEN_URL, array('query' => array('grant_type' => 'client_credential', 'appid' => $this->wechat['appid'], 'secret' => $this->wechat['appsecret'])));
     $response = $request->json();
     if (array_key_exists('access_token', $response) && array_key_exists('expires_in', $response)) {
         return $response;
     }
     throw new \Exception($response['errcode'] . ': ' . $response['errmsg']);
 }
Esempio n. 3
0
 /**
  * 获取SNS用户信息
  *
  * @return void
  * @author
  **/
 public function getSnsApiUserinfo()
 {
     $access_token = $this->getAccessToken(false);
     if (!$access_token) {
         return false;
     }
     $data = $this->cache->get("niancode/wechat/auth/getSnsApiUserinfo/{$this->appid}/{$access_token->openid}");
     if ($data === false) {
         $url = self::WECHAT_BASE_URL . "sns/userinfo?";
         $params = ['lang' => 'zh_CN', 'access_token' => $access_token->access_token, 'openid' => $access_token->openid];
         $url .= http_build_query($params);
         $request = Http::get($url);
         $response = $request->json();
         if (array_key_exists('openid', $response)) {
             $res = $request->json(['object' => true]);
             if ($res->openid) {
                 $data = new \stdClass();
                 $data->access_token = $access_token->access_token;
                 $data->refresh_token = $access_token->refresh_token;
                 $data->openid = $res->openid;
                 $data->nickname = $res->nickname;
                 $data->sex = $res->sex;
                 $data->province = $res->province;
                 $data->city = $res->city;
                 $data->country = $res->country;
                 $data->headimgurl = $res->headimgurl;
                 $data->privilege = $res->privilege;
                 $data->unionid = isset($res->unionid) ? $res->unionid : '';
                 $this->cache->set("niancode/wechat/auth/getSnsApiUserinfo/{$this->appid}/{$access_token->openid}", $data, 7000);
                 return $data;
             }
         }
     } else {
         return $data;
     }
 }
Esempio n. 4
0
 /**
  * 获取component_access_token
  *
  * @return void
  * @author
  **/
 public function getComponentAccessToken($text = null)
 {
     $data = $this->cache->get("niancode/wechat/getComponentAccessToken");
     if ($data === false) {
         $url = self::WECHAT_BASE_URL . "cgi-bin/component/api_component_token";
         $post = ['component_appid' => $this->config->appId, 'component_appsecret' => $this->config->appSecret, 'component_verify_ticket' => $this->getVerifyTicket($text, false)];
         $request = Http::post($url, ['json' => $post]);
         $response = $request->json();
         if (array_key_exists('component_access_token', $response)) {
             $res = $request->json(['object' => true]);
             $access_token = $res->component_access_token;
             if ($access_token) {
                 // 写入日志
                 try {
                     $info['component_access_token'] = $access_token;
                     $info['verify_ticket'] = $text;
                     $new_text = sprintf("%s >>> %s\n", date('Y-m-d H:i:s'), var_export($info, true));
                     file_put_contents(Yii::getAlias('@runtime/logs/platform_access_token.log'), $new_text, FILE_APPEND);
                 } catch (ErrorException $e) {
                     Yii::warning("Token日志写入错误");
                 }
                 $data = new \stdClass();
                 $data->access_token = $access_token;
                 $this->cache->set("niancode/wechat/getComponentAccessToken", $data, 6000);
                 return $access_token;
             }
         }
     } else {
         return $data->access_token;
     }
 }