<?php
namespace App\Http\Controllers\Oauth;
use App\Http\Controllers\Utils\Code;
/**
* Class BaiDuController
* @author <fl140125@gmail.com>
* @package App\Http\Controllers\Oauth
*/
class BaiDuController extends OAuthController
{
protected $appid;
protected $appsecret;
protected $apiUrl = 'https://openapi.baidu.com/';
protected $redirectUri;
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/baidu';
}
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 = '')
{
$arr = [
'response_type' =>'code',
'client_id' => $this->appid,
'redirect_uri' => empty($callback) ? $this->redirectUri : $callback,
'scope' => 'basic super_msg',
'state' => $this->getState($length),
'display' => 'dialog',
'force_login' => '0', 'confirm_login' =>'1', 'login_type' => 'sms' ];
return $this->apiUrl.'oauth/2.0/authorize?'.http_build_query($arr);
}
public function getAccessToken(string $code)
{
$arr = [
'grant_type' => 'authorization_code',
'code' => $code,
'client_id' => $this->appid,
'client_secret' => $this->appsecret,
'redirect_uri' => $this->redirectUri,
];
$result = $this->curl->get($this->apiUrl.'oauth/2.0/token?'.http_build_query($arr));
if (!$result) {
return $this->error(Code::ERROR, 'request interface failed');
}
$result = (array)$result;
return isset($result['error']) ? $this->error(Code::ERROR, $result['error_description']) : $result;
}
public function getUserInfo2(string $access_token)
{
$arr = [
'access_token' => $access_token,
'format' => 'json'
];
$result = $this->curl->get($this->apiUrl.'rest/2.0/passport/users/getLoggedInUser?'.http_build_query($arr));
if (!$result) {
return $this->error(Code::ERROR, 'request interface failed');
}
$result = (array)$result;
return isset($result['error']) ? $this->error(Code::ERROR, $result['error_description']) : $result;
}
public function getUserInfo(string $access_token)
{
$arr = [
'access_token' => $access_token,
'format' => 'json',
'get_unionid' => '1',
];
$result = $this->curl->get($this->apiUrl.'rest/2.0/passport/users/getInfo?'.http_build_query($arr));
if (!$result) {
return $this->error(Code::ERROR, 'request interface failed');
}
$result = (array)$result;
return isset($result['error']) ? $this->error(Code::ERROR, $result['error_description']) : $result;
}
public function refreshToken(string $refreshToken)
{
$arr = array(
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken,
'client_id' => $this->appid,
'client_secret' => $this->appsecret,
'scope' => 'basic'
);
$result = $this->curl->get($this->apiUrl.'oauth/2.0/token?'.http_build_query($arr));
if (!$result) {
return $this->error(Code::ERROR, 'request interface failed');
}
$result = (array)$result;
return isset($result['error']) ? $this->error(Code::ERROR, $result['error_description']) : $result;
}
}