Model_dpdxw.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. class Model_Dpdxw extends Lin_Model {
  3. function __construct(){
  4. parent::__construct();
  5. }
  6. public function get_data($data)
  7. {
  8. if($data['printcode'] == 'DPDXW')
  9. {
  10. $PtId = 'ESBRM';
  11. }
  12. else if($data['printcode'] == 'FTFEDEXXW')
  13. {
  14. $PtId = 'FEDEX-EU';
  15. }
  16. else
  17. {
  18. $PtId = 'ESBRMW-P';
  19. }
  20. $ItemId = time().rand(10,99);
  21. $url = "http://exorderwebapi.flytcloud.com/api/OrderSyn/ErpUploadOrder";
  22. $UAccount = '40637';
  23. $Password = strtoupper(md5('192450'));
  24. $Token = '39F682A9-FA83-E26B-B51B-D85B18F94E90';
  25. //创建并预报订单
  26. $json = array(
  27. 'Token' => $Token,
  28. 'UAccount'=>$UAccount,
  29. 'Password'=>$Password,
  30. 'OrderList' => array(
  31. 0 => array(
  32. 'Address1' => $data['address'],
  33. 'Address2' => $data['address2'],
  34. 'ApiOrderId' => $data['number'],
  35. 'City' => $data['city'],
  36. 'CiId' => $data['lb'],
  37. 'County' => $data['province'],
  38. 'PackType' => 3,//包装类型(1:信封,2:文件,3:包裹)(默认包裹)
  39. 'Phone'=>$data['phone'],
  40. 'PtId' => $PtId,//邮政方式简码
  41. 'ReceiverName' => $data['name'],
  42. 'Remark'=> $data['number'],
  43. 'SalesPlatformFlag' => 0,
  44. 'SyncPlatformFlag' => 'flyt.logistics.longying',//第三平台标识
  45. 'Zip' => trim($data['zipcode'],' '),
  46. 'OrderDetailList' => array(0=>array(
  47. 'ItemName'=>$data['sbpm'],
  48. 'OriginalPlatformOrderId'=>$data['orderinfo'],
  49. 'Price'=>$data['dtsbjz'],
  50. 'Quantities'=>$data['ts']
  51. )
  52. ),
  53. 'HaikwanDetialList' => array(0=>array(
  54. 'HwCode'=>'67041100',
  55. 'ItemCnName'=>$data['zwpm'],
  56. 'ItemEnName'=>$data['sbpm'],
  57. 'ProducingArea'=>'CN',
  58. 'Quantities'=>$data['ts'],
  59. 'UnitPrice'=>$data['dtsbjz'],
  60. 'Weight'=>$data['zzl'],
  61. 'CCode'=>'USD',
  62. )
  63. )
  64. )
  65. )
  66. );
  67. $json = json_encode($json,true);
  68. $ch = curl_init();
  69. curl_setopt($ch,CURLOPT_URL,$url);
  70. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  71. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  72. 'Accept-Language: zh-cn',
  73. 'Connection: Keep-Alive',
  74. 'Cache-Control: no-cache',
  75. 'Content-type: application/json;charset=UTF-8'));
  76. curl_setopt($ch,CURLOPT_POST, 1);
  77. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,600);
  78. curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
  79. $res = curl_exec($ch);
  80. curl_close($ch);
  81. $res = json_decode($res,true);
  82. if(!isset($res['ErpSuccessOrders'][0]['TraceId']) && !isset($res['ErpSuccessOrders'][0]['OrderId']))
  83. {
  84. $g = array('x'=>'0','Description'=>$res['Remark'].':'.$res['ErpFailOrders'][0]['Remark'],'res'=>json_encode(array($res),true));
  85. }
  86. else
  87. {
  88. for($i=0;$i<86;$i++)
  89. {
  90. sleep(1);
  91. $yd = $this->get_yd($res['ErpSuccessOrders'][0]['OrderId']);
  92. if(stripos($yd[1],'跟踪号或配送跟踪号为空') === false)
  93. {
  94. break;
  95. }
  96. if($yd[0] == '1')
  97. {
  98. break;
  99. }
  100. }
  101. if($yd[0] != '2')
  102. {
  103. $g = array('x'=>'1','waybill'=>$res['ErpSuccessOrders'][0]['TraceId'],'yd'=>$yd[1],'res'=>json_encode(array($res,$yd),true));
  104. }
  105. else
  106. {
  107. $g = array('x'=>'0','Description'=>$yd[1],'res'=>json_encode(array($res,$yd),true));
  108. }
  109. }
  110. return $g;
  111. }
  112. public function get_token()
  113. {
  114. $url = "http://exapi.flytcloud.com/api/auth/Authorization/GetAccessToken";
  115. $grant_type = 'password';
  116. $username = 'longying';
  117. $password = md5('ljygt&0IP!');
  118. //创建并预报订单
  119. $json = array(
  120. 'grant_type' => $grant_type,
  121. 'username' => $username,
  122. 'password' => $password,
  123. );
  124. $json = json_encode($json,true);
  125. $ch = curl_init();
  126. curl_setopt($ch,CURLOPT_URL,$url);
  127. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  128. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  129. 'Accept-Language: zh-cn',
  130. 'Connection: Keep-Alive',
  131. 'Cache-Control: no-cache',
  132. 'Content-type: application/json;charset=UTF-8'));
  133. curl_setopt($ch,CURLOPT_POST, 1);
  134. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,600);
  135. curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
  136. $res = curl_exec($ch);
  137. curl_close($ch);
  138. $res = json_decode($res,true);
  139. return $res;
  140. }
  141. public function get_yd($data)
  142. {
  143. $path = $_SERVER["DOCUMENT_ROOT"] . '/data/dpd_access_token.txt';
  144. $access_token = '';
  145. if(file_exists($path))
  146. {
  147. $file_read = fopen($path, 'r');
  148. $access_str = fgets($file_read);
  149. fclose($file_read);
  150. $access_arr = explode(':', $access_str);
  151. if (time() < intval($access_arr[0]))
  152. {
  153. $access_token = isset($access_arr[1]) ? $access_arr[1] : '';
  154. }
  155. }
  156. if(!$access_token)
  157. {
  158. $file = fopen($path, 'w');
  159. $info = $this->get_token();
  160. if(!$info['expires_in'])
  161. {
  162. return array(2,'令牌获取错误,请重试!');exit;
  163. }
  164. $access_new = (time() + $info['expires_in']) . ':' . $info['access_token'];
  165. fwrite($file, $access_new);
  166. fclose($file);
  167. $access_token = $info['access_token'];
  168. }
  169. $url = "http://exapi.flytcloud.com/api/label/LabelProvider/GetLabelBatchExt";
  170. $Token = $access_token;
  171. //创建并预报订单
  172. $json = array(
  173. 'OrderIdlst' => array($data),
  174. 'Format' => '0'
  175. );
  176. $json = json_encode($json,true);
  177. $ch = curl_init();
  178. curl_setopt($ch,CURLOPT_URL,$url);
  179. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  180. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  181. 'token:'.$Token,
  182. 'Content-type: application/json;charset=UTF-8'));
  183. curl_setopt($ch,CURLOPT_POST, 1);
  184. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,600);
  185. curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
  186. $res = curl_exec($ch);
  187. curl_close($ch);
  188. $res = json_decode($res,true);
  189. if(isset($res['Data']['Label']))
  190. {
  191. return array(1,$res['Data']['Label']);exit;
  192. }
  193. else
  194. {
  195. return array(2,$res['ErrMsg']);exit;
  196. }
  197. }
  198. public function get_logistics($waybill)
  199. {
  200. $url = "http://tracking.flytcloud.com/Trackingod/TrackOrder";
  201. $Token = '119904B54744499F9C7A29A84C2C7051';
  202. //创建并预报订单
  203. $json = array(
  204. 'key' => $Token,
  205. 'tracking_number' => $waybill,
  206. );
  207. $json = json_encode($json,true);
  208. $ch = curl_init();
  209. curl_setopt($ch,CURLOPT_URL,$url);
  210. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  211. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  212. 'Accept-Language: zh-cn',
  213. 'Connection: Keep-Alive',
  214. 'Cache-Control: no-cache',
  215. 'Content-type: application/json;charset=UTF-8'));
  216. curl_setopt($ch,CURLOPT_POST, 1);
  217. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,600);
  218. curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
  219. $res = curl_exec($ch);
  220. curl_close($ch);
  221. $res = json_decode($res,true);
  222. $list = '';
  223. if(isset($res['dat']['0']['track']['z1'][0]['trackingStatus']))
  224. {
  225. if($res['dat']['0']['track']['e'] > '0')
  226. {
  227. $zt = $res['dat']['0']['track']['e'];
  228. if($zt >= '2' && $zt< '10')
  229. {
  230. $exstate = 1;
  231. }
  232. else if($zt >= '10' && $zt < '30')
  233. {
  234. $exstate = 2;
  235. }
  236. else if($zt >= '30' && $zt < '40')
  237. {
  238. $exstate = 3;
  239. }
  240. else if($zt == '40' || $zt == '41')
  241. {
  242. $exstate = 4;
  243. }
  244. else
  245. {
  246. $exstate = 0;
  247. }
  248. if($se)
  249. {
  250. $list = array('f'=>2);
  251. }
  252. else
  253. {
  254. foreach($res['dat']['0']['track']['z1'] as $v)
  255. {
  256. $xq .= date('m-d H:i',strtotime($v['a'])).' '.$v['z'].'<br />';
  257. }
  258. $content = date('Y-m-d H',time()).'-查询成功';
  259. $list = array('exstate'=>$exstate,'content'=>$content,'f'=>1,'data'=>$xq);//正常可发送站内信
  260. }
  261. }
  262. else
  263. {
  264. $list = '';
  265. }
  266. }
  267. return $res;
  268. }
  269. } //end class