<!-- 
登录按钮
-->
<a href="https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id={$qq_appid}&redirect_uri=http://{$_SERVER[\'HTTP_HOST\']}{:U(\'Index/login\')}&scope=get_user_info"><img src="http://qzonestyle.gtimg.cn/qzone/vas/opensns/res/img/Connect_logo_7.png" alt="" /></a>
                                
<?php 

public function login(){
    if($_GET[\'clear_qq\']){
        session(\'qq_openid\',null);
        session(\'access_token\',null);
        session(\'refresh_token\',null);
        header("location:".U(\'Index/login\'));
        exit();
    }


    if(IS_AJAX){
        $User_mod= M(\'users\');
        $client_ip=get_client_ip();
        if($_POST[\'ac\']=="check_username"){
            $map[\'username\']=array(\'eq\',$_POST[\'username\']);
            $user=$User_mod->where($map)->find();
            if($user){
                if($user[\'state\']==1){
                    session($client_ip.$_POST[\'username\'],null);
                    $this->ajaxReturn(1,"用户名存在!",1);
                }else{
                    $this->ajaxReturn(3,"账户异常、被限制登录,请联系客服!",3);
                }
            }else{
                $this->ajaxReturn(2,"用户名不存在!",2);
            }
        }else if($_POST[\'ac\']=="do_login"){
            if($_POST[\'username\']==null or $_POST[\'password\']==null ){
                $this->ajaxReturn(0,"用户名或密码为空",0);
            }
            if($_POST[\'verify\']==null ){
                $this->ajaxReturn(4,"请输入验证码",4);
            }
            if(md5($_POST[\'verify\'])!=$_SESSION[\'verify\']){
                $this->ajaxReturn(8,"验证码错误",8);
            }
            $map[\'username\']=array(\'eq\',$_POST[\'username\']);
            $user=$User_mod->where($map)->find();
            if($user){
                if($user[\'state\']==1){
                    if(md5($_POST[\'password\'])==$user[\'password\']){
                        if($_POST[\'save_pw\']){
                            setcookies(\'uid\', $user[\'id\'],604800);
                        }else{
                            setcookies(\'uid\', $user[\'id\']);
                        }

                        if($_SESSION[\'qq_openid\'] && $_SESSION[\'qq_access_token\'] && $_SESSION[\'qq_refresh_token\']){
                            if(update_user_qq_token($user[\'id\'])){
                                $this->ajaxReturn($user[\'username\'],"绑定成功!",5);
                            }else{
                                $this->ajaxReturn($user[\'username\'],"绑定失败!",6);
                            }
                        }else{
                            $this->ajaxReturn($user[\'username\'],"登录成功!",5);
                        }

                    }else{
                        $this->ajaxReturn($user[\'username\'],"密码错误!",6);
                    }
                }else{
                     $this->ajaxReturn(3,"账户异常、被限制登录,请联系客服!",3);
                }
            }else{
                $this->ajaxReturn(2,"用户名不存在!",2);
            }
        }else{
            $this->ajaxReturn(0,"非法请求!",0);
        }
    }else{
        if($this->userinfo){
            $this->error("已经登录无需登录",U(\'Index/index\'));
        }else{
            $this->assign(\'qq_appid\',C(\'qq_appid\'));
            if($_SESSION[\'qq_openid\'] && $_SESSION[\'qq_access_token\']){
                $user_qq_info=get_qq_info($_SESSION[\'qq_openid\'],$_SESSION[\'qq_access_token\'],C(\'qq_appid\'));
                $this->assign(\'nickname\',$user_qq_info[\'nickname\']);
                $this->assign(\'img\',$user_qq_info[\'figureurl\']);
                $this->assign(\'qq_appid\',C(\'qq_appid\'));
                $this->display(\'link_qq\');
                exit();
            }

            if ($_GET [\'code\']) {  //qq登录
                $res_qq_login=$this->qq_login($_GET [\'code\']);
                if($res_qq_login[\'retcode\']){
                    $this->success("登陆成功!",U(\'Index/index\'));
                }else{
                    session(\'qq_openid\',$res_qq_login[\'openid\']);
                    session(\'qq_access_token\',$res_qq_login[\'token\'][\'access_token\']);
                    session(\'qq_refresh_token\',$res_qq_login[\'token\'][\'refresh_token\']);
                    $this->success("第一次使用 ,请绑定用户!",U(\'Index/login\'));
                }
            }else{
                if(is_mobile()){
                    $this->display(\'m_login\');
                }else{
                    $this->display();
                }
                }
            }



    }

}




private function qq_login($code){
    $_SESSION[\'qq_login_state\'] = md5(uniqid(rand(), TRUE));
    $redirect_uri=urlencode("http://{$_SERVER[\'HTTP_HOST\']}");
    $url_get_access_token = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=".C(\'qq_appid\')."&client_secret=".C(\'qq_appsecret\')."&code={$code}&state={$_SESSION[\'qq_login_state\']}&redirect_uri=".$redirect_uri;
    $res_access_token=https_request ( $url_get_access_token );
    $arr_access_token=get_parse($res_access_token);
    $url_openid= "https://graph.qq.com/oauth2.0/me?".$res_access_token;
    $re_openid = https_request ( $url_openid );
    if (preg_match ( \'/\"openid\":\"(\w+)\"/i\', $re_openid, $match )) {
        $qq_openid = $match [1];
    }
    $map_user[\'qq_openid\']=array(\'eq\',$qq_openid);
    $user=M(\'users\')->where($map_user)->find();
    if($user){
        setcookies(\'uid\', $user[\'id\'],604800);
        update_qq_access_token_data($qq_openid,$arr_access_token);
        $data[\'retcode\']=1;
        $data[\'id\']=$user[\'id\'];
        return $data;
    }else{
        $data[\'openid\']=$qq_openid;
        $data[\'token\']=$arr_access_token;
        return $data;
    }

}




?>

 

版权声明:本文为yutaoyao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yutaoyao/p/4291846.html