<?php
class taobaologinControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
		$this->loadModel(array("user","invite"));
	}
	
	public function onGeturl()
	{
	   require_once(ROOT_PATH."config/taobao_config.php"); 
	   $login_url = "https://oauth.taobao.com/authorize?response_type=code&client_id={$tao_appconfig[0][0]}&redirect_uri=http://".$_SERVER['HTTP_HOST']."/index.php?m=taobaologin";
		header("Location:$login_url");
		exit();
	}
	
	public function onDefault()
	{
		 require_once(ROOT_PATH."config/taobao_config.php"); 
		 $code=get('code');
		 $grant_type = 'authorization_code';
		 $redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].'/index.php?m=taobaologin';  //此处回调url要和后台设置的回调url相同
		 $client_id = $tao_appconfig[0][0];//自己的APPKEY
		 $client_secret = $tao_appconfig[0][1];//自己的appsecret
		 
		 //请求参数
		 $postfields= array('grant_type'     => $grant_type,
		                     'client_id'     => $client_id,
		                     'client_secret' => $client_secret,
		                     'code'          => $code,
		                     'redirect_uri'  => $redirect_uri
		 );
		 
		 $url = 'https://oauth.taobao.com/token';
		 $response=$this->curl($url,$postfields);
		  
		 $token = json_decode($response);
		 $array['nick']=urldecode($token->taobao_user_nick);
		 $array['userid']=$token->taobao_user_id;
		if(!$array['nick'] || !$array['userid']) $this->gomsg('淘宝接口错误','/index.php?m=index');;
		$nickname=$array['nick']; 
		if($user=$this->user->getRow("SELECT *  FROM ".table('user')." WHERE openid='".$array['userid']."' AND xfrom='taobao' "))
		{
			$_SESSION['ssuser']=$user;
			$this->gomsg('登陆成功','/index.php');
		}else
		{
			//生成账户
			$i=0;
			$u=$nickname;
			while($this->user->getOne("SELECT userid FROM ".table('user')." WHERE  nickname='$u' "))
			{
				$i++;
				$u=$nickname.$i;
			}
			
			//关联插件
			$data=array(
				"nickname"=>$u,
				"xfrom"=>'taobao',
				"openid"=>$array['userid'],
				"dateline"=>time(),
				"lastfeed"=>time(),
			);
			if(isset($_COOKIE['invite_uid'])){
				$data['invite_userid']=intval($_COOKIE['invite_uid']);
			}
			$userid=$this->user->insert($data);
			$this->loadControl("inviteapi");
			$this->inviteapiControl->invite_reg($userid);
			$_SESSION['ssuser']=$this->user->getRow("SELECT * FROM ".table('user')." WHERE userid='$userid' ");
			//账号绑定处理
			$this->gourl("/index.php?m=register&a=OpenReg");
			//$this->gomsg('注册登陆成功','/index.php');
		}
	}


	public function curl($url, $postFields = null)
	{
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_FAILONERROR, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		 
		if (is_array($postFields) && 0 < count($postFields))
		{
			$postBodyString = "";
			foreach ($postFields as $k => $v)
			{
				$postBodyString .= "$k=" . urlencode($v) . "&"; 
			}
			unset($k, $v);
			curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);  
 			curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
		}
		
		$reponse = curl_exec($ch);
		 
		
		curl_close($ch);
		
		return $reponse;
	}

}
?>