Model_express_tt.php 9.9 KB

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