下载地址:
https://githubfast.com/anerg2046/sns_auth
安装方式建议使用composer进行安装
如果linux执行composer不方便的话,可以在本地新建个文件夹,然后执行上面的composer命令,把代码sdk和composer文件一起上传到项目适当位置:然后,
示例代码:
<?php//以thinkCMF框架为例namespace app\home\controller;use cmf\controller\HomeBaseController;use think\facade\Db;use anerg\OAuth2\OAuth;class UserController extends HomebaseController{private function getThirdConfig($name){$site='http://xxxx.com'; //更改为自己站点域名//下面的参数需要根据自己的实际需求替换$line_config=['app_id'=>'login_line_appid','app_secret'=>'login_line_secret','scope'=>'profile'];$facebook_config=['app_id'=>'login_facebook_appid','app_secret'=>'login_facebook_secret','scope'=>'public_profile'];$google_config=['app_id'=>'login_google_appid'.'.apps.googleusercontent.com','app_secret'=>'login_google_secret','scope'=>'https://www.googleapis.com/auth/userinfo.profile'];$twitter_config=['app_id'=>'login_twitter_appid','app_secret'=>'login_twitter_secret'];$config=[];switch ($name) {case 'line':$config=$line_config;break;case 'facebook':$config=$facebook_config;break;case 'google':$config=$google_config;break;case 'twitter':$config=$twitter_config;break;}$config['callback']=$site.'/home/user/'.$name.'_callback';return $config;}/*** Line登录*/public function line(){require_once '/sns_auth/vendor/autoload.php';$name='line';$config=$this->getThirdConfig($name);return redirect(OAuth::$name($config)->getRedirectUrl());}/**Line登陆回调**/public function line_callback(){$code= $_GET['code'] ?? '';if($code!=""){$this->third_callback('line');}else{$this->error('三方回调信息获取失败');}}/*** facebook登录*/public function facebook(){require_once '/sns_auth/vendor/autoload.php';$name='facebook';$config=$this->getThirdConfig($name);return redirect(OAuth::$name($config)->getRedirectUrl());}/*** Facebook登陆回调*/public function facebook_callback(){$code= $_GET['code'] ?? '';if($code!=""){$this->third_callback('facebook');}else{$this->error('三方回调信息获取失败');}}/*** Google登录*/public function google(){require_once '/sns_auth/vendor/autoload.php';$name='google';$config=$this->getThirdConfig($name);return redirect(OAuth::$name($config)->getRedirectUrl());}/*** Google登陆回调*/public function google_callback(){$code= $_GET['code'] ?? '';if($code!=""){$this->third_callback('google');}else{$this->error('三方回调信息获取失败');}}/*** twitter登录*/public function twitter(){require_once '/sns_auth/vendor/autoload.php';$name='twitter';$config=$this->getThirdConfig($name);return redirect(OAuth::$name($config)->getRedirectUrl());}/*** twitter登陆回调*/public function twitter_callback(){$code= $_GET['code'] ?? '';if($code!=""){$this->third_callback('twitter');}else{$this->error('三方回调信息获取失败');}}protected function third_callback($name){require_once '/sns_auth/vendor/autoload.php';$config=$this->getThirdConfig($name);$openinfo = OAuth::$name($config)->userinfo();if(!$openinfo){$this->error('三方信息验证失败');return;}/*$openinfo=['openid'=>'唯一标识','channel'=>'line','nick'=>'line昵称','gender'=>'性别','avatar'=>'line头像'];*/$openid=$openinfo['openid'];$channel=$openinfo['channel'];$nickname=$openinfo['nick'];$avatar=$openinfo['avatar'];$userinfo=$this->loginByThird($channel,$openid,$nickname,$avatar);if($userinfo==1001){$this->assign("jumpUrl",'/');$this->error('该账号已被禁用');return;}if($userinfo==1002){$this->assign("jumpUrl",'/');$this->error('该账号已被注销');return;}if($userinfo==1003){$this->assign("jumpUrl",'/');$this->error('该账号已被禁用');return;}$configpub=getConfigPub();session('uid',$userinfo['id']);session('token',$userinfo['token']);session('user',$userinfo);$href='http://xxxx.com'; //替换为自己站点的域名echo "<meta http-equiv=refresh content='0; url=$href'>";}protected function loginByThird($type,$openid,$nickname,$avatar){//实际业务代码,根据自己项目做修改$info=DB::name('user')->where("openid='{$openid}' and login_type='{$type}' and user_type=2")->find();if(!$info){/* 注册 */$user_pass='abcdefg123456';$user_pass=cmf_password($user_pass); //密码加密,方法请自己实现$user_login=$type.'_'.time().rand(100,999);if(!$nickname){$nickname=$type.'用户-'.substr($openid,-4);}else{$nickname=urldecode($nickname);}if(!$avatar){$avatar='/default.jpg';$avatar_thumb='/default_thumb.jpg';}else{$avatar=urldecode($avatar);$avatar_thumb=$avatar;}$data=array('user_login' => $user_login,'user_nickname' =>$nickname,'user_pass' =>$user_pass,'signature' =>'这家伙很懒,什么都没留下','avatar' =>$avatar,'avatar_thumb' =>$avatar_thumb,'last_login_ip' =>get_client_ip(0,true),'create_time' => time(),'user_status' => 1,'openid' => $openid,'login_type' => $type,"user_type" =>2,//会员"source" =>'pc');$uid=DB::name('user')->insertGetId($data);$info['id']=$uid;$info['user_nickname']=$data['user_nickname'];$info['avatar']=$data['avatar'];$info['avatar_thumb']=$data['avatar_thumb'];$info['sex']='2';$info['signature']=$data['signature'];$info['coin']='0';$info['login_type']=$data['login_type'];$info['province']='';$info['city']='';$info['birthday']='';$info['consumption']='0';$info['votestotal']='0';$info['user_status']=1;$info['last_login_time']='';$info['end_bantime']='0';}else{if(!$avatar){$avatar='/default.jpg';$avatar_thumb='/default_thumb.jpg';}else{$avatar=urldecode($avatar);$avatar_thumb=$avatar;}$info['avatar']=$avatar;$info['avatar_thumb']=$avatar_thumb;$data=array('avatar' =>$avatar,'avatar_thumb' =>$avatar_thumb,);}if($info['user_status']=='0'){return 1001; }if($info['user_status']=='3'){return 1002; }if($info['user_status']=='1'&& $info['end_bantime']>time()){return 1003; }$info['isreg']='0';$info['isagent']='0';if($info['last_login_time']=='' ){$info['isreg']='1';$info['isagent']='1';}$info['avatar']=get_upload_path($info['avatar']);$info['avatar_thumb']=get_upload_path($info['avatar_thumb']);return $info;}}
代码中各个配置参数如何获取:
facebook:
Line:
Google: