Apitt.php 48 KB

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