当前位置: 首页 > news >正文

集成Line、Facebook、Twitter、Google、微信、QQ、微博、支付宝的三方登录sdk

下载地址:

https://github.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:

相关文章:

  • 开启创造力之门:掌握Vue中Slot插槽的使用技巧与灵感
  • Intellij IDEA 内存设置的问题 及解决
  • Python高级语法----Python C扩展与性能优化
  • windiws docker 部署jar window部署docker 转载
  • 微软允许OEM对Win10不提供关闭Secure Boot
  • pytorch tensor数据类型转换为python数据
  • 使用 typescript + express 创建 NodeJs 后端服务
  • Angular 依赖注入介绍及使用(五)
  • 使用 webpack 打包 express 应用
  • Flutter笔记:getX库中的GetView中间件
  • 【计算思维】少儿编程蓝桥杯青少组计算思维题考试真题及解析B
  • iOS应用加固方案解析:ipa加固安全技术全面评测
  • 《网络协议》06. HTTP 补充 · HTTPS · SSL/TLS
  • PPT转PDF转换器:便捷的批量PPT转PDF转换软件
  • 安全区域边界(设备和技术注解)
  • JavaScript-如何实现克隆(clone)函数
  • 自己简单写的 事件订阅机制
  • 【RocksDB】TransactionDB源码分析
  • Angular 响应式表单 基础例子
  • Apache的80端口被占用以及访问时报错403
  • download使用浅析
  • ES学习笔记(12)--Symbol
  • JavaScript DOM 10 - 滚动
  • React-redux的原理以及使用
  • text-decoration与color属性
  • VUE es6技巧写法(持续更新中~~~)
  • 给Prometheus造假数据的方法
  • 基于OpenResty的Lua Web框架lor0.0.2预览版发布
  • 简单数学运算程序(不定期更新)
  • 名企6年Java程序员的工作总结,写给在迷茫中的你!
  • 前端路由实现-history
  • 如何合理的规划jvm性能调优
  • 突破自己的技术思维
  • RDS-Mysql 物理备份恢复到本地数据库上
  • Semaphore
  • ​力扣解法汇总946-验证栈序列
  • #!/usr/bin/python与#!/usr/bin/env python的区别
  • ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTr
  • (HAL库版)freeRTOS移植STMF103
  • (Matalb时序预测)WOA-BP鲸鱼算法优化BP神经网络的多维时序回归预测
  • (附源码)计算机毕业设计SSM基于健身房管理系统
  • (附源码)计算机毕业设计SSM教师教学质量评价系统
  • (转)【Hibernate总结系列】使用举例
  • .net 受管制代码
  • ??eclipse的安装配置问题!??
  • @html.ActionLink的几种参数格式
  • [ 蓝桥杯Web真题 ]-Markdown 文档解析
  • [.net] 如何在mail的加入正文显示图片
  • [.NET]桃源网络硬盘 v7.4
  • [4.9福建四校联考]
  • [AIGC] Nacos:一个简单 yet powerful 的配置中心和服务注册中心
  • [Android]Android开发入门之HelloWorld
  • [BZOJ] 2044: 三维导弹拦截
  • [DevEpxress]GridControl 显示Gif动画
  • [FFmpeg学习]从视频中获取图片