<?php
	/* 
	 * 采集商品
	 */
	 
	 function combineURL($baseURL,$keysArr){
        $combined = $baseURL."&";
        $valueArr = array();

        foreach($keysArr as $key => $val){
            $valueArr[] = "$key=$val";
        }

        $keyStr = implode("&",$valueArr);
        $combined .= ($keyStr);
        
        return $combined;
    }
	
	switch($ts){
	
		//cookies
		case "":
		
			include template("admin/cookies");
			break;
		
		//筛选
		case "spider_s":
		
			
			if(!$cookies = _get('cookies', 'trim')){
				
				qiMsg("必须添加cookies的值！");
				
			}else{
				if(!_get('back'))
				$cookies = urlencode($cookies);
				
			};
			
			
		
			include template("admin/spider");
			break;
	
		case  "search":
        	//搜索结果
			$taobaoke_item_list = array();

			if(!$map['cookies'] = _get('cookies')){
				
				qiMsg("必须添加cookies的值！");
				
			};
			
			
			
			$map['keyword'] = _get('keyword', 'trim'); //关键词
				
				$p = _get('p', 'intval', 1);
				
				if (!$map['keyword']) {
					qiMsg("关键词必须要设置，多个请用空格分开！",'<a href="index.php?app=share&ac=admin&mg=spider">点击返回</a>');
				}
				
				
				
				$map['start_price']                = _get('start_price', 'trim'); //价格下限
				$map['end_price']                  = _get('end_price', 'trim'); //价格上限
				$map['start_commissionRate']       = _get('start_commissionRate', 'trim'); //佣金比率下限
				$map['end_commissionRate']         = _get('end_commissionRate', 'trim'); //佣金比率上限
				$map['start_commissionNum']        = _get('start_commissionNum', 'intval'); //30天推广量下限
				$map['end_commissionNum']          = _get('end_commissionNum', 'intval'); //30天推广量上限
				$map['start_totalnum']             = _get('start_totalnum', 'intval'); //总销量下限
				$map['end_totalnum']               = _get('end_totalnum', 'intval'); //总销量上限
				$map['start_credit']               = _get('start_credit', 'intval'); //卖家信用下限
				$map['end_credit']                 = _get('end_credit', 'trim'); //卖家信用上限
				$map['user_type']                  = _get('user_type'); //天猫还是集市，还是所有
				$map['discountId']          	   = _get('discountId'); //打折类型
	
				$map['like_init']                  = _get('like_init', 'trim');
				$map['p']                  		   = '';
				
				$url = combineURL('index.php?app=share&ac=admin&mg=spider&ts=search',$map);
				
				//$result = $new['share']->_get_list($map, $p);

			$url_alimama = 'http://pub.alimama.com/pubauc/searchAuctionList.json?q='.urlencode($map['keyword']).'&toPage='.$p.'&startPrice='.$map['start_price'].'&endPrice='.$map['end_price'].'&startCommrate='.$map['start_commissionRate'].'&startTotalnum='.$map['start_commissionNum'].'&endTotalnum='.$map['end_commissionNum'].'&startRatesum='.$map['start_credit'].'&discountId=&user_type='.$map['user_type'];
			include_once 'letutao/Snoopy.class.php';
				$snoopy = new Snoopy();  
				  
				$snoopy->agent = "(compatible; MSIE 8.01; MSN 2.5; AOL 4.0; Windows xp)";  
				$snoopy->referer = "http://www.tuntron.com";  
				 $map['cookies'] = 'cookie2='.$map['cookies'];
				$arr_cookie = explode(';',urldecode($map['cookies']));
				foreach($arr_cookie as $key=>$item){
					$item_cookie = explode('=',$item);
					$snoopy->cookies[$item_cookie[0]] = $item_cookie[1];  
				}
				$snoopy->rawheaders["Pragma"] = "no-cache";  
				$snoopy->maxredirs = 2;  
				$snoopy->offsiteok = false;  
				$snoopy->expandlinks = false;  
				  
				$snoopy->user = "joe";
				$snoopy->pass = "bloe";  
				
				if($snoopy->fetch($url_alimama)) { 
					$taobaojson =  $snoopy->results;  
			} else {  
				echo "error fetching document: " . $snoopy->error . "\n";  
				exit;
			}  
			if(!$taobaojson){
				
				qiMsg("Cookies可能已经过期，请获取最新cookies值并填写！",'<a href="index.php?app=share&ac=admin&mg=spider">点击返回</a>');
			}
			
			$results_arr= json_decode($taobaojson,true);
				
				//分页

				//列表内容
				foreach($results_arr['data']['pagelist'] as $key=>$item){
					$taobaoke_item_list[$item['auctionId']] = $item;
		}
				
				$rand = rand(1,5);
				$taobaoke_item_list && F('taobaoke_item_list', $taobaoke_item_list);
			$pageUrl = pagination($results_arr['data']['paginator']['items'], 40, $p, $url);
			include template("admin/search");
   		 break;
		 
		case  "publish":
		
        		//发布已选商品
				$url = _post('url');
				$ids = implode(',',_post('num_iid'));
			    $users  = $db->fetch_all_assoc("select userid,username FROM ".dbprefix."user_info");
				$userarray=array();
				foreach($users as $key=>$item){
					$userarray[]=$item;
				}
				include template("admin/publish");	
			
			
		break;
		
		case  "redirect":
		
			$url = _post('url')?urlencode(_post('url', 'trim')):urlencode(_get('url', 'trim'));
			$ids = _post('ids', 'trim')?_post('ids', 'trim'):_get('ids', 'trim');
			$catename = _post('catename', 'trim')?_post('catename', 'trim'):_get('catename', 'trim');
			$tag = _post('tag', 'trim')?_post('tag', 'trim'):_get('tag', 'trim');
			$i=_get('step', 'trim',0);
			$i=$i+1;
			$stop = false;//采集完毕
			$isExists=false;//商品存在
			$Cantresizeimage=false;//无法生成缩略图
			
			//从缓存中获取本页商品数据
			$ids_arr = explode(',', $ids);
			
			$taobaoke_item_list = F('taobaoke_item_list');
			$auid=$_REQUEST['auid'];
			
			$users  = $db->fetch_all_assoc("select userid FROM ".dbprefix."user");
			$users = arr2_arr1($users,'userid');
			if(!$auid){
			$users_key = array_rand($users);
			$adduid = $users[$users_key];
			}else{
			$adduid = $auid;	
			}
			if ($ids_arr[$i-2]) {
				
				$result =$new['share']->publish_good($taobaoke_item_list[$ids_arr[$i-2]], $catename,$tag,$adduid);
				$result=='isExists' && $isExists = true;
				$result=='Cantresizeimage' && $Cantresizeimage = true;
			}
			
			if($i>=count($ids_arr)+1) $stop = true;
			
			include template("admin/redirect");	
			
		break;
		
	}