<?php
/**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
class TopAction extends BaseAction{
	private $appKey = '12310315';
	private $appSecret = 'e35f608cc56cda3130e500af9956400e';
	public $msg = '';
	function __construct(){
		$config = getConfig("taoke");
		if($config && $config['appKey'] && $config['appSecret']){
			$this->appKey = $config['appKey'];
			$this->appSecret = $config['appSecret'];
		}
	}
	function query($api,$param){
		$paramArr['method'] = $api;
		$paramArr = $paramArr+$param;
		return $this->send($paramArr);
	}
	function send($paramArr){
		$paramArr['app_key'] = $this->appKey;
		$paramArr['format'] = "json";
		$paramArr['v'] = "2.0";
		$paramArr['sign_method'] = "md5";
		$paramArr['timestamp'] = date('Y-m-d H:i:s');

		$sign = $this->createSign($paramArr);
		$strParam = $this->createStrParam($paramArr);
		$strParam .= 'sign='.$sign;
		set_time_limit(60);
		$url = 'http://gw.api.taobao.com/router/rest?'.$strParam; 
		$result = file_get_contents($url);
		$result = json_decode($result,true);
		return $result;		
	}
	function shops($keyword,$only_mall=false){
		if($only_mall==1){$only_mall="true";}
		$paramArr = array(
		"method"=>"taobao.taobaoke.shops.get",//淘宝客店铺搜索
		"fields"=>"seller_nick,user_id,shop_title,click_url,commission_rate,seller_credit,shop_type,total_auction,auction_count",
		"keyword"=>$keyword,
		"only_mall"=>$only_mall
		);
		return $this->send($paramArr);	
		
	}
	function taobaokeitems($cid){
		$paramArr = array(
		"method"=>"taobao.taobaoke.items.get",//taobao.taobaoke.items.convert
		"fields"=>"num_iid,title",
		"cid"=>$cid
		);
		return $this->send($paramArr);	
	}
	function getCat($param=array()){
		$res = $this->itemcats($param);
		if($res['itemcats_get_response']['item_cats']['item_cat']){
			$cats = $res['itemcats_get_response']['item_cats']['item_cat'];
			return $cats;
		}
		return false;
	}
	function itemcats($param=array()){
		if(!$param['parent_cid']){$param['parent_cid']='0';}
		settype($param['parent_cid'],"string");
		$paramArr = array(
		"method"=>"taobao.itemcats.get",//taobao.taobaoke.items.convert
		"fields"=>"cid,parent_cid,name,is_parent",
		"parent_cid"=>$param['parent_cid'],
		"cids"=>''
		);//print_r($paramArr);
		return $this->send($paramArr);
	}
	function getClickUrl($iid){
		$info = $this->getInfo($iid);
		$url = $info['taobaoke_items_detail_get_response']['taobaoke_item_details']['taobaoke_item_detail'][0]['click_url'];
		return $url;
	}
	function getItem($iid,$type="all"){
		if($type!="all"){$fields='';}else{$fields=',desc';}
		$paramArr = array(
		"app_key"=>$this->appKey,
		"method"=>"taobao.item.get",
		"format"=>'json',
		'v'=>'2.0',
		'sign_method'=>'md5',
		'timestamp'=>date('Y-m-d H:i:s'),
		"track_iids"=>"",
		"fields"=>"num_iid,click_url,detail_url,title,nick,pic_url,shop_click_url,price,second_kill,num".$fields,
		"outer_code"=>"50show",
		"num_iid"=>$iid,
		"is_mobile"=>"false"
		);
		//print_r($paramArr);exit;
		$sign = $this->createSign($paramArr);
		$strParam = $this->createStrParam($paramArr);
		$strParam .= 'sign='.$sign;

		$url = 'http://gw.api.taobao.com/router/rest?'.$strParam; 
		$result = file_get_contents($url);
		$result = json_decode($result,true);

		if($result['error_response']){
			$code = $result['error_response']['code'];
			if($result['error_response']['sub_msg']){
				$ret['message'] = $code.':'.$result['error_response']['sub_msg'];
			}else{
				$ret['message'] = $code.':'.$result['error_response']['msg'];		
			}
			$ret['success'] = false;
			return $ret;
		}

		$item = $result['item_get_response'];
		$ret['success'] = true;
		$ret['data'] = $item;
		
		//$ret = json_encode($ret);
		//print_r($ret);exit;
		//if($item['second_kill']){
			//$couponInfo = $this->getPromotionInfo($iid);
			if(!$couponInfo['error_response'] && $couponInfo['ump_promotion_get_response']){
				$ret['promotionInfo'] = $couponInfo['ump_promotion_get_response']['promotions']['promotion_in_item']['promotion_in_item'][0];
			}else{
				//$couponInfo = $this->getCouponInfo($iid);
			//	print_r($couponInfo);
			}
		//};
		
		return $ret;
	
	}
	function getInfo($iid,$type="all"){
		return  $this->getItem($iid,$type);
		if($this->appKey=='12310315'){ return  $this->getItem($iid,$type); }
		if($type!="all"){$fields='';}else{$fields=',desc';}
		$paramArr = array(
		"app_key"=>$this->appKey,
		"method"=>"taobao.taobaoke.items.detail.get",//taobao.taobaoke.items.convert
		"format"=>'json',
		'v'=>'2.0',
		'sign_method'=>'md5',
		'timestamp'=>date('Y-m-d H:i:s'),
		"track_iids"=>"",
		"fields"=>"num_iid,click_url,detail_url,title,nick,pic_url,shop_click_url,price,second_kill,num".$fields,
		"outer_code"=>"50show",
		"num_iids"=>$iid,
		"is_mobile"=>"false"
		);
		$sign = $this->createSign($paramArr);
		$strParam = $this->createStrParam($paramArr);
		$strParam .= 'sign='.$sign;

		$url = 'http://gw.api.taobao.com/router/rest?'.$strParam; 
		$result = file_get_contents($url);
		$result = json_decode($result,true);

		if($result['error_response']){
			$code = $result['error_response']['code'];
			if($result['error_response']['sub_msg']){
				$ret['message'] = $code.':'.$result['error_response']['sub_msg'];
			}else{
				$ret['message'] = $code.':'.$result['error_response']['msg'];		
			}
			$ret['success'] = false;
			return $ret;
		}
		if($result['taobaoke_items_detail_get_response']['total_results']==0){
			$ret['success'] = false;
			$ret['message'] = '获取成功，但取到的商品信息为空';
			return $ret;
		}
		$item = $result['taobaoke_items_detail_get_response']['taobaoke_item_details']['taobaoke_item_detail'][0];
		$ret['success'] = true;
		$ret['data'] = $item;
		
		//$ret = json_encode($ret);
		//print_r($ret);exit;
		//if($item['second_kill']){
			//$couponInfo = $this->getPromotionInfo($iid);
			if(!$couponInfo['error_response'] && $couponInfo['ump_promotion_get_response']){
				$ret['promotionInfo'] = $couponInfo['ump_promotion_get_response']['promotions']['promotion_in_item']['promotion_in_item'][0];
			}else{
				//$couponInfo = $this->getCouponInfo($iid);
			//	print_r($couponInfo);
			}
		//};
		
		return $ret;
	}
	function marketing($iid){
		$paramArr = array(
		"method"=>"taobao.marketing.promotions.get",
		"fields"=>'promotion_id,promotion_title,promotion_desc,decrease_num,num_iid,discount_value,discount_type,start_date,end_date,tag_id,status',
		"tag_id "=>'',
		"num_iid"=>$iid
		);
		return $this->send($paramArr);		
		
	}

	function ump($limit_discount_id){
		$paramArr = array(
		"method"=>"taobao.promotion.limitdiscount.detail.get",
		"limit_discount_id"=>$limit_discount_id
		);
		return $this->send($paramArr);		
		
	}
	function getCouponInfo($iid){
		$paramArr = array(
		"method"=>"taobao.taobaoke.items.coupon.get",
		"fields"=>"name,desc,start_time,end_time,item_promo_price",
		"num_iids"=>$iid
		);
		//print_r($paramArr);exit;
		return $this->send($paramArr);		
	}
	function getCoupon($param){
		$page_no = 1;
		if(!is_array($param)){$cid=$param;}else{
			$cid = $param['cid'];
			$page_no = $param['page'];
			$shop_type = $param['shop_type'];
		}
		if(!$page_no || !is_numeric($page_no)){$page_no=1;}
		if(!in_array($shop_type,array("all","b","c"))){$shop_type = 'all';}
		$paramArr = array(
		"method"=>"taobao.taobaoke.items.coupon.get",
		"fields"=>"num_iid,nick,title,price,item_location,seller_credit_score,click_url,shop_click_url,pic_url,coupon_rate,coupon_price,coupon_start_time,coupon_end_time,commission_rate,commission,commission_num,commission_volume,volume,shop_type",
		"cid"=>$cid,
		"page_no"=>$page_no,
		"shop_type"=>$shop_type
		);
		$res =  $this->send($paramArr);
		if($res['taobaoke_items_coupon_get_response']['taobaoke_items']){
			$items = $res['taobaoke_items_coupon_get_response']['taobaoke_items']['taobaoke_item'];
			return $items;
		}else{
			$this->msg = $ret['error_response']['msg'];
			return false;
		}
	}

	function getGoods($param){
		$page_no = 1;
		if(!is_array($param)){$cid=$param;}else{
			$cid = $param['cid'];
			$page_no = $param['page'];
			$shop_type = $param['shop_type'];
			$keyword = $param['keyword'];
		}
		if(!$page_no || !is_numeric($page_no)){$page_no=1;}
		if(!in_array($shop_type,array("b"))){$shop_type = 'true';}else{$shop_type='false';}
		$paramArr = array(
		"method"=>"taobao.taobaoke.items.get",
		"fields"=>"num_iid,nick,title,price,item_location,seller_credit_score,click_url,shop_click_url,pic_url,coupon_rate,coupon_price,coupon_start_time,coupon_end_time,commission_rate,commission,commission_num,commission_volume,volume,shop_type,promotion_price",
		"cid"=>$cid,
		"page_no"=>$page_no,
		"mall_item"=>$shop_type,
		"keyword"=>$keyword
		);
		$res =  $this->send($paramArr);
		//print_r($res);
		if($res['taobaoke_items_get_response']['taobaoke_items']){
			$items = $res['taobaoke_items_get_response']['taobaoke_items']['taobaoke_item'];
			return $items;
		}else{
			$this->msg = $res['error_response']['msg'];
			saveLog($this->msg);
			return false;
		}
	}

	function getPromotion($iid){
		$paramArr = array(
		"method"=>"taobao.ump.promotion.get",
		"fields"=>"num_iid,click_url,detail_url,title,nick,pic_url,shop_click_url,price,coupon_price,coupon_rate,coupon_start_time,coupon_end_time",
		"item_id"=>$iid
		);//print_r($paramArr);exit;
		$ret = $this->send($paramArr);		
		if($ret['error_response']){
			$this->msg = $ret['error_response']['msg'];
			return false;
		}else{
			$res =  $ret['ump_promotion_get_response']['promotions']['promotion_in_item']['promotion_in_item'][0];
			//print_r($res);exit;
			return $res;
		}
	}

	function getPromotionInfo($iid){
		$paramArr = array(
		"method"=>"taobao.ump.promotion.get",
		"fields"=>"num_iid,click_url,detail_url,title,nick,pic_url,shop_click_url,price,coupon_price,coupon_rate,coupon_start_time,coupon_end_time",
		"item_id"=>$iid
		);
		return $this->send($paramArr);		
		
	}

	function temai($para=false){
		if($para){
			$start = $para['start']>0?$para['start']:0;
			$cat = $para['cat']?$para['cat']:'50100982';
		}
		if(!$cat){$cat="50100982";}
		$param = array(
			"method"=>"tmall.temai.items.search",
			"cat"=>$cat,
			"start"=>$start,
			"sort"=>"s"
		);
		return $this->send($param);
	}
//start 表示查询起始位置: start=0:返回第1条记录到第48条记录（即第一页）； start=48:返回第48条记录到第96条记录（即第二页）； start=96，start=144，start=192...... 依此类推，每次加start值加48(每页返回记录数固定为48条) 
//sort 排序类型。类型包括： s: 人气排序 p: 价格从低到高; pd: 价格从高到低; d: 月销量从高到低; pt: 按发布时间排序. 
	//签名函数
	
	function temaicat(){
		$param = array(
			"method"=>"tmall.temai.subcats.search",
			"cat"=>"50100982"
		);
		return $this->send($param);
	
	}
	function createSign ($paramArr) {

		 $sign = $this->appSecret;

		 ksort($paramArr);

		 foreach ($paramArr as $key => $val) {

			 if ($key != '' && $val != '') {

				 $sign .= $key.$val;

			 }

		 }

		 $sign.=$this->appSecret;

		 $sign = strtoupper(md5($sign));

		 return $sign;

	}

	//组参函数

	function createStrParam ($paramArr) {

		 $strParam = '';

		 foreach ($paramArr as $key => $val) {

		 if ($key != '' && $val != '') {

				 $strParam .= $key.'='.urlencode($val).'&';

			 }

		 }

		 return $strParam;

	}
}
?>