Model_express_tt.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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($res['code'] != 0){
  211. return [
  212. 'x'=>0,
  213. 'Description'=>$res['message'],
  214. ];
  215. }
  216. $extra_text = json_decode($info['extra_text'],true);
  217. $extra_text['server_info'] = $res['data'];
  218. $info['extra_text'] = json_encode($extra_text,JSON_UNESCAPED_UNICODE);
  219. $this->db->update('fullordertt', [
  220. 'extra_text' => $info['extra_text']
  221. ], ['id' => $info['id']]) ;
  222. return [
  223. 'x'=>1,
  224. 'msg'=>'获取成功1',
  225. 'data'=>$info
  226. ];
  227. }
  228. //创建发货单和运单标签
  229. private function createExpressLabel($info,$shop_info){
  230. if(is_string($info['extra_text'])){
  231. $extra_text = json_decode($info['extra_text'],true);
  232. }else{
  233. $extra_text = $info['extra_text'];
  234. }
  235. if(isset($extra_text['label_info'])){
  236. return [
  237. 'x'=>1,
  238. 'msg'=>'获取成功2',
  239. 'data'=>$info
  240. ];
  241. }
  242. $res = $this->apitt->createLabel($info,$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['label_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'=>'获取成功2',
  258. 'data'=>$info
  259. ];
  260. }
  261. //下载快递面单
  262. private function downloadExpressLabel($info,$shop){
  263. if(is_string($info['extra_text'])){
  264. $extra_text = json_decode($info['extra_text'],true);
  265. }else{
  266. $extra_text = $info['extra_text'];
  267. }
  268. $packageid = $extra_text['label_info']['package_id'];
  269. $res = $this->apitt->downloadLabel($packageid,$shop,$info);
  270. if($res['code'] != 0){
  271. return [
  272. 'x'=>0,
  273. 'Description'=>$res['message'],
  274. ];
  275. }
  276. $extra_text = json_decode($info['extra_text'],true);
  277. $extra_text['express_info'] = $res['data'];
  278. $info['extra_text'] = json_encode($extra_text,JSON_UNESCAPED_UNICODE);
  279. $this->db->update('fullordertt', [
  280. 'extra_text' => $info['extra_text']
  281. ], ['id' => $info['id']]) ;
  282. return [
  283. 'x'=>1,
  284. 'msg'=>'获取成功3',
  285. 'waybill'=>$res['data']['tracking_number'],
  286. 'label'=>$res['data']['doc_url'],
  287. ];
  288. }
  289. public function doCreateCBTLabel($info,$shop_info){
  290. //获取快递服务信息
  291. $res = $this->getExpressCompany($info,$shop_info);
  292. if($res['x'] == 0){
  293. return $res;
  294. }
  295. $res1 = $this->createExpressLabel($res['data'],$shop_info);
  296. if($res1['x'] == 0){
  297. return $res1;
  298. }
  299. return $res1;
  300. }
  301. }