Apitt_new.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Apitt extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_api','api');
  7. $this->load->_model('Model_shop','shop');
  8. $this->load->_model('Model_fullorder','fullorder');
  9. $this->load->_model('Model_fullordertt','fullordertt');
  10. $this->load->_model('Model_fullordersmt','fullordersmt');
  11. $this->load->_model('Model_country','country');
  12. $this->load->_model('Model_hl','hl');
  13. $this->load->_model('Model_is','is');
  14. $this->load->_model('Model_kdniao','kdniao');
  15. $this->load->_model('Model_typeclass','typeclass');
  16. $this->load->_model('Model_warehouse','warehouse');
  17. $this->load->_model('Model_fullorderexcel','fullorderexcel');
  18. $this->load->_model('Model_dhl','dhl');
  19. $this->load->_model('Model_usps','usps');
  20. $this->load->_model('Model_ups','ups');
  21. $this->load->_model('Model_dhltest','dhltest');
  22. $this->load->_model('Model_customer','customer');
  23. $this->load->_model('Model_express','express');
  24. $this->load->_model('Model_notice','notice');
  25. $this->load->_model('Model_emaildata','emaildata');
  26. $this->load->_model('Model_whlabel','whlabel');
  27. $this->load->_model('Model_productdescribe','productdescribe');
  28. $this->load->_model('Model_ljg','ljg');
  29. $this->load->_model('Model_commodityread','commodityread');
  30. $this->load->_model('Model_service','service');
  31. $this->load->_model('Model_fedex','fedex');
  32. $this->load->_model('Model_apismt','apismt');
  33. $this->load->_model('Model_cne','cne');
  34. $this->load->_model('Model_specialstock','specialstock');
  35. $this->load->_model('Model_whlabellabel','whlabellabel');
  36. $this->load->_model('Model_allocation','allocation');
  37. $this->load->_model('Model_yswaybill','yswaybill');
  38. $this->load->_model('Model_paypal','paypal');
  39. $this->load->_model('Model_17track','17track');
  40. $this->load->_model('Model_ck','ck');
  41. $this->load->_model('Model_weight','weight');
  42. $this->load->_model('Model_pay','pay');
  43. $this->load->_model('Model_setting','setting');
  44. $this->load->_model('Model_classid','classid');
  45. $this->load->_model('Model_apitt','apitt');
  46. $this->load->_model('Model_customertt','customertt');
  47. $this->load->_model('Model_als','als');
  48. $this->load->_model('Model_apiyy','apiyy');
  49. $this->load->_model('Model_whlabel_fc','whlabel_fc');
  50. $this->load->_model('Model_shopsku','shopsku');
  51. }
  52. //定义方法的调用规则 获取URI第二段值
  53. public function _remap($arg,$arg_array)
  54. {
  55. if($arg == 'sq')
  56. {
  57. $this->_sq();
  58. }
  59. else if($arg == 'token')
  60. {
  61. $this->_token();
  62. }
  63. else if($arg == 'tb')
  64. {
  65. $this->_tb();
  66. }
  67. else if($arg == 'webhook')
  68. {
  69. $this->_webhook();
  70. }
  71. else if($arg == 'cs')
  72. {
  73. $this->_cs();
  74. }
  75. else if($arg == 'csa')
  76. {
  77. $this->_csa();
  78. }
  79. }
  80. public function get_list($shop,$from,$to,$order_status,$cuy,$num,$dtc,$money,$int,$dtctitle,$pay,$typeclass,$ex,$sjc)//获取订单列表
  81. {
  82. $url = 'https://open-api.tiktokglobalshop.com';
  83. $link = '/order/202309/orders/search';
  84. $time = time();
  85. $post['create_time_ge'] = $from;
  86. $post['create_time_lt'] = $to;
  87. $post['order_status'] = $order_status;
  88. $queryParams['page_size'] = 100;
  89. $queryParams['sort_order'] = 'DESC';
  90. $queryParams['sort_field'] = 'create_time';
  91. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  92. $queryParams['shop_id'] =$shop['codeid'];
  93. $queryParams['app_key'] =$shop['app_key'];
  94. $queryParams['timestamp'] =$time;
  95. $link .= '?'.http_build_query($queryParams);
  96. $sign = $this->apitt->sign($link,$shop['app_secret'],$post);
  97. $url .= $link.'&sign='.$sign;
  98. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  99. $res = $this->apitt->su_curl($post,$url,$headers);
  100. $res = json_decode($res,true);
  101. $list = array();$page_token = '';$arr = array();
  102. if(isset($res['data']['orders']))
  103. {
  104. if($res['data']['total_count'] > 100)
  105. {
  106. $n = ceil($res['data']['total_count']/100);
  107. for($i=0;$i<$n;$i++)
  108. {
  109. $net_date = $this->get_next_list($shop,$from,$to,$order_status,$page_token);
  110. $page_token = $net_date[1];
  111. foreach ($net_date['0'] as $val)
  112. {
  113. $d = $this->fullordertt->get_orderinfo($val['id']);
  114. if(!$d)
  115. {
  116. $list[] = $val['id'];
  117. }
  118. }
  119. }
  120. }
  121. else
  122. {
  123. foreach ($res['data']['orders'] as $val)
  124. {
  125. $d = $this->fullordertt->get_orderinfo($val['id']);
  126. if(!$d)
  127. {
  128. $list[] = $val['id'];
  129. }
  130. }
  131. }
  132. $cs = array();
  133. if(count($list) > 50)
  134. {
  135. $n = ceil(count($list)/50);$x = 0;
  136. for($i=0;$i<$n;$i++)
  137. {
  138. $k = ($i<1)?0:$i*50;
  139. $data = array_slice($list,$k,50);
  140. $d = $this->get_data($data,$shop);
  141. $cs[] = $d;
  142. if(isset($d['data']))
  143. {
  144. foreach ($d['data']['orders'] as $val)
  145. {
  146. $arr[$x] = $val;
  147. $x++;
  148. }
  149. }
  150. }
  151. }
  152. else
  153. {
  154. $d = $this->get_data($list,$shop);
  155. if(isset($d['data']))
  156. {
  157. foreach ($d['data']['orders'] as $val)
  158. {
  159. $arr[] = $val;
  160. }
  161. }
  162. }
  163. }
  164. $read = array();
  165. $gnum = array();
  166. foreach ($arr as $val)
  167. {
  168. $create_time = substr($val['create_time'],0,10)-$sjc;
  169. if(isset($gnum[date('ymd',$create_time)]))
  170. {
  171. $gnum[date('ymd',$create_time)] += 1;
  172. }
  173. else
  174. {
  175. $num = $this->fullordertt->find_count('gtime = "'.date('Ymd',$create_time).'" and shop = "'.$shop['id'].'"');
  176. $gnum[date('ymd',$create_time)] = $num+1;
  177. }
  178. $read[] = $this->get_read($val,$time,$shop,$cuy,$gnum[date('ymd',$create_time)],$dtc,$money,$int,$dtctitle,$pay,$typeclass,$ex,$sjc);
  179. }
  180. return array($read,$list);
  181. }
  182. public function get_next_list($shop,$from,$to,$order_status,$page_token='')//获取订单列表
  183. {
  184. $url = 'https://open-api.tiktokglobalshop.com';
  185. $link = '/order/202309/orders/search';
  186. $time = time();
  187. $post['create_time_ge'] = $from;
  188. $post['create_time_lt'] = $to;
  189. $post['order_status'] = $order_status;
  190. $queryParams['page_size'] = 100;
  191. $queryParams['sort_order'] = 'DESC';
  192. $queryParams['sort_field'] = 'create_time';
  193. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  194. $queryParams['shop_id'] =$shop['codeid'];
  195. $queryParams['app_key'] =$shop['app_key'];
  196. $queryParams['timestamp'] =$time;
  197. if($page_token)
  198. {
  199. $queryParams['page_token'] = $page_token;
  200. }
  201. $link .= '?'.http_build_query($queryParams);
  202. $sign = $this->apitt->sign($link,$shop['app_secret'],$post);
  203. $url .= $link.'&sign='.$sign;
  204. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  205. $res = $this->apitt->su_curl($post,$url,$headers);
  206. $res = json_decode($res,true);
  207. if(isset($res['data']['orders']))
  208. {
  209. return array($res['data']['orders'],$res['data']['next_page_token']);
  210. }
  211. }
  212. public function get_data($list,$shop)
  213. {
  214. $url = 'https://open-api.tiktokglobalshop.com';
  215. $link = '/order/202309/orders';
  216. $time = time();
  217. $ids = implode(",",$list);
  218. $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'&timestamp='.$time.'&ids='.$ids;
  219. $sign = $this->apitt->sign($link,$shop['app_secret'],'');
  220. $url .= $link.'&sign='.$sign;
  221. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  222. $res = $this->apitt->su_curl('',$url,$headers,$date_type='json',$timeout=300,$httptype="GET");
  223. $res = json_decode($res,true);
  224. return $res;
  225. }
  226. public function _csa()
  227. {
  228. $data = $this->fullordertt->get_orderinfo(576607595986915436);
  229. $shop = $this->shop->read($data['shop']);
  230. $url = 'https://open-api.tiktokglobalshop.com';
  231. $link = '/order/202309/orders';
  232. $time = time();
  233. $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'&timestamp='.$time.'&ids='.$data['orderinfo'];
  234. $sign = $this->apitt->sign($link,$shop['token'],$shop['app_secret']);
  235. $url .= $link.'&sign='.$sign;
  236. $headers = array('x-tts-access-token:'.$shop['token']);
  237. $res = $this->api->su_curl('',$url,$headers,$date_type='json',$timeout=300,$httptype="GET",$userpwd='');
  238. $res = json_decode($res,true);
  239. echo "<pre>";
  240. print_r($res);
  241. exit;
  242. $data = $this->fullordertt->get_orderinfo(576607595986915436);
  243. $shop = $this->shop->read($data['shop']);
  244. $list = array($data['orderinfo']);
  245. $url = 'https://open-api.tiktokglobalshop.com';
  246. $link = '/api/orders/detail/query';
  247. $time = time();
  248. $post = array();
  249. $post['order_id_list'] = json_encode($list);
  250. $sign = $link.'app_key'.$shop['app_key'].'timestamp'.$time;
  251. $sign = $shop['app_secret'].$sign.$shop['app_secret'];
  252. $sign = hash_hmac('sha256', $sign, $shop['app_secret']);
  253. $url .= $link.'?access_token='.$shop['token'].'&app_key='.$shop['app_key'].'&sign='.$sign.'&timestamp='.$time;
  254. $ch = curl_init();
  255. curl_setopt($ch, CURLOPT_URL,$url);
  256. curl_setopt($ch, CURLOPT_POST, 1);
  257. curl_setopt($ch, CURLOPT_HEADER, 0);
  258. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  259. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  260. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  261. curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));
  262. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  263. $res = curl_exec($ch);
  264. curl_close($ch);
  265. $res = json_decode($res,true);
  266. echo "<pre>";
  267. print_r($res);
  268. }
  269. public function _sq()
  270. {
  271. //授权获取app_key和code用于下一步获取token
  272. $post = $this->input->get(NULL, TRUE);
  273. if(isset($post['app_key']))
  274. {
  275. $app_key = $this->input->get('app_key',true);
  276. $code = $this->input->get('code',true);
  277. if($code && $app_key)
  278. {
  279. $shop = $this->shop->app_key($app_key);
  280. if($shop)
  281. {
  282. $url = 'https://auth.tiktok-shops.com/api/v2/token/get?app_key='.$app_key.'&auth_code='.$code.'&app_secret='.$shop['app_secret'].'&grant_type=authorized_code';
  283. $ch = curl_init();
  284. curl_setopt($ch, CURLOPT_URL,$url);//?status=any所有订单 &limit=200每页多少单
  285. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  286. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  287. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  288. $res = curl_exec($ch);
  289. curl_close($ch);
  290. $res = json_decode($res,true);
  291. if(isset($res['data']))
  292. {
  293. $shop_cipher = '';
  294. if($shop['shop_cipher'] == '')
  295. {
  296. $shopcipher = $this->_shop_cipher($shop['id']);
  297. if(isset($shopcipher['data']['shops'][0]['cipher']))
  298. {
  299. $shop_cipher = $shopcipher['data']['shops'][0]['cipher'];
  300. }
  301. }
  302. $this->shop->save(array('token'=>$res['data']['access_token'],'tokentime'=>$res['data']['access_token_expire_in']-48*3600,'refresh_token'=>$res['data']['refresh_token'],'app_key'=>$app_key,'code'=>$code,'shop_cipher'=>$shop_cipher),$shop['id']);
  303. echo 'ok';
  304. }
  305. else
  306. {
  307. echo json_encode($res);
  308. }
  309. }
  310. else
  311. {
  312. echo "没有此店铺";
  313. }
  314. }
  315. }
  316. }
  317. public function _token()//判断tokentime通过refresh_token刷新token
  318. {
  319. $shop = $this->shop->find_all("type = '1514'");//找TT店铺
  320. foreach ($shop as $v)
  321. {
  322. if($v['tokentime'] < time()+20*24*3600)
  323. {
  324. $url = 'https://auth.tiktok-shops.com/api/v2/token/refresh?app_key='.$v['app_key'].'&app_secret='.$v['app_secret'].'&grant_type=refresh_token&refresh_token='.$v['refresh_token'];
  325. $ch = curl_init();
  326. curl_setopt($ch, CURLOPT_URL,$url);
  327. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  328. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  329. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  330. $res = curl_exec($ch);
  331. curl_close($ch);
  332. $res = json_decode($res,true);
  333. if(isset($res['data']))
  334. {
  335. $shop_cipher = '';
  336. if($v['shop_cipher'] == '')
  337. {
  338. $shopcipher = $this->_shop_cipher($v['id']);
  339. if(isset($shopcipher['data']['shops'][0]['cipher']))
  340. {
  341. $shop_cipher = $shopcipher['data']['shops'][0]['cipher'];
  342. }
  343. }
  344. $this->shop->save(array('token'=>$res['data']['access_token'],'tokentime'=>$res['data']['access_token_expire_in']-48*3600,'refresh_token'=>$res['data']['refresh_token'],'shop_cipher'=>$shop_cipher),$v['id']);
  345. }
  346. else
  347. {
  348. echo json_encode($res);
  349. exit;
  350. }
  351. }
  352. }
  353. echo "ok";
  354. }
  355. public function _shop_cipher($id)
  356. {
  357. $shop = $this->shop->read($id);
  358. $url = 'https://open-api.tiktokglobalshop.com';
  359. $link = '/authorization/202309/shops';
  360. $time = time();
  361. $queryParams = array();
  362. $queryParams['access_token'] = $shop['token'];
  363. $queryParams['app_key'] = $shop['app_key'];
  364. $queryParams['shop_id'] = $shop['codeid'];
  365. $queryParams['timestamp'] = $time;
  366. $queryParams['version'] = '202309';
  367. $link .= '?' . http_build_query($queryParams);
  368. $sign = $this->apitt->sign($link,$shop['token'],$shop['app_secret']);
  369. $url .= $link.'&sign='.$sign;
  370. $headers = array('x-tts-access-token:'.$shop['token']);
  371. $ch = curl_init();
  372. curl_setopt($ch, CURLOPT_URL, $url);
  373. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  374. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  375. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  376. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  377. $res = curl_exec($ch);
  378. curl_close($ch);
  379. $res = json_decode($res,true);
  380. return $res;
  381. }
  382. public function _tb()
  383. {
  384. $post = $this->input->post(NULL, TRUE);
  385. $api = $this->input->get('api',true);
  386. $dshop = $this->input->get('dshop',true);
  387. $gethl = array();
  388. /* 匹配加入 */
  389. $dictionaries = $this->typeclass->find_all('spare!= "" and classid != 1 and classid != 2 and classid != 3 and classid != 4 and classid != 5 and classid != 11 and classid != 16 and classid != 17 and classid != 20 and classid != 21 and classid != 23 and classid != 24 and classid != 29 and classid != 30 and classid != 31 and classid != 32 and classid != 36');
  390. // and (classid=13 or classid=22 or classid=8 or classid=15 or classid=27 or classid=25 or classid=26 or classid=18 or classid=14 or classid=9 or classid=12 or classid=10 or classid=6 or classid=100 or classid=999)
  391. $dtc = array();
  392. foreach ($dictionaries as $v)
  393. {
  394. if(stripos($v['spare'],'|') !== false)//如果有多个值
  395. {
  396. $v['spare'] = explode('|',$v['spare']);
  397. foreach ($v['spare'] as $k=>$vs)
  398. {
  399. if(stripos($v['zh'],'|') !== false)
  400. {
  401. $vzh = explode('|',$v['zh']);
  402. $dtc[strtolower($vs)] = array('classid'=>$v['classid'],'zh'=>$vzh[$k],'id'=>$v['id']);
  403. }
  404. else
  405. {
  406. $dtc[strtolower($vs)] = array('classid'=>$v['classid'],'zh'=>$v['zh'],'id'=>$v['id']);
  407. }
  408. }
  409. }
  410. else
  411. {
  412. $dtc[strtolower($v['spare'])] = array('classid'=>$v['classid'],'zh'=>$v['zh'],'id'=>$v['id']);
  413. }
  414. }
  415. $dictionaries2 = $this->typeclass->find_all();
  416. foreach ($dictionaries2 as $v)
  417. {
  418. $dtctitle[$v['id']] = $v['title'];
  419. }
  420. /* 匹配结束 */
  421. /* 价格加入 */
  422. $money = array();
  423. /**
  424. $productdescribe = $this->productdescribe->find_all('1=1','number,purchase,cost,salesprice,title');
  425. foreach ($productdescribe as $v)
  426. {
  427. $money[$v['number']] = array('purchase'=>$v['purchase'],'cost'=>$v['cost'],'salesprice'=>$v['salesprice'],'title'=>$v['title']);
  428. }
  429. **/
  430. /* 价格加入 */
  431. /* 匹配ID加入 */
  432. $int = array();
  433. $intdata = $this->typeclass->find_all('classid=14','id,spare');
  434. foreach ($intdata as $v)
  435. {
  436. $v['spare'] = explode('|',$v['spare']);
  437. $int[$v['id']] = $v['spare'][0];
  438. }
  439. /* 匹配ID结束 */
  440. /* 国家加入键值-k */
  441. $cuy = array();
  442. $cuydata = $this->country->find_all('1=1','lb,id');
  443. foreach ($cuydata as $v)
  444. {
  445. $cuy[$v['lb']] = $v['id'];//获取到国家ID
  446. }
  447. /* 国家加入键值-j */
  448. $typeclass = array();
  449. $tdata = $this->typeclass->find_all('classid=30','id,title');
  450. foreach ($tdata as $v)
  451. {
  452. $typeclass[$v['title']] = $v['id'];
  453. }
  454. $ex = array();
  455. $express = $this->express->find_all('1=1','id,title');
  456. foreach ($express as $v)
  457. {
  458. $ex[$v['title']] = array('id'=>$v['id']);
  459. }
  460. /* 币种加入键值-j */
  461. /* 支付方式-k */
  462. $pay = array();$sd = array();
  463. $tdata = $this->typeclass->find_all('classid=4','id,spare');
  464. foreach ($tdata as $v)
  465. {
  466. $paytype = $this->pay->get_typeclass($v['id']);
  467. if(isset($paytype['estimaterate']))
  468. {
  469. $estimaterate = $paytype['estimaterate'];
  470. }
  471. else
  472. {
  473. $estimaterate = 0;
  474. }
  475. if(stripos($v['spare'],'|') !== false)//如果有多个值
  476. {
  477. $v['spare'] = explode('|',$v['spare']);
  478. foreach ($v['spare'] as $k=>$vs)
  479. {
  480. $pay[$vs] = array('id'=>$v['id'],'estimaterate'=>$estimaterate);
  481. }
  482. }
  483. else
  484. {
  485. $pay[$v['spare']] = array('id'=>$v['id'],'estimaterate'=>$estimaterate);
  486. }
  487. }
  488. /* 支付方式-j */
  489. /* 新增SKU-k */
  490. $zjsku = array();
  491. $shopsku = $this->shopsku->find_all();
  492. foreach ($shopsku as $val)
  493. {
  494. $ss = explode(',',trim($val['shop'],','));
  495. foreach ($ss as $v)
  496. {
  497. if(isset($zjsku[$v]))
  498. {
  499. $zjsku[$v] .= ','.trim($val['sku'],',');
  500. }
  501. else
  502. {
  503. $zjsku[$v] = trim($val['sku'],',');
  504. }
  505. }
  506. }
  507. /* 新增SKU-j */
  508. $time = time();
  509. $tb = 0;
  510. if($api = '89757')
  511. {
  512. $shop = $this->shop->find_all("type = '1514' and tb = '1'");//TT店铺
  513. foreach ($shop as $value)
  514. {
  515. if((time()-$value['tbtime']) < 500)
  516. {
  517. $tb = 1;//正在同步中
  518. break;
  519. }
  520. $setting = $this->setting->get_settings();
  521. $sjc = 15*3600+$setting['sctime'];//时间差
  522. $num = $this->fullordertt->find_count('gtime = "'.date('Ymd',$time).'" and shop = "'.$value['id'].'"');
  523. //$this->db->trans_begin();
  524. $res = $this->apitt->get_list($value,$time-18*3600,$time,'AWAITING_SHIPMENT',$cuy,$num,$dtc,$money,$int,$dtctitle,$pay,$typeclass,$ex,$sjc,$zjsku);//查询2小时数 据-等待装运=AWAITING_SHIPMENT(111) ;-等待收集=AWAITING_COLLECTION (112);在途=IN_TRANSIT (121);-已交付=DELIVERED (122);
  525. $sd[] = $res;
  526. $res = $res[0];
  527. if($res)
  528. {
  529. foreach ($res as $v)
  530. {
  531. $post = $v;
  532. $dataorder = $this->fullordertt->get_orderinfo($v['orderinfo']);
  533. if(!isset($dataorder['id']))
  534. {
  535. /** 启用自动分配仓库 **/
  536. $warehouse = $this->warehouse->find_all("fpzd = '1'",'*','fpsx desc');
  537. if($post['cf'] == 1)
  538. {
  539. $post['type'] = 34;//TT仓
  540. }
  541. else
  542. {
  543. foreach ($warehouse as $v)
  544. {
  545. if($v['bdcountry'] != 0 && $v['bdcountry'] != $post['country'])
  546. {
  547. continue;
  548. }
  549. $yx = 'get_yx_'.$v['bdck'];
  550. $kczd = $this->ck->$yx($post['state'],$v,$post['shop'],$post['number'],$post['whlabel'],$post['fpdata']);
  551. if($kczd['t'] == 0)
  552. {
  553. $country = $this->country->read($post['country']);//获取国家中配置物流
  554. if($country['express'] != 0)
  555. {
  556. $post['express'] = $country['express'];
  557. }
  558. if($v['express'] != 0)//优先使用仓库指定物流,替换掉上面的国家配置物流
  559. {
  560. $post['express'] = $v['express'];
  561. }
  562. $post['type'] = $v['id'];
  563. $post['whlabel'] = $kczd['whlabel'];
  564. $post['fpdata'] = $kczd['fpdata'];
  565. if(stripos($kczd['whlabel'],$v['hz']) !== false)
  566. {
  567. break 1;
  568. }
  569. }
  570. }
  571. $qdw = $this->warehouse->read($post['type']);
  572. $fl = $this->ck->get_fl($qdw['hz'],$post['whlabel'],$post['fpdata']);
  573. $post['sfxh'] = $fl['sfxh'];
  574. $post['ckfl'] = $fl['ckfl'];
  575. //自动写位置 开始
  576. $zclp = array();$pxsl = array();
  577. $slpx = explode('|',$post['slpx']);//排序
  578. foreach ($slpx as $k=>$v)
  579. {
  580. $v = explode('-',$v);
  581. if(!isset($v[1]))
  582. {
  583. $v[1] == 1;
  584. }
  585. for($i=$v[0];$i<$v[1];$i++)
  586. {
  587. $pxsl[$i] = $k;
  588. }
  589. }
  590. $mc = $qdw['bdck'];$zdidjl = '';$zdwzjl = '';
  591. if($qdw['wz'] == 1)
  592. {
  593. $pm = explode(';',trim($post['shipremarks'],';'));
  594. foreach ($pm as $k=>$vv)
  595. {
  596. $vv = trim($vv,' ');
  597. $lp = $this->typeclass->find_all("zh = '$vv'");
  598. if(isset($lp[0]['classid']))
  599. {
  600. $zclp[] = $vv;
  601. unset($pm[$k]);
  602. }
  603. }
  604. $pm = array_values($pm);
  605. $w = explode('|',trim($post['whlabel'],'|'));
  606. foreach ($w as $k=>$val)
  607. {
  608. $hwm = '';
  609. if(stripos($val,$qdw['hz']) !== false)
  610. {
  611. $num = explode('-',trim($val,'-'));
  612. $xq = explode('~',trim($num[2],'~'));
  613. $pmxq = '(';
  614. foreach ($xq as $v)
  615. {
  616. $zdidjl .= '('.trim($v,$qdw['hz']).')';
  617. $h = $this->$mc->read(trim($v,$qdw['hz']));
  618. if($h['details'] != '')
  619. {
  620. $pmxq .= $qdw['title'].':'.$h['details'].(($h['cpid']>0)?'-'.$h['cpid']:'').',';
  621. }
  622. }
  623. $xrid = ($h['cpid']>0)?'-'.$h['cpid']:'';
  624. if(isset($pxsl[$k]))
  625. {
  626. if(stripos($pm[$pxsl[$k]],$qdw['title'].':'.$h['details'].$xrid) === false)
  627. {
  628. $pm[$pxsl[$k]] .= trim($pmxq,',').')';
  629. }
  630. }
  631. $zdwzjl .= $h['details'].(($h['cpid']>0)?'-'.$h['cpid']:'');
  632. }
  633. }
  634. if($zclp)
  635. {
  636. $zclp = ';'.implode(";",$zclp);
  637. }
  638. else
  639. {
  640. $zclp = '';
  641. }
  642. $post['shipremarks'] = implode(";",$pm).$zclp;
  643. $post['contents'] = $zdidjl.' - '.$zdwzjl;
  644. }
  645. }
  646. //自动写位置 结束
  647. /** **/
  648. //合并重复项
  649. $hbcfw = explode('|',trim($post['whlabel'],'|'));
  650. $hbcff = explode(';',trim($post['fpdata'],';'));
  651. $c = 0;
  652. $hbw = array();
  653. $hbf = array();
  654. for($i=0;$i<count($hbcfw);$i++)
  655. {
  656. $w = explode('-',$hbcfw[$i]);
  657. $f = explode('|',$hbcff[$i]);
  658. $w2 = ($w[2]==0)?0:'';
  659. if(!isset($hbw[$w[0].$w2]))
  660. {
  661. $hbw[$w[0].$w2] = $hbcfw[$i];
  662. $hbf[$w[0].$w2] = $hbcff[$i];
  663. }
  664. else
  665. {
  666. $c++;
  667. $cw = explode('-',$hbw[$w[0].$w2]);
  668. $cf = explode('|',$hbf[$w[0].$w2]);
  669. if($cw[2] != 0 && $w[2] != 0)
  670. {
  671. $cf[2] += $f[2];
  672. $cf[9] .= '~'.$f[9];
  673. $hbf[$w[0].$w2] = implode("|",$cf);
  674. $cw[1] += $f[2];
  675. $cw[2] .= '~'.$f[9];
  676. $hbw[$w[0].$w2] = implode("-",$cw);
  677. }
  678. else if($cw[2] == 0 && $w[2] == 0)
  679. {
  680. $cf[2] += $f[2];
  681. $hbf[$w[0].$w2] = implode("|",$cf);
  682. $cw[1] += $f[2];
  683. $hbw[$w[0].$w2] = implode("-",$cw);
  684. }
  685. else
  686. {
  687. $hbw[] = $hbcfw[$i];
  688. $hbf[] = $hbcff[$i];
  689. }
  690. }
  691. }
  692. if($c > 0)
  693. {
  694. $post['fpdata'] = implode(";",$hbf).';';
  695. $post['whlabel'] = '|'.implode("|",$hbw).'|';
  696. }
  697. //合并重复项结束
  698. //非占单设置为禁止占单开始
  699. $hbw = explode('|',trim($post['whlabel'],'|'));
  700. $hbf = explode(';',trim($post['fpdata'],';'));
  701. foreach ($hbw as $k=>$val)
  702. {
  703. $w = explode('-',$val);
  704. $f = explode('|',$hbf[$k]);
  705. if(!isset($w[2]) || $w[2] == 0)
  706. {
  707. $w[2] = "DNOTO";
  708. $f[9] = "DNOTO";
  709. $hbf[$k] = implode("|",$f);
  710. $hbw[$k] = implode("-",$w);
  711. }
  712. }
  713. $post['fpdata'] = implode(";",$hbf).';';
  714. $post['whlabel'] = '|'.implode("|",$hbw).'|';
  715. //非占单设置为禁止占单结束
  716. //查询净重开始
  717. $post['jweight'] = $this->weight->get_weightcx($post['fpdata']);
  718. //查询净重结束
  719. $tjid = $this->fullordertt->insert($post);
  720. if($post['email'] == '')
  721. {
  722. $post['email'] = $post['name'].'@qq.com';
  723. }
  724. $adrs = $this->customertt->get_email($post['email'],$post['shop']);
  725. preg_match_all('/[0-9]/u',$post['phone'],$result);
  726. $numphone = join('',$result[0]);
  727. $hmdaddress1 = str_replace(array(' parkway',' drive',' street',' road',' avenue',' boulevard',' alley',' court',' place',' lane',' manor',' terrace',' trail',' view',' cove'),array(' pkwy',' dr',' st',' rd',' av',' blvd',' aly',' ct',' pl',' ln',' mnr',' ter',' trl',' vw',' cv'),strtolower($post['address']));
  728. $hmdaddress2 = str_replace(array(' pkwy',' dr',' st',' rd',' av',' blvd',' aly',' ct',' pl',' ln',' mnr',' ter',' trl',' vw',' cv'),array(' parkway',' drive',' street',' road',' avenue',' boulevard',' alley',' court',' place',' lane',' manor',' terrace',' trail',' view',' cove'),strtolower($post['address']));
  729. $hmdaddress3 = str_replace(array(' avenue',' ave',' av'),array(' ave',' avenue',' ave'),strtolower($post['address']));
  730. $hmdid = '';
  731. $post['address'] = str_replace("'","&#8217;",$post['address']);
  732. $hmdaddress1 = str_replace("'","&#8217;",$hmdaddress1);
  733. $hmdaddress2 = str_replace("'","&#8217;",$hmdaddress2);
  734. $customerb = $this->customertt->find_all('type = 2 and (email = "'.$post['email'].'" or address = "'.$post['address'].'" or address = "'.$hmdaddress1.'" or address = "'.$hmdaddress2.'" or address = "'.$hmdaddress3.'" or numphone = "'.$numphone.'")','*','id desc');
  735. $lkh = (isset($customerb[0]['id']))?2:1;//白名单1黑名单2
  736. if($lkh == 2)
  737. {
  738. $hmdid = $customerb[0]['id'].'-('.$post['email'].'|'.$post['address'].'|'.$numphone.')'.'('.$customerb[0]['email'].'|'.$customerb[0]['address'].$customerb[0]['numphone'].')';
  739. }
  740. if($adrs)
  741. {
  742. $pdlkh = 1;//这个是判断是否老客户
  743. $this->customertt->save(array('time'=>$post['dtime'],'num'=>$adrs['num']+1,'money'=>$adrs['money']+$post['shouldmoney']),$adrs['id']);
  744. $khid = $adrs['id'];
  745. }
  746. else
  747. {
  748. $str = $post['phone'];
  749. preg_match_all('/[0-9]/u',$str,$result);
  750. $numphone = join('',$result[0]);
  751. $ct['shop'] = $post['shop'];
  752. $ct['source'] = 9;
  753. $ct['level'] = 5;
  754. $ct['country'] = $post['country'];
  755. $ct['time'] = $post['dtime'];
  756. $ct['num'] = 1;
  757. $ct['money'] = $post['shouldmoney'];
  758. $ct['zipcode'] = $post['zipcode'];
  759. $ct['name'] = $post['name'];
  760. $ct['phone'] = $post['phone'];
  761. $ct['numphone'] = $numphone;
  762. $ct['email'] = preg_replace('/( | | |\s)+/','',$post['email']);
  763. $ct['province'] = $post['province'];
  764. $ct['city'] = $post['city'];
  765. $ct['address'] = $post['address'];
  766. $ct['zhcx'] = $post['shop'].'-'.$ct['email'];
  767. $khid = $this->customertt->insert($ct);
  768. $pdlkh = 0;
  769. }
  770. }//else{}如果有此订单
  771. }
  772. }
  773. /**
  774. if ($this->db->trans_status() === FALSE)
  775. {
  776. $this->db->trans_commit();
  777. }
  778. else
  779. {
  780. $this->db->trans_commit();
  781. }
  782. **/
  783. //sleep(1);//停留2秒
  784. $this->shop->save(array('tbtime'=>time()),$value['id']);
  785. }
  786. if($tb == 1)
  787. {
  788. echo json_encode(array('msg'=>'有店铺正在同步中,请5分钟后再试!','success'=>true));exit;
  789. }
  790. //echo json_encode(array('msg'=>'OK!','success'=>true));exit;
  791. echo "<pre>";
  792. print_r($sd);
  793. }
  794. }
  795. public function _webhook()
  796. {
  797. $j = '';
  798. $xq = array();
  799. $j = file_get_contents('PHP://input');
  800. $j = json_decode($j, true);
  801. if(isset($j['type']) && isset($j['data']['order_id']))
  802. {
  803. $d = $this->fullordertt->get_orderinfo($j['data']['order_id']);
  804. if($d)
  805. {
  806. $qchz = array();
  807. $f = explode('|',trim($d['whlabel'],'|'));
  808. foreach ($f as $v)
  809. {
  810. $hz = explode('-',trim($v,'-'));
  811. if(isset($hz[2]) && $hz[2] != 'DNOTO' && $hz[2] != '0')
  812. {
  813. $qchz[] = $hz[2];
  814. }
  815. }
  816. $d['whlabel']= str_replace($qchz,'0',$d['whlabel']);
  817. $d['fpdata']= str_replace($qchz,'0',$d['fpdata']);
  818. $this->db->trans_begin();
  819. if($j['type'] == 1)
  820. {
  821. if($j['data']['order_status'] == 'CANCEL' || $j['data']['order_status'] == 'Canceled')
  822. {
  823. $whlabel = $this->whlabel->find_all("zd = '".$d['number']."'");
  824. foreach ($whlabel as $v)
  825. {
  826. $this->whlabel->save(array('zd'=>''),$v['id']);
  827. }
  828. $this->fullordertt->save(array('state'=>217,'whlabel'=>$d['whlabel'],'fpdata'=>$d['fpdata']),$d['id']);
  829. }
  830. else if(($j['data']['order_status'] == 'IN_TRANSIT' || $j['data']['order_status'] == 'DELIVERED' || $j['data']['order_status'] == 'COMPLETED currently)') && $d['state'] != 216)
  831. {
  832. if($d['cf'] == 1 && $d['type'] == 34 && $j['data']['order_status'] == 'IN_TRANSIT')
  833. {
  834. $shop = $this->shop->read($d['shop']);
  835. $wb = $this->apitt->get_data(array($d['orderinfo']),$shop);
  836. $waybill = $wb['data']['order_list']['0']['order_line_list']['0']['tracking_number'];
  837. $time = time();
  838. $this->fullordertt->save(array('state'=>216,'whlabel'=>$d['whlabel'],'fpdata'=>$d['fpdata'],'waybill'=>$waybill,'print'=>3,'printtime'=>$time,'printnumber'=>1,'libraryconfirm'=>2,'library'=>2,'librarytime'=>$time,'weight'=>'0.4kg','
  839. review'=>6,'reviewtime'=>$time),$d['id']);
  840. }
  841. else
  842. {
  843. $this->fullordertt->save(array('state'=>216,'whlabel'=>$d['whlabel'],'fpdata'=>$d['fpdata']),$d['id']);
  844. }
  845. }
  846. }
  847. if($j['type'] == 2)
  848. {
  849. if($j['reverse_order_status '] == 50 || $j['reverse_order_status '] == 51)
  850. {
  851. $whlabel = $this->whlabel->find_all("zd = '".$d['number']."'");
  852. foreach ($whlabel as $v)
  853. {
  854. $this->whlabel->save(array('zd'=>''),$v['id']);
  855. }
  856. $this->fullordertt->save(array('state'=>214,'whlabel'=>$d['whlabel'],'fpdata'=>$d['fpdata']),$d['id']);
  857. }
  858. /**
  859. reverse_order_status
  860. 售后申请 = 1
  861. 售后拒绝申请 = 2
  862. 售后退货 = 3
  863. 售后买家已发货 = 4
  864. 售后卖家_拒绝接收 = 5
  865. 售后成功 = 50
  866. 取消成功 = 51
  867. 关闭 = 99
  868. 完成 = 100
  869. **/
  870. }
  871. if ($this->db->trans_status() === TRUE)
  872. {
  873. $this->db->trans_commit();
  874. header('HTTP/1.1 200 OK');
  875. }
  876. else
  877. {
  878. $this->db->trans_rollback();
  879. }
  880. }
  881. }
  882. }
  883. }