Apitt.php 43 KB

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