FACEBOOK登录
相关设置:https://developers.facebook.com/apps
调用方法
public function third ( )
{
if ( empty ( $type = Input :: get ( 'type' , [ 'facebook' , 'tuite' ] ) ) ) {
return $this -> error ( '参数错误' ) ;
}
if ( ! Config :: get ( 'oauth.power' ) ) {
return $this -> error ( '未开启三方登录' ) ;
}
$config = Config :: get ( "oauth.{ $type } " ) ;
if ( empty ( $config [ 'appid' ] ) || empty ( $config [ 'appsecret' ] ) ) {
return $this -> error ( '三方登录配置错误' ) ;
}
$callback = Config :: get ( 'siteurl' ) . Url :: build ( 'user.auth.third' , [ 'type' => $type ] ) ;
if ( Input :: has ( 'code' ) && ( $code = Input :: get ( 'code' , 'str' , '' ) ) ) {
$oauth = Oauth :: getInstance ( $type ) ;
$token = $oauth -> getAccessToken ( $code , $callback ) ;
if ( ! is_array ( $token ) ) return $this -> error ( $token ) ;
$thirdInfo = $oauth -> getInfo ( ) ;
$openId = $thirdInfo [ 'id' ] ;
$nickname = $thirdInfo [ 'name' ] ;
$avatar = $thirdInfo [ 'avatar' ] ;
$email = $thirdInfo [ 'email' ] ;
$unionLogin = UnionLogin :: I ( ) -> where ( [ 'type' => $type , 'openid' => $openId ] ) -> find ( ) ;
if ( $unionLogin && $unionLogin [ 'user_id' ] ) {
if ( ! ( $info = UserModel :: I ( ) -> getFullInfo ( $unionLogin [ 'user_id' ] ) ) || ! $info [ 'status' ] ) {
return $this -> error ( '您帐号已经被禁用了' ) ;
}
UserModel :: I ( ) -> setLoginStatus ( $info [ 'id' ] , $info [ 'name' ] ) ;
$this -> redirect ( Url :: build ( 'user.index.index' ) ) ;
} else {
if ( $this -> isLogin ) {
UserModel :: I ( ) -> oauthBind ( $this -> userInfo [ 'id' ] , $type , $openId ) ;
$this -> redirect ( Url :: build ( 'user.info.index' ) ) ;
} else {
$username = strtoupper ( $type ) . "_" . date ( 's' ) . sp_random_string ( 6 ) ;
$res = UserModel :: I ( ) -> add ( $username , $avatar , $email ) ;
if ( is_numeric ( $res ) ) {
UserModel :: I ( ) -> oauthBind ( $res , $type , $openId ) ;
return $this -> message ( '注册成功' , SUCCESS , Url :: build ( 'user.info.index' ) ) ;
} else {
return $this -> message ( $res , ERROR , '#back#' ) ;
}
}
}
} else {
$this -> redirect ( Oauth :: getInstance ( $type ) -> authorize ( $callback ) ) ;
}
}
认证类
<?php
namespace Kuxin\ Oauth ;
use Kuxin\ Config ;
use Kuxin\ Helper\ Http ;
use Kuxin\ Loader ;
abstract class Oauth
{
protected $appid = '' ;
protected $appsecret = '' ;
protected $responseType = 'code' ;
protected $grantType = 'authorization_code' ;
protected $authorizeParam = [ ] ;
protected $getTokenParam = [ ] ;
protected $getRequestCodeURL = '' ;
protected $getAccessTokenURL = '' ;
protected $apiBase = '' ;
protected $token = null ;
protected $openid = null ;
protected $type = null ;
public function __construct ( array $config , $token = null )
{
$this -> appid = $config [ 'appid' ] ;
$this -> appsecret = $config [ 'appsecret' ] ;
$this -> token = $token ;
$this -> type = '' ;
Config :: set ( 'user_agent' , '' ) ;
}
public static function getInstance ( $type , $token = null )
{
$config = Config :: get ( "oauth.{ $type } " ) ;
$classname = '\\Kuxin\\Oauth\\' . $type ;
return Loader :: instance ( $classname , [ $config , $token ] ) ;
}
public function authorize ( $url )
{
$query = parse_url ( $url ) [ 'query' ] ;
$param = [
"response_type" => $this -> responseType ,
"redirect_uri" => $url ,
"state" => time ( ) ,
] ;
$param [ 'client_id' ] = $this -> appid ;
$param = array_merge ( $param , $this -> authorizeParam ) ;
if ( strpos ( $this -> getRequestCodeURL , '?' ) === false ) {
return $this -> getRequestCodeURL . '?' . http_build_query ( $param ) ;
} else {
return $this -> getRequestCodeURL . '&' . http_build_query ( $param ) ;
}
}
public function getAccessToken ( $code , $url )
{
$param = [
"grant_type" => $this -> grantType ,
"redirect_uri" => $url ,
"client_secret" => $this -> appsecret ,
"code" => $code ,
] ;
$query = parse_url ( $url ) [ 'query' ] ;
if ( $query == 'type=weixin' ) {
$param [ 'appid' ] = $this -> appid ;
} else {
$param [ 'client_id' ] = $this -> appid ;
}
$param = array_merge ( $param , $this -> getTokenParam ) ;
$response = Http :: post ( $this -> getAccessTokenURL , http_build_query ( $param ) ) ;
return $this -> parseToken ( $response ) ;
}
abstract protected function call ( $api , $param = '' , $method = 'GET' , $multi = false ) ;
abstract protected function parseToken ( $result ) ;
abstract public function getOpenId ( ) ;
abstract public function getUionId ( ) ;
abstract public function getInfo ( ) ;
}
FACEBOOK类
<?php
namespace Kuxin\ Oauth ;
use Kuxin\ Helper\ Http ;
use Kuxin\ Helper\ Json ;
class Facebook extends Oauth {
protected $getRequestCodeURL = 'https://www.facebook.com/dialog/oauth' ;
protected $getAccessTokenURL = 'https://graph.facebook.com/oauth/access_token' ;
protected $Authorize = 'scope=email' ;
protected $apiBase = 'https://graph.facebook.com/' ;
public function __construct ( array $config , $token = null )
{
parent :: __construct ( $config , $token ) ;
$this -> getTokenParam = [
'appid' => $this -> appid ,
'secret' => $this -> appsecret ,
] ;
}
public function call ( $api , $param = [ ] , $method = 'GET' , $multi = false )
{
$params = [
'access_token' => $this -> token ,
] ;
$params = array_merge ( $params , $param ) ;
$data = Http :: get ( $this -> apiBase . $api , $params ) ;
return Json :: decode ( $data , true ) ;
}
protected function parseToken ( $result )
{
$data = Json :: decode ( $result , true ) ;
if ( isset ( $data [ 'access_token' ] ) ) {
$this -> token = $data [ 'access_token' ] ;
return [
'token' => $this -> token ,
'expires' => $data [ 'expires_in' ] ,
'refresh' => $data [ 'refresh_token' ] ,
] ;
} else
return "获取 facebook ACCESS_TOKEN 出错:{ $result } " ;
}
public function getOpenId ( ) {
if ( isset ( $this -> openid ) )
return $this -> openid ;
$data = $this -> call ( 'me?fields=id' ) ;
if ( ! empty ( $data [ 'id' ] ) )
return $data [ 'id' ] ;
else
return '没有获取到 facebook 用户ID!' ;
}
public function getUionId ( ) {
}
public function getInfo ( )
{
$data = $this -> call ( '/me?fields=id,name,email' ) ;
return [
'id' => $data [ 'id' ] ,
'name' => $data [ 'name' ] ,
'email' => $data [ 'email' ] ,
'avatar' => '' ,
] ;
}
}