Model_express_tt.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. //获取快递服务信息
  28. $res = $this->getExpressCompany($info,$shop_info[0]);
  29. if($res['x'] == 0){
  30. return $res;
  31. }
  32. $res1 = $this->createExpressLabel($res['data'],$shop_info[0]);
  33. if($res1['x'] == 0){
  34. return $res1;
  35. }
  36. if(is_string($info['extra_text'])){
  37. $info['extra_text'] = json_decode($info['extra_text'],true);
  38. }
  39. if(empty($info['extra_text']['label_info'])){
  40. sleep(2);
  41. }
  42. $res2 = $this->downloadExpressLabel($res1['data'],$shop_info[0]);
  43. return $res2;
  44. }else if($type == 'FBT'){
  45. $extra_text = json_decode($info['extra_text'],true);
  46. if(empty($extra_text)){
  47. return $this->getFBTOrderDetail($info,$shop_info);
  48. }
  49. if(empty($extra_text['packages'])){
  50. return $this->getFBTOrderDetail($info,$shop_info);
  51. }
  52. $info['extra_text'] = $extra_text;
  53. return $this->getFBTPackage($info,$shop_info);
  54. }else{
  55. return [
  56. 'x'=>0,
  57. 'Description'=>'暂不支持该快递类型',
  58. ];
  59. }
  60. }
  61. private function getFBTOrderDetail($info,$shop_info){
  62. $res = $this->apitt->get_data([$info['orderinfo']],$shop_info[0]);
  63. if($res['code'] != 0){
  64. return [
  65. 'x'=>0,
  66. 'Description'=>$res['message'],
  67. ];
  68. }
  69. if(!isset($res['data']['orders'])){
  70. return [
  71. 'x'=>0,
  72. 'Description'=>"获取TT订单信息异常",
  73. ];
  74. }
  75. if(empty($res['data']['orders'])){
  76. return [
  77. 'x'=>0,
  78. 'Description'=>"获取TT订单信息异常",
  79. ];
  80. }
  81. if(empty($res['data']['orders'][0]['packages'])){
  82. return [
  83. 'x'=>0,
  84. 'Description'=>"获取TT订单的快递信息",
  85. ];
  86. }
  87. $packages = $res['data']['orders'][0]['packages'];
  88. if(!is_array($info['extra_text'])){
  89. $extra_text = json_decode($info['extra_text'],true);
  90. }else{
  91. $extra_text = $info['extra_text'];
  92. }
  93. $extra_text['packages'] = $packages;
  94. $this->db->update('fullordertt', [
  95. 'extra_text' => json_encode($extra_text,JSON_UNESCAPED_UNICODE)
  96. ], ['id' => $info['id']]) ;
  97. $packageid = $packages[0]['id'];
  98. $rr = $this->apitt->downloadLabel($packageid,$shop_info[0],$info);
  99. if($rr['code']!=0){
  100. return [
  101. 'x'=>0,
  102. 'Description'=>$rr['message'],
  103. ];
  104. }else{
  105. return [
  106. 'x'=>1,
  107. 'msg'=>'获取成功',
  108. 'waybill'=>$rr['data']['tracking_number'],
  109. 'label'=>$rr['data']['doc_url'],
  110. ];
  111. }
  112. }
  113. private function getFBTPackage($info,$shop_info){
  114. $extra_text = $info['extra_text'];
  115. if(is_string($extra_text)){
  116. $extra_text = json_decode($extra_text,true);
  117. }
  118. $packages = $extra_text['packages'];
  119. $packageid = $packages[0]['id'];
  120. if(!isset($extra_text['is_send'])){
  121. $rr1 = $this->apitt->ShipByTiktok($packageid,$shop_info[0],$info);
  122. if(!isset($rr1['code'])){
  123. //避免因为网络原因获取信息失败,再次尝试获取
  124. usleep(100);
  125. $rr1 = $this->apitt->ShipByTiktok($packageid,$shop_info[0],$info);
  126. if(!isset($rr1['code'])){
  127. return [
  128. 'x'=>0,
  129. 'Description'=>"Tiktok接口异常,请重试",
  130. ];
  131. }
  132. }
  133. if(($rr1['code'] == 0) && ($rr1['message'] == 'Success')){
  134. $extra_text['is_send'] = 1;
  135. $this->db->update('fullordertt', [
  136. 'extra_text' => json_encode($extra_text,JSON_UNESCAPED_UNICODE)
  137. ], ['id' => $info['id']]) ;
  138. }else{
  139. return [
  140. 'x'=>0,
  141. 'Description'=>$rr1['message'],
  142. ];
  143. }
  144. }
  145. // if(!isset($extra_text['server_info'])){
  146. // $rr1 = $this->getExpressCompany($info,$shop_info[0]);
  147. // if($rr1['x'] == 0){
  148. // return $rr1;
  149. // }
  150. // $info = $rr1['data'];
  151. // if(is_string($info['extra_text'])){
  152. // $info['extra_text'] = json_decode($info['extra_text'],true);
  153. // }
  154. // }
  155. // 确定没有多包裹的情况 就不再写循环了 直接获取第一个包裹
  156. // foreach($packages as $package){
  157. // $rr = $this->apitt->downloadLabel($package['id'],$shop_info[0],$info);
  158. // if($rr['code']!=0){
  159. // $flag_status = 1;
  160. // $err_arr[] = $rr['message'];
  161. // }else{
  162. // $label_list[] = [
  163. // 'waybill'=>$rr['data']['tracking_number'],
  164. // 'label'=>$rr['data']['doc_url'],
  165. // ];
  166. // }
  167. // }
  168. $rr = $this->apitt->downloadLabel($packageid,$shop_info[0],$info);
  169. if(!isset($rr['code'])){
  170. usleep(100);
  171. $rr = $this->apitt->downloadLabel($packageid,$shop_info[0],$info);
  172. if(!isset($rr['code'])){
  173. return [
  174. 'x'=>0,
  175. 'Description'=>"tiktok获取快递面单异常,请重试",
  176. ];
  177. }
  178. }
  179. if($rr['code']!=0){
  180. return [
  181. 'x'=>0,
  182. 'Description'=>$rr['message'],
  183. ];
  184. }else{
  185. return [
  186. 'x'=>1,
  187. 'msg'=>'获取成功',
  188. 'waybill'=>$rr['data']['tracking_number'],
  189. 'label'=>$rr['data']['doc_url'],
  190. ];
  191. }
  192. }
  193. private function getOrderDetail($info,$shop_info){
  194. }
  195. //获取订单承运的快递商
  196. private function getExpressCompany($info,$shop_info){
  197. if(is_string($info['extra_text'])){
  198. $extra_text = json_decode($info['extra_text'],true);
  199. }else{
  200. $extra_text = $info['extra_text'];
  201. }
  202. if(isset($extra_text['server_info'])){
  203. return [
  204. 'x'=>1,
  205. 'msg'=>'获取成功1',
  206. 'data'=>$info
  207. ];
  208. }
  209. $res = $this->apitt->getExpressCompany($info,$shop_info);
  210. if(empty($res)){
  211. return [
  212. 'x'=>0,
  213. 'Description'=>"返回信息为空",
  214. ];
  215. }
  216. if($res['code'] != 0){
  217. return [
  218. 'x'=>0,
  219. 'Description'=>$res['message'],
  220. ];
  221. }
  222. $extra_text = json_decode($info['extra_text'],true);
  223. $extra_text['server_info'] = $res['data'];
  224. $info['extra_text'] = json_encode($extra_text,JSON_UNESCAPED_UNICODE);
  225. $this->db->update('fullordertt', [
  226. 'extra_text' => $info['extra_text']
  227. ], ['id' => $info['id']]) ;
  228. return [
  229. 'x'=>1,
  230. 'msg'=>'获取成功1',
  231. 'data'=>$info
  232. ];
  233. }
  234. //创建发货单和运单标签
  235. private function createExpressLabel($info,$shop_info){
  236. if(is_string($info['extra_text'])){
  237. $extra_text = json_decode($info['extra_text'],true);
  238. }else{
  239. $extra_text = $info['extra_text'];
  240. }
  241. if(isset($extra_text['label_info'])){
  242. return [
  243. 'x'=>1,
  244. 'msg'=>'获取成功2',
  245. 'data'=>$info
  246. ];
  247. }
  248. $res = $this->apitt->createLabel($info,$shop_info);
  249. if(empty($res)){
  250. return [
  251. 'x'=>0,
  252. 'Description'=>"返回信息为空",
  253. ];
  254. }
  255. if($res['code'] != 0){
  256. return [
  257. 'x'=>0,
  258. 'Description'=>$res['message'],
  259. ];
  260. }
  261. $extra_text = json_decode($info['extra_text'],true);
  262. $extra_text['label_info'] = $res['data'];
  263. $info['extra_text'] = json_encode($extra_text,JSON_UNESCAPED_UNICODE);
  264. $this->db->update('fullordertt', [
  265. 'extra_text' => $info['extra_text']
  266. ], ['id' => $info['id']]) ;
  267. return [
  268. 'x'=>1,
  269. 'msg'=>'获取成功2',
  270. 'data'=>$info
  271. ];
  272. }
  273. //下载快递面单
  274. private function downloadExpressLabel($info,$shop){
  275. if(is_string($info['extra_text'])){
  276. $extra_text = json_decode($info['extra_text'],true);
  277. }else{
  278. $extra_text = $info['extra_text'];
  279. }
  280. $packageid = $extra_text['label_info']['package_id'];
  281. $res = $this->apitt->downloadLabel($packageid,$shop,$info);
  282. if($res['code'] != 0){
  283. return [
  284. 'x'=>0,
  285. 'Description'=>$res['message'],
  286. ];
  287. }
  288. $extra_text = json_decode($info['extra_text'],true);
  289. $extra_text['express_info'] = $res['data'];
  290. $info['extra_text'] = json_encode($extra_text,JSON_UNESCAPED_UNICODE);
  291. $this->db->update('fullordertt', [
  292. 'extra_text' => $info['extra_text']
  293. ], ['id' => $info['id']]) ;
  294. return [
  295. 'x'=>1,
  296. 'msg'=>'获取成功3',
  297. 'waybill'=>$res['data']['tracking_number'],
  298. 'label'=>$res['data']['doc_url'],
  299. ];
  300. }
  301. public function doCreateCBTLabel($info,$shop_info){
  302. //获取快递服务信息
  303. $res = $this->getExpressCompany($info,$shop_info);
  304. if($res['x'] == 0){
  305. return $res;
  306. }
  307. $res1 = $this->createExpressLabel($res['data'],$shop_info);
  308. if($res1['x'] == 0){
  309. return $res1;
  310. }
  311. return $res1;
  312. }
  313. /**
  314. * 获取快递承运商
  315. */
  316. public function getExpressInfo($info,$shop_info){
  317. $res = $this->getExpressCompany($info,$shop_info);
  318. if($res['x'] == 0){
  319. return $res;
  320. }
  321. return $res;
  322. }
  323. }