Apitt.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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. $this->load->_model('Model_ttwebhook','ttwebhook');
  53. $this->load->_model('Model_ttcf','ttcf');
  54. $this->load->_model('Model_producttitle','producttitle');
  55. $this->load->_model('Model_17track','17track');
  56. $this->load->_model("Model_logic_ding","logic_ding");
  57. }
  58. //定义方法的调用规则 获取URI第二段值
  59. public function _remap($arg,$arg_array)
  60. {
  61. if($arg == 'sq')
  62. {
  63. $this->_sq();
  64. }
  65. else if($arg == 'token')
  66. {
  67. $this->_token();
  68. }
  69. else if($arg == 'tb')
  70. {
  71. $this->_tb();
  72. }
  73. else if($arg == 'webhook')
  74. {
  75. $this->_webhook();
  76. }
  77. else if($arg == 'ttwebhook')
  78. {
  79. $this->_ttwebhook();
  80. }
  81. else if($arg == 'cs')
  82. {
  83. $this->_cs();
  84. }
  85. else if($arg == 'cf')
  86. {
  87. $this->_cf();//催付
  88. }
  89. else if($arg == 'csa')
  90. {
  91. $this->_csa($arg_array);
  92. }
  93. else if($arg == 'shipping')
  94. {
  95. $this->_shipping();//获取承运商
  96. }
  97. else if($arg == 'update')
  98. {
  99. $this->_update();//上传运输信息
  100. }
  101. else if($arg == 'gxbudget')
  102. {
  103. $this->_gxbudget();//更新预估金额
  104. }
  105. }
  106. public function _cf()//获取订单列表
  107. {
  108. $j = '{
  109. "type": 1,
  110. "tts_notification_id": "7374501892938712875",
  111. "shop_id": "7495158362826508884",
  112. "timestamp": 1717010025,
  113. "data": {
  114. "is_on_hold_order": true,
  115. "order_id": "576643322058150895",
  116. "order_status": "UNPAID",
  117. "update_time": 1717010025
  118. }
  119. }';
  120. $j = json_decode($j, true);
  121. if($j['type'] == 1 && $j['data']['order_status'] == 'UNPAID')
  122. {
  123. $shop = $this->shop->get_shopid($j['shop_id']);
  124. if(!$shop)
  125. {
  126. exit;
  127. }
  128. $d = $this->apitt->get_data(array($j['data']['order_id']),$shop);
  129. if(isset($d['data']['orders'][0]['user_id']))
  130. {
  131. $post = array('orderinfo'=>$j['data']['order_id'],'user_id'=>$d['data']['orders'][0]['user_id']);
  132. $id = $this->ttcf->insert($post);
  133. $post['shop'] = $shop['id'];
  134. $post['id'] = $id;
  135. //$this->_znxc($post,$v,3);
  136. }
  137. exit;
  138. }
  139. /*
  140. $time = time();$cs = array();
  141. $shop = $this->shop->find_all("type = '1514' and tb = '1'");//TT店铺
  142. foreach ($shop as $value)
  143. {
  144. //$this->db->trans_begin();
  145. $res = $this->get_list($value,$time-7*24*3600,$time,'UNPAID');//查询2小时数 据-等待装运=AWAITING_SHIPMENT(111) ;-等待收集=AWAITING_COLLECTION (112);在途=IN_TRANSIT (121);-已交付=DELIVERED (122);
  146. //$cs[] = array($res,$value['id']);
  147. foreach ($res as $v)
  148. {
  149. $d = $this->apitt->get_data(array($v),$value);
  150. if(isset($d['data']['orders'][0]['user_id']))
  151. {
  152. $post = array('orderinfo'=>$v,'user_id'=>$d['data']['orders'][0]['user_id']);
  153. $id = $this->ttcf->insert($post);
  154. $post['shop'] = $value['id'];
  155. $post['id'] = $id;
  156. $cs[] = $this->_znxc($post,$v,3);
  157. }
  158. }
  159. }
  160. echo "<pre>";
  161. print_r($cs);
  162. */
  163. }
  164. public function get_list($shop,$from,$to,$order_status)//获取订单列表
  165. {
  166. $url = 'https://open-api.tiktokglobalshop.com';
  167. $link = '/order/202309/orders/search';
  168. $time = time();
  169. $post['create_time_ge'] = $from;
  170. $post['create_time_lt'] = $to;
  171. $post['order_status'] = $order_status;
  172. $queryParams['page_size'] = 100;
  173. $queryParams['sort_order'] = 'DESC';
  174. $queryParams['sort_field'] = 'create_time';
  175. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  176. $queryParams['shop_id'] =$shop['codeid'];
  177. $queryParams['app_key'] =$shop['app_key'];
  178. $queryParams['timestamp'] =$time;
  179. $link .= '?'.http_build_query($queryParams);
  180. $sign = $this->apitt->sign($link,$shop['app_secret'],$post);
  181. $url .= $link.'&sign='.$sign;
  182. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  183. $res = $this->apitt->su_curl($post,$url,$headers);
  184. $res = json_decode($res,true);
  185. $list = array();$page_token = '';$arr = array();
  186. if(isset($res['data']['orders']))
  187. {
  188. if($res['data']['total_count'] > 100)
  189. {
  190. $n = ceil($res['data']['total_count']/100);
  191. for($i=0;$i<$n;$i++)
  192. {
  193. $net_date = $this->apitt->get_next_list($shop,$from,$to,$order_status,$page_token);
  194. $page_token = $net_date[1];
  195. foreach ($net_date['0'] as $val)
  196. {
  197. $d = $this->ttcf->get_orderinfo($val['id']);
  198. if(!$d)
  199. {
  200. $list[] = $val['id'];
  201. }
  202. }
  203. }
  204. }
  205. else
  206. {
  207. foreach ($res['data']['orders'] as $val)
  208. {
  209. $d = $this->ttcf->get_orderinfo($val['id']);
  210. if(!$d)
  211. {
  212. $list[] = $val['id'];
  213. }
  214. }
  215. }
  216. }
  217. return $list;
  218. }
  219. public function _csa($arg_array)
  220. {
  221. $shop = $this->shop->read($arg_array[0]);
  222. $url = 'https://open-api.tiktokglobalshop.com';
  223. $link = '/order/202309/orders/search';
  224. $time = time();
  225. $post['create_time_ge'] = $time-72*3600;
  226. $post['create_time_lt'] = $time;
  227. $post['order_status'] = 'AWAITING_SHIPMENT';
  228. $queryParams['page_size'] = 100;
  229. $queryParams['sort_order'] = 'ASC';
  230. $queryParams['sort_field'] = 'create_time';
  231. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  232. $queryParams['shop_id'] =$shop['codeid'];
  233. $queryParams['app_key'] =$shop['app_key'];
  234. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  235. $queryParams['timestamp'] =$time;
  236. $link .= '?'.http_build_query($queryParams);
  237. $sign = $this->apitt->sign($link,$shop['app_secret'],$post);
  238. $url .= $link.'&sign='.$sign;
  239. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  240. $res = $this->apitt->su_curl($post,$url,$headers);
  241. $res = json_decode($res,true);
  242. echo "<pre>";
  243. print_r($res);
  244. exit;
  245. $data = $this->fullordertt->get_orderinfo(576696136595051237);
  246. $shop = $this->shop->read($data['shop']);
  247. $url = 'https://open-api.tiktokglobalshop.com';
  248. $link = '/order/202309/orders';
  249. $time = time();
  250. $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'&timestamp='.$time.'&ids='.$data['orderinfo'];
  251. $sign = $this->apitt->sign($link,$shop['app_secret'],'');
  252. $url .= $link.'&sign='.$sign;
  253. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  254. $res = $this->apitt->su_curl('',$url,$headers,$date_type='json',$timeout=300,$httptype="GET");
  255. $res = json_decode($res,true);
  256. echo "<pre>";
  257. print_r($res);
  258. exit;
  259. $data = $this->fullordertt->get_orderinfo(576607595986915436);
  260. $shop = $this->shop->read($data['shop']);
  261. $list = array($data['orderinfo']);
  262. $url = 'https://open-api.tiktokglobalshop.com';
  263. $link = '/api/orders/detail/query';
  264. $time = time();
  265. $post = array();
  266. $post['order_id_list'] = json_encode($list);
  267. $sign = $link.'app_key'.$shop['app_key'].'timestamp'.$time;
  268. $sign = $shop['app_secret'].$sign.$shop['app_secret'];
  269. $sign = hash_hmac('sha256', $sign, $shop['app_secret']);
  270. $url .= $link.'?access_token='.$shop['token'].'&app_key='.$shop['app_key'].'&sign='.$sign.'&timestamp='.$time;
  271. $ch = curl_init();
  272. curl_setopt($ch, CURLOPT_URL,$url);
  273. curl_setopt($ch, CURLOPT_POST, 1);
  274. curl_setopt($ch, CURLOPT_HEADER, 0);
  275. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  276. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  277. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  278. curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));
  279. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  280. $res = curl_exec($ch);
  281. curl_close($ch);
  282. $res = json_decode($res,true);
  283. echo "<pre>";
  284. print_r($res);
  285. }
  286. public function _shipping()//获取shipping_providers(运输商)
  287. {
  288. $data = $this->fullordertt->find_all("delivery_type != 1 and express = 63 and waybill != '' and library = '2' and librarytime > 1723257000 and state = 207");
  289. foreach ($data as $v)
  290. {
  291. $shop = $this->shop->read($v['shop']);
  292. $url = 'https://open-api.tiktokglobalshop.com';
  293. $link = '/logistics/202309/delivery_options/'.$v['delivery_option_id'].'/shipping_providers';//delivery_option_id
  294. $time = time();
  295. $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'&timestamp='.$time;
  296. $sign = $this->apitt->sign($link,$shop['app_secret'],'');
  297. $url .= $link.'&sign='.$sign;
  298. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  299. $res = $this->apitt->su_curl('',$url,$headers,$date_type='json',$timeout=300,$httptype="GET");
  300. $res = json_decode($res,true);
  301. if(!isset($res['data']['shipping_providers']))
  302. {
  303. continue;
  304. }
  305. $uid = '';
  306. foreach ($res['data']['shipping_providers'] as $val)
  307. {
  308. if($val['name'] == 'FedEx')
  309. {
  310. $uid = $val['id'];
  311. }
  312. }
  313. if($uid != '')
  314. {
  315. $this->_update($v,$uid);
  316. }
  317. }
  318. }
  319. public function _update($data,$uid)//上传运单
  320. {
  321. $shop = $this->shop->read($data['shop']);
  322. $url = 'https://open-api.tiktokglobalshop.com';
  323. $link = '/fulfillment/202309/orders/'.$data['orderinfo'].'/shipping_info/update';
  324. $time = time();
  325. $post['tracking_number'] = $data['waybill'];//运单号
  326. $post['shipping_provider_id'] = $uid;//承运商ID
  327. $queryParams['app_key'] =$shop['app_key'];
  328. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  329. $queryParams['timestamp'] =$time;
  330. $link .= '?'.http_build_query($queryParams);
  331. $sign = $this->apitt->sign($link,$shop['app_secret'],$post);
  332. $url .= $link.'&sign='.$sign;
  333. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  334. $res = $this->apitt->su_curl($post,$url,$headers);
  335. $res = json_decode($res,true);
  336. if($res['message'] == 'Success')
  337. {
  338. $this->fullordertt->save(array('delivery_type'=>1),$data['id']);
  339. }
  340. else
  341. {
  342. $this->fullordertt->save(array('delivery_type'=>2),$data['id']);
  343. }
  344. }
  345. public function _sq()
  346. {
  347. //授权获取app_key和code用于下一步获取token
  348. $post = $this->input->get(NULL);
  349. if(isset($post['app_key']))
  350. {
  351. $app_key = $this->input->get('app_key');
  352. $code = $this->input->get('code');
  353. if($code && $app_key)
  354. {
  355. $shop = $this->shop->app_key($app_key);
  356. if($shop)
  357. {
  358. $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';
  359. $ch = curl_init();
  360. curl_setopt($ch, CURLOPT_URL,$url);
  361. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  362. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  363. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  364. $res = curl_exec($ch);
  365. curl_close($ch);
  366. $res = json_decode($res,true);
  367. if(isset($res['data']))
  368. {
  369. $shop['token'] = $res['data']['access_token'];
  370. $shop['refresh_token'] = $res['data']['refresh_token'];
  371. $shop_cipher = '';
  372. $shopcipher = $this->_shop_cipher($shop);
  373. if(isset($shopcipher['data']['shops'][0]['cipher']))
  374. {
  375. $shop_cipher = $shopcipher['data']['shops'][0]['cipher'];
  376. }
  377. $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']);
  378. echo 'ok';
  379. echo "<pre>";
  380. print_r($res);
  381. }
  382. else
  383. {
  384. echo json_encode($res);
  385. }
  386. }
  387. else
  388. {
  389. echo "没有此店铺";
  390. }
  391. }
  392. }
  393. }
  394. public function _token()//判断tokentime通过refresh_token刷新token
  395. {
  396. $shop = $this->shop->find_all("type = '1514' and tb = 1");//找TT店铺
  397. foreach ($shop as $v)
  398. {
  399. if($v['tokentime'] < time()+3*24*3600)
  400. {
  401. $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'];
  402. $ch = curl_init();
  403. curl_setopt($ch, CURLOPT_URL,$url);
  404. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  405. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  406. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  407. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);//等待时间 10秒
  408. $res = curl_exec($ch);
  409. curl_close($ch);
  410. $res = json_decode($res,true);
  411. if(isset($res['data']))
  412. {
  413. $v['token'] = $res['data']['access_token'];
  414. $v['refresh_token'] = $res['data']['refresh_token'];
  415. $shop_cipher = '';
  416. $shopcipher = $this->_shop_cipher($v);
  417. if(isset($shopcipher['data']['shops'][0]['cipher']))
  418. {
  419. $shop_cipher = $shopcipher['data']['shops'][0]['cipher'];
  420. }
  421. $this->shop->save(array('token'=>$res['data']['access_token'],'tokentime'=>$res['data']['access_token_expire_in']-48*3600,'refresh_token'=>$res['data']['refresh_token']),$v['id']);
  422. }
  423. else
  424. {
  425. echo json_encode($res);
  426. exit;
  427. }
  428. }
  429. }
  430. echo "ok";
  431. }
  432. public function _shop_cipher($shop,$x=0)
  433. {
  434. $url = 'https://open-api.tiktokglobalshop.com';
  435. $link = '/authorization/202309/shops';
  436. $time = time();$post = array();
  437. $queryParams = array();
  438. $queryParams['access_token'] = $shop['token'];
  439. $queryParams['app_key'] = $shop['app_key'];
  440. $queryParams['shop_id'] = $shop['codeid'];
  441. $queryParams['timestamp'] = $time;
  442. $queryParams['version'] = '202309';
  443. $link .= '?'.http_build_query($queryParams);
  444. $sign = $this->apitt->sign($link,$shop['app_secret'],$post);
  445. $url .= $link.'&sign='.$sign;
  446. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  447. $ch = curl_init();
  448. curl_setopt($ch, CURLOPT_URL, $url);
  449. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  450. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  451. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  452. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  453. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  454. $res = curl_exec($ch);
  455. curl_close($ch);
  456. $res = json_decode($res,true);
  457. if(isset($res['data']['shops'][0]['cipher']) || $x == 5)
  458. {
  459. return $res;
  460. }
  461. else
  462. {
  463. $this->_shop_cipher($shop,1);
  464. }
  465. }
  466. public function _tb()
  467. {
  468. $post = $this->input->post(NULL, TRUE);
  469. $api = $this->input->get('api',true);
  470. $dshop = $this->input->get('dshop',true);
  471. $gethl = array();
  472. /* 匹配加入 */
  473. $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');
  474. // 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)
  475. $dtc = array();
  476. foreach ($dictionaries as $v)
  477. {
  478. if(stripos($v['spare'],'|') !== false)//如果有多个值
  479. {
  480. $v['spare'] = explode('|',$v['spare']);
  481. foreach ($v['spare'] as $k=>$vs)
  482. {
  483. if(stripos($v['zh'],'|') !== false)
  484. {
  485. $vzh = explode('|',$v['zh']);
  486. $dtc[strtolower($vs)] = array('classid'=>$v['classid'],'zh'=>$vzh[$k],'id'=>$v['id']);
  487. }
  488. else
  489. {
  490. $dtc[strtolower($vs)] = array('classid'=>$v['classid'],'zh'=>$v['zh'],'id'=>$v['id']);
  491. }
  492. }
  493. }
  494. else
  495. {
  496. $dtc[strtolower($v['spare'])] = array('classid'=>$v['classid'],'zh'=>$v['zh'],'id'=>$v['id']);
  497. }
  498. }
  499. $dictionaries2 = $this->typeclass->find_all();
  500. foreach ($dictionaries2 as $v)
  501. {
  502. $dtctitle[$v['id']] = $v['title'];
  503. }
  504. /* 匹配结束 */
  505. /* 价格加入 */
  506. $money = array();
  507. /**
  508. $productdescribe = $this->productdescribe->find_all('1=1','number,purchase,cost,salesprice,title');
  509. foreach ($productdescribe as $v)
  510. {
  511. $money[$v['number']] = array('purchase'=>$v['purchase'],'cost'=>$v['cost'],'salesprice'=>$v['salesprice'],'title'=>$v['title']);
  512. }
  513. **/
  514. /* 价格加入 */
  515. /* 匹配ID加入 */
  516. $int = array();
  517. $intdata = $this->typeclass->find_all('classid=14','id,spare');
  518. foreach ($intdata as $v)
  519. {
  520. $v['spare'] = explode('|',$v['spare']);
  521. $int[$v['id']] = $v['spare'][0];
  522. }
  523. /* 匹配ID结束 */
  524. /* 国家加入键值-k */
  525. $cuy = array();
  526. $cuydata = $this->country->find_all('1=1','lb,id');
  527. foreach ($cuydata as $v)
  528. {
  529. $cuy[$v['lb']] = $v['id'];//获取到国家ID
  530. }
  531. /* 国家加入键值-j */
  532. $typeclass = array();
  533. $tdata = $this->typeclass->find_all('classid=30','id,title');
  534. foreach ($tdata as $v)
  535. {
  536. $typeclass[$v['title']] = $v['id'];
  537. }
  538. $ex = array();
  539. $express = $this->express->find_all('1=1','id,title');
  540. foreach ($express as $v)
  541. {
  542. $ex[$v['title']] = array('id'=>$v['id']);
  543. }
  544. /* 币种加入键值-j */
  545. /* 支付方式-k */
  546. $pay = array();$sd = array();
  547. $tdata = $this->typeclass->find_all('classid=4','id,spare');
  548. foreach ($tdata as $v)
  549. {
  550. $paytype = $this->pay->get_typeclass($v['id']);
  551. if(isset($paytype['estimaterate']))
  552. {
  553. $estimaterate = $paytype['estimaterate'];
  554. }
  555. else
  556. {
  557. $estimaterate = 0;
  558. }
  559. if(stripos($v['spare'],'|') !== false)//如果有多个值
  560. {
  561. $v['spare'] = explode('|',$v['spare']);
  562. foreach ($v['spare'] as $k=>$vs)
  563. {
  564. $pay[$vs] = array('id'=>$v['id'],'estimaterate'=>$estimaterate);
  565. }
  566. }
  567. else
  568. {
  569. $pay[$v['spare']] = array('id'=>$v['id'],'estimaterate'=>$estimaterate);
  570. }
  571. }
  572. /* 支付方式-j */
  573. /* 新增SKU-k */
  574. $zjsku = array();
  575. $shopsku = $this->shopsku->find_all();
  576. foreach ($shopsku as $val)
  577. {
  578. $ss = explode(',',trim($val['shop'],','));
  579. foreach ($ss as $v)
  580. {
  581. if(isset($zjsku[$v]))
  582. {
  583. $zjsku[$v] .= ','.trim($val['sku'],',');
  584. }
  585. else
  586. {
  587. $zjsku[$v] = trim($val['sku'],',');
  588. }
  589. }
  590. }
  591. /* 新增SKU-j */
  592. $time = time();
  593. $tb = 0;
  594. if($api = '89757')
  595. {
  596. $shop = $this->shop->find_all("type = '1514' and tb = '1'");//TT店铺
  597. foreach ($shop as $value)
  598. {
  599. if((time()-$value['tbtime']) < 1)
  600. {
  601. $tb = 1;//正在同步中
  602. break;
  603. }
  604. $setting = $this->setting->get_settings();
  605. $sjc = 15*3600+$setting['sctime'];//时间差
  606. $num = $this->fullordertt->find_count('gtime = "'.date('Ymd',$time).'" and shop = "'.$value['id'].'"');
  607. //$this->db->trans_begin();
  608. $res = $this->apitt->get_list($value,$time-3*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);
  609. $sd[] = $res;
  610. $res = $res[0];
  611. if($res)
  612. {
  613. foreach ($res as $v)
  614. {
  615. $post = $v;
  616. $dataorder = $this->fullordertt->get_orderinfo($v['orderinfo']);
  617. if(!isset($dataorder['id']))
  618. {
  619. /** 启用自动分配仓库 **/
  620. $warehouse = $this->warehouse->find_all("fpzd = '1'",'*','fpsx desc');
  621. if($post['cf'] == 1)
  622. {
  623. $post['type'] = 34;//TT仓
  624. }
  625. else
  626. {
  627. foreach ($warehouse as $v)
  628. {
  629. if($v['bdcountry'] != 0 && $v['bdcountry'] != $post['country'])
  630. {
  631. continue;
  632. }
  633. $yx = 'get_yx_'.$v['bdck'];
  634. $kczd = $this->ck->$yx($post['state'],$v,$post['shop'],$post['number'],$post['whlabel'],$post['fpdata']);
  635. if($kczd['t'] == 0)
  636. {
  637. $country = $this->country->read($post['country']);//获取国家中配置物流
  638. if($country['express'] != 0)
  639. {
  640. $post['express'] = $country['express'];
  641. }
  642. if($v['express'] != 0)//优先使用仓库指定物流,替换掉上面的国家配置物流
  643. {
  644. $post['express'] = $v['express'];
  645. }
  646. $post['type'] = $v['id'];
  647. $post['whlabel'] = $kczd['whlabel'];
  648. $post['fpdata'] = $kczd['fpdata'];
  649. if(stripos($kczd['whlabel'],$v['hz']) !== false)
  650. {
  651. break 1;
  652. }
  653. }
  654. }
  655. $qdw = $this->warehouse->read($post['type']);
  656. $fl = $this->ck->get_fl($qdw['hz'],$post['whlabel'],$post['fpdata']);
  657. $post['sfxh'] = $fl['sfxh'];
  658. $post['ckfl'] = $fl['ckfl'];
  659. $zipcode = substr($post['zipcode'],0,1);
  660. if($post['type'] == 5 && $zipcode > 6)
  661. {
  662. $post['express'] = 63;
  663. }
  664. //自动写位置 开始
  665. $zclp = array();$pxsl = array();
  666. $slpx = explode('|',$post['slpx']);//排序
  667. //解决礼物类没有slpx的数据
  668. if(!empty($post['slpx'])){
  669. foreach ($slpx as $k=>$v)
  670. {
  671. $v = explode('-',$v);
  672. if(!isset($v[1]))
  673. {
  674. $v[1] == 1;
  675. }
  676. for($i=$v[0];$i<$v[1];$i++)
  677. {
  678. $pxsl[$i] = $k;
  679. }
  680. }
  681. }
  682. $mc = $qdw['bdck'];$zdidjl = '';$zdwzjl = '';
  683. if($qdw['wz'] == 1)
  684. {
  685. $pm = explode(';',trim($post['shipremarks'],';'));
  686. foreach ($pm as $k=>$vv)
  687. {
  688. $vv = trim($vv,' ');
  689. $lp = $this->typeclass->find_all("zh = '$vv'");
  690. if(isset($lp[0]['classid']))
  691. {
  692. $zclp[] = $vv;
  693. unset($pm[$k]);
  694. }
  695. }
  696. $pm = array_values($pm);
  697. $w = explode('|',trim($post['whlabel'],'|'));
  698. foreach ($w as $k=>$val)
  699. {
  700. $hwm = '';
  701. if(stripos($val,$qdw['hz']) !== false)
  702. {
  703. $num = explode('-',trim($val,'-'));
  704. $xq = explode('~',trim($num[2],'~'));
  705. $pmxq = '(';
  706. foreach ($xq as $v)
  707. {
  708. $zdidjl .= '('.trim($v,$qdw['hz']).')';
  709. $h = $this->$mc->read(trim($v,$qdw['hz']));
  710. if($h['details'] != '')
  711. {
  712. $pmxq .= $qdw['title'].':'.$h['details'].(($h['cpid']>0)?'-'.$h['cpid']:'').',';
  713. }
  714. }
  715. $xrid = ($h['cpid']>0)?'-'.$h['cpid']:'';
  716. if(isset($pxsl[$k]))
  717. {
  718. if(stripos($pm[$pxsl[$k]],$qdw['title'].':'.$h['details'].$xrid) === false)
  719. {
  720. $pm[$pxsl[$k]] .= trim($pmxq,',').')';
  721. }
  722. }
  723. $zdwzjl .= $h['details'].(($h['cpid']>0)?'-'.$h['cpid']:'');
  724. }
  725. }
  726. if($zclp)
  727. {
  728. $zclp = ';'.implode(";",$zclp);
  729. }
  730. else
  731. {
  732. $zclp = '';
  733. }
  734. $post['shipremarks'] = implode(";",$pm).$zclp;
  735. $post['contents'] = $zdidjl.' - '.$zdwzjl;
  736. }
  737. }
  738. //自动写位置 结束
  739. //条数及申报
  740. $ts = 0;
  741. $dtsbjz = 0;
  742. $zsbjz = 0;
  743. $w = explode('|',trim($post['whlabel'],'|'));
  744. foreach ($w as $k=>$val)
  745. {
  746. $num = explode('-',trim($val,'-'));
  747. //判断是否是礼物 礼物不计数
  748. $is_gift_str = substr($num[0], 0, 3);
  749. if($is_gift_str != '131'){
  750. $ts += $num[1];
  751. }
  752. }
  753. if($ts > 0 && $ts < 6 && $post['country'] == 192)
  754. {
  755. $dtsbjz = ($ts==3)?sprintf("%.1f",100/$ts):100/$ts;
  756. $zsbjz = sprintf("%.2f",$dtsbjz*$ts);
  757. }
  758. $post['ts'] = $ts;
  759. $post['dtsbjz'] = $dtsbjz;
  760. $post['zsbjz'] = $zsbjz;
  761. /** **/
  762. //合并重复项
  763. $hbcfw = explode('|',trim($post['whlabel'],'|'));
  764. $hbcff = explode(';',trim($post['fpdata'],';'));
  765. $c = 0;
  766. $hbw = array();
  767. $hbf = array();
  768. for($i=0;$i<count($hbcfw);$i++)
  769. {
  770. $w = explode('-',$hbcfw[$i]);
  771. $f = explode('|',$hbcff[$i]);
  772. $w2 = ($w[2]==0)?0:'';
  773. if(!isset($hbw[$w[0].$w2]))
  774. {
  775. $hbw[$w[0].$w2] = $hbcfw[$i];
  776. $hbf[$w[0].$w2] = $hbcff[$i];
  777. }
  778. else
  779. {
  780. $c++;
  781. $cw = explode('-',$hbw[$w[0].$w2]);
  782. $cf = explode('|',$hbf[$w[0].$w2]);
  783. if($cw[2] != 0 && $w[2] != 0)
  784. {
  785. $cf[2] += $f[2];
  786. $cf[9] .= '~'.$f[9];
  787. $hbf[$w[0].$w2] = implode("|",$cf);
  788. $cw[1] += $f[2];
  789. $cw[2] .= '~'.$f[9];
  790. $hbw[$w[0].$w2] = implode("-",$cw);
  791. }
  792. else if($cw[2] == 0 && $w[2] == 0)
  793. {
  794. $cf[2] += $f[2];
  795. $hbf[$w[0].$w2] = implode("|",$cf);
  796. $cw[1] += $f[2];
  797. $hbw[$w[0].$w2] = implode("-",$cw);
  798. }
  799. else
  800. {
  801. $hbw[] = $hbcfw[$i];
  802. $hbf[] = $hbcff[$i];
  803. }
  804. }
  805. }
  806. if($c > 0)
  807. {
  808. $post['fpdata'] = implode(";",$hbf).';';
  809. $post['whlabel'] = '|'.implode("|",$hbw).'|';
  810. }
  811. //合并重复项结束
  812. //非占单设置为禁止占单开始
  813. $hbw = explode('|',trim($post['whlabel'],'|'));
  814. $hbf = explode(';',trim($post['fpdata'],';'));
  815. foreach ($hbw as $k=>$val)
  816. {
  817. $w = explode('-',$val);
  818. $f = explode('|',$hbf[$k]);
  819. if(!isset($w[2]) || $w[2] == 0)
  820. {
  821. $w[2] = "DNOTO";
  822. $f[9] = "DNOTO";
  823. $hbf[$k] = implode("|",$f);
  824. $hbw[$k] = implode("-",$w);
  825. }
  826. }
  827. $post['fpdata'] = implode(";",$hbf).';';
  828. $post['whlabel'] = '|'.implode("|",$hbw).'|';
  829. //非占单设置为禁止占单结束
  830. //查询净重开始
  831. $post['jweight'] = $this->weight->get_weightcx($post['fpdata']);
  832. $post['zzl'] = ($post['jweight']+100)/1000;
  833. if($post['express'] == 2)
  834. {
  835. $post['zzl'] = intval(($post['jweight']+100)*0.035);
  836. }
  837. //查询净重结束
  838. $tjid = $this->fullordertt->insert($post);
  839. if($post['email'] == '')
  840. {
  841. $post['email'] = $post['name'].'@qq.com';
  842. }
  843. $adrs = $this->customertt->get_email($post['email'],$post['shop']);
  844. preg_match_all('/[0-9]/u',$post['phone'],$result);
  845. $numphone = join('',$result[0]);
  846. $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']));
  847. $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']));
  848. $hmdaddress3 = str_replace(array(' avenue',' ave',' av'),array(' ave',' avenue',' ave'),strtolower($post['address']));
  849. $hmdid = '';
  850. $post['address'] = str_replace("'","&#8217;",$post['address']);
  851. $hmdaddress1 = str_replace("'","&#8217;",$hmdaddress1);
  852. $hmdaddress2 = str_replace("'","&#8217;",$hmdaddress2);
  853. $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');
  854. $lkh = (isset($customerb[0]['id']))?2:1;//白名单1黑名单2
  855. if($lkh == 2)
  856. {
  857. $hmdid = $customerb[0]['id'].'-('.$post['email'].'|'.$post['address'].'|'.$numphone.')'.'('.$customerb[0]['email'].'|'.$customerb[0]['address'].$customerb[0]['numphone'].')';
  858. }
  859. if($adrs)
  860. {
  861. $pdlkh = 1;//这个是判断是否老客户
  862. $this->customertt->save(array('time'=>$post['dtime'],'num'=>$adrs['num']+1,'money'=>$adrs['money']+$post['shouldmoney']),$adrs['id']);
  863. $khid = $adrs['id'];
  864. }
  865. else
  866. {
  867. $str = $post['phone'];
  868. preg_match_all('/[0-9]/u',$str,$result);
  869. $numphone = join('',$result[0]);
  870. $ct['shop'] = $post['shop'];
  871. $ct['source'] = 9;
  872. $ct['level'] = 5;
  873. $ct['country'] = $post['country'];
  874. $ct['time'] = $post['dtime'];
  875. $ct['num'] = 1;
  876. $ct['money'] = $post['shouldmoney'];
  877. $ct['zipcode'] = $post['zipcode'];
  878. $ct['name'] = $post['name'];
  879. $ct['phone'] = $post['phone'];
  880. $ct['numphone'] = $numphone;
  881. $ct['email'] = preg_replace('/( | | |\s)+/','',$post['email']);
  882. $ct['province'] = $post['province'];
  883. $ct['city'] = $post['city'];
  884. $ct['address'] = $post['address'];
  885. $ct['zhcx'] = $post['shop'].'-'.$ct['email'];
  886. $khid = $this->customertt->insert($ct);
  887. $pdlkh = 0;
  888. }
  889. if($post['cf'] != 1)
  890. {
  891. $this->_znxc($post,$post['orderinfo'],1);
  892. }
  893. }//else{}如果有此订单
  894. }
  895. }
  896. /**
  897. if ($this->db->trans_status() === FALSE)
  898. {
  899. $this->db->trans_commit();
  900. }
  901. else
  902. {
  903. $this->db->trans_commit();
  904. }
  905. **/
  906. //sleep(1);//停留2秒
  907. $this->shop->save(array('tbtime'=>time()),$value['id']);
  908. }
  909. if($tb == 1)
  910. {
  911. echo json_encode(array('msg'=>'有店铺正在同步中,请5分钟后再试!','success'=>true));exit;
  912. }
  913. //echo json_encode(array('msg'=>'OK!','success'=>true));exit;
  914. echo "<pre>";
  915. print_r($sd);
  916. }
  917. }
  918. public function _znxc($fullorder,$order_id,$type,$logisticstatus='')
  919. {
  920. $time = time();
  921. if($type == 1)//地址确认需要状态对应
  922. {
  923. $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'");//有条件不同模板匹配
  924. $express = array();
  925. }
  926. else if($type == 4)//运单号通知
  927. {
  928. $notice = $this->notice->find_all("shop = '".$fullorder['shop']."' and message = '3' and state = '1' and ktime < '$time' and jtime > '$time' and type = '$type'");//有条件不同模板匹配
  929. $express = $this->express->read($fullorder['express']);
  930. }
  931. else if($type == 3)//催付
  932. {
  933. $notice = $this->notice->find_all("shop = '".$fullorder['shop']."' and message = '3' and state = '1' and ktime < '$time' and jtime > '$time' and type = '$type'");//有条件不同模板匹配
  934. }
  935. else if($type == 2)//物流发送
  936. {
  937. $notice = $this->notice->find_all("shop = '".$fullorder['shop']."' and message = '3' and state = '1' and ktime < '$time' and jtime > '$time' and type = '$type' and logisticstatus = '$logisticstatus'");//有条件不同模板匹配
  938. $express = $this->express->read($fullorder['express']);
  939. }
  940. if(isset($notice[0]['content']) && $type != 3)
  941. {
  942. $fid = $this->fullordertt->get_orderinfo($order_id);
  943. $shop = $this->shop->read($fullorder['shop']);
  944. $country = $this->country->read($fullorder['country']);
  945. $fullorder['country'] = $country['ename'];
  946. $go = $this->notice->get_god($fullorder,$shop,$express,$notice[0]);
  947. if($go == 1)
  948. {
  949. $this->fullordertt->save(array('content'=>$fid['content'].'自动发送-成功-'.$type.'-'.date("Y-m-d H:i:s",time()),'zdstate'=>$fid['zdstate'].$notice[0]['type'].'-'.$notice[0]['tab'].'|','exstateerror'=>''),$fid['id']);
  950. return 1;
  951. }
  952. else
  953. {
  954. $this->fullordertt->save(array('exstateerror'=>'自动发送失败1:'.date('Y-m-d H',time()).'-'.$go.'('.$notice[0]['type'].'-'.$notice[0]['message'].')'),$fid['id']);
  955. }
  956. }
  957. else if(isset($notice[0]['content']) && $type == 3)
  958. {
  959. $shop = $this->shop->read($fullorder['shop']);
  960. $go = $this->notice->get_god($fullorder,$shop,array(),$notice[0]);
  961. if($go == 1)
  962. {
  963. $this->ttcf->save(array('cg'=>1,'shop'=>$fullorder['shop'],'data'=>'自动发送-成功-'.$type.'-'.date("Y-m-d H:i:s",time())),$fullorder['id']);
  964. return 1;
  965. }
  966. else
  967. {
  968. $this->ttcf->save(array('cg'=>2,'shop'=>$fullorder['shop'],'data'=>'自动发送失败1:'.date('Y-m-d H',time()).'-'.$go.'('.$notice[0]['type'].'-'.$notice[0]['message'].')'),$fullorder['id']);
  969. }
  970. }
  971. else
  972. {
  973. if($type == 3)
  974. {
  975. $this->ttcf->save(array('cg'=>3,'shop'=>$fullorder['shop'],'data'=>'当前订单状态无匹配模板'),$fullorder['id']);
  976. }
  977. else
  978. {
  979. $this->fullordertt->save(array('exstateerror'=>'当前订单状态无匹配模板 - state:'.$fullorder['state'].' - shop:'.$fullorder['shop'].' - time:'.$time.' - type:'.$type.' - notice:'.json_encode($notice)),$fid['id']);
  980. }
  981. }
  982. }
  983. public function _webhook()
  984. {
  985. header('HTTP/1.1 200 OK');
  986. ob_flush();
  987. flush();
  988. $j = file_get_contents('php://input');
  989. $j = json_decode($j, true);
  990. if(isset($j['type']) && isset($j['data']['order_id']))
  991. {
  992. $data = json_encode($j['data']);
  993. $tid = $this->ttwebhook->get_tid($j['tts_notification_id']);
  994. if($tid)
  995. {
  996. exit;
  997. }
  998. else
  999. {
  1000. $this->ttwebhook->insert(array('tid'=>$j['tts_notification_id'],'orderinfo'=>$j['data']['order_id'],'data'=>$data,'time'=>date("Y-m-d H:i:s",time())));
  1001. }
  1002. $ch = curl_init();
  1003. curl_setopt($ch,CURLOPT_URL,'http://a10.wepolicy.cn/apitt/ttwebhook');
  1004. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  1005. curl_setopt($ch,CURLOPT_HEADER,0);
  1006. curl_setopt($ch,CURLOPT_POST, 1);
  1007. curl_setopt($ch,CURLOPT_TIMEOUT,1);
  1008. //设置post数据
  1009. $post = array();
  1010. $post['j'] = json_encode($j);
  1011. curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));
  1012. $res = curl_exec($ch);
  1013. curl_close($ch);
  1014. }
  1015. exit;
  1016. }
  1017. public function _ttwebhook()
  1018. {
  1019. $post = $this->input->post(NULL, TRUE);
  1020. if(isset($post['j']))
  1021. {
  1022. $j = $this->input->post('j',true);
  1023. $j = json_decode($j, true);
  1024. $xq = array();
  1025. if($j['type'] == 1 && $j['data']['order_status'] == 'UNPAID')
  1026. {
  1027. $shop = $this->shop->get_shopid($j['shop_id']);
  1028. if(!$shop)
  1029. {
  1030. exit;
  1031. }
  1032. $d = $this->apitt->get_data(array($j['data']['order_id']),$shop);
  1033. if(isset($d['data']['orders'][0]['user_id']))
  1034. {
  1035. $post = array('orderinfo'=>$j['data']['order_id'],'user_id'=>$d['data']['orders'][0]['user_id']);
  1036. $id = $this->ttcf->insert($post);
  1037. $post['shop'] = $shop['id'];
  1038. $post['id'] = $id;
  1039. $this->_znxc($post,$v,3);
  1040. }
  1041. exit;
  1042. }
  1043. $d = $this->fullordertt->get_orderinfo($j['data']['order_id']);
  1044. if($d)
  1045. {
  1046. $post = $this->fullordertt->read($d['id']);
  1047. $shop = $this->shop->read($d['shop']);
  1048. $qchz = array();
  1049. $f = explode('|',trim($d['whlabel'],'|'));
  1050. foreach ($f as $v)
  1051. {
  1052. $hz = explode('-',trim($v,'-'));
  1053. if(isset($hz[2]) && $hz[2] != 'DNOTO' && $hz[2] != '0')
  1054. {
  1055. $qchz[] = $hz[2];
  1056. }
  1057. }
  1058. $d['whlabel']= str_replace($qchz,'0',$d['whlabel']);
  1059. $d['fpdata']= str_replace($qchz,'0',$d['fpdata']);
  1060. $this->db->trans_begin();
  1061. if($j['type'] == 1)
  1062. {
  1063. if($j['data']['order_status'] == 'CANCEL' || $j['data']['order_status'] == 'Canceled')
  1064. {
  1065. if($d['library'] != 2)
  1066. {
  1067. $whlabel = $this->whlabel->find_all("zd = '".$d['number']."'");
  1068. foreach ($whlabel as $v)
  1069. {
  1070. $this->whlabel->save(array('zd'=>''),$v['id']);
  1071. }
  1072. $this->fullordertt->save(array('state'=>217,'whlabel'=>$d['whlabel'],'fpdata'=>$d['fpdata']),$d['id']);
  1073. }
  1074. else
  1075. {
  1076. $this->fullordertt->save(array('state'=>214),$d['id']);
  1077. }
  1078. }
  1079. else if(($j['data']['order_status'] == 'IN_TRANSIT' || $j['data']['order_status'] == 'COMPLETED currently)') && $d['state'] != 216)
  1080. {
  1081. if($d['cf'] == 1 && $d['type'] == 34 && $j['data']['order_status'] == 'IN_TRANSIT')
  1082. {
  1083. $wb = $this->apitt->get_data(array($d['orderinfo']),$shop);
  1084. $waybill = $wb['data']['orders']['0']['tracking_number'];
  1085. $ex = $wb['data']['orders']['0']['shipping_provider'];
  1086. $express = 0;
  1087. if(stripos($ex,"UPS") !== false)
  1088. {
  1089. $express = 3;
  1090. }
  1091. else if(stripos($ex,"DHL") !== false)
  1092. {
  1093. $express = 1;
  1094. }
  1095. else if(stripos($ex,"FEDEX") !== false)
  1096. {
  1097. $express = 24;
  1098. }
  1099. else if(stripos($ex,"USPS") !== false)
  1100. {
  1101. $express = 2;
  1102. }
  1103. else if(stripos($ex,"ONTRAC") !== false)
  1104. {
  1105. $express = 62;
  1106. }
  1107. $time = time();
  1108. $post['waybill'] = $waybill;
  1109. $post['express'] = $express;
  1110. $express_id = $express;
  1111. $d['waybill'] = $waybill;
  1112. $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']);
  1113. }
  1114. else
  1115. {
  1116. $this->fullordertt->save(array('state'=>216,'whlabel'=>$d['whlabel'],'fpdata'=>$d['fpdata']),$d['id']);
  1117. $express_id = $d['express'];
  1118. }
  1119. if($express_id != 0)
  1120. {
  1121. if(stripos('|'.$post['zdstate'],'|4-') === false)
  1122. {
  1123. $this->_znxc($post,$post['orderinfo'],4);
  1124. }
  1125. $expressdata = $this->express->read($express_id);
  1126. $service = $this->service->read($expressdata['service']);
  1127. if(($expressdata['cxcode'] == 'ups' || $expressdata['cxcode'] == '17track' || $expressdata['cxcode'] == 'usps') && $d['webhookregister'] < 1)
  1128. {
  1129. $logistics = $expressdata['cxcode'];
  1130. $d['servicename'] =$service['title'];//增加快递名称
  1131. $d['url'] = $expressdata['url'];//增加快递链接
  1132. @$fs = $this->$logistics->get_logistics($d);
  1133. if(isset($fs) && $fs['f'] == '1')
  1134. {
  1135. $this->fullordertt->save(array('expressstate'=>$fs['exstate'],'excontent'=>$fs['data'],'webhookregister'=>1),$d['id']);
  1136. }
  1137. }
  1138. }
  1139. }
  1140. else if($j['data']['order_status'] == 'DELIVERED')
  1141. {
  1142. $je = $this->apitt->get_je($d,$shop);
  1143. $budget = 0;
  1144. if(isset($je['data']['statement_transactions']['0']['settlement_amount']))
  1145. {
  1146. foreach ($je['data']['statement_transactions'] as $val)
  1147. {
  1148. $budget += $val['settlement_amount'];
  1149. }
  1150. $this->fullordertt->save(array('budget'=>$budget),$d['id']);
  1151. }
  1152. if(stripos('|'.$post['zdstate'],'|2-') === false)
  1153. {
  1154. $this->_znxc($post,$post['orderinfo'],2,6);//2物流模板,6已签收
  1155. }
  1156. }
  1157. }
  1158. if($j['type'] == 2)
  1159. {
  1160. if($j['reverse_order_status '] == 50 || $j['reverse_order_status '] == 51)
  1161. {
  1162. $whlabel = $this->whlabel->find_all("zd = '".$d['number']."'");
  1163. foreach ($whlabel as $v)
  1164. {
  1165. $this->whlabel->save(array('zd'=>''),$v['id']);
  1166. }
  1167. $this->fullordertt->save(array('state'=>214,'whlabel'=>$d['whlabel'],'fpdata'=>$d['fpdata']),$d['id']);
  1168. }
  1169. /**
  1170. reverse_order_status
  1171. 售后申请 = 1
  1172. 售后拒绝申请 = 2
  1173. 售后退货 = 3
  1174. 售后买家已发货 = 4
  1175. 售后卖家_拒绝接收 = 5
  1176. 售后成功 = 50
  1177. 取消成功 = 51
  1178. 关闭 = 99
  1179. 完成 = 100
  1180. **/
  1181. }
  1182. if ($this->db->trans_status() === TRUE)
  1183. {
  1184. $this->db->trans_commit();
  1185. }
  1186. else
  1187. {
  1188. $this->db->trans_rollback();
  1189. }
  1190. }
  1191. }
  1192. }
  1193. public function _gxbudget()
  1194. {
  1195. $time = time();
  1196. $data = $this->fullordertt->find_all("source != 1 and budget = 0 and librarytime > '".($time-6*24*3600)."' and librarytime < '".($time-9*24*3600)."'");
  1197. foreach ($data as $v)
  1198. {
  1199. $shop = $this->shop->read($v['shop']);
  1200. $je = $this->apitt->get_je($v,$shop);
  1201. $budget = 0;
  1202. if(isset($je['data']['statement_transactions']['0']['settlement_amount']))
  1203. {
  1204. foreach ($je['data']['statement_transactions'] as $val)
  1205. {
  1206. $budget += $val['settlement_amount'];
  1207. }
  1208. $this->fullordertt->save(array('budget'=>$budget),$v['id']);
  1209. }
  1210. }
  1211. }
  1212. }