<?php
/*
*幸福女官网api接口
*/
class xfapiControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
		$this->loadModel(array("product","product_data","category","siteauth"));
		$this->loadConfig("c_category_children");
	}
	
	public function onDefault(){
		
		
	}
	
	public function OnApi(){
		set_time_limit(100);
		$site=$this->check();
		
		if($site['stole_day']<date("Ymd")){
			$site['stole_day']=date("Ymd");
			$site['stole_num']=0;
		}
		$catids=explode(",",$_GET['catids']);
		$eTime=strtotime(date("Y-m-d"));
		$sTime=$eTime-3600*24;
		$limit=$site['stole_max']-$site['stole_num'];
		if($limit<=0) exit(json_encode(array("error"=>2,"message"=>"今天你已经采集够了，明天再来吧")));
		//
		//$eTime=1374993363;
		//$sTime=1372993363;
		$where="  status>0 AND status<10 AND dateline>=$sTime    ";
		$cat_children=$this->config_item('category_children');
		$in_catids=array();
		if(!empty($catids)){
			foreach($catids as $k=>$v){
				$temp_c=$cat_children[$v];
				if(isset($temp_c)){
					$in_catids=array_merge($in_catids,$temp_c);
				}
			}
			!empty($in_catids) && $where.=" AND catid in(".implode(",",$in_catids).")  ";
		}else{
			exit(json_encode(array("error"=>2,"message"=>"必须选择一个分类")));
		}
		$option=array(
			"where"=>$where,
			"order"=>" id DESC",
			"fields"=>" id,title,catid,goodsid,goodsurl,imgurl,price,keywords,description,grade,thumbsinfo,month",
			"start"=>0,
			"limit"=>$limit
		);
		$data=$this->product->select($option,$rscount);
		foreach($data as $k=>$v){
			$v['content']=$this->product_data->selectOne(array("where"=>"id=".$v['id'],"fields"=>"content"));
			$data[$k]=$v;
		}
		//更改站点数据
		$rscount=count($data);
		$host=get('domain','h');
		if($this->siteauth->selectRow(array("where"=>" domain='$host' AND  stole_day =".date("Ymd")."  "))){
			$this->siteauth->changenum("stole_num",$rscount,"domain='$host'");
		}else{
			$this->siteauth->update(
				array(
					"stole_day"=>date("Ymd"),
					"stole_num"=>$rscount
				),
				"domain='$host'"
			);
		}
		echo json_encode($data);
	}
	
	/*官网分类*/
	public function onCategory(){
		$data=$this->category->select(array(
			"where"=>" is_stole=1 AND status=1 AND pid>0 AND id<60",
			"order"=>" orderindex asc",
			"fields"=>"id,cname"
		));
		exit(json_encode($data));
	}
	
	//检验是否有资格获取
	public function check(){
		//exit(json_encode(array('error'=>1,"message"=>'你无权限')));
		$referer=$_SERVER['HTTP_REFERER'];
		$arr=parse_url($url);
		$host=get('domain','h');
		/*
		$st=microtime(true);
		$content=app_file_get_contents("http://".$host);
		$ed=microtime(true);
		 
		if(strpos($content,"http://www.xingfunv.com")===false or strpos($content,"幸福女人")===false ){
			exit(json_encode(array("error"=>1,"message"=>" $referer $host 请在首页添加http://www.xingfunv.com 幸福女人 作为友情链接")));
		}
		*/
		if($site=$this->siteauth->selectRow(array("where"=>"domain='$host'"))){
			return $site;
		}else{
			$this->siteauth->insert(array(
				"title"=>$host,
				"domain"=>$host,
				"dateline"=>time(),
				"status"=>2,
				"stole_max"=>20
			));
			return $site=$this->siteauth->selectRow(array("where"=>"domain='$host'"));
		}
	}
	
}
?>