Model_apitt.php 16 KB

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