Kuaidi100.php 9.1 KB

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