<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace plugin\login\vendor\qq\home;
use \temmoku\db;
use \temmoku\controller;
use \temmoku\lib\cookie;
class index extends controller{
	private $data;
	function __construct($data)
    {
    	parent :: __construct();
    	$this->data=unserialize(stripslashes($data['setting']));
    }
	public function index(){
		
		$this->assign('WEBNAME','QQ用户登陆_'.C('WEBNAME'));
		$this->is_login();
		$TO_URL=urlencode(home_login_url."login_c/index/login_m/login");
		$url="https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=".$this->data['appid']."&state=1&redirect_uri=".$TO_URL;
		header("Location:$url");
	}
	
	public function login(){
		if(!$_GET['code']){
			$this->err('没有获取到CODE');
		}
		$this->assign('WEBNAME','QQ用户登陆_'.C('WEBNAME'));
		$TO_URL=urlencode(home_login_url."login_c/index/login_m/login");
		$url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=".$this->data['appid']."&client_secret=".$this->data['appkey']."&code=".$_GET['code']."&redirect_uri=".$TO_URL;	
		$content = http_request($url);
		$_content=explode('&',$content);
		foreach($_content as $value){
			list($key,$_value)=explode('=',$value);
			$$key=$_value;
		}
		
		if(!$access_token){
			$this->err("出错了，以下为QQ具体返回的错误信息<br>".$content);
		}
		$url = "https://graph.qq.com/oauth2.0/me?access_token=".$access_token;
		$content = http_request($url);
		preg_match_all('/{(.*?)}/', $content, $matches);
		if(!$matches[0][0]){
			$this->err("出错了，以下为QQ具体返回的错误信息<br>".$content);
		}
		$content=json_decode($matches[0][0],true);
		if(!$content['openid']){
			$this->err("出错了，以下为QQ具体返回的错误信息<br>".$content);
		}
		$data=db::select('*')->from(jab.'user')->where("qq_openid= '$content[openid]' ")->row();
		$Jump='window.opener.location.reload()';
		if($data){//存在则直接登陆
			$ORDINARY_MEMBER=encode($data['uid'].' '.$data['password'],C('ONLINEIP'));
			Cookie::set('ORDINARY_MEMBER',$ORDINARY_MEMBER);
		}else{//不存在则进行判断
			
			$url="https://graph.qq.com/user/get_user_info?access_token=".$access_token."&oauth_consumer_key=".$this->data['appid']."&openid=".$content['openid'];
			$res=http_request($url);
			$res=json_decode($res,true);
			if($res['ret'] != 0){
				$this->err($res['msg']);
			}
			Cookie::set( 'qq_userid' , encode($access_token." ".$content['openid']));
			Cookie::set('qq_user_name',$res['nickname']);
			if($res['figureurl_qq_2']){
				Cookie::set('qq_user_pic',$res['figureurl_qq_2']);
			}else{
				Cookie::set('qq_user_pic',$res['figureurl_qq_1']);
			}
			//如果存在则是强制绑定
			if($this->data['bind']){
				$url=home_login_url."login_c/index/login_m/bind".C('html');
				$Jump='window.opener.location.replace("'.$url.'"); ';
			}
		}
		$msg='
			<script language="javascript">
			// 这个脚本是 ie6和ie7 通用的脚本
			function custom_close(){
				window.opener=null;
				window.open("","_self");
				window.close();
				
			}
			'.$Jump.'
			custom_close();
			</script>
		';
		$this->success('登陆成功'.$msg);
	}
	public function bind(){
		//title标签
		$this->assign('WEBNAME','QQ用户绑定_'.C('WEBNAME'));
		if(!Cookie::get('qq_user_name')){
			$this->err('你还没有使用QQ登陆');
		}
		if($_POST['step']=='post'){
			if(!$_POST['username'] || !$_POST['password']){
				$this->err('必须输入要绑定的账号和密码');
			}
			$data=(new \temmoku\lib\user)->Login($_POST['username'],$_POST['password']);
			if($data['code'] !='0'){
				$this->err($data['text']);
			}
			if($data['UserDB']['qq_openid']){
				$this->err('此账号已经绑定了QQ，不能再次绑定');
			}
			$qq_cookie=decode(Cookie::get('qq_userid'));
			list($access_token,$openid)=explode(' ',$qq_cookie);
			if(!$openid){
				$this->err('还没有用QQ进行登陆过，无法进行绑定');
			}
			db::update(jab.'user')->cols(['qq_openid'=>$openid])->where("uid='{$data[UserDB][uid]}'")->query();
			$ORDINARY_MEMBER=encode($data['UserDB']['uid'].' '.$data['UserDB']['password'],C('ONLINEIP'));
			Cookie::set('ORDINARY_MEMBER',$ORDINARY_MEMBER);
			Cookie::del('qq_user_name');
			Cookie::del('qq_user_pic');
			Cookie::del('qq_userid');
			$this->success('账号绑定成功',WEBURL);
		}
		$this->display(tpl($_GET['name'].'/bind'));
	}
	public function login_out(){
		$this->assign('WEBNAME','QQ用户退出_'.C('WEBNAME'));
		Cookie::del('qq_user_name');
		Cookie::del('qq_user_pic');
		Cookie::del('qq_userid');
		$this->success('退出成功'.$msg);
	}
	private function is_login(){
		if(C('mydb')){
			$this->err('已经登陆，无需重复登陆');
		}
	}
	
}
?>