Model_express_tt.php 9.3 KB

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