Model_express_tt.php 9.9 KB

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