<?php
namespace App\Http\Controllers\Oauth;
use App\Http\Controllers\Utils\Code;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
/**
* Class QQController
* @author <fl140125@gmail.com>
* @package App\Http\Controllers\Oauth
*/
class QQController extends OAuthController
{
protected $appid;
protected $appsecret;
protected $apiUrl = 'https://graph.qq.com/';
protected $redirectUri;
public $openid;
protected static $instance;
public function __construct(string $appid, string $appsecret)
{
parent::__construct();
$this->appid = $appid;
$this->appsecret = $appsecret;
$this->redirectUri = config('app.url').'api/v1/callback/qq';
}
public static function getInstance(string $appid, string $appsecret)
{
if (!self::$instance instanceof self) {
self::$instance = new static($appid, $appsecret);
}
return self::$instance;
}
public function getAuthUrl($length = 32, $callback = '', $scope = 'get_user_info')
{
$arr = [
'response_type' => 'code',
'client_id' => $this->appid,
'redirect_uri' => empty($callback)?$this->redirectUri:$callback,
'state' => $this->getState($length),
'scope' => $scope,
'display' => ''
];
return $this->apiUrl.'oauth2.0/authorize?'.http_build_query($arr);
}
public function getAccessToken(string $code)
{
$arr = [
'grant_type' => 'authorization_code',
'client_id' => $this->appid,
'client_secret' => $this->appsecret,
'code' => $code,
'redirect_uri' => $this->redirectUri
];
$result = $this->curl->get($this->apiUrl.'oauth2.0/token?'.http_build_query($arr));
if (!$result) {
return $this->error(Code::ERROR, 'request interface failed');
}
if (isset($this->json($result)['error'])) {
return $this->error(Code::ERROR, $this->json($result)['error_description']);
}
return $this->__getAccessToken($result);
}
public function getOpenId(string $access_token)
{
$arr = [
'access_token' =>$access_token
];
$result = $this->curl->get($this->apiUrl.'oauth2.0/me?'.http_build_query($arr));
if (!$result) {
return $this->error(Code::ERROR, 'request interface failed');
}
if (isset($this->json($result)['error'])) {
return $this->error(Code::ERROR, $this->json($result)['error_description']);
}
return $this->json($result)['openid'];
}
public function refreshToken(string $refreshToken)
{
$arr = [
'grant_type' => 'refresh_token',
'client_id' => $this->appid,
'client_secret' => $this->appsecret,
'refresh_token' => $refreshToken
];
$result = $this->curl->get($this->apiUrl.'oauth2.0/token?'.http_build_query($arr));
if (!$result) {
return $this->error(Code::ERROR, 'request interface failed');
}
if (isset($this->json($result)['error'])) {
return $this->error(Code::ERROR, $this->json($result)['error_description']);
}
return $this->__getAccessToken($result);
}
public function getUserInfo(string $access_token)
{
$this->openid = $this->getOpenId($access_token);
$arr = [
'access_token' => $access_token,
'oauth_consumer_key' => $this->appid,
'openid' => $this->openid
];
$result = $this->curl->get($this->apiUrl.'user/get_user_info?'.http_build_query($arr));
if (!$result) {
return $this->error(Code::ERROR, 'request interface failed');
}
$result = json_decode($result, true);
if (isset($result['ret']) && $result['ret']!=0) {
return $this->error(Code::ERROR, $result['msg']);
}
return $result;
}
}