Apitt.php 44 KB

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