Kuaidi100.php 9.4 KB

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