Model_apitt.php 21 KB

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