<?php
/**
 *  作者：梦雨
 *  @ QQ50361804
 */
namespace app\index\controller;
use think\Controller;
use think\Db;
use qqconnect\QC;
class Qq extends Controller
{
 
    protected function _initialize()
    {
        parent::_initialize();
        $config['appid']       = config('qq.qq-appid');
        $config['appkey']       = config('qq.qq-appkey');
        $config['callback']       = config('qq.qq-callback');
        $config['scope']       = 'get_user_info,add_share,list_album,add_album,upload_pic,add_topic,add_one_blog,add_weibo,check_page_fans,add_t,add_pic_t,del_t,get_repost_list,get_info,get_other_info,get_fanslist,get_idolist,add_idol,del_idol,get_tenpay_addr';
        $config['errorReport'] = true;
        $this->config          = $config;
        session('qqconnect', $config);
    }
    public function qqcb()
    {
        $qc           = new QC();
        $askurl       = getenv("HTTP_REFERER");
        $access_token = $qc->qq_callback(); // openid
        $openid       = $qc->get_openid(); // openid
        $qc           = new QC($access_token, $openid);
        $info         = $qc->get_user_info(); // access_token
        if ($info['ret'] < 0) {
            $this->error('获取用户信息失败');

        } else {

            $uid = session('userid');

            if ($uid > 0) {
                //绑定的动作
                $map['openid'] = $openid;
                $m             = Db::name('qqconnect')->where($map)->find();
                if (!empty($m)) {
                    $_openid = Db::name('member')->find($uid);
                    $hasopenid = Db::name('member')->where('openid',$openid)->count();
                    if (!$_openid['openid']&&$hasopenid==0) {
                        Db::name('member')->where('userid', $uid)->setField('openid', $openid);
                    }else{
                        $this->error('该QQ已经在本站被绑定过');
                    }
                } else {
                    $data['head']      = $info['figureurl_2'];
                    $data['nickname']  = $info['nickname'];
                    $data['status']    = 1;
                    $data['errorcode'] = 0;
                    $data['add_time']  = time();
                    $data['openid']    = $openid;
                    $data['uid']       = $uid;
                    Db::name('qqconnect')->insert($data);
                    Db::name('member')->where('userid', $uid)->setField('openid', $openid);
                }
                $this->success('绑定成功', 'bbs/user/set');
            } else {
                $map['openid'] = $openid;
                $m             = Db::name('qqconnect')->where($map)->find();
                if (!empty($m)) {
                    $usermap['userid'] = $m['uid'];

                    $_openid = Db::name('member')->where('userid',$m['uid'])->value('openid');
                    if (!$_openid) {
                        Db::name('member')->where('userid', $m['uid'])->setField('openid', $openid);
                    }


                } else {

                    if (empty($info['figureurl_2']) || $info['figureurl_2'] == '') {
                        $info['figureurl_2'] = $info['figureurl_qq_1'];
                    }
                    $data['head']      = $info['figureurl_2'];
                    $data['nickname']  = $info['nickname'];
                    $data['status']    = 1;
                    $data['errorcode'] = 0;
                    $data['add_time']  = time();
                    $data['openid']    = $openid;
                    $data1['username'] = $info['nickname'];
                         
                  
                    $count             = Db::name('member')->where($data1)->count();
                    if ($count > 0) {
                        $data1['username'] = $info['nickname'] . rand(10,100);
                    }
                    $data1['password'] = 0;
                    $data1['userip']   = $this->request->ip();
                  
                    $data1['userhead'] = $info['figureurl_2'];
                    $data1['regtime']  = time();
                    $data1['status']   = 1;
                    $userid                = Db::name('member')->insertGetId($data1);
                    $data['uid']       = $userid;
                    Db::name('qqconnect')->insert($data);
                    $usermap['userid'] = $userid;
                    Db::name('member')->where('userid', $uid)->setField('openid', $openid);
                }
                $user = Db::name('member')->where($usermap)->find();
                if ($user['userhead'] == '') {
                    $user['userhead'] = '/public/img/touxiang.jpg';
                }
                session('userstatus', $user['status']);
                session('userhead', $user['userhead']);
                session('username', $user['username']);
                session('userid', $user['userid']);
                session('point', $user['point']);
                Db::name('member')->update(
                    [
                        'last_login_time' => time(),
                        'last_login_ip'   => $this->request->ip(),
                        'userid'              => $user['userid'],
                    ]
                );
               // $this->success('登录成功', session('callbackurl'));
               $this->success('登录成功', 'qq/qqlogo');
            }

        }

    }
	public function unbind(){
		
		$data=request()->param();
		
		$uid=session('userid');
		$info=Db::name('member')->where('userid',$uid)->find();
		if($info['password']==0){
			return array('code'=>0,'msg'=>'请先设置密码，然后再解除绑定');
		}else{
			$pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
			
			
			if(!preg_match( $pattern, $info['usermail'] )){
				return array('code'=>0,'msg'=>'请先设置邮箱，然后再解除绑定');
			}else{
				$map['openid']=$data['openid'];
				Db::name('qqconnect')->where($map)->setField('uid',0);
				return array('code'=>200,'msg'=>'解除绑定成功');
			}
			
			
		}
		
		
		
	}
    public function qqlogo()
    {
    echo'<script type="text/javascript">
	window.opener.location.href = window.opener.location.href;
    window.close();
    </script>';
      
    }
    public function qqlogin()
    {
        $qc = new QC();
        $this->redirect($qc->qq_login());
        $datac = $this->request->post(); 
    }
}
