TP QQ 微信 微博登录

use Org\Util\QQconnect;
use Org\Util\Wechatauth;
use Org\Util\SaeTOAuthV2;
use Org\Util\SaeTClientV2;

 

  switch ($_GET['type']) {
            /* QQ互联登录 */
            case 'qq':
                $app_id = C('QQ_AUTH.APP_ID');
                $scope = C('QQ_AUTH.SCOPE');
                $callback = C('QQ_AUTH.CALLBACK');
                $sns = new QQConnect;
                $sns->login($app_id, $callback, $scope);
                break;
            /* 新浪微博登录 */
            case 'sina':
                $app_id = C('SINA_AUTH.APP_ID');
                $app_key = C('SINA_AUTH.APP_KEY');
                $callback = C('SINA_AUTH.CALLBACK');
                $o = new SaeTOAuthV2($app_id,$app_key);
                $code_url = $o->getAuthorizeURL($callback);
                if($code_url){
                    header('Location:'.$code_url);
                }else{
                    $this->error('授权失败','/Login/index');
                }
                break;
            /* 微信扫码登陆 */
            case 'wechat':
                $app_id = C('WECHAT_AUTH.APP_ID');
                $sid  = session_id();
                $options = array(
                    'account'=>$sid,
                    'datapath'=>'../data/cookiecode_',
                    'debug'=>true,
                    'logcallback'=>'logdebug'
                );
                $wechat = new Wechatauth($options);

                if (isset($_POST['code'])) {
                    $logincode = $_POST['code'];
                    $vres = $wechat->set_login_code($logincode)->verify_code();
                    if ($vres===false) {
                        $result = array('status'=>0);
                    } else {
                        $result = array('status'=>$vres);
                        if ($vres==200) {
                            $result['info'] = $wechat->get_login_info();
                            $result['cookie'] = $wechat->get_login_cookie(true);
                        }
                    }
                    die(json_encode($result));
                }
                $wechat->get_login_code($app_id);//跳转登录
                $wechat->get_code_image();
//                var_dump($logincode);
//                echo ($qrimg);
                //$this->
                break;
            /* 支付登陆 */
            case 'pay':
                $mobile = trim(I('post.mobile'));
                $verifyCode = trim(I('post.verify_code'));
                if(!$mobile||!$verifyCode){$this->error('手机号或验证码不能为空~!');exit;}

                $token = $this->token;
                $url = $this->api_url.'/user/reqUserLoginMsg';//登录
                $method = 'POST';

                $data['token'] = $token;
                $data['mobile'] = $mobile;
                $data['rand_code'] = $verifyCode;
                $data['deviceToken'] = 'PC';
                $data['timestamp'] = time();

                //加密转码
                $data = ENCRYPT($data);
                //获取数据
                $result = CURL($url,$method,$data);

                if($result['resultCode']==200){
                    //登录成功,更新token
                    $_SESSION['login_token'] = $result['resultInfo']['token'];
                    $_SESSION['head_img'] = $result['resultInfo']['head_img'];
                    $_SESSION['nickname'] = $result['resultInfo']['nickname'];
                    $this->redirect('/Pay/payList');
                }else{
                    $this->error($result['resultMsg']);
//                    echo $result['resultMsg'];
                }
                break;
            /* 默认无登录 */
            default:
                $mobile = trim(I('post.mobile'));
                $verifyCode = trim(I('post.verify_code'));
                if(!$mobile||!$verifyCode){$this->error('手机号或验证码不能为空~!');exit;}

                $token = $this->token;
                $url = $this->api_url.'/user/reqUserLoginMsg';//登录
                $method = 'POST';

                $data['token'] = $token;
                $data['mobile'] = $mobile;
                $data['rand_code'] = $verifyCode;
                $data['deviceToken'] = 'PC';
                if(trim(I('get.uid')))$data['uid'] = trim(I('get.uid'));
                $data['timestamp'] = time();

                //加密转码
                $data = ENCRYPT($data);
                //获取数据
                $result = CURL($url,$method,$data);

                if($result['resultCode']==200){
                    //登录成功,更新token、头像、昵称
                    $_SESSION['login_token'] = $result['resultInfo']['token'];
                    $_SESSION['head_img'] = $result['resultInfo']['head_img'];
                    $_SESSION['nickname'] = $result['resultInfo']['nickname'];
                    $this->redirect('Course/index');
                }elseif($result['resultCode']==305) {
                    session(null);
                    $this->error('无此权限','/Course/index');
                }else{
                    $this->error($result['resultMsg']);exit;
                }
                break;
        }
//        var_dump($result);
    }
/*
      * 互联登录返回信息
      * 获取code 和 state状态,查询数据库
      *  */
    public function callback() {
        switch ($_GET['type']) {
            /* 接受QQ互联登录返回值 */
            case 'qq':
                empty($_GET['code']) && $this->error("无效的第三方方式",U('/Login/index'));
                $app_id = C('QQ_AUTH.APP_ID');
                $app_key = C('QQ_AUTH.APP_KEY');
                $callback = C('QQ_AUTH.CALLBACK');
                $qq = new QQConnect;
                /* callback返回openid和access_token */
                $back = $qq->callback($app_id, $app_key, $callback);
                //防止刷新
                empty($back) && $this->error("请重新授权登录",U('/Login/index'));
                //此处省略数据库查询,查询返回的$back['openid']
                $openId = $back['openid'];
                $token = $back['token'];

                //获取用户信息
                $userInfo = $qq->get_user_info($app_id,$token,$openId);
                //失败判断
                empty($userInfo) && $this->error("获取信息失败,重新授权",U('/Login/index'));

              
                //var_dump($userInfo);
                break;

            /* 接受新浪微博登录返回值     */
            case 'sina':
                empty($_GET['code']) && $this->error("无效的第三方方式",U('/Login/index'));
                $app_id = C('SINA_AUTH.APP_ID');
                $app_key = C('SINA_AUTH.APP_KEY');
                //$scope = C('SINA_AUTH.SCOPE');
                $callback = C('SINA_AUTH.CALLBACK');

                /* 组建数组 传递新浪 */
                $arr['code'] = $_GET['code'];
                $arr['redirect_uri'] = $callback;

                $weibo = new SaeTOAuthV2($app_id,$app_key);
                /*  */
                $back = $weibo->getAccessToken('code',$arr);
                //array(4) { ["access_token"]=> string(32) "2.00SzTCnD0az6hG412356f4f506JCuO" ["remind_in"]=> string(9) "157679999" ["expires_in"]=> int(157679999) ["uid"]=> string(10) "3473030892" }
                //var_dump($back);die();

                /*
                * follow_by_id 关注一个用户
                * account_profile_basic 获取用户基本信息
                * show_user_by_id 获取用户资料
                *
                */
                $w = new SaeTClientV2($app_id,$app_key,$back['access_token']);
                $userInfo = $w->show_user_by_id($back['uid']);

                //失败判断
                empty($userInfo) && $this->error("获取信息失败,重新授权",U('/Login/index'));

                //var_dump($userInfo);die();
                $nickname = $userInfo['name'];
                $head_img = $userInfo['profile_image_url'];//的头像

              
                break;
            case 'wechat':
               //获取access_token
                $app_id = C('WECHAT_AUTH.APP_ID');
                $app_key = C('WECHAT_AUTH.APP_KEY');
                $code = trim(I('get.code'));
                if(!$code)$this->error("无效的第三方方式",U('/Login/index'));
                $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$app_id."&secret=".$app_key."&code=".$code."&grant_type=authorization_code";

                $result = CURL($url,'GET');
                empty($result) && $this->error("获取信息失败,重新授权",U('/Login/index'));

                $access_token = $result['access_token'];
                $openId = $result['openid'];

                $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openId;
                $userInfo = CURL($url,'GET');
                empty($userInfo) && $this->error("获取信息失败,重新授权",U('/Login/index'));

                //var_dump($userInfo);die();
         
                break;
            /* 默认错误跳转到登录页面  */
            default:
                $this->error("无效的第三方方式",'/Login/index');
                break;
        }
    }

  类文件打包下载:

https://pan.baidu.com/s/1slH1VEt

posted on 2016-07-21 19:53  studyphp  阅读(1488)  评论(0编辑  收藏  举报

导航