Thinkphp基于ThinkSDK的QQ和微信登陆
jianfly.com 2019-01-31 1704次浏览
//注:请先参照会员设计设计会员模块
1.将修改过的ThinkSDk文件夹放置于\ThinkPHP\Library\ORG\文件夹下
2.config中配置参数
//QQ登录 define('URL_CALLBACK', 'http://www.jianfly.com/Extlogin/'), //腾讯QQ登录配置 'THINK_SDK_QQ' => array( 'APP_KEY' => '***', //应用注册成功后分配的 APP ID 'APP_SECRET' => '***', //应用注册成功后分配的KEY 'CALLBACK' => URL_CALLBACK."qqcallback", ), 'THINK_SDK_WEIXIN' => array( 'APP_KEY' => '***', //应用注册成功后分配的 APP ID 'APP_SECRET' => '***', //应用注册成功后分配的KEY 'CALLBACK' => URL_CALLBACK . 'weixincallback', ),
3.php,其中用到了User中的两个查找用户方法,如需避免重复代码,可将其放置于PublicController
<?php namespace Home\Controller; class ExtloginController extends PublicController { function __construct() { parent::__construct(); } //QQ登录后等待页面 public function qqwait() { if(!$_SESSION['qqinfo']) { $this->redirect('qqlogin'); exit; } $this->display(); } //绑定平台账号的页面 public function bindm() { if(!$_SESSION['qqinfo']) { $this->redirect('qqlogin'); exit; } $this->display(); } //QQ绑定账户 public function qqbindm() { if(!$_SESSION['qqinfo']) { $this->redirect('qqlogin'); exit; } //获取数据 $mobile = htmlspecialchars($_POST['mobile']); $password = htmlspecialchars($_POST['password']); //检测用户名是否存在 if(!$this->findUserByMobile($mobile)) { $this->error("用户名不存在");exit; } //查找用户信息 $member = $this->findUserByPwd( $mobile , $password ); if(!$member) $this->error("密码错误"); else { if($member['openid'] != '') { $this->error('该账户已绑定QQ'); exit; } $data = array(); $data['openid'] = $_SESSION['qqinfo']['openid']; M('member')->where(array('id'=>$member['id']))->save($data); $member = $this->findUserByPwd( $mobile , $password ); $_SESSION['member'] = $member; unset($_SESSION['qqinfo']); $this->success('登录成功',U('House/pub')); } } public function qqlogin($type="qq"){ empty($type) && $this->error('参数错误'); //加载ThinkOauth类并实例化一个对象 import("ORG.ThinkSDK.ThinkOauth"); $sns = \ORG\ThinkSDK\ThinkOauth::getInstance();//加载ThinkOauth类并实例化一个对象 //跳转到授权页面 redirect($sns->getRequestCodeURL()); } //授权回调地址 public function qqcallback($type='qq', $code = null){ (empty($type) || empty($code)) && $this->error('参数错误'); //加载ThinkOauth类并实例化一个对象 import("ORG.ThinkSDK.ThinkOauth"); $sns = \ORG\ThinkSDK\ThinkOauth::getInstance('qq'); //腾讯微博需传递的额外参数 $extend = null; if($type == 'tencent'){ $extend = array('openid' => $this->_get('openid'), 'openkey' => $this->_get('openkey')); } //请妥善保管这里获取到的Token信息,方便以后API调用 //调用方法,实例化SDK对象的时候直接作为构造函数的第二个参数传入 //如: $qq = ThinkOauth::getInstance('qq', $token); $token = $sns->getAccessToken($code , $extend); //获取当前登录用户信息 if(is_array($token)){ function qq($token){ //import("ORG.ThinkSDK.ThinkOauth"); $qq = \ORG\ThinkSDK\ThinkOauth::getInstance('qq', $token); $data = $qq->call('user/get_user_info'); if($data['ret'] == 0){ $userInfo['type'] = 'QQ'; $userInfo['name'] = $data['nickname']; $userInfo['nick'] = $data['nickname']; $userInfo['head'] = $data['figureurl_2']; return $userInfo; } else { throw_exception("获取腾讯QQ用户信息失败:{$data['msg']}"); } } //$user_info = qq($token); $access_token=$token['access_token']; $openid=$token['openid']; $name=$user_info['name']; //查询有没有用户 $model = M('member'); $data = $model->where(array('openid'=>$openid))->find(); if (is_null($data)) {//需要绑定账户 if($_SESSION['member']) { M('member')->where(array('id'=>$_SESSION['member']['id']))->save(array('openid'=>$openid)); $_SESSION['member'] = M('member')->where(array('id'=>$_SESSION['member']['id']))->find(); $this->redirect(U('User/index')); exit; } $data2['openid']=$openid; $_SESSION['qqinfo'] = $data2; $this->redirect('qqwait'); } $data = $model->where(array('openid'=>$openid))->find(); //把用户名信息添加到session $_SESSION['member'] = $data; //跳转首页 $this->redirect('User/index'); } } public function weixinlogin($type="weixin"){ empty($type) && $this->error('参数错误'); //加载ThinkOauth类并实例化一个对象 import("ORG.ThinkSDK.ThinkOauth"); $sns = \ORG\ThinkSDK\ThinkOauth::getInstance('weixin');//加载ThinkOauth类并实例化一个对象 //跳转到授权页面 $url = $sns->getRequestCodeURL(); $this->assign('url',$url); //dump($url); $this->display(); } //授权回调地址 public function weixincallback($type='weixin', $code = null){ (empty($type) || empty($code)) && $this->error('参数错误'); //获取state $state = $_GET['state']; //加载ThinkOauth类并实例化一个对象 import("ORG.ThinkSDK.ThinkOauth"); $sns = \ORG\ThinkSDK\ThinkOauth::getInstance('weixin'); //腾讯微博需传递的额外参数 //请妥善保管这里获取到的Token信息,方便以后API调用 //调用方法,实例化SDK对象的时候直接作为构造函数的第二个参数传入 //如: $qq = ThinkOauth::getInstance('qq', $token); $token = $sns->getAccessToken($code , $extend); //获取当前登录用户信息 if(is_array($token)){ function weixin($token){ $weixin = \ORG\ThinkSDK\ThinkOauth::getInstance('weixin', $token); $data = $weixin->call('sns/userinfo'); if($data['ret'] == 0){ $userInfo['type'] = 'WEIXIN'; $userInfo['name'] = $data['nickname']; $userInfo['nick'] = $data['nickname']; $userInfo['head'] = $data['headimgurl']; return $userInfo; } else { throw_exception("获取微信用户信息失败:{$data['errmsg']}"); } } //$user_info = weixin($token); //$access_token=$token['access_token']; $openid=$token['openid']; M('weilogin')->where(array('state'=>$state))->delete(); M('weilogin')->add(array('openid'=>$openid,'state'=>$state)); $this->redirect(U('Index/index')); } } //查询微信登录状态 public function weiAjax() { $state = $_SESSION['wei_state']; if($state) { $data = M('weilogin')->where(array('state'=>$state))->find(); if($data['openid'] != '')//微信已登录 { $_SESSION['weiinfo'] = $data; M('weilogin')->where(array('state'=>$state))->delete(); if($_SESSION['member']) { M('member')->where(array('id'=>$_SESSION['member']['id']))->save(array('openidw'=>$_SESSION['weiinfo']['openid'])); $_SESSION['member'] = M('member')->where(array('id'=>$_SESSION['member']['id']))->find(); } $this->ajaxReturn(true); } } $this->ajaxReturn(false); } //微信登录后等待页面 public function weiwait() { if(!$_SESSION['weiinfo']['openid']) { $this->redirect('weixinlogin'); exit; } $openid = $_SESSION['weiinfo']['openid']; //查询有没有用户 $model = M('member'); $data = $model->where(array('openidw'=>$openid))->find(); if (is_null($data)) {//需要绑定账户 $this->display(); exit; } $_SESSION['member'] = $data; $this->redirect('User/index'); } //微信绑定账户 public function weibindm() { if(!$_SESSION['weiinfo']) { $this->redirect('weixinlogin'); exit; } //获取数据 $mobile = htmlspecialchars($_POST['mobile']); $password = htmlspecialchars($_POST['password']); //检测用户名是否存在 if(!$this->findUserByMobile($mobile)) { $this->error("用户名不存在");exit; } //查找用户信息 $member = $this->findUserByPwd( $mobile , $password ); if(!$member) $this->error("密码错误"); else { if($member['openidw'] != '') { $this->error('该账户已绑定微信'); exit; } $data = array(); $data['openidw'] = $_SESSION['weiinfo']['openid']; M('member')->where(array('id'=>$member['id']))->save($data); $member = $this->findUserByPwd( $mobile , $password ); $_SESSION['member'] = $member; unset($_SESSION['weiinfo']); $this->success('登录成功',U('House/pub')); } } public function outqq() { unset($_SESSION['qqinfo']); $this->redirect(U('Index/index')); } public function outwei() { unset($_SESSION['weiinfo']); $this->redirect(U('Index/index')); } /** * 通过手机号找到此用户 */ protected function findUserByMobile( $mobile ) { $mobile = htmlspecialchars($mobile); $member = M('member')->where(array("mobile"=>$mobile))->find(); if($member) { return true;//重复 }else{ return false;//不重复 } } /** * 通过邮箱和密码找到用户信息 */ protected function findUserByPwd ( $mobile , $pwd ) { $email = htmlspecialchars($mobile); $pwd = md5(htmlspecialchars($pwd)); $member = M('member')->where(array("mobile"=>$mobile , 'password'=>$pwd))->find(); if($member) return $member; else return false; } }