Apitt_old.php 32 KB

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