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;
        }
    }

  類文件打包下載:php

https://pan.baidu.com/s/1slH1VEt數據庫

相關文章
相關標籤/搜索