Alicloud.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils\Express\Trackers;
  8. use Curl\Curl;
  9. use Cyy\Express\Exceptions\TrackingException;
  10. use Cyy\Express\Waybill;
  11. use Cyy\Express\Status;
  12. use Cyy\Express\Trackers\BaseTracker;
  13. use Cyy\Express\Trackers\TrackerInterface;
  14. use Cyy\Express\Trackers\TrackerTrait;
  15. class Alicloud extends BaseTracker implements TrackerInterface
  16. {
  17. use TrackerTrait;
  18. public $AppCode;
  19. public $receive_mobile;
  20. public static function getSupportedExpresses()
  21. {
  22. $arr = [
  23. '京东' => 'JD',
  24. '顺丰' => 'SF',
  25. '顺丰速运' => 'SF',
  26. '申通' => 'STO',
  27. '韵达' => 'YD',
  28. '圆通' => 'YTO',
  29. '中通(ZTO)' => 'ZTO',
  30. '中通' => 'ZTO',
  31. '百世' => 'HTKY',
  32. 'EMS' => 'EMS',
  33. '天天' => 'HHTT',
  34. '邮政' => 'YZPY',
  35. '宅急送' => 'ZJS',
  36. '国通' => 'GTO',
  37. '全峰' => 'QFKD',
  38. '优速' => 'UC',
  39. '中铁' => 'ZTKY',
  40. '中铁' => 'ZTWL',
  41. '亚马逊' => 'AMAZON',
  42. '城际' => 'CJKD',
  43. '德邦' => 'DBL',
  44. '汇丰' => 'HFWL',
  45. // '百世' => 'BTWL',
  46. '安捷' => 'AJ',
  47. '安能' => 'ANE',
  48. '安信达' => 'AXD',
  49. '北青小红帽' => 'BQXHM',
  50. '百福东方' => 'BFDF',
  51. 'CCES' => 'CCES',
  52. '城市100' => 'CITY100',
  53. 'COE东方' => 'COE',
  54. '长沙创一' => 'CSCY',
  55. '成都善途' => 'CDSTKY',
  56. 'D速' => 'DSWL',
  57. '大田' => 'DTWL',
  58. '快捷' => 'FAST',
  59. '联邦' => 'FEDEX',
  60. 'FEDEX' => 'FEDEX_GJ',
  61. '飞康达' => 'FKD',
  62. '广东邮政' => 'GDEMS',
  63. '共速达' => 'GSD',
  64. '高铁' => 'GTSD',
  65. '恒路' => 'HLWL',
  66. '天地华宇' => 'HOAU',
  67. '华强' => 'hq568',
  68. '华夏龙' => 'HXLWL',
  69. '好来运' => 'HYLSD',
  70. '京广' => 'JGSD',
  71. '九曳供应链' => 'JIUYE',
  72. '佳吉' => 'JJKY',
  73. '嘉里' => 'JLDT',
  74. '捷特' => 'JTKD',
  75. '急先达' => 'JXD',
  76. '晋越' => 'JYKD',
  77. '加运美' => 'JYM',
  78. '佳怡' => 'JYWL',
  79. '跨越' => 'KYWL',
  80. '龙邦' => 'LB',
  81. '联昊通' => 'LHT',
  82. '民航' => 'MHKD',
  83. '明亮' => 'MLWL',
  84. '能达' => 'NEDA',
  85. '平安达腾飞' => 'PADTF',
  86. '全晨' => 'QCKD',
  87. '全日通' => 'QRT',
  88. '如风达' => 'RFD',
  89. '赛澳递' => 'SAD',
  90. '圣安' => 'SAWL',
  91. '盛邦' => 'SBWL',
  92. '上大' => 'SDWL',
  93. '盛丰' => 'SFWL',
  94. '盛辉' => 'SHWL',
  95. '速通' => 'ST',
  96. '速腾' => 'STWL',
  97. '速尔' => 'SURE',
  98. '唐山申通' => 'TSSTO',
  99. '全一' => 'UAPEX',
  100. '万家' => 'WJWL',
  101. '万象' => 'WXWL',
  102. '新邦' => 'XBWL',
  103. '信丰' => 'XFEX',
  104. '希优特' => 'XYT',
  105. '新杰' => 'XJ',
  106. '源安达' => 'YADEX',
  107. '远成' => 'YCWL',
  108. '义达' => 'YDH',
  109. '越丰' => 'YFEX',
  110. '原飞航' => 'YFHEX',
  111. '亚风' => 'YFSD',
  112. '运通' => 'YTKD',
  113. '亿翔' => 'YXKD',
  114. '增益' => 'ZENY',
  115. '汇强' => 'ZHQKD',
  116. '众通' => 'ZTE',
  117. '中邮' => 'ZYWL',
  118. '速必达' => 'SUBIDA',
  119. '瑞丰' => 'RFEX',
  120. '快客' => 'QUICK',
  121. 'CNPEX中邮' => 'CNPEX',
  122. '鸿桥供应链' => 'HOTSCM',
  123. '海派通' => 'HPTEX',
  124. '澳邮专线' => 'AYCA',
  125. '泛捷' => 'PANEX',
  126. 'PCA Express' => 'PCA',
  127. 'UEQ Express' => 'UEQ',
  128. '程光' => 'CG',
  129. '富腾达' => 'FTD',
  130. '中通快运' => 'ZTOKY',
  131. '品骏快递' => 'PJ',
  132. '极兔' => 'JITU',
  133. '壹米滴答' => 'YIMIDIDA',
  134. '顺心捷达' => 'SXJD'
  135. ];
  136. $res = [];
  137. foreach ($arr as $k => $v) {
  138. $res[$k] = $v;
  139. $res[$k . '快递'] = $v;
  140. $res[$k . '快运'] = $v;
  141. $res[$k . '速递'] = $v;
  142. $res[$k . '速运'] = $v;
  143. }
  144. return $res;
  145. }
  146. public function track(Waybill $waybill)
  147. {
  148. $ShipperCode = static::getExpressCode($waybill->express);
  149. // $curl = new Curl();
  150. // $requestData = [
  151. // 'ShipperCode' => $ShipperCode,
  152. // 'LogisticCode' => $waybill->id,
  153. // 'OrderCode' => $waybill->orderId,
  154. // ];
  155. if(in_array($ShipperCode, ['SF', 'ZTO'])){
  156. $waybill->id .= ':'.substr(trim($this->receive_mobile), -4);
  157. }
  158. $res = $this->get($waybill->id, $ShipperCode);
  159. $response = json_decode($res);
  160. if($response === null){
  161. throw new TrackingException($res);
  162. }
  163. if ($response->Success == false) {
  164. throw new TrackingException($response->Reason, $response);
  165. }
  166. $statusMap = [
  167. -1 => 10,
  168. 0 => 9,
  169. 1 => 0,
  170. 2 => 2,
  171. 3 => 4,
  172. 4 => 6,
  173. 5 => 6,
  174. 6 => 8,
  175. ];
  176. $waybill->status = $statusMap[intval($response->State)];
  177. foreach ($response->Traces as $trace) {
  178. $remark = isset($trace->Remark) ? $trace->Remark : '';
  179. $waybill->traces->append($trace->AcceptTime, $trace->AcceptStation, $remark);
  180. }
  181. }
  182. private function get($n = '', $t = ''){
  183. $host = "https://wdexpress.market.alicloudapi.com";
  184. $path = "/gxali";
  185. $method = "GET";
  186. $appcode = $this->AppCode;//开通服务后 买家中心-查看AppCode
  187. $headers = array();
  188. array_push($headers, "Authorization:APPCODE " . $appcode);
  189. $querys = "n=780098068058&t=ZTO";
  190. $querys = "n={$n}&t={$t}";
  191. $bodys = "";
  192. $url = $host . $path . "?" . $querys;
  193. $curl = curl_init();
  194. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  195. curl_setopt($curl, CURLOPT_URL, $url);
  196. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  197. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  198. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  199. curl_setopt($curl, CURLOPT_HEADER, true);
  200. if (1 == strpos("$" . $host, "https://")) {
  201. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  202. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  203. }
  204. $out_put = curl_exec($curl);
  205. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  206. list($header, $body) = explode("\r\n\r\n", $out_put, 2);
  207. if ($httpCode == 200) {
  208. // print("正常请求计费(其他均不计费)<br>");
  209. return ($body);
  210. } else {
  211. ob_start();
  212. if ($httpCode == 400 && strpos($header, "Invalid Param Location") !== false) {
  213. print("参数错误");
  214. } elseif ($httpCode == 400 && strpos($header, "Invalid AppCode") !== false) {
  215. print("AppCode错误");
  216. } elseif ($httpCode == 400 && strpos($header, "Invalid Url") !== false) {
  217. print("请求的 Method、Path 或者环境错误");
  218. } elseif ($httpCode == 403 && strpos($header, "Unauthorized") !== false) {
  219. print("服务未被授权(或URL和Path不正确)");
  220. } elseif ($httpCode == 403 && strpos($header, "Quota Exhausted") !== false) {
  221. print("套餐包次数用完");
  222. } elseif ($httpCode == 403 && strpos($header, "Api Market Subscription quota exhausted") !== false) {
  223. print("套餐包次数用完,请续购套餐");
  224. } elseif ($httpCode == 500) {
  225. print("API网关错误");
  226. } elseif ($httpCode == 0) {
  227. print("URL错误");
  228. } else {
  229. print("参数名错误 或 其他错误");
  230. print($httpCode);
  231. $headers = explode("\r\n", $header);
  232. $headList = array();
  233. foreach ($headers as $head) {
  234. $value = explode(':', $head);
  235. $headList[$value[0]] = $value[1];
  236. }
  237. print($headList['x-ca-error-message']);
  238. }
  239. $err = ob_get_contents();
  240. ob_end_clean();
  241. return $err;
  242. }
  243. }
  244. }