Model_apitt.php 16 KB

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