<?php
class taoControl extends skymvc{
	public function __construct(){
		
		parent::__construct();
		$this->loadClass("taobao");
		require_once(ROOT_PATH."config/taobao_config.php");
		$this->loadClass("image"); 
		if(!in_array(strtolower(get('a')),array('getcomment','get','randstole','sold_out'))){
			if(empty($_SESSION['ssadmin'])){
				$this->gourl("".APPADMIN."?m=admin&a=login");	
			}
		}
		$this->taobao->setConfig($tao_appconfig);
		$this->loadModel(array("category","product","stole","product_data"));
	}
	/*自动采集*/
	public function onRandStole(){
		
		$data=$this->category->selectCols(array(
			"where"=>"status=1 AND is_stole=1",
			"fields"=>"id"
		));
		if(empty($data)) exit('暂无分类');
		shuffle($data);
		$row=$this->category->selectRow(array("where"=>"id=".$data[0]));
		if(empty($row['tags'])) exit('关键词缺少');
		$tags=explode("\n",str_replace("\r\n","\n",str_replace(" ","",$row['tags'])));
		shuffle($tags);
		
		$_GET['catid']=$row['id'];
		$_GET['keyword']=$tags[0];
		$this->onStole();
	}
	//淘宝搜索页面采集
	public function onStole(){
				
				$keyword=get('keyword','h');
				$page=intval(get('page')); 
				$start=$page*40;
				$catid=get('catid','i');
				if(empty($keyword) or $page>3 or $catid==0){
					$this->smarty->assign("catlist",$this->category->select(array("where"=>"is_stole=1 AND status=1")));
					$this->smarty->display("stole.html");
					exit; 
				}
				
				$arr=$this->taobao->getUrl($keyword,$start);
				 
				if(empty($arr) ){
					$this->smarty->assign("catlist",$this->category->select(array("where"=>"is_stole=1 AND status=1")));
					$this->smarty->display("stole.html");
					exit;
				}
				foreach($arr as $v){
					if(!$this->stole->getOne("SELECT id FROM ".table('stole')." WHERE goodsurl='$v' ")){
						$data['goodsurl']=$v;
						$data['keyword']=$keyword;
						$data['dateline']=time();
						$data['catid']=$catid;
						$this->stole->insert($data);
						
					}
				}
				$page=$page+1;
				echo "<script>location.href='/index.php?m=tao&a=stole&keyword=$keyword&page=$page&catid=$catid';</script>";
	}
	
	public function onGetByurl(){
		$goodsurl=get('goodsurl');
		$data=$this->taobao->getDetail($goodsurl);
		if(!isset($data['click_url'])) exit(json_encode($data));
		if(get('getImg')){
			$img=new image();
			$this->loadClass("upload");
			$id=get('id','i');
			$dir="attach/product/".$this->upload->dirId($id);
			umkdir($dir);
			$imgurl=$dir.time().md5($data['pic_url']).".jpg";
			if(defined("SAE_GET") && SAE_GET){
				app_file_put_contents($imgurl,curl_get_contents(SAE_GET.urlencode($data['pic_url'])));
			}else{
				app_file_put_contents($imgurl,curl_get_contents($data['pic_url']));
			}
			$c=getimagesize($imgurl);			 
			if(!isset($c[0])) exit(json_encode(array("error"=>2,"message"=>"淘宝图片禁止访问，请手动上传")));
			$img->makethumb($imgurl.".100x100.jpg",$imgurl,"100","100",1);
			$img->makethumb($imgurl.".small.jpg",$imgurl,"240");
			$img->makethumb($imgurl.".middle.jpg",$imgurl,"440");
			$data['imgurl']=$imgurl;
		}
		preg_match_all("/<img.*src=[\'\"](.*)[\'\"].*[^>]>/iUs",$data['content'],$im);							
							$c=preg_replace("/<table.*<\/table>/iUs","",$data['content']);
							$c=strip_tags($c);
							$data['content']="";
							if($im[1]){
								foreach($im[1] as $k=>$v){
									if($k>6) break;
									$data['content'].="<img src='{$v}'>";
								}
							}
							$data['content'].="<p>$c</p>";
		echo json_encode($data);
	}
	
	//获取淘宝客商品信息
	public function onget(){
					
					$row=$this->stole->getRow("SELECT * FROM ".table('stole')." WHERE status=0 LIMIT 1");
					
					if($row){
						
						$r=$this->taobao->getDetail($row['goodsurl']);
						
						
						$this->stole->query("UPDATE ".table('stole')." SET status=1 WHERE id='{$row['id']}' ");
						 
						$sex=1;
						if(strpos($r['title'],"男")===false){
								$sex=0;
						}
						$s=$this->product->getOne("SELECT id FROM ".table('product')." WHERE goodsid='{$r['num_iid']}' limit 1 ");
						session_write_close();
						$this->product->close();
						set_time_limit(120);
							 
						if($r['click_url'] && !$s && !$sex ){
							$id=$this->product->insert(array("title"=>"临时文件","goodsid"=>$r['num_iid']));
							$rwap=$this->taobao->getDetail($row['goodsurl'],1);
							$c=explode(" ",$row['keyword']);											
							$cname=$c[0];
							$data['title']=strip_tags($r['title']);
							$data['goodsid']=$r['num_iid'];
							$data['goodsurl']=$r['click_url'];	
							$data['wap_goodsurl']=$rwap['click_url'];					
							$data['price']=$r['price'];
							$data['keywords']=$r['title'];
							$data['dateline']=time();
							$data['last_time']=time();
							$data['grade']=50;//默认50分
							$data['month']=date("m");	
							$img=new image();
							$this->loadClass("upload");
							$dir="attach/product/".$this->upload->dirId($id);
							umkdir($dir);						 
							$imgurl=$dir.time().md5($r['pic_url']).".jpg";
							if(defined("SAE_GET") && SAE_GET){
								app_file_put_contents($imgurl,app_file_get_contents(SAE_GET.urlencode($r['pic_url'])));
							}else{
								app_file_put_contents($imgurl,app_file_get_contents($r['pic_url']));
							}
							$im=@getimagesize($imgurl);
							
							if(!isset($im[0])){
								echo "<script>location.href='/index.php?m=tao&a=get&t=".time()."';</script>";
								exit;
							}
							$mini_img=$img->makethumb($imgurl.".100x100.jpg",$imgurl,"100","100",1);
							$small_img=$img->makethumb($imgurl.".small.jpg",$imgurl,"240");
							$im=getimagesize($small_img);
							 
							$middle_img=$img->makethumb($imgurl.".middle.jpg",$imgurl,"440");
							$big_img=$imgurl;
							$data['thumbsinfo']=json_encode(array(
								 "width"=>$im[0],
								 "height"=>$im[1]
							));
							$data['imgurl']=$imgurl;		 
							$data['catid']=$row['catid'];
							$this->product->update($data,"id=$id");
							preg_match_all("/<img.*src=[\'\"](.*)[\'\"].*[^>]>/iUs",$r['content'],$im);							
							$c=preg_replace("/<table.*<\/table>/iUs","",$r['content']);
							$c=strip_tags($c);
							$r['content']="";
							if($im[1]){
								foreach($im[1] as $k=>$v){
									if($k>6) break;
									$r['content'].="<img src='{$v}'>";
								}
							}
							$r['content'].="<p>$c</p>";
							$this->product_data->insert(array("id"=>$id,"content"=>$r['content'],"dateline"=>time()));
							
						}
						echo "正在获取中..."; 
						echo "<script>location.href='/index.php?m=tao&a=get&t=".time()."';</script>";
					}else{
						echo "结束";
					}
	}
	
	/*检查是否产品下架*/
	public function onSold_out(){
		$id=get('id','i');
		$key="tao_sold_out_$id";
		if(!$this->cache->get($key)){
			$this->cache->set($key,1,600);	
			$this->loadModel(array("product"));
			$data=$this->product->selectRow(array("where"=>"id=$id"));
			$tao=$this->taobao->getunTao($data['goodsid']);
			if($tao['error']){
				$this->product->update(array("unsold"=>1),"id=$id");
			}else{
				$this->product->update(array("unsold"=>0),"id=$id");
			}
		}else{
			//exit('获取过了');
		}
	}
	
	/*清除采集库*/
	public function onClear(){
		$this->stole->update(array("status"=>1));
		echo "清空完毕";
	}

}
?>