541469799@qq.com 1 年之前
父节点
当前提交
5bed153f15
共有 4 个文件被更改,包括 150 次插入0 次删除
  1. 103 0
      app/common/library/express/Usps.php
  2. 15 0
      app/common/model/Express.php
  3. 26 0
      app/index/controller/Order.php
  4. 6 0
      config/usps.php

+ 103 - 0
app/common/library/express/Usps.php

@@ -0,0 +1,103 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\common\library\express;
+
+use think\facade\Cache;
+use cores\traits\ErrorTrait;
+use app\common\library\helper;
+
+/**
+ * 快递USPS模块
+ * Class Usps
+ * @package app\common\library\express
+ */
+class Usps
+{
+    use ErrorTrait;
+
+    // 物流跟踪查询API地址
+    const QUERY_URL = 'http://production.shippingapis.com/ShippingAPI.dll';
+
+    // 查询轨迹账号
+    /* @var string $userId */
+    private string $userId;
+
+    /**
+     * 构造方法
+     * @param $userId
+     */
+    public function __construct($userId)
+    {
+        $this->userId = $userId;
+    }
+
+    /**
+     * 执行查询
+     * @param string $expressNo
+     * @return string|bool
+     */
+    public function query(string $expressNo)
+    {
+        // 缓存索引
+        $cacheIndex = 'express_usps_'. $expressNo;
+        if ($cacheData = Cache::get($cacheIndex)) {
+            return $cacheData;
+        }
+        // 参数设置
+        $postDataXML = '<?xml version="1.0" encoding="UTF-8"?><TrackRequest USERID="' . $this->userId . '"><TrackID ID="' . $expressNo . '"></TrackID></TrackRequest>';
+        // 请求快递100 api
+        $result = $this->curlPost(self::QUERY_URL, $postDataXML);
+
+        if(array_key_exists('HTTP_RAW_POST_DATA', $GLOBALS))
+        {
+            $post_data = $GLOBALS['HTTP_RAW_POST_DATA'];//php接收xml文件的唯一方式
+            $post_obj = SimpleXml_load_String($post_data,'SimpleXMLElement',LIBXML_NOCDATA);//加载xml
+            //$username = (string)$post_obj->username;//直接获取xml中username的值
+            //todo
+        }else{
+            return false;
+        }
+
+        $express = helper::jsonDecode($result);
+        // 记录错误信息
+        if (isset($express['returnCode']) || !isset($express['data'])) {
+            $this->error = $express['message'] ?? '查询失败';
+            return false;
+        }
+        // 记录缓存, 时效30分钟
+        Cache::set($cacheIndex, $express['data'], 3000);
+        return $express['data'];
+    }
+
+    /**
+     * curl请求指定url (post)
+     * @param $url
+     * @param string $data
+     * @return bool|string
+     */
+    private function curlPost($url, string $data = '')
+    {
+        $header[] = "Content-type: text/xml";//设置http报文头text/xml
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_POST, 1);//1:post方式 0:get方式
+        curl_setopt($ch, CURLOPT_HEADER, 0);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+        $result = curl_exec($ch);
+        curl_close($ch);
+        return $result;
+    }
+}

+ 15 - 0
app/common/model/Express.php

@@ -12,10 +12,12 @@ declare (strict_types = 1);
 
 namespace app\common\model;
 
+use app\common\library\express\Usps;
 use cores\BaseModel;
 use app\common\library\express\Kuaidi100;
 use app\common\model\store\Setting as SettingModel;
 use app\common\enum\Setting as SettingEnum;
+use think\facade\Log;
 
 /**
  * 物流公司模型
@@ -115,6 +117,19 @@ class Express extends BaseModel
         return $data;
     }
 
+    public function dynamicUsps(string $uspsUserId, string $expressNo)
+    {
+        // 实例化快递100类
+        $Usps = new Usps($uspsUserId);
+        // 请求查询接口
+        $data['list'] = $Usps->query($expressNo);
+        if ($data['list'] === false) {
+            Log::error('有问题');
+            return false;
+        }
+        return $data;
+    }
+
     /**
      * 获取快递100配置项
      * @return mixed

+ 26 - 0
app/index/controller/Order.php

@@ -100,6 +100,32 @@ class Order extends Controller
     }
 
     /**
+     * 获取物流信息
+     * @param int $orderId 订单ID
+     * @return Json
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws BaseException
+     */
+    public function expressUsps(int $orderId): Json
+    {
+        // 订单信息
+        $order = OrderModel::getDetail($orderId);
+        if (!$order['express_no']) {
+            return $this->renderError('没有物流信息');
+        }
+        $expressUserId  =config('usps.exp_user_id');
+        // 获取物流信息
+        $model = new ExpressModel();
+        $express = $model->dynamicUsps($expressUserId, $model['kuaidi100_code'], $order['express_no']);
+        if ($express === false) {
+            return $this->renderError($model->getError());
+        }
+        return $this->renderSuccess(compact('express'));
+    }
+
+    /**
      * 确认收货
      * @param int $orderId
      * @return Json

+ 6 - 0
config/usps.php

@@ -0,0 +1,6 @@
+<?php
+
+return [
+    // 请求成功
+    'user_id' => env('usps.exp_user_id', ''),
+];