<?php
	vendor('taobao.TopSdk');
class JumptoAction extends FirstendAction{
	public function _initialize()
	{
		parent::_initialize();
		$this->assign('nav_curr', 'index');
		$this->_mod = D('items');
		$this->_cate_mod = D('itemscate')->cache(true, 10 * 60);

	}

public function index(){
$this->assign('mdomain',str_replace('/index.php/m','',C('yh_headerm_html')));
$id=I('item');
$this->assign('itemid', $id);

$cach_name='jump_'.$id;
$sinfo = S($cach_name);
$wx_quan=I('quan', '', 'trim');
$wx_quanid=I('quanid', '', 'trim');
$data=array(
    		'quan'=>$wx_quan,
    		'quanid'=>$wx_quanid,
    		'id'=>I('item', '', 'trim'),
    		'pid'=>I('pid', '', 'trim')?I('pid', '', 'trim'):trim(C('yh_taobao_pid')),
    		);
    	$appkey = trim(C('yh_taobao_appkey'));
    	$appsecret = trim(C('yh_taobao_appsecret'));
    	if (! empty($appkey) && ! empty($appsecret) && !empty($id)) {
    		$c = new TopClient();
    		$c->appkey = $appkey;
    		$c->secretKey = $appsecret;
    		$req = new TbkItemInfoGetRequest();
    		$req->setFields("num_iid,user_type,title,seller_id,volume,pict_url,reserve_price,zk_final_price,item_url");
    		$req->setPlatform("1");
    		$req->setNumIids($id);
    		$resp = $c->execute($req);
    		$resparr = xmlToArray($resp);
    		$newitem = $resparr['results']['n_tbk_item'];
		$newitem['coupon_price']=$newitem['zk_final_price']-$data['quan'];
    		$newitem['quan']=$data['quan'];
    		$newitem['quanid']=$data['quanid'];
    		$newitem['pid']=$data['pid'];
		}

$agent_pid='';	
if(!$sinfo && !$wx_quan && !$wx_quanid){
 $text = $newitem['title'];
 $url=$this->tqkapi."/so";
 $data=array(
 'key'=>$this->_userappkey,
 'time'=>time(),
 'tqk_uid'=>	$this->tqkuid,
 'k'=>$text,
 'pid'=>$agent_pid
 );
 $result=json_decode($datas,true);
if($result['status']=='1'){
$sinfo=	$result['result'][0];
$newitem['coupon_price']=$newitem['zk_final_price']-$sinfo['quan'];
$newitem['quan']=$sinfo['quan'];
$newitem['pict_url']=$sinfo['pic_url'];
$newitem['quanid']=$sinfo['Quan_id'];
$newitem['quanurl']=$sinfo['coupon_click_url'];

if(empty($agent_pid)){
S($cach_name,$newitem,86400);	
}

}
	
}elseif(!$wx_quan && !$wx_quanid){
	
$newitem['coupon_price']=$newitem['zk_final_price']-$sinfo['quan'];
$newitem['quan']=$sinfo['quan'];
$newitem['quanid']=$sinfo['quanid'];
$newitem['quanurl']=$sinfo['coupon_click_url'];
//$newitem['pid']=$data['pid'];
	
}

$orlike = $this->_mod->field('id,title,pic_url,coupon_price,price,shop_type')->limit('0,6')->order('id desc')->select();
$this->assign('orlike', $orlike);

if(!isset($sinfo['kouling']) || empty($sinfo['kouling']) || !empty($agent_pid)){
$apiurl=$this->tqkapi.'/gconvert';
 //$agentpid=$this->agent_pid();
 
if(!empty($agent_pid) && $agent_pid){
	$apidata=array(
	    					'tqk_uid'=>$this->tqkuid,
	    					'good_id'=>''.$id.'',
	    					'pid'=>$agent_pid,
	    					'time'=>time()
	    					);
$token=$this->create_token(trim(C('yh_gongju')),$apidata);
$apidata['token']=$token;
$pid=$agent_pid;	
 }else{
$apidata=array(
    					'tqk_uid'=>$this->tqkuid,
    					'good_id'=>''.$id.'',
    					'time'=>time()
    					);	
$token=$this->create_token(trim(C('yh_gongju')),$apidata);
$apidata['token']=$token;
$pid=trim(C('yh_taobao_pid'));
 }
$ret = json_decode($res, true);
if($res && strlen($ret['me'])>5){
if(!empty($sinfo['quanid'])){
$quanurl='https://uland.taobao.com/coupon/edetail?e='.$ret['me'].'&activityId='.$sinfo['quanid'].'&itemId='.$id.'&pid='. $pid .'&af=1';	
}else{
$quanurl	=$ret['quanurl'];
}
	
}else{
	
if(!empty($sinfo['quanurl'])){
$quanurl=$sinfo['quanurl'];	
}elseif(!empty($sinfo['quanid'])){
$quanurl='https://uland.taobao.com/coupon/edetail?e=&activityId='.$newitem['quanid'].'&itemId='.$id.'&pid='. $pid .'';
}else{
echo('<script>alert("此商品优惠券失效！");history.go(-1)</script>');	
}
	
}

$newitem['quanurl']=$quanurl;
$kouling=kouling($newitem['pict_url'].'_200x200.jpg',$newitem['title'],$quanurl);
$newitem['kouling']=$kouling;


if(empty($agent_pid)){
S($cach_name,$newitem,86400);
}

}else{
	
$sinfo = S($cach_name);	
$newitem['quanurl']=$sinfo['quanurl'];
$newitem['kouling']=$sinfo['kouling'];
}

//$agent=strtolower($_SERVER['HTTP_USER_AGENT']);
//if(strpos($agent,'ucbrowser')>10 || strpos($agent,'mqqbrowser')>10){
//$newitem['kouling']=str_replace("￥","《",$newitem['kouling']);	
//}

$this->assign('item', $newitem);


$this->_config_seo(C('yh_seo_config.item'), array(
			'title' => $newitem['title'],
			'price' =>  $newitem['reserve_price'],
			'quan' =>  $this->floattostr($newitem['quan']),
			'coupon_price' => $newitem['zk_final_price']-$sinfo['quan'],
			'seo_title' => $newitem['title']
			));
$this->display();	
	
}
	
	
	
public function jumpclick(){
$params=I('param.');
$params['quanurl'] = htmlspecialchars_decode($params['quanurl']);
$numid=$params['numid'];
if(is_numeric($numid) && $numid>0){
$cach_name='jump_'.$numid;
$value = S($cach_name);
if(false === $value){
S($cach_name,$params,86400);
}
$json=array(
'status'=>1,
'urls'=>U('jumpto/index',array('item'=>$numid))
);

exit(json_encode($json));
}	
}

public function out(){
$id=I('item');
$cach_name='jump_'.$id;
$sinfo = S($cach_name);
if(false === $sinfo){
echo('<script>alert("此商品优惠券失效！");history.go(-1)</script>');	
	
}else{
	
$quanurl=$sinfo['quanurl'];
	
}

    	$this->assign('quanurl',$quanurl);
    	$this->display();
}
	
	
protected function floattostr($val)
{
	preg_match("#^([\+\-]|)([0-9]*)(\.([0-9]*?)|)(0*)$#", trim($val), $o);
	return $o[1] . sprintf('%d', $o[2]) . ($o[3] != '.' ? $o[3] : '');
}	
	
	
	
}