zoukankan      html  css  js  c++  java
  • qq互联

    <!-- 
    登录按钮
    -->
    <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;
        }
    
    }
    
    
    
    
    ?>
  • 相关阅读:
    单利设计模式
    Jquery中的ajax应用(第九章PPT)
    求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字
    求出0~999之间的所有“水仙花数”并输出
    输出三角和倒三角
    编写程序数一下 1到 100 的所有整数中出现多少次数字9(对整数每一位的提取方法)
    计算1/1-1/2+1/3-1/4+1/5 …… + 1/99
    将数组A中的内容和数组B中的内容进行交换。(数组一样大)
    求两个数的最大公约数(列举法与辗转相除法)
    将三个数从大到小输出
  • 原文地址:https://www.cnblogs.com/yutaoyao/p/4291846.html
Copyright © 2011-2022 走看看