Model_express_tt.php 9.2 KB

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