// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\common\library\express; use app\common\model\Kuaidi; use think\facade\Cache; use app\common\library\helper; use think\facade\Log; /** * 快递100API模块 * Class Kuaidi100 * @package app\common\library\express */ class Kuaidi100 { /* @var array $config 微信支付配置 */ private $config; /* @var string $error 错误信息 */ private $error; /** * 构造方法 * WxPay constructor. * @param $config */ public function __construct($config) { $this->config = $config; } /** * 执行查询 --查询接口没有订阅接口异步返回的数据全 * @param $code * @param $expressNo * @return bool */ public function query($code, $expressNo) { // 缓存索引 $cacheIndex = "express_{$code}_$expressNo"; if ($data = Cache::instance()->get($cacheIndex)) { return $data; } // 参数设置 $postData = [ 'customer' => $this->config['customer'], 'param' => helper::jsonEncode([ 'resultv2' => '1', 'com' => $code, 'num' => $expressNo ]) ]; $postData['sign'] = strtoupper(md5($postData['param'] . $this->config['key'] . $postData['customer'])); // 请求快递100 api $url = 'http://poll.kuaidi100.com/poll/query.do'; $result = curl_post($url, http_build_query($postData)); $express = helper::jsonDecode($result); Log::info('query:'.$expressNo.':'.json_encode($express)); // 记录错误信息 if (isset($express['returnCode']) || !isset($express['data'])) { $this->error = isset($express['message']) ? $express['message'] : '查询失败'; return false; } // 记录缓存, 时效5分钟 Cache::instance()->set($cacheIndex, $express['data'], 300); return $express['data']; } //获取物流信息,$type=1获取列表 $type=2获取最新消息 /** * 获取物流信息 * @param string $nu 快递单号 * @param string $delivery_time 订单发货的时间 * @param int $type 1获取物流信息列表 2获取最新一条物流消息 * @return array * @author: zjwhust * @Time: 2021/9/29 11:57 */ public function deliverylist(string $nu, string $delivery_time, int $type = 1){ $newList = []; $order_fahuo['state'] = -1; //添加一条上传物流单号的数据最为最开始的数据--快递100最迟4小时才把物流信息回调给我们 $order_fahuo['status'] = '已发货'; //添加一条上传物流单号的数据最为最开始的数据 $first = [ "time" => $delivery_time, "context" => "包裹正在等待揽收", "ftime" => $delivery_time, "areaCode" => null, "areaName" => null, "status" => "已发货", "state" => -1, ]; $order_fahuo['data'][] = $first; $kuaidi = Kuaidi::where('number',$nu)->find(); if(!$kuaidi){ $newList[] = $order_fahuo; return $type==2 ? $first : $newList; } $data = unserialize($kuaidi['json']); //物流信息 $list = $data['lastResult']['data']; if(!count($list)){ $newList[] = $order_fahuo; return $type==2 ? $first : $newList; } $statusList = self::statusList(); $statusNewList = self::statusNewList(); foreach ($list as $k=>&$v){ //改造物流数据结构 $v['state'] = array_search($v['status'], $statusList); if($type==2){ return $v; } $v['status'] = $statusNewList[$v['state']]; $newList[$v['status']]['state'] = $v['state']; $newList[$v['status']]['status'] = $v['status']; $newList[$v['status']]['data'][] = $v; } $newList = array_values($newList); //重置为数值键 array_push($newList,$order_fahuo); return $newList; } private static function statusList(){ return [ 0 =>'在途', 1 =>'揽收', 2 =>'疑难', 3 =>'签收', 4 =>'退签', 5 =>'派件', 6 =>'退回', 7 =>'转单', 10=>'待清关', 11=>'清关中', 12=>'已清关', 13=>'清关异常', 14=>'拒签', ]; } //名厨优选新版列表 private static function statusNewList(){ return [ 0 =>'运输中', 1 =>'已揽件', 2 =>'疑难件', 3 =>'已签收', 4 =>'已退签', 5 =>'派送中', 6 =>'已退回', 7 =>'转单', 10=>'待清关', 11=>'清关中', 12=>'已清关', 13=>'清关异常', 14=>'已拒签', ]; } /** * 订阅接口 * @param $number * @param int $type 1平台发货 2用户退货 3拆分批量发货 4实物卡兑换发货 * @return bool|mixed|string * @author: zjwhust * @Time: 2021/9/29 13:55 */ public function subscribe($number,$type=1,$company){ $callbackurl = env('APP_URL').'/api/kuaidi/callback?type='.$type; $param = [ 'company' => $company??'', //快递公司编码 'number' => $number??'', //快递单号 'from' => '', //出发地城市 'to' => '', //目的地城市 'key' => $this->config['key'],//客户授权key 'parameters' => [ 'callbackurl' => $callbackurl, //回调地址 'salt' => 'zhongao_mcyx',//加密串 'resultv2' => '1', //行政区域解析 'autoCom' => '1', //单号智能识别,1表示开始智能判断单号所属公司的功能,开启后,company字段可为空,即只传运单号(number字段), 'interCom' => '0', //开启国际版 // 'departureCountry' => '', //出发国 // 'departureCom' => '', //出发国快递公司编码 // 'destinationCountry' => '', //目的国 // 'destinationCom' => '', //目的国快递公司编码 // 'phone' => '' //手机号 ] ]; //请求参数 $post_data = array(); $post_data["schema"] = 'json'; $post_data["param"] = json_encode($param); $url = 'http://poll.kuaidi100.com/poll'; //订阅请求地址 $params = ""; foreach ($post_data as $k=>$v) { $params .= "$k=".urlencode($v)."&"; //默认UTF-8编码格式 } $post_data = substr($params, 0, -1); // echo '请求参数
'.$post_data; $data = self::curl_request($url,$post_data,['Content-Type: application/x-www-form-urlencoded']); $data = json_decode($data); return $data; } /** * 返回错误信息 * @return string */ public function getError() { return $this->error; } /** * curl post * @param $url * @param string $post * @param array $headers * @return bool|string * @author: zjwhust * @Time: 2021/9/28 18:22 */ public static function curl_request($url,$post='',$headers= array('Content-Type: text/plain')){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)'); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_AUTOREFERER, 1); //设置https curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, false); if($post) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $post); } curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // 自定义 Headers curl_setopt($curl, CURLOPT_TIMEOUT, 10); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($curl); if (curl_errno($curl)) { return curl_error($curl); } curl_close($curl); return $data; } }