增加社会化登录
This commit is contained in:
parent
b475154ffe
commit
4cbd141416
7
config/cloud.php
Normal file
7
config/cloud.php
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Administrator
|
||||||
|
* Date: 2019/1/26
|
||||||
|
* Time: 14:41
|
||||||
|
*/
|
2
extend/.gitignore
vendored
2
extend/.gitignore
vendored
@ -1,2 +0,0 @@
|
|||||||
*
|
|
||||||
!.gitignore
|
|
30
extend/socialite/command/SocialiteCommand.php
Normal file
30
extend/socialite/command/SocialiteCommand.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Administrator
|
||||||
|
* Date: 2018/12/29
|
||||||
|
* Time: 16:19
|
||||||
|
*/
|
||||||
|
namespace thinking\socialite\command;
|
||||||
|
|
||||||
|
use think\console\Command;
|
||||||
|
use think\console\Input;
|
||||||
|
use think\console\Output;
|
||||||
|
|
||||||
|
class SocialiteCommand extends Command
|
||||||
|
{
|
||||||
|
protected function configure()
|
||||||
|
{
|
||||||
|
$this->setName('socialite publish')
|
||||||
|
->setDescription('publish socialite config');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(Input $input, Output $output)
|
||||||
|
{
|
||||||
|
$config = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'socialite.php';
|
||||||
|
|
||||||
|
copy($config, app('config_path'));
|
||||||
|
|
||||||
|
$output->writeln('publish successfully, check it' . PHP_EOL);
|
||||||
|
}
|
||||||
|
}
|
17
extend/socialite/composer.json
Normal file
17
extend/socialite/composer.json
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"name": "thinking/socialite",
|
||||||
|
"description": "a socialite package for thinkphp5.1*",
|
||||||
|
"license": "MIT",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "yanwnewu",
|
||||||
|
"email": "82664165@qq.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"minimum-stability": "dev",
|
||||||
|
"require": {
|
||||||
|
"php": "^7.1.3",
|
||||||
|
"ext-json": "*",
|
||||||
|
"guzzlehttp/guzzle": "~6.0"
|
||||||
|
}
|
||||||
|
}
|
35
extend/socialite/config/socialite.php
Normal file
35
extend/socialite/config/socialite.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Administrator
|
||||||
|
* Date: 2018/12/28
|
||||||
|
* Time: 11:21
|
||||||
|
*/
|
||||||
|
return [
|
||||||
|
'default' => 'qq',
|
||||||
|
|
||||||
|
'qq' => [
|
||||||
|
'app_id' => '101540418',
|
||||||
|
'app_secret' => 'fae33241fa62f8b375565365e216cde7',
|
||||||
|
'redirect_url' => 'http://127.0.0.1:8000/oauth',
|
||||||
|
],
|
||||||
|
|
||||||
|
'weibo' => [
|
||||||
|
'app_id' => '2539916570',
|
||||||
|
'app_secret' => 'aae0e36c96ecc04f873be3e0dc06f71b',
|
||||||
|
'redirect_url' => 'http://www.rllady.com/home/index/sinaLogin',
|
||||||
|
],
|
||||||
|
|
||||||
|
'github' => [
|
||||||
|
'app_id' => '5b8a9a7e8dded81c128d',
|
||||||
|
'app_secret' => '12657a874d3c036a5b9e55c7512ffd5292e67118',
|
||||||
|
'redirect_url' => 'http://127.0.0.1:8000/oauth',
|
||||||
|
],
|
||||||
|
|
||||||
|
'wx' => [
|
||||||
|
'app_id' => 'wx83f3fcd9e0cfdff8',
|
||||||
|
'app_secret' => '2a736290f3af0fb40710a65c938b40b3',
|
||||||
|
'redirect_url' => 'http://127.0.0.1:8000/oauth',
|
||||||
|
'scope' => 'snsapi_login',
|
||||||
|
],
|
||||||
|
];
|
110
extend/socialite/src/Socialite.php
Normal file
110
extend/socialite/src/Socialite.php
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Administrator
|
||||||
|
* Date: 2018/12/28
|
||||||
|
* Time: 14:25
|
||||||
|
*/
|
||||||
|
namespace thinking\socialite;
|
||||||
|
|
||||||
|
use thinking\socialite\provider\GithubProvider;
|
||||||
|
use thinking\socialite\provider\QqProvider;
|
||||||
|
use thinking\socialite\provider\WeiBoProvider;
|
||||||
|
use thinking\socialite\provider\WxProvider;
|
||||||
|
|
||||||
|
class Socialite
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* QQ 登录
|
||||||
|
*
|
||||||
|
* @time at 2018年12月28日
|
||||||
|
* @return QqProvider
|
||||||
|
*/
|
||||||
|
protected function qqDriver()
|
||||||
|
{
|
||||||
|
$config = config('socialite.qq');
|
||||||
|
|
||||||
|
return new QqProvider(
|
||||||
|
$config['app_id'], $config['app_secret'], $config['redirect_url'], $config['scope'] ?? ''
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微博 登录
|
||||||
|
*
|
||||||
|
* @time at 2018年12月28日
|
||||||
|
* @return WeiBoProvider
|
||||||
|
*/
|
||||||
|
protected function weiboDriver()
|
||||||
|
{
|
||||||
|
$config = config('socialite.weibo');
|
||||||
|
|
||||||
|
return new WeiBoProvider(
|
||||||
|
$config['app_id'], $config['app_secret'], $config['redirect_url'], $config['scope'] ?? ''
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Github 登录
|
||||||
|
*
|
||||||
|
* @time at 2018年12月28日
|
||||||
|
* @return GithubProvider
|
||||||
|
*/
|
||||||
|
protected function githubDriver()
|
||||||
|
{
|
||||||
|
$config = config('socialite.github');
|
||||||
|
|
||||||
|
return new GithubProvider(
|
||||||
|
$config['app_id'], $config['app_secret'], $config['redirect_url'], $config['scope'] ?? ''
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信 登录
|
||||||
|
*
|
||||||
|
* @time at 2018年12月28日
|
||||||
|
* @return WxProvider
|
||||||
|
*/
|
||||||
|
protected function wxDriver()
|
||||||
|
{
|
||||||
|
$config = config('socialite.wx');
|
||||||
|
|
||||||
|
return new WxProvider(
|
||||||
|
$config['app_id'], $config['app_secret'], $config['redirect_url'], $config['scope'] ?? ''
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return provider
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @param $type
|
||||||
|
* @return \thinking\socialite\provider\AbstractProvider
|
||||||
|
*/
|
||||||
|
public function driver($type = null)
|
||||||
|
{
|
||||||
|
$driver = $this->createDriver($type);
|
||||||
|
|
||||||
|
$driver->oauth();
|
||||||
|
|
||||||
|
return $driver;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create oauth provider
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @param $type
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function createDriver($type)
|
||||||
|
{
|
||||||
|
$defaultDriver = config('socialite.default');
|
||||||
|
|
||||||
|
$driver = $type ? : $defaultDriver;
|
||||||
|
|
||||||
|
$function = $driver . 'Driver';
|
||||||
|
|
||||||
|
return call_user_func([$this, $function]);
|
||||||
|
}
|
||||||
|
}
|
165
extend/socialite/src/User.php
Normal file
165
extend/socialite/src/User.php
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Administrator
|
||||||
|
* Date: 2018/12/29
|
||||||
|
* Time: 9:08
|
||||||
|
*/
|
||||||
|
namespace thinking\socialite;
|
||||||
|
|
||||||
|
class User
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
public $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $nickname;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $email;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $avatar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public $user;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get userId
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getUserId()
|
||||||
|
{
|
||||||
|
return (int)$this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get nickname
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getNickName()
|
||||||
|
{
|
||||||
|
return $this->nickname;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get email
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getEmail()
|
||||||
|
{
|
||||||
|
return $this->email;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get avatar
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getAvatar()
|
||||||
|
{
|
||||||
|
return $this->avatar;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get name
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set user
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @param $user
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setUser($user)
|
||||||
|
{
|
||||||
|
$this->user = $user;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set property
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @param $user
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function map($user)
|
||||||
|
{
|
||||||
|
foreach ($user as $attr => $value) {
|
||||||
|
if ($this->hasProperty($attr)) {
|
||||||
|
$this->{$attr} = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* has property
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @param $attr
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function hasProperty($attr)
|
||||||
|
{
|
||||||
|
return property_exists($this, $attr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __get($name)
|
||||||
|
{
|
||||||
|
// TODO: Implement __get() method.
|
||||||
|
return $this->user[$name];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __isset($name)
|
||||||
|
{
|
||||||
|
// TODO: Implement __isset() method.
|
||||||
|
return isset($this->user[$name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __set($name, $value)
|
||||||
|
{
|
||||||
|
// TODO: Implement __set() method.
|
||||||
|
$this->user[$name] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __unset($name)
|
||||||
|
{
|
||||||
|
// TODO: Implement __unset() method.
|
||||||
|
unset($this->user[$name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
14
extend/socialite/src/contract/Provider.php
Normal file
14
extend/socialite/src/contract/Provider.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Administrator
|
||||||
|
* Date: 2018/12/28
|
||||||
|
* Time: 11:36
|
||||||
|
*/
|
||||||
|
namespace thinking\socialite\contract;
|
||||||
|
|
||||||
|
interface Provider
|
||||||
|
{
|
||||||
|
|
||||||
|
public function user();
|
||||||
|
}
|
24
extend/socialite/src/facade/Socialite.php
Normal file
24
extend/socialite/src/facade/Socialite.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Administrator
|
||||||
|
* Date: 2018/12/29
|
||||||
|
* Time: 10:28
|
||||||
|
*/
|
||||||
|
namespace thinking\socialite\facade;
|
||||||
|
|
||||||
|
use think\Facade;
|
||||||
|
|
||||||
|
class Socialite extends Facade
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @method static \thinking\socialite\Socialite driver($type) get provider
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function getFacadeClass()
|
||||||
|
{
|
||||||
|
return \thinking\socialite\Socialite::class;
|
||||||
|
}
|
||||||
|
}
|
170
extend/socialite/src/provider/AbstractProvider.php
Normal file
170
extend/socialite/src/provider/AbstractProvider.php
Normal file
@ -0,0 +1,170 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Administrator
|
||||||
|
* Date: 2018/12/28
|
||||||
|
* Time: 11:50
|
||||||
|
*/
|
||||||
|
namespace thinking\socialite\provider;
|
||||||
|
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
use think\exception\HttpException;
|
||||||
|
use think\exception\HttpResponseException;
|
||||||
|
use think\facade\Session;
|
||||||
|
use GuzzleHttp\ClientInterface;
|
||||||
|
|
||||||
|
class AbstractProvider
|
||||||
|
{
|
||||||
|
// 应用ID
|
||||||
|
protected $appId;
|
||||||
|
// 应用秘钥
|
||||||
|
protected $appSecret;
|
||||||
|
// 回调地址
|
||||||
|
protected $redirectUrl;
|
||||||
|
// scope
|
||||||
|
protected $scope;
|
||||||
|
// http 客户端
|
||||||
|
protected $httpClient = null;
|
||||||
|
// request
|
||||||
|
protected $request;
|
||||||
|
|
||||||
|
protected $clientIdKey = 'client_id';
|
||||||
|
|
||||||
|
public function __construct($appId = '', $appSecret='', $redirectUrl='', $scope = '')
|
||||||
|
{
|
||||||
|
$this->appId = $appId;
|
||||||
|
$this->appSecret = $appSecret;
|
||||||
|
$this->redirectUrl = $redirectUrl;
|
||||||
|
$this->scope = $scope;
|
||||||
|
$this->request = app('request');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getHttpClient()
|
||||||
|
{
|
||||||
|
if (is_null($this->httpClient)) {
|
||||||
|
$this->httpClient = new Client();
|
||||||
|
}
|
||||||
|
return $this->httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* oauth login
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function oauth()
|
||||||
|
{
|
||||||
|
if (!$this->request->get('code')) {
|
||||||
|
throw new HttpResponseException(redirect($this->authorizeUrl . '?' . http_build_query($this->createOauthParams())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create oauth params
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function createOauthParams()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'response_type' => 'code',
|
||||||
|
$this->clientIdKey => $this->appId,
|
||||||
|
'redirect_uri' => $this->redirectUrl,
|
||||||
|
'scope' => $this->getScope(),
|
||||||
|
'state' => $this->state(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set scope
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @param $scope
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setScope($scope)
|
||||||
|
{
|
||||||
|
$this->scope = $scope;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get scope
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getScope()
|
||||||
|
{
|
||||||
|
return is_array($this->scope) ? trim(implode($this->scope), ',') : $this->scope;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get state
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function getState()
|
||||||
|
{
|
||||||
|
$state = $this->request->session('state');
|
||||||
|
|
||||||
|
$this->request->session('state', null);
|
||||||
|
|
||||||
|
return $state;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check state
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function checkState()
|
||||||
|
{
|
||||||
|
if ($this->request->param('state') != $this->getState()) {
|
||||||
|
throw new HttpException(401, 'Authorized login State verification failed, Please check it');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* generate state
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function state()
|
||||||
|
{
|
||||||
|
$state = md5(rand(1,100000));
|
||||||
|
|
||||||
|
Session::set('state', $state);
|
||||||
|
|
||||||
|
return $state;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get token params
|
||||||
|
*
|
||||||
|
* @time at 2018年12月29日
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getTokenParams()
|
||||||
|
{
|
||||||
|
$this->checkState();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'code' => $this->request->get('code'),
|
||||||
|
'client_secret' => $this->appSecret,
|
||||||
|
$this->clientIdKey => $this->appId,
|
||||||
|
'redirect_uri' => $this->redirectUrl,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getPostKey()
|
||||||
|
{
|
||||||
|
return (version_compare(ClientInterface::VERSION, '6') === 1) ? 'form_params' : 'body';
|
||||||
|
}
|
||||||
|
}
|
68
extend/socialite/src/provider/GithubProvider.php
Normal file
68
extend/socialite/src/provider/GithubProvider.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Administrator
|
||||||
|
* Date: 2018/12/28
|
||||||
|
* Time: 11:18
|
||||||
|
*/
|
||||||
|
namespace thinking\socialite\provider;
|
||||||
|
|
||||||
|
use think\exception\HttpException;
|
||||||
|
use thinking\socialite\User;
|
||||||
|
|
||||||
|
class GithubProvider extends AbstractProvider
|
||||||
|
{
|
||||||
|
protected $authorizeUrl = 'https://github.com/login/oauth/authorize';
|
||||||
|
|
||||||
|
protected $accessTokenUrl = 'https://github.com/login/oauth/access_token';
|
||||||
|
|
||||||
|
protected $userUrl = 'https://api.github.com/user';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 Access Token
|
||||||
|
*
|
||||||
|
* @time at 2018年12月28日
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function getAccessToken()
|
||||||
|
{
|
||||||
|
$response = $this->getHttpClient()->post($this->accessTokenUrl, [
|
||||||
|
'verify' => false,
|
||||||
|
'headers' => ['Accept' => 'application/json'],
|
||||||
|
$this->getPostKey() => array_merge($this->getTokenParams())
|
||||||
|
]);
|
||||||
|
|
||||||
|
$token = json_decode($response->getBody()->getContents(), true);
|
||||||
|
|
||||||
|
if (!isset($token['access_token'])) {
|
||||||
|
throw new HttpException(401, 'Access Token Missing, Please ReLogin');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $token['access_token'];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户信息
|
||||||
|
*
|
||||||
|
* @time at 2018年12月28日
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
$response = $this->getHttpClient()->get($this->userUrl,[
|
||||||
|
'verify' => false,
|
||||||
|
'headers' => ['Authorization' => sprintf('token %s', $this->getAccessToken())]
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = json_decode($response->getBody(), true);
|
||||||
|
|
||||||
|
return (new User)->setUser($user)->map([
|
||||||
|
'id' => $user['id'],
|
||||||
|
'nickname' => $user['login'],
|
||||||
|
'name' => $user['name'],
|
||||||
|
'email' => $user['email'],
|
||||||
|
'avatar' => $user['avatar_url'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
100
extend/socialite/src/provider/QqProvider.php
Normal file
100
extend/socialite/src/provider/QqProvider.php
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Administrator
|
||||||
|
* Date: 2018/12/28
|
||||||
|
* Time: 11:18
|
||||||
|
*/
|
||||||
|
namespace thinking\socialite\provider;
|
||||||
|
|
||||||
|
use think\exception\HttpException;
|
||||||
|
use thinking\socialite\contract\Provider;
|
||||||
|
use thinking\socialite\User;
|
||||||
|
|
||||||
|
class QqProvider extends AbstractProvider implements Provider
|
||||||
|
{
|
||||||
|
protected $authorizeUrl = 'https://graph.qq.com/oauth2.0/authorize';
|
||||||
|
|
||||||
|
protected $accessTokenUrl = 'https://graph.qq.com/oauth2.0/token';
|
||||||
|
|
||||||
|
protected $openIdUrl = 'https://graph.qq.com/oauth2.0/me';
|
||||||
|
|
||||||
|
protected $userUrl = 'https://graph.qq.com/user/get_user_info';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 Access Token
|
||||||
|
*
|
||||||
|
* @time at 2018年12月28日
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function getAccessToken()
|
||||||
|
{
|
||||||
|
|
||||||
|
$response = $this->getHttpClient()->get($this->accessTokenUrl, [
|
||||||
|
'verify' => false,
|
||||||
|
'query' => array_merge($this->getTokenParams(), ['grant_type' => 'authorization_code'])
|
||||||
|
]);
|
||||||
|
|
||||||
|
parse_str($response->getBody()->getContents(), $token);
|
||||||
|
|
||||||
|
if (!isset($token['access_token'])) {
|
||||||
|
throw new HttpException(401, 'Access Token Missing, Please ReLogin');
|
||||||
|
}
|
||||||
|
return $token['access_token'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 Open ID
|
||||||
|
*
|
||||||
|
* @time at 2018年12月28日
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getOpenId()
|
||||||
|
{
|
||||||
|
$accessToken = $this->getAccessToken();
|
||||||
|
|
||||||
|
$response = $this->getHttpClient()->get($this->openIdUrl, [
|
||||||
|
'verify' => false,
|
||||||
|
'query' => ['access_token' => $accessToken]
|
||||||
|
]);
|
||||||
|
|
||||||
|
$openidStr = (string)$response->getBody()->getContents();
|
||||||
|
|
||||||
|
$openIdArr = json_decode(substr($openidStr,strpos($openidStr,'(')+1,-3),true);
|
||||||
|
|
||||||
|
return array_merge($openIdArr, ['access_token' => $accessToken]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户信息
|
||||||
|
*
|
||||||
|
* @time at 2018年12月28日
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
$getUserParams = $this->getOpenId();
|
||||||
|
|
||||||
|
unset($getUserParams['app_id']);
|
||||||
|
$getUserParams['oauth_consumer_key'] = $this->appId;
|
||||||
|
|
||||||
|
$response = $this->getHttpClient()->get($this->userUrl, [
|
||||||
|
'verify' => false,
|
||||||
|
'headers' => ['Accept' => 'application/json'],
|
||||||
|
'query' => $getUserParams,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = json_decode($response->getBody()->getContents(), true);
|
||||||
|
|
||||||
|
$user['open_id'] = $getUserParams['openid'];
|
||||||
|
|
||||||
|
return (new User)->setUser($user)->map([
|
||||||
|
'id' => $getUserParams['openid'],
|
||||||
|
'nickname' => $user['nickname'],
|
||||||
|
'avatar' => $user['figureurl_2'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
87
extend/socialite/src/provider/WeiBoProvider.php
Normal file
87
extend/socialite/src/provider/WeiBoProvider.php
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Administrator
|
||||||
|
* Date: 2018/12/28
|
||||||
|
* Time: 11:18
|
||||||
|
*/
|
||||||
|
namespace thinking\socialite\provider;
|
||||||
|
|
||||||
|
use think\exception\HttpException;
|
||||||
|
use thinking\socialite\User;
|
||||||
|
|
||||||
|
class WeiBoProvider extends AbstractProvider
|
||||||
|
{
|
||||||
|
protected $authorizeUrl = 'https://api.weibo.com/oauth2/authorize';
|
||||||
|
|
||||||
|
protected $accessTokenUrl = 'https://api.weibo.com/oauth2/access_token';
|
||||||
|
|
||||||
|
protected $tokenInfoUrl = 'https://api.weibo.com/oauth2/get_token_info';
|
||||||
|
|
||||||
|
protected $userUrl = 'https://api.weibo.com/2/users/show.json';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 Access Token
|
||||||
|
*
|
||||||
|
* @time at 2018年12月28日
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function getAccessToken()
|
||||||
|
{
|
||||||
|
|
||||||
|
$response = $this->getHttpClient()->post($this->accessTokenUrl, [
|
||||||
|
'verify' => false,
|
||||||
|
$this->getPostKey() => array_merge($this->getTokenParams(), ['grant_type' => 'authorization_code'])
|
||||||
|
]);
|
||||||
|
|
||||||
|
$token = json_decode($response->getBody()->getContents(), true);
|
||||||
|
|
||||||
|
if (!isset($token['access_token'])) {
|
||||||
|
throw new HttpException(401, 'Access Token Missing, Please ReLogin');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $token['access_token'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 Open ID
|
||||||
|
*
|
||||||
|
* @time at 2018年12月28日
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getTokenInfo()
|
||||||
|
{
|
||||||
|
$accessToken = $this->getAccessToken();
|
||||||
|
|
||||||
|
$response = $this->getHttpClient()->post($this->tokenInfoUrl, [
|
||||||
|
'verify' => false,
|
||||||
|
$this->getPostKey() => ['access_token' => $accessToken]
|
||||||
|
]);
|
||||||
|
|
||||||
|
$tokenInfo = json_decode($response->getBody()->getContents(), true);
|
||||||
|
|
||||||
|
return ['access_token' => $accessToken, 'uid' => $tokenInfo['uid']];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户信息
|
||||||
|
*
|
||||||
|
* @time at 2018年12月28日
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
$response = $this->getHttpClient()->get($this->userUrl,[
|
||||||
|
'verify' => false,
|
||||||
|
'query' => $this->getTokenInfo(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = json_decode($response->getBody(), true);
|
||||||
|
|
||||||
|
return (new User)->setUser($user)->map([
|
||||||
|
'id' => $user['idstr'],
|
||||||
|
'nickname' => $user['name'],
|
||||||
|
'avatar' => $user['profile_image_url'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
101
extend/socialite/src/provider/WxProvider.php
Normal file
101
extend/socialite/src/provider/WxProvider.php
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Administrator
|
||||||
|
* Date: 2018/12/28
|
||||||
|
* Time: 11:18
|
||||||
|
*/
|
||||||
|
namespace thinking\socialite\provider;
|
||||||
|
|
||||||
|
use think\exception\HttpException;
|
||||||
|
use thinking\socialite\contract\Provider;
|
||||||
|
use thinking\socialite\User;
|
||||||
|
|
||||||
|
class WxProvider extends AbstractProvider implements Provider
|
||||||
|
{
|
||||||
|
protected $authorizeUrl = 'https://open.weixin.qq.com/connect/qrconnect';
|
||||||
|
|
||||||
|
protected $accessTokenUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token';
|
||||||
|
|
||||||
|
protected $openIdUrl = 'https://graph.qq.com/oauth2.0/me';
|
||||||
|
|
||||||
|
protected $userUrl = 'https://api.weixin.qq.com/sns/userinfo';
|
||||||
|
|
||||||
|
protected $clientIdKey = 'appid';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 Access Token
|
||||||
|
*
|
||||||
|
* @time at 2018年12月28日
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function getAccessToken()
|
||||||
|
{
|
||||||
|
|
||||||
|
$response = $this->getHttpClient()->get($this->accessTokenUrl, [
|
||||||
|
'verify' => false,
|
||||||
|
'query' => array_merge($this->getTokenParams(), ['grant_type' => 'authorization_code'])
|
||||||
|
]);
|
||||||
|
|
||||||
|
parse_str($response->getBody()->getContents(), $token);
|
||||||
|
|
||||||
|
if (!isset($token['access_token'])) {
|
||||||
|
throw new HttpException(401, 'Access Token Missing, Please ReLogin');
|
||||||
|
}
|
||||||
|
return $token['access_token'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 Open ID
|
||||||
|
*
|
||||||
|
* @time at 2018年12月28日
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getOpenId()
|
||||||
|
{
|
||||||
|
$accessToken = $this->getAccessToken();
|
||||||
|
|
||||||
|
$response = $this->getHttpClient()->get($this->openIdUrl, [
|
||||||
|
'verify' => false,
|
||||||
|
'query' => ['access_token' => $accessToken]
|
||||||
|
]);
|
||||||
|
|
||||||
|
$openidStr = (string)$response->getBody()->getContents();
|
||||||
|
|
||||||
|
$openIdArr = json_decode(substr($openidStr,strpos($openidStr,'(')+1,-3),true);
|
||||||
|
|
||||||
|
return array_merge($openIdArr, ['access_token' => $accessToken]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户信息
|
||||||
|
*
|
||||||
|
* @time at 2018年12月28日
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
$getUserParams = $this->getOpenId();
|
||||||
|
|
||||||
|
unset($getUserParams['app_id']);
|
||||||
|
$getUserParams['oauth_consumer_key'] = $this->appId;
|
||||||
|
|
||||||
|
$response = $this->getHttpClient()->get($this->userUrl, [
|
||||||
|
'verify' => false,
|
||||||
|
'headers' => ['Accept' => 'application/json'],
|
||||||
|
'query' => $getUserParams,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = json_decode($response->getBody()->getContents(), true);
|
||||||
|
|
||||||
|
$user['open_id'] = $getUserParams['openid'];
|
||||||
|
|
||||||
|
return (new User)->setUser($user)->map([
|
||||||
|
'id' => $getUserParams['openid'],
|
||||||
|
'nickname' => $user['nickname'],
|
||||||
|
'avatar' => $user['figureurl_2'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user