第三方登录(微信,QQ,微博,Line,Twitter,支付宝,Google)

  /**
     * $name 名称
     * (weixin,qq,weibo,alipay,facebook,twitter,line,google)
     * 微信扫码登录用的是(微信公众平台)
     */
    public function login($name)
    {
        global $myviewwebsite;
        //获取配置
        $config = D('Api/Getplugmsg')->plug_config_get(31);
        $config = $config['plug_config']['31'];
        if ($config['line_login'] == 1){
            $config['scope'] = 'profile';
            //设置回跳地址
            $config['callback'] = "https://******.com/Home/Line_login/callback?http_website_domain_name=$myviewwebsite";

            //可以设置代理服务器,一般用于调试国外平台
            // $config['proxy'] = 'http://127.0.0.1:1080';
            /**
             * 对于微博,如果登录界面要适用于手机,则需要设定->setDisplay('mobile')
             * 对于微信,如果是公众号登录,则需要设定->setDisplay('mobile'),否则是WEB网站扫码登录
             * 其他登录渠道的这个设置没有任何影响,为了统一,可以都写上
             */
            if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])){
                return redirect(OAuth::$name($config)->getRedirectUrl());
            }else{
                return redirect(OAuth::$name($config)->setDisplay('mobile')->getRedirectUrl());
            }
        }else{
            self::showJson(array('status'=>0,'msg'=>'插件未启用'));
        }

    }

    public function callback()
    {
        global $myviewwebsite;
        //获取配置
         $config = D('Api/Getplugmsg')->plug_config_get(31);
         $config = $config['plug_config']['31'];

        //设置回跳地址
        $config['scope'] = 'profile';
        $config['code'] = $_GET['code'];
        //设置回跳地址
        $config['callback'] = "https://*****/Home/Line_login/callback?http_website_domain_name=$myviewwebsite";
        //获取格式化后的第三方用户信息
        $snsInfo = OAuth::line($config)->userinfo();
        $code = rand(0, 10000) . time();
        $third_login = array(
            'openid' => $snsInfo['openid'],
            'third_type' => $snsInfo['channel'],
            'nick' => $snsInfo['nick'],
            'avatar' => $snsInfo['avatar'],
            'code' => $code,
            'login_web'=>$myviewwebsite,
            'msg'=>json_encode($snsInfo)
        );
        $type = $snsInfo['channel'];
        if ($snsInfo) {
            M('third_login')->add($third_login);
            $state_type['login_web'] = $myviewwebsite;
            return redirect("http://{$state_type['login_web']}/public/login-success?code=$code&type=$type");
        }

    }

    /**
     * 判断状态
     */
    public function codelogin(){

        $code =self::$postget['code'];
        // dump($code);
        $codedata=M('third_login')->where(array("code"=>$code,'status'=>0))->field('id,msg,login_web')->find();
        $codedata['msg'] = json_decode($codedata['msg'],true);
        if(!empty($codedata['msg']['nick'])){ //根据用户是否已经登录
            $savedata=array();
            $codedata_username=rand(100000,999999);
            if(strtolower($codedata['msg']['channel'])=='line' && !empty($codedata['msg']['openid'])){
                $codedata_username=$codedata['msg']['openid'];
                $savedata['linenick']=$codedata['msg']['nick'];
                $savedata['linetoken']=$codedata['msg']['openid'];
                $savedata['lineavatar']=$codedata['msg']['avatar'];
                $where=array('linetoken'=>$savedata['linetoken']);
                $userMsg=D('User')->finduser($where);
                $plug_id=31;
                $plug_str='line_login';
                // $uid=getMsg  uidGetUserMsg
            }elseif (strtolower($codedata['msg']['channel'])=='weixin' && !empty($codedata['msg']['openid'])){
                $codedata_username=$codedata['msg']['openid'];
                $savedata['linenick']=$codedata['msg']['nick'];
                $savedata['linetoken']=$codedata['msg']['unionid'];
                $savedata['lineavatar']=$codedata['msg']['avatar'];
                $where=array('linetoken'=>$savedata['linetoken']);
                $userMsg=D('User')->finduser($where);
                $plug_id=28;
                $plug_str='wechat_login';
            }
            $configmsg_all=D("Api/Getplugmsg")->plug_config_get($plug_id);
            $ifcanuseplug=$configmsg_all['plug_config'][$plug_id][$plug_str];
            if($ifcanuseplug==1){
                if(!empty(self::$userInfo['id'])){ //用户已经登录了 绑定账户
                    $user_id=self::$userInfo['id'];
                    if(!empty($userMsg['uid'])){
                        $usermsg_uid=$userMsg['uid'];
                    }else{
                        $usermsg_uid=$user_id;
                    }
                    $savedata['id']=$user_id;
                    if($user_id==$usermsg_uid){

                        $upRes=D('User')->updateUserMsg($savedata);
                        $userMsg=$upRes['userinfo'];
                    }
                }else if(empty($userMsg)){ //用户注册
                    $savedata['avatar']=$codedata['msg']['avatar'];
                    // $savedata['username']=$codedata_username;
                    $savedata['username']=$codedata['msg']['nick'];
                    $savedata['nickname']=$codedata['msg']['nick'];
                    $register_res=D("User")->register($savedata,0);
                    $userMsg=$register_res['userinfo'];
                }else if(empty($userMsg['usertoken'])){
                    $showdata['status']=0;
                    $showdata['msg']='未知错误';
                    self::showJson($showdata); exit;
                }
                $userMsg=deal_image($userMsg,'avatar');
                $login_obj=A("login");
                if($userMsg['status']==1){
                    //将 code整失效
                    M('third_login')->where(array('id'=>$codedata['id']))->setField("status",1);
                    $userMsg['uid'] = $userMsg['id'];
                    self::$postget['name']=$userMsg['username'];
                    $login_obj->login_lost(2);
                    $showdata['status']=1;
                    $showdata['msg']="登录成功";
                    $showdata['usertoken']=$userMsg['usertoken'];
                    $showdata['userMsg']=$userMsg;
                    $showdata['savedata']=$savedata;
                }else if($userMsg['status']==2){
                    self::$postget['name']=$userMsg['username'];
                    $login_obj->login_lost(0);
                    $showdata['status']=0;
                    $showdata['msg']='登录失败,您的账号被禁用了,请联系管理员';
                    $showdata['savedata']=$savedata;
                }else{
                    self::$postget['name']=$userMsg['username'];
                    $login_obj->login_lost(0);
                    $showdata['status']=0;
                    $showdata['msg']='登录失败,您的账号和密码不正确';
                    $showdata['userMsg']=$userMsg;
                    $showdata['savedata']=$savedata;
                }
                $showdata['return_url']=$configmsg_all['plug_config'][$plug_id]['return_url'];
            }else{
                $showdata['status']=0;
                $showdata['msg']='登录失败,插件不可用';
                $showdata['savedata']=$savedata;
                $showdata['userMsg']=$userMsg;
            }
        }else{
            $login_obj=A("login");
            $login_obj->login_lost(0);
            $showdata['status']=0;
            $showdata['msg']='登录失败,您的code值不正确';
        }
        self::showJson($showdata); exit;
    }
//git地址
https://github.com/xinmokeji/sns_helper/tree/876f54b9bdfc0e630136d300621b664dd3f8e377

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值