Model_apitt.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. class Model_apitt extends Lin_Model {
  3. function __construct(){
  4. parent::__construct();
  5. }
  6. public function get_list($shop,$from,$to,$order_status,$cuy,$num,$dtc,$money,$int,$dtctitle,$pay,$typeclass,$ex,$sjc,$zjsku)//获取订单列表
  7. {
  8. $url = 'https://open-api.tiktokglobalshop.com';
  9. $link = '/order/202309/orders/search';
  10. $time = time();
  11. $post['create_time_ge'] = $from;
  12. $post['create_time_lt'] = $to;
  13. $post['order_status'] = $order_status;
  14. $queryParams['page_size'] = 100;
  15. $queryParams['sort_order'] = 'ASC';
  16. $queryParams['sort_field'] = 'create_time';
  17. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  18. $queryParams['shop_id'] =$shop['codeid'];
  19. $queryParams['app_key'] =$shop['app_key'];
  20. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  21. $queryParams['timestamp'] =$time;
  22. $link .= '?'.http_build_query($queryParams);
  23. $sign = $this->sign($link,$shop['app_secret'],$post);
  24. $url .= $link.'&sign='.$sign;
  25. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  26. $res = $this->su_curl($post,$url,$headers);
  27. $res = json_decode($res,true);
  28. $list = array();$page_token = '';$arr = array();
  29. if(isset($res['data']['orders']))
  30. {
  31. if($res['data']['total_count'] > 100)
  32. {
  33. $n = ceil($res['data']['total_count']/100);
  34. for($i=0;$i<$n;$i++)
  35. {
  36. $net_date = $this->get_next_list($shop,$from,$to,$order_status,$page_token);
  37. $page_token = $net_date[1];
  38. foreach ($net_date['0'] as $val)
  39. {
  40. $d = $this->fullordertt->get_orderinfo($val['id']);
  41. if(!$d)
  42. {
  43. $list[] = $val['id'];
  44. }
  45. }
  46. }
  47. }
  48. else
  49. {
  50. foreach ($res['data']['orders'] as $val)
  51. {
  52. $d = $this->fullordertt->get_orderinfo($val['id']);
  53. if(!$d)
  54. {
  55. $list[] = $val['id'];
  56. }
  57. }
  58. }
  59. $cs = array();
  60. if(count($list) > 50)
  61. {
  62. $n = ceil(count($list)/50);$x = 0;
  63. for($i=0;$i<$n;$i++)
  64. {
  65. $k = ($i<1)?0:$i*50;
  66. $data = array_slice($list,$k,50);
  67. $d = $this->get_data($data,$shop);
  68. $cs[] = $d;
  69. if(isset($d['data']))
  70. {
  71. foreach ($d['data']['orders'] as $val)
  72. {
  73. $arr[$x] = $val;
  74. $x++;
  75. }
  76. }
  77. }
  78. }
  79. else
  80. {
  81. $d = $this->get_data($list,$shop);
  82. if(isset($d['data']))
  83. {
  84. foreach ($d['data']['orders'] as $val)
  85. {
  86. $arr[] = $val;
  87. }
  88. }
  89. }
  90. }
  91. $read = array();
  92. $gnum = array();
  93. foreach ($arr as $val)
  94. {
  95. $create_time = substr($val['create_time'],0,10)-$sjc;
  96. if(isset($gnum[date('ymd',$create_time)]))
  97. {
  98. $gnum[date('ymd',$create_time)] += 1;
  99. }
  100. else
  101. {
  102. $num = $this->fullordertt->find_count('gtime = "'.date('Ymd',$create_time).'" and shop = "'.$shop['id'].'"');
  103. $gnum[date('ymd',$create_time)] = $num+1;
  104. }
  105. $read[] = $this->get_read($val,$time,$shop,$cuy,$gnum[date('ymd',$create_time)],$dtc,$money,$int,$dtctitle,$pay,$typeclass,$ex,$sjc,$zjsku);
  106. }
  107. return array($read,$list);
  108. }
  109. public function get_next_list($shop,$from,$to,$order_status,$page_token='')//获取订单列表
  110. {
  111. $url = 'https://open-api.tiktokglobalshop.com';
  112. $link = '/order/202309/orders/search';
  113. $time = time();
  114. $post['create_time_ge'] = $from;
  115. $post['create_time_lt'] = $to;
  116. $post['order_status'] = $order_status;
  117. $queryParams['page_size'] = 100;
  118. $queryParams['sort_order'] = 'ASC';
  119. $queryParams['sort_field'] = 'create_time';
  120. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  121. $queryParams['shop_id'] =$shop['codeid'];
  122. $queryParams['app_key'] =$shop['app_key'];
  123. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  124. $queryParams['timestamp'] =$time;
  125. if($page_token)
  126. {
  127. $queryParams['page_token'] = $page_token;
  128. }
  129. $link .= '?'.http_build_query($queryParams);
  130. $sign = $this->sign($link,$shop['app_secret'],$post);
  131. $url .= $link.'&sign='.$sign;
  132. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  133. $res = $this->su_curl($post,$url,$headers);
  134. $res = json_decode($res,true);
  135. if(isset($res['data']['orders']))
  136. {
  137. return array($res['data']['orders'],$res['data']['next_page_token']);
  138. }
  139. }
  140. public function get_data($list,$shop)
  141. {
  142. $url = 'https://open-api.tiktokglobalshop.com';
  143. $link = '/order/202309/orders';
  144. $time = time();
  145. $ids = implode(",",$list);
  146. $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'&timestamp='.$time.'&ids='.$ids;
  147. $sign = $this->sign($link,$shop['app_secret'],'');
  148. $url .= $link.'&sign='.$sign;
  149. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  150. $res = $this->su_curl('',$url,$headers,$date_type='json',$timeout=300,$httptype="GET");
  151. $res = json_decode($res,true);
  152. return $res;
  153. }
  154. public function get_read($data,$time,$shop,$cuy,$num,$dtc,$money,$int,$dtctitle,$pay,$typeclass,$ex,$sjc,$zjsku)
  155. {
  156. $purl = 'https://shop.tiktok.com/view/product/';
  157. $link = '';$product = '';$sku = '';$quantity = '';
  158. $lpsq = array();
  159. foreach ($data['line_items'] as $v)
  160. {
  161. if(isset($lpsq[$v['seller_sku']]))
  162. {
  163. $lpsq[$v['seller_sku']]['quantity'] += 1;
  164. }
  165. else
  166. {
  167. $lpsq[$v['seller_sku']] = array('link'=>$purl.$v['product_id'],'product'=>$v['product_name'],'sku'=>$v['seller_sku'],'quantity'=>1);
  168. }
  169. }
  170. foreach ($lpsq as $v)
  171. {
  172. $link .= $v['link'].',';
  173. $product .= $v['product'].',';
  174. $sku .= $v['sku'].',';
  175. $quantity .= $v['quantity'].';';
  176. }
  177. if($data['status'] == 'DELIVERED' || $data['status'] == 'IN_TRANSIT' || $data['status'] == 'COMPLETED')
  178. {
  179. $s = 216;
  180. }
  181. else if($data['status'] == 'AWAITING_SHIPMENT')
  182. {
  183. $s = 207;
  184. }
  185. else if($data['status'] == 'CANCELLED')
  186. {
  187. $s = 217;
  188. }
  189. else
  190. {
  191. $s = 283;
  192. }
  193. if(isset($zjsku[$shop['id']]))//判断是否找到店铺SKU
  194. {
  195. $sku = rtrim($sku,',').','.$zjsku[$shop['id']];
  196. }
  197. $post['user_id'] = $data['user_id'];
  198. $post['state'] = $s;
  199. $post['shop'] = $shop['id'];//店铺ID
  200. $post['user'] = $shop['shopuser'];//店铺负责人
  201. $post['product'] = rtrim($product,',');//产品名称
  202. $post['link'] = rtrim($link,',');//产品链接
  203. $post['issku'] = rtrim($sku,',');
  204. $post['quantity'] = rtrim($quantity,';');
  205. $post['clientremarks'] = $data['buyer_message'];//客户备注
  206. $post['paypal'] = '';//交易号
  207. $post['guarantee'] = '';//卖家保障
  208. $post['parameter'] = '';//属性
  209. $post['shippingmethod'] = $data['delivery_option_name'].' '.$data['payment']['shipping_fee'];//用户选择快递方式及运费金额
  210. $post['source'] = 2;//订单类型
  211. $post['type'] = 1;//发货仓库
  212. $post['capital'] = 3;//资金支付状况,全部付款
  213. $post['number'] = $shop['shortname'].'-'.substr(date('ymd',substr($data['create_time'],0,10)-$sjc),1).'-'.(substr(strval($num+1000),1,3));//编号
  214. $post['orderinfo'] = $data['id'];//订单号
  215. $post['insurance'] = 0;//运输保险费用
  216. $address = $data['recipient_address']['address_detail'];
  217. $post['baddress'] = $address.','.$data['recipient_address']['district_info'][3]['address_name'].','.$data['recipient_address']['district_info'][1]['address_name'].','.$data['recipient_address']['postal_code'].','.$data['recipient_address']['region_code'].','.$data['recipient_address']['phone_number'];
  218. $post['saddress'] = $address.','.$data['recipient_address']['district_info'][3]['address_name'].','.$data['recipient_address']['district_info'][1]['address_name'].','.$data['recipient_address']['postal_code'].','.$data['recipient_address']['region_code'].','.$data['recipient_address']['phone_number'];
  219. $post['country'] = $cuy[$data['recipient_address']['region_code']];//此国家的ID
  220. $post['al'] = $data['recipient_address']['region_code'];//联邦名称
  221. $post['bname'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//名称
  222. $post['sname'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//名称
  223. $post['client'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//公司名称
  224. $post['name'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//收件人名称
  225. $post['phone'] = preg_replace('/\D/s','',$data['recipient_address']['phone_number']);//收件人电话
  226. $post['email'] = preg_replace('/( | | |\s)+/','',$data['buyer_email']);//客户邮箱
  227. $post['zipcode'] = $data['recipient_address']['postal_code'];//收件人邮编
  228. $post['province'] = $data['recipient_address']['district_info'][1]['address_name'];//收件人省份
  229. $post['city'] = $data['recipient_address']['district_info'][3]['address_name'];//收件人城市
  230. $post['address'] = $address;//收件人地址
  231. $post['shouldmoney'] = $data['payment']['total_amount'];//应收金额
  232. $post['skje'] = $post['shouldmoney'];//收款金额
  233. $post['ismoney'] = $data['payment']['currency'].$post['shouldmoney'];//支付币种金额
  234. $yga = $post['shouldmoney'];//金额
  235. $post['cf'] = ($data['shipping_type']=='SELLER')?0:1;//是否仓发TIKTOK平台仓发 SELLER商家发,ERP中1平台仓发,0商家发
  236. $seller_note = isset($data['seller_note'])?preg_replace('/[^0-9]/','',$data['seller_note']):0;
  237. $post['budget'] = ($seller_note>0)?($seller_note/100):0;//预估到账金额
  238. /**
  239. if(isset($data['pay']) && $yga > 0)
  240. {
  241. $post['pay'] = $pay[$data['pay']]['id'];
  242. $ygc = $pay[$data['pay']]['estimaterate'];//预估到账公式
  243. $post['estimaterate'] = $pay[$data['pay']]['estimaterate'];
  244. $ifbudget = eval("return $yga*1.$ygc;");
  245. if($ifbudget > 0)
  246. {
  247. $post['budget'] = eval("return $yga*1.$ygc;");//预估到帐金额
  248. }
  249. }
  250. else if($yga > 0)
  251. {
  252. $post['pay'] = 0;
  253. $ygb = $shop['estimaterate'];//店铺默认到账公式
  254. $post['estimaterate'] = $shop['estimaterate'];
  255. $ifbudget = eval("return $yga*1.$ygb;");
  256. if($ifbudget > 0)
  257. {
  258. $post['budget'] = eval("return $yga*1.$ygb;");//预估到帐金额
  259. }
  260. }
  261. **/
  262. $post['overtime'] = $data['shipping_due_time']-$sjc;//超时时间
  263. $post['buytime'] = substr($data['paid_time'],0,10)-$sjc;//格式化付款时间
  264. $post['dtime'] = substr($data['create_time'],0,10)-$sjc;//订单时间
  265. $post['gtime'] = date('Ymd',substr($data['create_time'],0,10)-$sjc);//格式化订单时间
  266. $post['currencytitle'] = $data['payment']['currency'];//结算币种名称
  267. $post['currency'] = $typeclass[$data['payment']['currency']];//钱币ID
  268. $post['time'] = time();//同步时间
  269. $post['hl'] = '6.4';//汇率
  270. if(isset($data['delivery_option_name']))
  271. {
  272. $post['express'] = (isset($ex[$data['delivery_option_name']]))?$ex[$data['delivery_option_name']]['id']:0;//快递方式
  273. }
  274. if(isset($data['line_items'][0]['tracking_number']))
  275. {
  276. $post['waybill'] = $data['line_items'][0]['tracking_number'];
  277. }
  278. $matching = $this->api->matching($post['quantity'],$post['issku'],$dtc,$money,$int,$dtctitle);
  279. $post['shipremarks'] = $matching['title'];
  280. $post['cost'] = $matching['cost'];
  281. $post['purchase'] = $matching['purchase'];
  282. $post['fpdata'] = $matching['product'];
  283. $post['whlabel'] = $matching['whlabel'];
  284. $post['slpx'] = $matching['wcslpx'];
  285. return $post;
  286. }
  287. public function sign($url,$appSecret,$body='')
  288. {
  289. // 解析URL获取查询参数
  290. $queryParams = array();
  291. parse_str(parse_url($url, PHP_URL_QUERY), $queryParams);
  292. // 排除'sign'和'access_token'
  293. unset($queryParams['sign'], $queryParams['access_token']);
  294. // 按字典顺序排序查询参数
  295. ksort($queryParams);
  296. // 拼接参数为{key}{value}格式
  297. $input = '';
  298. foreach ($queryParams as $key => $value)
  299. {
  300. $input .= $key . $value;
  301. }
  302. // 追加请求路径
  303. $path = parse_url($url, PHP_URL_PATH);
  304. $input = $path . $input;
  305. if ($body)
  306. {
  307. $input .= json_encode($body);
  308. }
  309. // 使用app_secret包裹生成的字符串
  310. $input = $appSecret . $input . $appSecret;
  311. // 生成签名
  312. $sign = hash_hmac('sha256', $input,$appSecret);
  313. return $sign;
  314. }
  315. public function su_curl($data,$url,$header=array(),$date_type='json',$timeout=300,$httptype="POST",$userpwd='')
  316. {
  317. if ($date_type == 'http_build_query')
  318. {
  319. $data = http_build_query($data);
  320. }
  321. else if ($date_type == 'json')
  322. {
  323. $data = json_encode($data);
  324. }
  325. $ch = curl_init();
  326. curl_setopt($ch, CURLOPT_URL, $url);
  327. curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  328. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  329. curl_setopt($ch, CURLOPT_HEADER, false);
  330. switch ($httptype)
  331. {
  332. case "GET":
  333. curl_setopt($ch, CURLOPT_HTTPGET, true);
  334. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  335. break;
  336. case "POST":
  337. curl_setopt($ch, CURLOPT_POST, true);
  338. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  339. break;
  340. case "PUT":
  341. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  342. break;
  343. case "DELETE":
  344. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  345. break;
  346. }
  347. $isSecure = strpos($url, "https://");
  348. if ($isSecure === 0)
  349. {
  350. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  351. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  352. }
  353. if(!empty($header))
  354. {
  355. curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
  356. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  357. }
  358. if(!empty($userpwd))
  359. {
  360. curl_setopt($ch,CURLOPT_USERPWD,$userpwd);
  361. }
  362. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  363. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  364. $result = curl_exec($ch);
  365. curl_close($ch);
  366. return $result;
  367. }
  368. } //end class