Model_apitt.php 17 KB

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