Apitt.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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. if($value['id'] == 48){
  610. $this->logic_ding->sendToDing("获取TT订单48".json_encode($res)."||".json_encode($value));
  611. continue;
  612. }
  613. $sd[] = $res;
  614. $res = $res[0];
  615. if($res)
  616. {
  617. foreach ($res as $v)
  618. {
  619. //$this->logic_ding->sendToDing("获取TT订单".json_encode($v));
  620. $post = $v;
  621. $dataorder = $this->fullordertt->get_orderinfo($v['orderinfo']);
  622. if(!isset($dataorder['id']))
  623. {
  624. /** 启用自动分配仓库 **/
  625. $warehouse = $this->warehouse->find_all("fpzd = '1'",'*','fpsx desc');
  626. if($post['cf'] == 1)
  627. {
  628. $post['type'] = 34;//TT仓
  629. }
  630. else
  631. {
  632. foreach ($warehouse as $v)
  633. {
  634. if($v['bdcountry'] != 0 && $v['bdcountry'] != $post['country'])
  635. {
  636. continue;
  637. }
  638. $yx = 'get_yx_'.$v['bdck'];
  639. $kczd = $this->ck->$yx($post['state'],$v,$post['shop'],$post['number'],$post['whlabel'],$post['fpdata']);
  640. if($kczd['t'] == 0)
  641. {
  642. $country = $this->country->read($post['country']);//获取国家中配置物流
  643. if($country['express'] != 0)
  644. {
  645. $post['express'] = $country['express'];
  646. }
  647. if($v['express'] != 0)//优先使用仓库指定物流,替换掉上面的国家配置物流
  648. {
  649. $post['express'] = $v['express'];
  650. }
  651. $post['type'] = $v['id'];
  652. $post['whlabel'] = $kczd['whlabel'];
  653. $post['fpdata'] = $kczd['fpdata'];
  654. if(stripos($kczd['whlabel'],$v['hz']) !== false)
  655. {
  656. break 1;
  657. }
  658. }
  659. }
  660. $qdw = $this->warehouse->read($post['type']);
  661. $fl = $this->ck->get_fl($qdw['hz'],$post['whlabel'],$post['fpdata']);
  662. $post['sfxh'] = $fl['sfxh'];
  663. $post['ckfl'] = $fl['ckfl'];
  664. $zipcode = substr($post['zipcode'],0,1);
  665. if($post['type'] == 5 && $zipcode > 6)
  666. {
  667. $post['express'] = 63;
  668. }
  669. //自动写位置 开始
  670. $zclp = array();$pxsl = array();
  671. $slpx = explode('|',$post['slpx']);//排序
  672. //解决礼物类没有slpx的数据
  673. if(!empty($post['slpx'])){
  674. foreach ($slpx as $k=>$v)
  675. {
  676. $v = explode('-',$v);
  677. if(!isset($v[1]))
  678. {
  679. $v[1] == 1;
  680. }
  681. for($i=$v[0];$i<$v[1];$i++)
  682. {
  683. $pxsl[$i] = $k;
  684. }
  685. }
  686. }
  687. $mc = $qdw['bdck'];$zdidjl = '';$zdwzjl = '';
  688. if($qdw['wz'] == 1)
  689. {
  690. $pm = explode(';',trim($post['shipremarks'],';'));
  691. foreach ($pm as $k=>$vv)
  692. {
  693. $vv = trim($vv,' ');
  694. $lp = $this->typeclass->find_all("zh = '$vv'");
  695. if(isset($lp[0]['classid']))
  696. {
  697. $zclp[] = $vv;
  698. unset($pm[$k]);
  699. }
  700. }
  701. $pm = array_values($pm);
  702. $w = explode('|',trim($post['whlabel'],'|'));
  703. foreach ($w as $k=>$val)
  704. {
  705. $hwm = '';
  706. if(stripos($val,$qdw['hz']) !== false)
  707. {
  708. $num = explode('-',trim($val,'-'));
  709. $xq = explode('~',trim($num[2],'~'));
  710. $pmxq = '(';
  711. foreach ($xq as $v)
  712. {
  713. $zdidjl .= '('.trim($v,$qdw['hz']).')';
  714. $h = $this->$mc->read(trim($v,$qdw['hz']));
  715. if($h['details'] != '')
  716. {
  717. $pmxq .= $qdw['title'].':'.$h['details'].(($h['cpid']>0)?'-'.$h['cpid']:'').',';
  718. }
  719. }
  720. $xrid = ($h['cpid']>0)?'-'.$h['cpid']:'';
  721. if(isset($pxsl[$k]))
  722. {
  723. if(stripos($pm[$pxsl[$k]],$qdw['title'].':'.$h['details'].$xrid) === false)
  724. {
  725. $pm[$pxsl[$k]] .= trim($pmxq,',').')';
  726. }
  727. }
  728. $zdwzjl .= $h['details'].(($h['cpid']>0)?'-'.$h['cpid']:'');
  729. }
  730. }
  731. if($zclp)
  732. {
  733. $zclp = ';'.implode(";",$zclp);
  734. }
  735. else
  736. {
  737. $zclp = '';
  738. }
  739. $post['shipremarks'] = implode(";",$pm).$zclp;
  740. $post['contents'] = $zdidjl.' - '.$zdwzjl;
  741. }
  742. }
  743. //自动写位置 结束
  744. //条数及申报
  745. $ts = 0;
  746. $dtsbjz = 0;
  747. $zsbjz = 0;
  748. $w = explode('|',trim($post['whlabel'],'|'));
  749. foreach ($w as $k=>$val)
  750. {
  751. $num = explode('-',trim($val,'-'));
  752. //判断是否是礼物 礼物不计数
  753. $is_gift_str = substr($num[0], 0, 3);
  754. if($is_gift_str != '131'){
  755. $ts += $num[1];
  756. }
  757. }
  758. if($ts > 0 && $ts < 6 && $post['country'] == 192)
  759. {
  760. $dtsbjz = ($ts==3)?sprintf("%.1f",100/$ts):100/$ts;
  761. $zsbjz = sprintf("%.2f",$dtsbjz*$ts);
  762. }
  763. $post['ts'] = $ts;
  764. $post['dtsbjz'] = $dtsbjz;
  765. $post['zsbjz'] = $zsbjz;
  766. /** **/
  767. //合并重复项
  768. $hbcfw = explode('|',trim($post['whlabel'],'|'));
  769. $hbcff = explode(';',trim($post['fpdata'],';'));
  770. $c = 0;
  771. $hbw = array();
  772. $hbf = array();
  773. for($i=0;$i<count($hbcfw);$i++)
  774. {
  775. $w = explode('-',$hbcfw[$i]);
  776. $f = explode('|',$hbcff[$i]);
  777. $w2 = ($w[2]==0)?0:'';
  778. if(!isset($hbw[$w[0].$w2]))
  779. {
  780. $hbw[$w[0].$w2] = $hbcfw[$i];
  781. $hbf[$w[0].$w2] = $hbcff[$i];
  782. }
  783. else
  784. {
  785. $c++;
  786. $cw = explode('-',$hbw[$w[0].$w2]);
  787. $cf = explode('|',$hbf[$w[0].$w2]);
  788. if($cw[2] != 0 && $w[2] != 0)
  789. {
  790. $cf[2] += $f[2];
  791. $cf[9] .= '~'.$f[9];
  792. $hbf[$w[0].$w2] = implode("|",$cf);
  793. $cw[1] += $f[2];
  794. $cw[2] .= '~'.$f[9];
  795. $hbw[$w[0].$w2] = implode("-",$cw);
  796. }
  797. else if($cw[2] == 0 && $w[2] == 0)
  798. {
  799. $cf[2] += $f[2];
  800. $hbf[$w[0].$w2] = implode("|",$cf);
  801. $cw[1] += $f[2];
  802. $hbw[$w[0].$w2] = implode("-",$cw);
  803. }
  804. else
  805. {
  806. $hbw[] = $hbcfw[$i];
  807. $hbf[] = $hbcff[$i];
  808. }
  809. }
  810. }
  811. if($c > 0)
  812. {
  813. $post['fpdata'] = implode(";",$hbf).';';
  814. $post['whlabel'] = '|'.implode("|",$hbw).'|';
  815. }
  816. //合并重复项结束
  817. //非占单设置为禁止占单开始
  818. $hbw = explode('|',trim($post['whlabel'],'|'));
  819. $hbf = explode(';',trim($post['fpdata'],';'));
  820. foreach ($hbw as $k=>$val)
  821. {
  822. $w = explode('-',$val);
  823. $f = explode('|',$hbf[$k]);
  824. if(!isset($w[2]) || $w[2] == 0)
  825. {
  826. $w[2] = "DNOTO";
  827. $f[9] = "DNOTO";
  828. $hbf[$k] = implode("|",$f);
  829. $hbw[$k] = implode("-",$w);
  830. }
  831. }
  832. $post['fpdata'] = implode(";",$hbf).';';
  833. $post['whlabel'] = '|'.implode("|",$hbw).'|';
  834. //非占单设置为禁止占单结束
  835. //查询净重开始
  836. $post['jweight'] = $this->weight->get_weightcx($post['fpdata']);
  837. $post['zzl'] = ($post['jweight']+100)/1000;
  838. if($post['express'] == 2)
  839. {
  840. $post['zzl'] = intval(($post['jweight']+100)*0.035);
  841. }
  842. //查询净重结束
  843. $tjid = $this->fullordertt->insert($post);
  844. if($post['email'] == '')
  845. {
  846. $post['email'] = $post['name'].'@qq.com';
  847. }
  848. $adrs = $this->customertt->get_email($post['email'],$post['shop']);
  849. preg_match_all('/[0-9]/u',$post['phone'],$result);
  850. $numphone = join('',$result[0]);
  851. $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']));
  852. $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']));
  853. $hmdaddress3 = str_replace(array(' avenue',' ave',' av'),array(' ave',' avenue',' ave'),strtolower($post['address']));
  854. $hmdid = '';
  855. $post['address'] = str_replace("'","&#8217;",$post['address']);
  856. $hmdaddress1 = str_replace("'","&#8217;",$hmdaddress1);
  857. $hmdaddress2 = str_replace("'","&#8217;",$hmdaddress2);
  858. $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');
  859. $lkh = (isset($customerb[0]['id']))?2:1;//白名单1黑名单2
  860. if($lkh == 2)
  861. {
  862. $hmdid = $customerb[0]['id'].'-('.$post['email'].'|'.$post['address'].'|'.$numphone.')'.'('.$customerb[0]['email'].'|'.$customerb[0]['address'].$customerb[0]['numphone'].')';
  863. }
  864. if($adrs)
  865. {
  866. $pdlkh = 1;//这个是判断是否老客户
  867. $this->customertt->save(array('time'=>$post['dtime'],'num'=>$adrs['num']+1,'money'=>$adrs['money']+$post['shouldmoney']),$adrs['id']);
  868. $khid = $adrs['id'];
  869. }
  870. else
  871. {
  872. $str = $post['phone'];
  873. preg_match_all('/[0-9]/u',$str,$result);
  874. $numphone = join('',$result[0]);
  875. $ct['shop'] = $post['shop'];
  876. $ct['source'] = 9;
  877. $ct['level'] = 5;
  878. $ct['country'] = $post['country'];
  879. $ct['time'] = $post['dtime'];
  880. $ct['num'] = 1;
  881. $ct['money'] = $post['shouldmoney'];
  882. $ct['zipcode'] = $post['zipcode'];
  883. $ct['name'] = $post['name'];
  884. $ct['phone'] = $post['phone'];
  885. $ct['numphone'] = $numphone;
  886. $ct['email'] = preg_replace('/( | | |\s)+/','',$post['email']);
  887. $ct['province'] = $post['province'];
  888. $ct['city'] = $post['city'];
  889. $ct['address'] = $post['address'];
  890. $ct['zhcx'] = $post['shop'].'-'.$ct['email'];
  891. $khid = $this->customertt->insert($ct);
  892. $pdlkh = 0;
  893. }
  894. if($post['cf'] != 1)
  895. {
  896. $this->_znxc($post,$post['orderinfo'],1);
  897. }
  898. }//else{}如果有此订单
  899. }
  900. }
  901. /**
  902. if ($this->db->trans_status() === FALSE)
  903. {
  904. $this->db->trans_commit();
  905. }
  906. else
  907. {
  908. $this->db->trans_commit();
  909. }
  910. **/
  911. //sleep(1);//停留2秒
  912. $this->shop->save(array('tbtime'=>time()),$value['id']);
  913. }
  914. if($tb == 1)
  915. {
  916. echo json_encode(array('msg'=>'有店铺正在同步中,请5分钟后再试!','success'=>true));exit;
  917. }
  918. //echo json_encode(array('msg'=>'OK!','success'=>true));exit;
  919. echo "<pre>";
  920. print_r($sd);
  921. }
  922. }
  923. public function _znxc($fullorder,$order_id,$type,$logisticstatus='')
  924. {
  925. $time = time();
  926. if($type == 1)//地址确认需要状态对应
  927. {
  928. $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'");//有条件不同模板匹配
  929. $express = array();
  930. }
  931. else if($type == 4)//运单号通知
  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. $express = $this->express->read($fullorder['express']);
  935. }
  936. else if($type == 3)//催付
  937. {
  938. $notice = $this->notice->find_all("shop = '".$fullorder['shop']."' and message = '3' and state = '1' and ktime < '$time' and jtime > '$time' and type = '$type'");//有条件不同模板匹配
  939. }
  940. else if($type == 2)//物流发送
  941. {
  942. $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'");//有条件不同模板匹配
  943. $express = $this->express->read($fullorder['express']);
  944. }
  945. if(isset($notice[0]['content']) && $type != 3)
  946. {
  947. $fid = $this->fullordertt->get_orderinfo($order_id);
  948. $shop = $this->shop->read($fullorder['shop']);
  949. $country = $this->country->read($fullorder['country']);
  950. $fullorder['country'] = $country['ename'];
  951. $go = $this->notice->get_god($fullorder,$shop,$express,$notice[0]);
  952. if($go == 1)
  953. {
  954. $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']);
  955. return 1;
  956. }
  957. else
  958. {
  959. $this->fullordertt->save(array('exstateerror'=>'自动发送失败1:'.date('Y-m-d H',time()).'-'.$go.'('.$notice[0]['type'].'-'.$notice[0]['message'].')'),$fid['id']);
  960. }
  961. }
  962. else if(isset($notice[0]['content']) && $type == 3)
  963. {
  964. $shop = $this->shop->read($fullorder['shop']);
  965. $go = $this->notice->get_god($fullorder,$shop,array(),$notice[0]);
  966. if($go == 1)
  967. {
  968. $this->ttcf->save(array('cg'=>1,'shop'=>$fullorder['shop'],'data'=>'自动发送-成功-'.$type.'-'.date("Y-m-d H:i:s",time())),$fullorder['id']);
  969. return 1;
  970. }
  971. else
  972. {
  973. $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']);
  974. }
  975. }
  976. else
  977. {
  978. if($type == 3)
  979. {
  980. $this->ttcf->save(array('cg'=>3,'shop'=>$fullorder['shop'],'data'=>'当前订单状态无匹配模板'),$fullorder['id']);
  981. }
  982. else
  983. {
  984. $fid = $this->fullordertt->get_orderinfo($order_id);
  985. $this->fullordertt->save(array('exstateerror'=>'当前订单状态无匹配模板 - state:'.$fullorder['state'].' - shop:'.$fullorder['shop'].' - time:'.$time.' - type:'.$type.' - notice:'.json_encode($notice)),$fid['id']);
  986. }
  987. }
  988. }
  989. public function _webhook()
  990. {
  991. header('HTTP/1.1 200 OK');
  992. ob_flush();
  993. flush();
  994. $j = file_get_contents('php://input');
  995. $j = json_decode($j, true);
  996. if(isset($j['type']) && isset($j['data']['order_id']))
  997. {
  998. $data = json_encode($j['data']);
  999. $tid = $this->ttwebhook->get_tid($j['tts_notification_id']);
  1000. if($tid)
  1001. {
  1002. exit;
  1003. }
  1004. else
  1005. {
  1006. $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())));
  1007. }
  1008. $ch = curl_init();
  1009. curl_setopt($ch,CURLOPT_URL,'http://a10.wepolicy.cn/apitt/ttwebhook');
  1010. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  1011. curl_setopt($ch,CURLOPT_HEADER,0);
  1012. curl_setopt($ch,CURLOPT_POST, 1);
  1013. curl_setopt($ch,CURLOPT_TIMEOUT,1);
  1014. //设置post数据
  1015. $post = array();
  1016. $post['j'] = json_encode($j);
  1017. curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));
  1018. $res = curl_exec($ch);
  1019. curl_close($ch);
  1020. }
  1021. exit;
  1022. }
  1023. public function _ttwebhook()
  1024. {
  1025. $post = $this->input->post(NULL, TRUE);
  1026. if(isset($post['j']))
  1027. {
  1028. $j = $this->input->post('j',true);
  1029. $j = json_decode($j, true);
  1030. //$this->logic_ding->sendToDing("获取TT的webhook数据:".json_encode($j));
  1031. $xq = array();
  1032. if($j['type'] == 1 && $j['data']['order_status'] == 'UNPAID')
  1033. {
  1034. $shop = $this->shop->get_shopid($j['shop_id']);
  1035. if(!$shop)
  1036. {
  1037. exit;
  1038. }
  1039. $d = $this->apitt->get_data(array($j['data']['order_id']),$shop);
  1040. if(isset($d['data']['orders'][0]['user_id']))
  1041. {
  1042. $post = array('orderinfo'=>$j['data']['order_id'],'user_id'=>$d['data']['orders'][0]['user_id']);
  1043. $num = $this->ttcf->find_count('orderinfo = '.$post['orderinfo']);
  1044. if(!$num){
  1045. $id = $this->ttcf->insert($post);
  1046. $post['shop'] = $shop['id'];
  1047. $post['id'] = $id;
  1048. $this->_znxc($post,$post['orderinfo'],3);
  1049. }
  1050. }
  1051. exit;
  1052. }
  1053. $d = $this->fullordertt->get_orderinfo($j['data']['order_id']);
  1054. if($d)
  1055. {
  1056. $post = $this->fullordertt->read($d['id']);
  1057. $shop = $this->shop->read($d['shop']);
  1058. $qchz = array();
  1059. $f = explode('|',trim($d['whlabel'],'|'));
  1060. foreach ($f as $v)
  1061. {
  1062. $hz = explode('-',trim($v,'-'));
  1063. if(isset($hz[2]) && $hz[2] != 'DNOTO' && $hz[2] != '0')
  1064. {
  1065. $qchz[] = $hz[2];
  1066. }
  1067. }
  1068. $d['whlabel']= str_replace($qchz,'0',$d['whlabel']);
  1069. $d['fpdata']= str_replace($qchz,'0',$d['fpdata']);
  1070. $this->db->trans_begin();
  1071. if($j['type'] == 1)
  1072. {
  1073. if($j['data']['order_status'] == 'CANCEL' || $j['data']['order_status'] == 'Canceled')
  1074. {
  1075. if($d['library'] != 2)
  1076. {
  1077. $whlabel = $this->whlabel->find_all("zd = '".$d['number']."'");
  1078. foreach ($whlabel as $v)
  1079. {
  1080. $this->whlabel->save(array('zd'=>''),$v['id']);
  1081. }
  1082. $this->fullordertt->save(array('state'=>217,'whlabel'=>$d['whlabel'],'fpdata'=>$d['fpdata']),$d['id']);
  1083. }
  1084. else
  1085. {
  1086. $this->fullordertt->save(array('state'=>214),$d['id']);
  1087. }
  1088. }
  1089. else if(($j['data']['order_status'] == 'IN_TRANSIT' || $j['data']['order_status'] == 'COMPLETED currently)') && $d['state'] != 216)
  1090. {
  1091. if($d['cf'] == 1 && $d['type'] == 34 && $j['data']['order_status'] == 'IN_TRANSIT')
  1092. {
  1093. $wb = $this->apitt->get_data(array($d['orderinfo']),$shop);
  1094. $waybill = $wb['data']['orders']['0']['tracking_number'];
  1095. $ex = $wb['data']['orders']['0']['shipping_provider'];
  1096. $express = 0;
  1097. if(stripos($ex,"UPS") !== false)
  1098. {
  1099. $express = 3;
  1100. }
  1101. else if(stripos($ex,"DHL") !== false)
  1102. {
  1103. $express = 1;
  1104. }
  1105. else if(stripos($ex,"FEDEX") !== false)
  1106. {
  1107. $express = 24;
  1108. }
  1109. else if(stripos($ex,"USPS") !== false)
  1110. {
  1111. $express = 2;
  1112. }
  1113. else if(stripos($ex,"ONTRAC") !== false)
  1114. {
  1115. $express = 62;
  1116. }
  1117. $time = time();
  1118. $post['waybill'] = $waybill;
  1119. $post['express'] = $express;
  1120. $express_id = $express;
  1121. $d['waybill'] = $waybill;
  1122. $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']);
  1123. }
  1124. else
  1125. {
  1126. $this->fullordertt->save(array('state'=>216,'whlabel'=>$d['whlabel'],'fpdata'=>$d['fpdata']),$d['id']);
  1127. $express_id = $d['express'];
  1128. }
  1129. if($express_id != 0)
  1130. {
  1131. if(stripos('|'.$post['zdstate'],'|4-') === false)
  1132. {
  1133. $this->_znxc($post,$post['orderinfo'],4);
  1134. }
  1135. $expressdata = $this->express->read($express_id);
  1136. $service = $this->service->read($expressdata['service']);
  1137. if(($expressdata['cxcode'] == 'ups' || $expressdata['cxcode'] == '17track' || $expressdata['cxcode'] == 'usps') && $d['webhookregister'] < 1)
  1138. {
  1139. $logistics = $expressdata['cxcode'];
  1140. $d['servicename'] =$service['title'];//增加快递名称
  1141. $d['url'] = $expressdata['url'];//增加快递链接
  1142. @$fs = $this->$logistics->get_logistics($d);
  1143. if(isset($fs) && $fs['f'] == '1')
  1144. {
  1145. $this->fullordertt->save(array('expressstate'=>$fs['exstate'],'excontent'=>$fs['data'],'webhookregister'=>1),$d['id']);
  1146. }
  1147. }
  1148. }
  1149. }
  1150. else if($j['data']['order_status'] == 'DELIVERED')
  1151. {
  1152. $je = $this->apitt->get_je($d,$shop);
  1153. $budget = 0;
  1154. if(isset($je['data']['statement_transactions']['0']['settlement_amount']))
  1155. {
  1156. foreach ($je['data']['statement_transactions'] as $val)
  1157. {
  1158. $budget += $val['settlement_amount'];
  1159. }
  1160. $this->fullordertt->save(array('budget'=>$budget),$d['id']);
  1161. }
  1162. if(stripos('|'.$post['zdstate'],'|2-') === false)
  1163. {
  1164. $this->_znxc($post,$post['orderinfo'],2,6);//2物流模板,6已签收
  1165. }
  1166. }
  1167. }
  1168. if($j['type'] == 2)
  1169. {
  1170. if(isset($j['reverse_order_status'])){
  1171. if($j['reverse_order_status'] == 50 || $j['reverse_order_status'] == 51)
  1172. {
  1173. $whlabel = $this->whlabel->find_all("zd = '".$d['number']."'");
  1174. foreach ($whlabel as $v)
  1175. {
  1176. $this->whlabel->save(array('zd'=>''),$v['id']);
  1177. }
  1178. $this->fullordertt->save(array('state'=>214,'whlabel'=>$d['whlabel'],'fpdata'=>$d['fpdata']),$d['id']);
  1179. }
  1180. }
  1181. /**
  1182. reverse_order_status
  1183. 售后申请 = 1
  1184. 售后拒绝申请 = 2
  1185. 售后退货 = 3
  1186. 售后买家已发货 = 4
  1187. 售后卖家_拒绝接收 = 5
  1188. 售后成功 = 50
  1189. 取消成功 = 51
  1190. 关闭 = 99
  1191. 完成 = 100
  1192. **/
  1193. }
  1194. if ($this->db->trans_status() === TRUE)
  1195. {
  1196. $this->db->trans_commit();
  1197. }
  1198. else
  1199. {
  1200. $this->db->trans_rollback();
  1201. }
  1202. }
  1203. }
  1204. }
  1205. public function _gxbudget()
  1206. {
  1207. $time = time();
  1208. $data = $this->fullordertt->find_all("source != 1 and budget = 0 and librarytime > '".($time-6*24*3600)."' and librarytime < '".($time-9*24*3600)."'");
  1209. foreach ($data as $v)
  1210. {
  1211. $shop = $this->shop->read($v['shop']);
  1212. $je = $this->apitt->get_je($v,$shop);
  1213. $budget = 0;
  1214. if(isset($je['data']['statement_transactions']['0']['settlement_amount']))
  1215. {
  1216. foreach ($je['data']['statement_transactions'] as $val)
  1217. {
  1218. $budget += $val['settlement_amount'];
  1219. }
  1220. $this->fullordertt->save(array('budget'=>$budget),$v['id']);
  1221. }
  1222. }
  1223. }
  1224. }