Model_express_tt.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. class Model_express_tt extends Lin_Model {
  3. function __construct(){
  4. parent::__construct();
  5. $this->load->_model("Model_apitt","apitt");
  6. }
  7. public function get_data($info,$type = 'CBT'){
  8. if(empty($info)){
  9. return [
  10. 'x'=>0,
  11. 'Description'=>'订单信息不存在',
  12. ];
  13. }
  14. if(is_numeric($info['shop'])){
  15. $shopid = $info['shop'];
  16. }else{
  17. $shopid = $info['tmp_shop_id'];
  18. }
  19. $shop_info = $this->db->from('shop')->where('id', $shopid)->get()->result_array();
  20. if(empty($shop_info)){
  21. return [
  22. 'x'=>0,
  23. 'Description'=>'该商铺不存在',
  24. ];
  25. }
  26. if($type == 'CBT'){
  27. if($info['merge'] == $info['id']){
  28. $extra_text = json_decode($info['extra_text'],true);
  29. if(is_string($extra_text)){
  30. $extra_text = json_decode($extra_text,true);
  31. }
  32. if(empty($extra_text['label_info'])){
  33. $res = $this->getDetail($info,$shop_info[0]);
  34. if($res['x'] == 0){
  35. return $res;
  36. }
  37. $info = $res['data'];
  38. }
  39. return $this->downloadCbtConbinLabel($info,$shop_info[0]);
  40. }else{
  41. //获取快递服务信息
  42. $res = $this->getExpressCompany($info,$shop_info[0]);
  43. if($res['x'] == 0){
  44. return $res;
  45. }
  46. $res1 = $this->createExpressLabel($res['data'],$shop_info[0]);
  47. if($res1['x'] == 0){
  48. return $res1;
  49. }
  50. if(is_string($info['extra_text'])){
  51. $info['extra_text'] = json_decode($info['extra_text'],true);
  52. }
  53. if(empty($info['extra_text']['label_info'])){
  54. sleep(2);
  55. }
  56. $res2 = $this->downloadExpressLabel($res1['data'],$shop_info[0]);
  57. return $res2;
  58. }
  59. }else if($type == 'FBT'){
  60. $extra_text = json_decode($info['extra_text'],true);
  61. if(empty($extra_text)){
  62. return $this->getFBTOrderDetail($info,$shop_info);
  63. }
  64. if(empty($extra_text['packages'])){
  65. return $this->getFBTOrderDetail($info,$shop_info);
  66. }
  67. $info['extra_text'] = $extra_text;
  68. return $this->getFBTPackage($info,$shop_info);
  69. }else{
  70. return [
  71. 'x'=>0,
  72. 'Description'=>'暂不支持该快递类型',
  73. ];
  74. }
  75. }
  76. private function getFBTOrderDetail($info,$shop_info){
  77. $res = $this->apitt->get_data([$info['orderinfo']],$shop_info[0]);
  78. if($res['code'] != 0){
  79. return [
  80. 'x'=>0,
  81. 'Description'=>$res['message'],
  82. ];
  83. }
  84. if(!isset($res['data']['orders'])){
  85. return [
  86. 'x'=>0,
  87. 'Description'=>"获取TT订单信息异常",
  88. ];
  89. }
  90. if(empty($res['data']['orders'])){
  91. return [
  92. 'x'=>0,
  93. 'Description'=>"获取TT订单信息异常",
  94. ];
  95. }
  96. if(empty($res['data']['orders'][0]['packages'])){
  97. return [
  98. 'x'=>0,
  99. 'Description'=>"获取TT订单的快递信息",
  100. ];
  101. }
  102. $packages = $res['data']['orders'][0]['packages'];
  103. if(!is_array($info['extra_text'])){
  104. $extra_text = json_decode($info['extra_text'],true);
  105. }else{
  106. $extra_text = $info['extra_text'];
  107. }
  108. $extra_text['packages'] = $packages;
  109. $this->db->update('fullordertt', [
  110. 'extra_text' => json_encode($extra_text,JSON_UNESCAPED_UNICODE)
  111. ], ['id' => $info['id']]) ;
  112. $packageid = $packages[0]['id'];
  113. $rr = $this->apitt->downloadLabel($packageid,$shop_info[0],$info);
  114. if($rr['code']!=0){
  115. return [
  116. 'x'=>0,
  117. 'Description'=>$rr['message'],
  118. ];
  119. }else{
  120. return [
  121. 'x'=>1,
  122. 'msg'=>'获取成功',
  123. 'waybill'=>$rr['data']['tracking_number'],
  124. 'label'=>$rr['data']['doc_url'],
  125. ];
  126. }
  127. }
  128. private function getFBTPackage($info,$shop_info){
  129. $extra_text = $info['extra_text'];
  130. if(is_string($extra_text)){
  131. $extra_text = json_decode($extra_text,true);
  132. }
  133. $packages = $extra_text['packages'];
  134. $packageid = $packages[0]['id'];
  135. if(!isset($extra_text['is_send'])){
  136. $rr1 = $this->apitt->ShipByTiktok($packageid,$shop_info[0],$info);
  137. if(!isset($rr1['code'])){
  138. //避免因为网络原因获取信息失败,再次尝试获取
  139. usleep(100);
  140. $rr1 = $this->apitt->ShipByTiktok($packageid,$shop_info[0],$info);
  141. if(!isset($rr1['code'])){
  142. return [
  143. 'x'=>0,
  144. 'Description'=>"Tiktok接口异常,请重试",
  145. ];
  146. }
  147. }
  148. if(($rr1['code'] == 0) && ($rr1['message'] == 'Success')){
  149. $extra_text['is_send'] = 1;
  150. $this->db->update('fullordertt', [
  151. 'extra_text' => json_encode($extra_text,JSON_UNESCAPED_UNICODE)
  152. ], ['id' => $info['id']]) ;
  153. }else{
  154. return [
  155. 'x'=>0,
  156. 'Description'=>$rr1['message'],
  157. ];
  158. }
  159. }
  160. // if(!isset($extra_text['server_info'])){
  161. // $rr1 = $this->getExpressCompany($info,$shop_info[0]);
  162. // if($rr1['x'] == 0){
  163. // return $rr1;
  164. // }
  165. // $info = $rr1['data'];
  166. // if(is_string($info['extra_text'])){
  167. // $info['extra_text'] = json_decode($info['extra_text'],true);
  168. // }
  169. // }
  170. // 确定没有多包裹的情况 就不再写循环了 直接获取第一个包裹
  171. // foreach($packages as $package){
  172. // $rr = $this->apitt->downloadLabel($package['id'],$shop_info[0],$info);
  173. // if($rr['code']!=0){
  174. // $flag_status = 1;
  175. // $err_arr[] = $rr['message'];
  176. // }else{
  177. // $label_list[] = [
  178. // 'waybill'=>$rr['data']['tracking_number'],
  179. // 'label'=>$rr['data']['doc_url'],
  180. // ];
  181. // }
  182. // }
  183. $rr = $this->apitt->downloadLabel($packageid,$shop_info[0],$info);
  184. if(!isset($rr['code'])){
  185. usleep(100);
  186. $rr = $this->apitt->downloadLabel($packageid,$shop_info[0],$info);
  187. if(!isset($rr['code'])){
  188. return [
  189. 'x'=>0,
  190. 'Description'=>"tiktok获取快递面单异常,请重试",
  191. ];
  192. }
  193. }
  194. if($rr['code']!=0){
  195. return [
  196. 'x'=>0,
  197. 'Description'=>$rr['message'],
  198. ];
  199. }else{
  200. return [
  201. 'x'=>1,
  202. 'msg'=>'获取成功',
  203. 'waybill'=>$rr['data']['tracking_number'],
  204. 'label'=>$rr['data']['doc_url'],
  205. ];
  206. }
  207. }
  208. private function getOrderDetail($info,$shop_info){
  209. }
  210. //获取订单承运的快递商
  211. private function getExpressCompany($info,$shop_info){
  212. if(is_string($info['extra_text'])){
  213. $extra_text = json_decode($info['extra_text'],true);
  214. }else{
  215. $extra_text = $info['extra_text'];
  216. }
  217. if(isset($extra_text['server_info'])){
  218. return [
  219. 'x'=>1,
  220. 'msg'=>'获取成功1',
  221. 'data'=>$info
  222. ];
  223. }
  224. $res = $this->apitt->getExpressCompany($info,$shop_info);
  225. if(empty($res)){
  226. return [
  227. 'x'=>0,
  228. 'Description'=>"返回信息为空",
  229. ];
  230. }
  231. if($res['code'] != 0){
  232. return [
  233. 'x'=>0,
  234. 'Description'=>$res['message'],
  235. ];
  236. }
  237. $extra_text = json_decode($info['extra_text'],true);
  238. $extra_text['server_info'] = $res['data'];
  239. $info['extra_text'] = json_encode($extra_text,JSON_UNESCAPED_UNICODE);
  240. if($info['ttsp_status'] == 0){
  241. $this->db->update('fullordertt', [
  242. 'extra_text' => $info['extra_text'],
  243. 'ttsp_status' => 1
  244. ], ['id' => $info['id']]) ;
  245. }else{
  246. $this->db->update('fullordertt', [
  247. 'extra_text' => $info['extra_text']
  248. ], ['id' => $info['id']]) ;
  249. }
  250. return [
  251. 'x'=>1,
  252. 'msg'=>'获取成功1',
  253. 'data'=>$info
  254. ];
  255. }
  256. //创建发货单和运单标签
  257. public function createExpressLabel($info,$shop_info){
  258. if(is_string($info['extra_text'])){
  259. $extra_text = json_decode($info['extra_text'],true);
  260. }else{
  261. $extra_text = $info['extra_text'];
  262. }
  263. if(isset($extra_text['label_info'])){
  264. return [
  265. 'x'=>1,
  266. 'msg'=>'获取成功2',
  267. 'data'=>$info
  268. ];
  269. }
  270. $res = $this->apitt->createLabel($info,$shop_info);
  271. if(empty($res)){
  272. return [
  273. 'x'=>0,
  274. 'Description'=>"返回信息为空",
  275. ];
  276. }
  277. if(strpos($res['message'],'These orders are already shipped.')!== false){
  278. return $this->getDetail($info,$shop_info);
  279. }
  280. if($res['code'] != 0){
  281. return [
  282. 'x'=>0,
  283. 'Description'=>$res['message'],
  284. ];
  285. }
  286. $extra_text = json_decode($info['extra_text'],true);
  287. $extra_text['label_info'] = $res['data'];
  288. $info['extra_text'] = json_encode($extra_text,JSON_UNESCAPED_UNICODE);
  289. $this->db->update('fullordertt', [
  290. 'extra_text' => $info['extra_text']
  291. ], ['id' => $info['id']]) ;
  292. return [
  293. 'x'=>1,
  294. 'msg'=>'获取成功2',
  295. 'data'=>$info
  296. ];
  297. }
  298. private function getDetail($info,$shop_info){
  299. if(is_string($info['extra_text'])){
  300. $extra_text = json_decode($info['extra_text'],true);
  301. }else{
  302. $extra_text = $info['extra_text'];
  303. }
  304. $ret_info = $this->apitt->get_detail($info['orderinfo'],$shop_info);
  305. if(empty($ret_info)){
  306. return [
  307. 'x'=>0,
  308. 'Description'=>"获取订单详细信息失败1",
  309. ];
  310. }
  311. if($ret_info['code'] != 0){
  312. return [
  313. 'x'=>0,
  314. 'Description'=>"获取订单详细信息失败2",
  315. ];
  316. }
  317. if(empty($ret_info['data']['orders'])){
  318. return [
  319. 'x'=>0,
  320. 'Description'=>"获取订单详细信息失败3",
  321. ];
  322. }
  323. $packages = $ret_info['data']['orders'][0]['packages'];
  324. if(empty($packages)){
  325. return [
  326. 'x'=>0,
  327. 'Description'=>"包裹信息异常不存在",
  328. ];
  329. }
  330. $package_id = $packages[0]['id'];
  331. $extra_text['label_info'] = ['package_id'=>$package_id];
  332. $info['extra_text'] = json_encode($extra_text,JSON_UNESCAPED_UNICODE);
  333. $this->db->update('fullordertt', [
  334. 'extra_text' => $info['extra_text']
  335. ], ['id' => $info['id']]) ;
  336. return [
  337. 'x'=>1,
  338. 'msg'=>'获取成功2',
  339. 'data'=>$info
  340. ];
  341. }
  342. public function downloadCbtConbinLabel($info,$shop){
  343. if(is_string($info['extra_text'])){
  344. $extra_text = json_decode($info['extra_text'],true);
  345. }else{
  346. $extra_text = $info['extra_text'];
  347. }
  348. $packageid = $extra_text['label_info']['package_id'];
  349. $res = $this->apitt->downloadLabel($packageid,$shop,$info);
  350. //允许二次请求吧 tiktok接口有时候会返回很奇葩的错误
  351. if( empty($res) || $res['code'] != 0){
  352. $res = $this->apitt->downloadLabel($packageid,$shop,$info);
  353. usleep(100);
  354. if(empty($res) || $res['code'] != 0){
  355. return [
  356. 'x'=>0,
  357. 'Description'=>$res['message'],
  358. ];
  359. }
  360. }
  361. $extra_text = json_decode($info['extra_text'],true);
  362. $extra_text['express_info'] = $res['data'];
  363. $info['extra_text'] = json_encode($extra_text,JSON_UNESCAPED_UNICODE);
  364. $this->db->update('fullordertt', [
  365. 'extra_text' => $info['extra_text']
  366. ], ['id' => $info['id']]) ;
  367. return [
  368. 'x'=>1,
  369. 'msg'=>'获取成功3',
  370. 'waybill'=>$res['data']['tracking_number'],
  371. 'label'=>$res['data']['doc_url'],
  372. ];
  373. }
  374. //下载快递面单
  375. public function downloadExpressLabel($info,$shop){
  376. if(is_string($info['extra_text'])){
  377. $extra_text = json_decode($info['extra_text'],true);
  378. }else{
  379. $extra_text = $info['extra_text'];
  380. }
  381. $packageid = $extra_text['label_info']['package_id'];
  382. $res = $this->apitt->downloadLabel($packageid,$shop,$info);
  383. //允许二次请求吧 tiktok接口有时候会返回很奇葩的错误
  384. if( empty($res) || $res['code'] != 0){
  385. $res = $this->apitt->downloadLabel($packageid,$shop,$info);
  386. usleep(100);
  387. if(empty($res) || $res['code'] != 0){
  388. return [
  389. 'x'=>0,
  390. 'Description'=>$res['message'],
  391. ];
  392. }
  393. }
  394. $extra_text = json_decode($info['extra_text'],true);
  395. $extra_text['express_info'] = $res['data'];
  396. $info['extra_text'] = json_encode($extra_text,JSON_UNESCAPED_UNICODE);
  397. $this->db->update('fullordertt', [
  398. 'extra_text' => $info['extra_text']
  399. ], ['id' => $info['id']]) ;
  400. return [
  401. 'x'=>1,
  402. 'msg'=>'获取成功3',
  403. 'waybill'=>$res['data']['tracking_number'],
  404. 'label'=>$res['data']['doc_url'],
  405. ];
  406. }
  407. /**
  408. * 创建CBT发货单和运单标签
  409. */
  410. public function doCreateCBTLabel($info,$shop_info){
  411. if($info['merge'] == $info['id']){
  412. $extra_text = json_decode($info['extra_text'],true);
  413. if(is_string($extra_text)){
  414. $extra_text = json_decode($extra_text,true);
  415. }
  416. if(empty($extra_text['label_info'])){
  417. $res = $this->getDetail($info,$shop_info);
  418. if($res['x'] == 0){
  419. return $res;
  420. }
  421. return [
  422. 'x'=>1,
  423. 'msg'=>"OK",
  424. 'data'=>$res['data']
  425. ];
  426. }else{
  427. return [
  428. 'x'=>1,
  429. 'msg'=>"OK",
  430. 'data'=>$info
  431. ];
  432. }
  433. }else{
  434. //获取快递服务信息
  435. $res = $this->getExpressCompany($info,$shop_info);
  436. if($res['x'] == 0){
  437. return $res;
  438. }
  439. $res1 = $this->createExpressLabel($res['data'],$shop_info);
  440. if($res1['x'] == 0){
  441. return $res1;
  442. }
  443. return $res1;
  444. }
  445. }
  446. /**
  447. * 获取快递承运商
  448. */
  449. public function getExpressInfo($info,$shop_info){
  450. $res = $this->getExpressCompany($info,$shop_info);
  451. if($res['x'] == 0){
  452. return $res;
  453. }
  454. return $res;
  455. }
  456. }