Apitt.php 34 KB

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