Apitt.php 42 KB

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