Model_apitt.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. $extra_text = [];
  205. $line_items = [];
  206. $extra_text['packages'] = $data['packages'];
  207. foreach ($data['line_items'] as $v)
  208. {
  209. // $line_items[] = [
  210. // "id"=>$v["id"],
  211. // "package_id"=>$v['package_id'],
  212. // 'product_id'=>$v['product_id']
  213. // ];
  214. if(isset($lpsq[$v['seller_sku']]))
  215. {
  216. $lpsq[$v['seller_sku']]['quantity'] += 1;
  217. $tmp_row_total = $v['sale_price'] ;
  218. if(empty($v['item_tax'])){
  219. }else{
  220. foreach($v['item_tax'] as $kk => $vv){
  221. if(isset($vv['tax_amount'])){
  222. $tmp_row_total = $tmp_row_total*1 + $vv['tax_amount']*1;
  223. }
  224. }
  225. }
  226. $lpsq[$v['seller_sku']]['row_total'] = $lpsq[$v['seller_sku']]['row_total']*1 + $tmp_row_total*1;
  227. }
  228. else
  229. {
  230. //$lpsq[$v['seller_sku']] = array('link'=>$purl.$v['product_id'],'product'=>$v['product_name'],'sku'=>$v['seller_sku'],'quantity'=>1);
  231. $tmp_row_total = $v['sale_price'] ;
  232. if(empty($v['item_tax'])){
  233. }else{
  234. foreach($v['item_tax'] as $kk => $vv){
  235. if(isset($vv['tax_amount'])){
  236. $tmp_row_total = $tmp_row_total*1 + $vv['tax_amount']*1;
  237. }
  238. }
  239. }
  240. $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);
  241. }
  242. }
  243. // $this->logic_ding->sendToDing($shop['shopname']."测试获取TT订单".json_encode($data));
  244. // $this->logic_ding->sendToDing($shop['shopname']."测试获取TT订单数据转化".json_encode($lpsq));
  245. foreach ($lpsq as $v)
  246. {
  247. $link .= $v['link'].',';
  248. $product .= $v['product'].',';
  249. $sku .= $v['sku'].',';
  250. $quantity .= $v['quantity'].';';
  251. $price .= $v['price'].',';
  252. $row_total .= $v['row_total'].',';
  253. }
  254. if($data['status'] == 'DELIVERED' || $data['status'] == 'IN_TRANSIT' || $data['status'] == 'COMPLETED')
  255. {
  256. $s = 216;
  257. }
  258. else if($data['status'] == 'AWAITING_SHIPMENT')
  259. {
  260. $s = 207;
  261. }
  262. else if($data['status'] == 'CANCELLED')
  263. {
  264. $s = 217;
  265. }
  266. else
  267. {
  268. $s = 283;
  269. }
  270. if(isset($zjsku[$shop['id']]))//判断是否找到店铺SKU
  271. {
  272. $sku = rtrim($sku,',').','.$zjsku[$shop['id']];
  273. }
  274. $post['user_id'] = $data['user_id'];
  275. $post['state'] = $s;
  276. $post['shop'] = $shop['id'];//店铺ID
  277. $post['user'] = $shop['shopuser'];//店铺负责人
  278. $post['product'] = rtrim($product,',');//产品名称
  279. $post['link'] = rtrim($link,',');//产品链接
  280. $post['issku'] = rtrim($sku,',');
  281. $post['quantity'] = rtrim($quantity,';');
  282. $post['clientremarks'] = $data['buyer_message'];//客户备注
  283. $post['paypal'] = '';//交易号
  284. $post['guarantee'] = '';//卖家保障
  285. $post['parameter'] = '';//属性
  286. $post['shippingmethod'] = $data['delivery_option_name'].' '.$data['payment']['shipping_fee'];//用户选择快递方式及运费金额
  287. $post['source'] = 2;//订单类型
  288. $post['type'] = 1;//发货仓库
  289. $post['capital'] = 3;//资金支付状况,全部付款
  290. $numbernew = $shop['shortname'].'-'.date('ymd',substr($data['create_time'],0,10)-$sjc);
  291. $numberwb = (substr(strval($num+1000),1,3));
  292. if($numbernew == 'TT-QQS-40807')
  293. {
  294. $numberwb = $numberwb+7;
  295. }
  296. //过完今天就删除
  297. $tmp_time = date("Y-m-d");
  298. // if($tmp_time == '20250114'){
  299. // $numberwb = $numberwb+7;
  300. // }
  301. $post['number'] = $shop['shortname'].'-'.date('ymd',substr($data['create_time'],0,10)-$sjc).'-'.$numberwb;//编号
  302. $post['orderinfo'] = $data['id'];//订单号
  303. $post['insurance'] = 0;//运输保险费用
  304. $address = $data['recipient_address']['address_detail'];
  305. if(isset($data['recipient_address']['district_info'][3])){
  306. $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'];
  307. $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'];
  308. }else{
  309. $post['baddress'] = $address.','.$data['recipient_address']['district_info'][2]['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'];
  310. $post['saddress'] = $address.','.$data['recipient_address']['district_info'][2]['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'];
  311. }
  312. $post['country'] = $cuy[$data['recipient_address']['region_code']];//此国家的ID
  313. $post['al'] = $data['recipient_address']['region_code'];//联邦名称
  314. $post['bname'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//名称
  315. $post['sname'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//名称
  316. $post['client'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//公司名称
  317. $post['name'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//收件人名称
  318. $post['phone'] = preg_replace('/\D/s','',$data['recipient_address']['phone_number']);//收件人电话
  319. $post['email'] = preg_replace('/( | | |\s)+/','',$data['buyer_email']);//客户邮箱
  320. $post['zipcode'] = $data['recipient_address']['postal_code'];//收件人邮编
  321. $post['province'] = $data['recipient_address']['district_info'][1]['address_name'];//收件人省份
  322. if(isset($data['recipient_address']['district_info'][3])){
  323. $post['city'] = $data['recipient_address']['district_info'][3]['address_name'];//收件人城市
  324. }else{
  325. $post['city'] = $data['recipient_address']['district_info'][2]['address_name'];//收件人城市
  326. }
  327. $post['address'] = $address;//收件人地址
  328. $post['shouldmoney'] = $data['payment']['total_amount'];//应收金额
  329. $post['skje'] = $post['shouldmoney'];//收款金额
  330. $post['ismoney'] = $data['payment']['currency'].$post['shouldmoney'];//支付币种金额
  331. $yga = $post['shouldmoney'];//金额
  332. $post['cf'] = ($data['shipping_type']=='SELLER')?0:1;//是否仓发TIKTOK平台仓发 SELLER商家发,ERP中1平台仓发,0商家发
  333. $post['budget'] = 0;//预估到账金额
  334. /**
  335. if(isset($data['pay']) && $yga > 0)
  336. {
  337. $post['pay'] = $pay[$data['pay']]['id'];
  338. $ygc = $pay[$data['pay']]['estimaterate'];//预估到账公式
  339. $post['estimaterate'] = $pay[$data['pay']]['estimaterate'];
  340. $ifbudget = eval("return $yga*1.$ygc;");
  341. if($ifbudget > 0)
  342. {
  343. $post['budget'] = eval("return $yga*1.$ygc;");//预估到帐金额
  344. }
  345. }
  346. else if($yga > 0)
  347. {
  348. $post['pay'] = 0;
  349. $ygb = $shop['estimaterate'];//店铺默认到账公式
  350. $post['estimaterate'] = $shop['estimaterate'];
  351. $ifbudget = eval("return $yga*1.$ygb;");
  352. if($ifbudget > 0)
  353. {
  354. $post['budget'] = eval("return $yga*1.$ygb;");//预估到帐金额
  355. }
  356. }
  357. **/
  358. $post['overtime'] = $data['shipping_due_time']-$sjc;//超时时间
  359. $post['buytime'] = substr($data['paid_time'],0,10)-$sjc;//格式化付款时间
  360. $post['dtime'] = substr($data['create_time'],0,10)-$sjc;//订单时间
  361. $post['gtime'] = date('Ymd',substr($data['create_time'],0,10)-$sjc);//格式化订单时间
  362. $post['currencytitle'] = $data['payment']['currency'];//结算币种名称
  363. $post['currency'] = $typeclass[$data['payment']['currency']];//钱币ID
  364. $post['time'] = time();//同步时间
  365. $post['hl'] = '6.4';//汇率
  366. $post['delivery_option_id'] = $data['delivery_option_id'];
  367. if(isset($data['delivery_option_name']))
  368. {
  369. $post['express'] = (isset($ex[$data['delivery_option_name']]))?$ex[$data['delivery_option_name']]['id']:0;//快递方式
  370. }
  371. if(isset($data['line_items'][0]['tracking_number']))
  372. {
  373. $post['waybill'] = $data['line_items'][0]['tracking_number'];
  374. }
  375. //$this->logic_ding->sendToDing("测试获取TT订单编码".$post['number']);
  376. $matching = $this->api->matching($post['quantity'],$post['issku'],$dtc,$money,$int,$dtctitle,$shop,[
  377. 'price'=>trim($price,','),
  378. 'row_total'=>trim($row_total,','),
  379. ]);
  380. $post['shipremarks'] = $matching['title'];
  381. $post['cost'] = $matching['cost'];
  382. $post['purchase'] = $matching['purchase'];
  383. $post['fpdata'] = $matching['product'];
  384. $post['whlabel'] = $matching['whlabel'];
  385. $post['slpx'] = $matching['wcslpx'];
  386. return $post;
  387. }
  388. public function sign($url,$appSecret,$body='')
  389. {
  390. // 解析URL获取查询参数
  391. $queryParams = array();
  392. parse_str(parse_url($url, PHP_URL_QUERY), $queryParams);
  393. // 排除'sign'和'access_token'
  394. unset($queryParams['sign'], $queryParams['access_token']);
  395. // 按字典顺序排序查询参数
  396. ksort($queryParams);
  397. // 拼接参数为{key}{value}格式
  398. $input = '';
  399. foreach ($queryParams as $key => $value)
  400. {
  401. $input .= $key . $value;
  402. }
  403. // 追加请求路径
  404. $path = parse_url($url, PHP_URL_PATH);
  405. $input = $path . $input;
  406. if ($body)
  407. {
  408. $input .= json_encode($body);
  409. }
  410. // 使用app_secret包裹生成的字符串
  411. $input = $appSecret . $input . $appSecret;
  412. // 生成签名
  413. $sign = hash_hmac('sha256', $input,$appSecret);
  414. return $sign;
  415. }
  416. public function su_curl($data,$url,$header=array(),$date_type='json',$timeout=300,$httptype="POST",$userpwd='')
  417. {
  418. if ($date_type == 'http_build_query')
  419. {
  420. $data = http_build_query($data);
  421. }
  422. else if ($date_type == 'json')
  423. {
  424. $data = json_encode($data);
  425. }
  426. $ch = curl_init();
  427. curl_setopt($ch, CURLOPT_URL, $url);
  428. curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  429. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  430. curl_setopt($ch, CURLOPT_HEADER, false);
  431. switch ($httptype)
  432. {
  433. case "GET":
  434. curl_setopt($ch, CURLOPT_HTTPGET, true);
  435. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  436. break;
  437. case "POST":
  438. curl_setopt($ch, CURLOPT_POST, true);
  439. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  440. break;
  441. case "PUT":
  442. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  443. break;
  444. case "DELETE":
  445. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  446. break;
  447. }
  448. $isSecure = strpos($url, "https://");
  449. if ($isSecure === 0)
  450. {
  451. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  452. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  453. }
  454. if(!empty($header))
  455. {
  456. curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
  457. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  458. }
  459. if(!empty($userpwd))
  460. {
  461. curl_setopt($ch,CURLOPT_USERPWD,$userpwd);
  462. }
  463. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  464. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  465. $result = curl_exec($ch);
  466. curl_close($ch);
  467. return $result;
  468. }
  469. } //end class