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); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | /* * 互联登录返回信息 * 获取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 ; } } |
类文件打包下载:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术