欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

thinkcmf5 QQ互联

程序员文章站 2024-02-23 14:10:40
...

1.前端:

<a href="{:url('user/login/qqLogin')}">QQ登录</a> 

2.后台:

    # qq登录
    public function qqLogin()
    {
        import('qqAuth.qqConnectAPI',EXTEND_PATH);
        $qc= new \QC();
        $qc->qq_login(); 
        exit;
    }
    # qq登录回调
    public function qqGoback()
    {
        import('qqAuth.qqConnectAPI',EXTEND_PATH);
        $qc = new \QC();
        $access_token = $qc->qq_callback(); // 获取access_token
        $open_id = $qc->get_openid();
        //判断用户是否存在
        $user = Db::name('user')->where(['qq_open_id'=>$open_id])->find();
        if ($user) {
            session('user',$user);
            Db::name('user')->where('id',cmf_get_current_user_id())->update(['last_login_time'=>time(),'last_login_ip'=>get_client_ip(0, true)]);
            Db::name('user_login_log')->insert(['user_id'=>cmf_get_current_user_id(),'ip'=>get_client_ip(),'create_time'=>time(),'type'=>3]);
        }else{
            $qc = new \QC($access_token,$open_id);
            $user_info = $qc->get_user_info(); 
            if($user_info && is_array($user_info)){
                $data['qq_open_id'] = $open_id;
                $data['login_type'] = 3;
                $data['user_type'] = 2;
                $data['user_nickname'] = $user_info['nickname'];
                $data['avatar'] = $user_info['figureurl_qq_2'];
                $data['create_time'] = time();
                $id = Db::name('user')->insertGetId($data);
                $u = Db::name('user')->where(['id'=>$id])->find();
                session('user',$u);
                Db::name('user')->where('id',cmf_get_current_user_id())->update(['last_login_time'=>time(),'last_login_ip'=>get_client_ip(0, true)]);
                Db::name('user_login_log')->insert(['user_id'=>cmf_get_current_user_id(),'ip'=>get_client_ip(),'create_time'=>time(),'type'=>3]);
            }
        }
        echo"<script>history.go(-2);</script>";

    }

3:官方sdk下载配置成功后  放在simplewind/extend/ 目录下即可