| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 | <?phpclass Model_apitt extends Lin_Model {function __construct(){		parent::__construct();}	public function get_list($shop,$from,$to,$order_status,$cuy,$num,$dtc,$money,$int,$dtctitle,$pay,$typeclass,$ex,$sjc,$zjsku)//获取订单列表	{		$url = 'https://open-api.tiktokglobalshop.com';		$link = '/api/orders/search';		$time = time();		$post = array();		$post['create_time_from'] = $from;		$post['create_time_to'] = $to;		$post['order_status'] = $order_status;		$post['page_size'] = 100;		$post['sort_type'] = 2;		$sign = $link.'app_key'.$shop['app_key'].'timestamp'.$time;		$sign = $shop['app_secret'].$sign.$shop['app_secret'];		$sign = hash_hmac('sha256', $sign, $shop['app_secret']);		$url .= $link.'?access_token='.$shop['token'].'&app_key='.$shop['app_key'].'&sign='.$sign.'×tamp='.$time;		$ch = curl_init();		curl_setopt($ch, CURLOPT_URL,$url);		curl_setopt($ch, CURLOPT_POST, 1);		curl_setopt($ch, CURLOPT_HEADER, 0);		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);		curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);		curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);		$res = curl_exec($ch);		curl_close($ch);		$res = json_decode($res,true);		$list = array();$cursor = '';$arr = array();		if(isset($res['data']['order_list']))		{			if($res['data']['total'] > 100)			{				$n = ceil($res['data']['total']/100);				for($i=0;$i<$n;$i++)				{					$net_date = $this->get_next_list($shop,$from,$to,$order_status,$cursor);					$cursor = $net_date[1];					foreach ($net_date['0'] as $val) 		        	{						$d = $this->fullordertt->get_orderinfo($val['order_id']);				    	if(!$d)				    	{							$list[] = $val['order_id'];						}					}				}			}			else			{			    foreach ($res['data']['order_list'] as $val) 		        {				    $d = $this->fullordertt->get_orderinfo($val['order_id']);				    if(!$d)				    {					    $list[] = $val['order_id'];				    }			    }			}			$cs = array();			if(count($list) > 50)			{				$n = ceil(count($list)/50);$x = 0;				for($i=0;$i<$n;$i++)				{					$k = ($i<1)?0:$i*50;					$data = array_slice($list,$k,50);					$d = $this->get_data($data,$shop);					$cs[] = $d;					if(isset($d['data']))		            {					    foreach ($d['data']['order_list'] as $val) 		                {						    $arr[$x] = $val;							$x++;					    }					}				}			}			else			{				$d = $this->get_data($list,$shop);				if(isset($d['data']))		        {					 foreach ($d['data']['order_list'] as $val) 		             {						 $arr[] = $val;					 }				}			}		}		$read = array();		$gnum = array();		foreach ($arr as $val) 		{			$create_time = substr($val['create_time'],0,10)-$sjc;			if(isset($gnum[date('ymd',$create_time)]))			{				$gnum[date('ymd',$create_time)] += 1;			}			else			{				$num = $this->fullordertt->find_count('gtime = "'.date('Ymd',$create_time).'" and shop = "'.$shop['id'].'"');				$gnum[date('ymd',$create_time)] = $num+1;			}			$read[] = $this->get_read($val,$time,$shop,$cuy,$gnum[date('ymd',$create_time)],$dtc,$money,$int,$dtctitle,$pay,$typeclass,$ex,$sjc,$zjsku);		}		return array($read,$list);	}		public function get_next_list($shop,$from,$to,$order_status,$cursor)//获取订单列表	{		$url = 'https://open-api.tiktokglobalshop.com';		$link = '/api/orders/search';		$time = time();		$post = array();		$post['create_time_from'] = $from;		$post['create_time_to'] = $to;		$post['order_status'] = $order_status;		$post['page_size'] = 100;		if($cursor)		{		    $post['cursor'] = $cursor;		}		$post['sort_type'] = 2;		$sign = $link.'app_key'.$shop['app_key'].'timestamp'.$time;		$sign = $shop['app_secret'].$sign.$shop['app_secret'];		$sign = hash_hmac('sha256', $sign, $shop['app_secret']);		$url .= $link.'?access_token='.$shop['token'].'&app_key='.$shop['app_key'].'&sign='.$sign.'×tamp='.$time;		$ch = curl_init();		curl_setopt($ch, CURLOPT_URL,$url);		curl_setopt($ch, CURLOPT_POST, 1);		curl_setopt($ch, CURLOPT_HEADER, 0);		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);		curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);		curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);		$res = curl_exec($ch);		curl_close($ch);		$res = json_decode($res,true);		if(isset($res['data']['order_list']))		{			return array($res['data']['order_list'],$res['data']['next_cursor']);		}	}		public function get_data($list,$shop)	{		$url = 'https://open-api.tiktokglobalshop.com';		$link = '/api/orders/detail/query';		$time = time();		$post = array();		$post['order_id_list'] = json_encode($list);		$sign = $link.'app_key'.$shop['app_key'].'timestamp'.$time;		$sign = $shop['app_secret'].$sign.$shop['app_secret'];		$sign = hash_hmac('sha256', $sign, $shop['app_secret']);		$url .= $link.'?access_token='.$shop['token'].'&app_key='.$shop['app_key'].'&sign='.$sign.'×tamp='.$time;		$ch = curl_init();		curl_setopt($ch, CURLOPT_URL,$url);		curl_setopt($ch, CURLOPT_POST, 1);		curl_setopt($ch, CURLOPT_HEADER, 0);		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);		curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);		curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);		$res = curl_exec($ch);		curl_close($ch);		$res = json_decode($res,true);		return $res;	}		public function get_read($data,$time,$shop,$cuy,$num,$dtc,$money,$int,$dtctitle,$pay,$typeclass,$ex,$sjc,$zjsku)	{		$purl = 'https://shop.tiktok.com/view/product/';		$link = '';$product = '';$sku = '';$quantity = '';		foreach ($data['item_list'] as $v) 		{			$link .= $purl.$v['product_id'].',';			$product .= $v['product_name'].',';			$sku .= $v['seller_sku'].',';			$quantity .= $v['quantity'].';';		}		if($data['order_status'] == 122 || $data['order_status'] == 121 || $data['order_status'] == 130)		{			$s = 216;		}		else if($data['order_status'] == 111)		{			$s = 207;		}		else		{			$s = 283;		}		if(isset($zjsku[$shop['id']]))//判断是否找到店铺SKU		{			$sku = rtrim($sku,',').','.$zjsku[$shop['id']];		}		$post['state'] = $s;		$post['shop'] = $shop['id'];//店铺ID		$post['user'] = $shop['shopuser'];//店铺负责人		$post['product'] = rtrim($product,',');//产品名称		$post['link'] = rtrim($link,',');//产品链接		$post['issku'] = rtrim($sku,',');		$post['quantity'] = rtrim($quantity,';');		$post['clientremarks'] = $data['buyer_message'];//客户备注		$post['paypal'] = '';//交易号		$post['guarantee'] = '';//卖家保障		$post['parameter'] = '';//属性		$post['shippingmethod'] = $data['delivery_option_description'].' '.$data['payment_info']['shipping_fee'];//用户选择快递方式及运费金额		$post['source'] = 2;//订单类型		$post['type'] = 1;//发货仓库		$post['capital'] = 3;//资金支付状况,全部付款		$post['number'] = $shop['shortname'].'-'.substr(date('ymd',substr($data['create_time'],0,10)-$sjc),1).'-'.(substr(strval($num+1000),1,3));//编号		$post['orderinfo'] = $data['order_id'];//订单号		$post['insurance'] = 0;//运输保险费用		$address = '';		foreach ($data['recipient_address']['address_line_list'] as $v) 		{			$address .= $v.' ';		}		$post['baddress'] = rtrim($address,' ').','.$data['recipient_address']['district'].','.$data['recipient_address']['state'].','.$data['recipient_address']['zipcode'].','.$data['recipient_address']['region_code'].','.$data['recipient_address']['phone'];		$post['saddress'] = rtrim($address,' ').','.$data['recipient_address']['district'].','.$data['recipient_address']['state'].','.$data['recipient_address']['zipcode'].','.$data['recipient_address']['region_code'].','.$data['recipient_address']['phone'];		$post['country'] = $cuy[$data['recipient_address']['region_code']];//此国家的ID		$post['al'] = $data['recipient_address']['region_code'];//联邦名称		$post['bname'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//名称		$post['sname'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//名称		$post['client'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//公司名称		$post['name'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//收件人名称		$post['phone'] = preg_replace('/\D/s','',$data['recipient_address']['phone']);//收件人电话		$post['email'] = preg_replace('/( | | |\s)+/','',$data['buyer_email']);//客户邮箱		$post['zipcode'] = $data['recipient_address']['zipcode'];//收件人邮编		$post['province'] = $data['recipient_address']['state'];//收件人省份		$post['city'] = $data['recipient_address']['district'];//收件人城市		$post['address'] = rtrim($address,' ');//收件人地址		$post['shouldmoney'] = $data['payment_info']['total_amount'];//应收金额		$post['skje'] = $post['shouldmoney'];//收款金额		$post['ismoney'] = $data['payment_info']['currency'].$post['shouldmoney'];//支付币种金额		$yga = $post['shouldmoney'];//金额		$post['cf'] = ($data['delivery_option_type']==2)?0:1;//是否仓发1平台仓发2商家发,ERP中1平台仓发,0商家发		$post['budget'] = 0;		if(isset($data['pay']) && $yga > 0)		{			$post['pay'] = $pay[$data['pay']]['id'];			$ygc = $pay[$data['pay']]['estimaterate'];//预估到账公式			$post['estimaterate'] = $pay[$data['pay']]['estimaterate'];			$ifbudget = eval("return $yga*1.$ygc;");			if($ifbudget > 0)			{			    $post['budget'] = eval("return $yga*1.$ygc;");//预估到帐金额			}		}		else if($yga > 0)		{			$post['pay'] = 0;			$ygb = $shop['estimaterate'];//店铺默认到账公式			$post['estimaterate'] = $shop['estimaterate'];			$ifbudget = eval("return $yga*1.$ygb;");			if($ifbudget > 0)			{			    $post['budget'] = eval("return $yga*1.$ygb;");//预估到帐金额			}		}		$post['overtime'] = $data['cancel_order_sla']-$sjc;//超时时间		$post['buytime'] = substr($data['paid_time'],0,10)-$sjc;//格式化付款时间		$post['dtime'] = substr($data['create_time'],0,10)-$sjc;//订单时间		$post['gtime'] = date('Ymd',substr($data['create_time'],0,10)-$sjc);//格式化订单时间		$post['currencytitle'] = $data['payment_info']['currency'];//结算币种名称		$post['currency'] = $typeclass[$data['payment_info']['currency']];//钱币ID		$post['time'] = time();//同步时间		$post['hl'] = '6.4';//汇率		if(isset($data['shipping_provider']))		{			$post['express'] = (isset($ex[$data['shipping_provider']]))?$ex[$data['shipping_provider']]['id']:8;		}		if($data['tracking_number'])		{			$post['waybill'] = $data['tracking_number'];		}		$matching = $this->api->matching($post['quantity'],$post['issku'],$dtc,$money,$int,$dtctitle);		$post['shipremarks'] = $matching['title'];		$post['cost'] = $matching['cost'];		$post['purchase'] = $matching['purchase'];		$post['fpdata'] = $matching['product'];		$post['whlabel'] = $matching['whlabel'];		$post['slpx'] = $matching['wcslpx'];		return $post;	}		public function sign($url,$token,$appSecret,$body='') 	{		// 解析URL获取查询参数		$queryParams = array();		parse_str(parse_url($url, PHP_URL_QUERY), $queryParams);		// 排除'sign'和'access_token'		unset($queryParams['sign'], $queryParams['access_token']);		// 按字典顺序排序查询参数		ksort($queryParams);		// 拼接参数为{key}{value}格式		$input = '';		foreach ($queryParams as $key => $value)		{    		$input .= $key . $value;		}		// 追加请求路径		$path = parse_url($url, PHP_URL_PATH);		$input = $path . $input;		if ($body) 		{			$input .= json_encode($body);	    }		// 使用app_secret包裹生成的字符串		$input = $appSecret . $input . $appSecret;		// 生成签名		$sign = hash_hmac('sha256', $input,$appSecret);        return $sign;	}}  //end class
 |