Kuaidi100.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\common\library\express;
  13. use app\common\model\Kuaidi;
  14. use think\facade\Cache;
  15. use app\common\library\helper;
  16. /**
  17. * 快递100API模块
  18. * Class Kuaidi100
  19. * @package app\common\library\express
  20. */
  21. class Kuaidi100
  22. {
  23. /* @var array $config 微信支付配置 */
  24. private $config;
  25. /* @var string $error 错误信息 */
  26. private $error;
  27. /**
  28. * 构造方法
  29. * WxPay constructor.
  30. * @param $config
  31. */
  32. public function __construct($config)
  33. {
  34. $this->config = $config;
  35. }
  36. /**
  37. * 执行查询 --查询接口没有订阅接口异步返回的数据全
  38. * @param $code
  39. * @param $expressNo
  40. * @return bool
  41. */
  42. public function query($code, $expressNo)
  43. {
  44. // 缓存索引
  45. $cacheIndex = "express_{$code}_$expressNo";
  46. if ($data = Cache::instance()->get($cacheIndex)) {
  47. return $data;
  48. }
  49. // 参数设置
  50. $postData = [
  51. 'customer' => $this->config['customer'],
  52. 'param' => helper::jsonEncode([
  53. 'resultv2' => '1',
  54. 'com' => $code,
  55. 'num' => $expressNo
  56. ])
  57. ];
  58. $postData['sign'] = strtoupper(md5($postData['param'] . $this->config['key'] . $postData['customer']));
  59. // 请求快递100 api
  60. $url = 'http://poll.kuaidi100.com/poll/query.do';
  61. $result = curl_post($url, http_build_query($postData));
  62. $express = helper::jsonDecode($result);
  63. // 记录错误信息
  64. if (isset($express['returnCode']) || !isset($express['data'])) {
  65. $this->error = isset($express['message']) ? $express['message'] : '查询失败';
  66. return false;
  67. }
  68. // 记录缓存, 时效5分钟
  69. Cache::instance()->set($cacheIndex, $express['data'], 300);
  70. return $express['data'];
  71. }
  72. //获取物流信息,$type=1获取列表 $type=2获取最新消息
  73. /**
  74. * 获取物流信息
  75. * @param string $nu 快递单号
  76. * @param string $delivery_time 订单发货的时间
  77. * @param int $type 1获取物流信息列表 2获取最新一条物流消息
  78. * @return array
  79. * @author: zjwhust
  80. * @Time: 2021/9/29 11:57
  81. */
  82. public function deliverylist(string $nu, string $delivery_time, int $type = 1){
  83. $newList = [];
  84. $order_fahuo['state'] = -1; //添加一条上传物流单号的数据最为最开始的数据--快递100最迟4小时才把物流信息回调给我们
  85. $order_fahuo['status'] = '已发货'; //添加一条上传物流单号的数据最为最开始的数据
  86. $first = [
  87. "time" => $delivery_time,
  88. "context" => "包裹正在等待揽收",
  89. "ftime" => $delivery_time,
  90. "areaCode" => null,
  91. "areaName" => null,
  92. "status" => "已发货",
  93. "state" => -1,
  94. ];
  95. $order_fahuo['data'][] = $first;
  96. $kuaidi = Kuaidi::where('number',$nu)->find();
  97. if(!$kuaidi){
  98. $newList[] = $order_fahuo;
  99. return $type==2 ? $first : $newList;
  100. }
  101. $data = unserialize($kuaidi['json']); //物流信息
  102. $list = $data['lastResult']['data'];
  103. if(!count($list)){
  104. $newList[] = $order_fahuo;
  105. return $type==2 ? $first : $newList;
  106. }
  107. $statusList = self::statusList();
  108. $statusNewList = self::statusNewList();
  109. foreach ($list as $k=>&$v){ //改造物流数据结构
  110. $v['state'] = array_search($v['status'], $statusList);
  111. if($type==2){
  112. return $v;
  113. }
  114. $v['status'] = $statusNewList[$v['state']];
  115. $newList[$v['status']]['state'] = $v['state'];
  116. $newList[$v['status']]['status'] = $v['status'];
  117. $newList[$v['status']]['data'][] = $v;
  118. }
  119. $newList = array_values($newList); //重置为数值键
  120. array_push($newList,$order_fahuo);
  121. return $newList;
  122. }
  123. private static function statusList(){
  124. return [
  125. 0 =>'在途',
  126. 1 =>'揽收',
  127. 2 =>'疑难',
  128. 3 =>'签收',
  129. 4 =>'退签',
  130. 5 =>'派件',
  131. 6 =>'退回',
  132. 7 =>'转单',
  133. 10=>'待清关',
  134. 11=>'清关中',
  135. 12=>'已清关',
  136. 13=>'清关异常',
  137. 14=>'拒签',
  138. ];
  139. }
  140. //名厨优选新版列表
  141. private static function statusNewList(){
  142. return [
  143. 0 =>'运输中',
  144. 1 =>'已揽件',
  145. 2 =>'疑难件',
  146. 3 =>'已签收',
  147. 4 =>'已退签',
  148. 5 =>'派送中',
  149. 6 =>'已退回',
  150. 7 =>'转单',
  151. 10=>'待清关',
  152. 11=>'清关中',
  153. 12=>'已清关',
  154. 13=>'清关异常',
  155. 14=>'已拒签',
  156. ];
  157. }
  158. /**
  159. * 订阅接口
  160. * @param $number
  161. * @param int $type 1平台发货 2用户退货 3拆分批量发货 4实物卡兑换发货
  162. * @return bool|mixed|string
  163. * @author: zjwhust
  164. * @Time: 2021/9/29 13:55
  165. */
  166. public function subscribe($number,$type=1,$company){
  167. $callbackurl = env('APP_URL').'/api/kuaidi/callback?type='.$type;
  168. $param = [
  169. 'company' => $company??'', //快递公司编码
  170. 'number' => $number??'', //快递单号
  171. 'from' => '', //出发地城市
  172. 'to' => '', //目的地城市
  173. 'key' => $this->config['key'],//客户授权key
  174. 'parameters' => [
  175. 'callbackurl' => $callbackurl, //回调地址
  176. 'salt' => 'zhongao_mcyx',//加密串
  177. 'resultv2' => '1', //行政区域解析
  178. 'autoCom' => '1', //单号智能识别,1表示开始智能判断单号所属公司的功能,开启后,company字段可为空,即只传运单号(number字段),
  179. 'interCom' => '0', //开启国际版
  180. // 'departureCountry' => '', //出发国
  181. // 'departureCom' => '', //出发国快递公司编码
  182. // 'destinationCountry' => '', //目的国
  183. // 'destinationCom' => '', //目的国快递公司编码
  184. // 'phone' => '' //手机号
  185. ]
  186. ];
  187. //请求参数
  188. $post_data = array();
  189. $post_data["schema"] = 'json';
  190. $post_data["param"] = json_encode($param);
  191. $url = 'http://poll.kuaidi100.com/poll'; //订阅请求地址
  192. $params = "";
  193. foreach ($post_data as $k=>$v) {
  194. $params .= "$k=".urlencode($v)."&"; //默认UTF-8编码格式
  195. }
  196. $post_data = substr($params, 0, -1);
  197. // echo '请求参数<br/>'.$post_data;
  198. $data = self::curl_request($url,$post_data,['Content-Type: application/x-www-form-urlencoded']);
  199. $data = json_decode($data);
  200. return $data;
  201. }
  202. /**
  203. * 返回错误信息
  204. * @return string
  205. */
  206. public function getError()
  207. {
  208. return $this->error;
  209. }
  210. /**
  211. * curl post
  212. * @param $url
  213. * @param string $post
  214. * @param array $headers
  215. * @return bool|string
  216. * @author: zjwhust
  217. * @Time: 2021/9/28 18:22
  218. */
  219. public static function curl_request($url,$post='',$headers= array('Content-Type: text/plain')){
  220. $curl = curl_init();
  221. curl_setopt($curl, CURLOPT_URL, $url);
  222. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  223. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  224. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  225. //设置https
  226. curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
  227. curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, false);
  228. if($post) {
  229. curl_setopt($curl, CURLOPT_POST, 1);
  230. curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  231. }
  232. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // 自定义 Headers
  233. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  234. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  235. $data = curl_exec($curl);
  236. if (curl_errno($curl)) {
  237. return curl_error($curl);
  238. }
  239. curl_close($curl);
  240. return $data;
  241. }
  242. }