WIN-2308041133\Administrator 1 week ago
parent
commit
28704c58c0
3 changed files with 143 additions and 0 deletions
  1. 99 0
      app/services/AliyunExpressService.php
  2. 42 0
      app/system/controller/Shop.php
  3. 2 0
      app/system/route/shop.php

+ 99 - 0
app/services/AliyunExpressService.php

@@ -0,0 +1,99 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\services;
+
+use app\model\api\Sys;
+
+/**
+ * 阿里云物流查询服务
+ */
+class AliyunExpressService
+{
+    /**
+     * 阿里云物流查询API
+     * @var string
+     */
+    protected $api = 'https://wuliu.market.alicloudapi.com/kdi';
+
+    /**
+     * AppCode
+     * @var string
+     */
+    protected $appCode;
+
+    /**
+     * 构造方法
+     */
+    public function __construct()
+    {
+        $sys = Sys::find(1);
+        $this->appCode = $sys ? (string)($sys->system_express_app_code ?? '') : '';
+    }
+
+    /**
+     * 查询物流信息
+     * @param string $no   快递单号
+     * @param string $type 快递公司编码(可选,阿里云接口可自动识别)
+     * @return array|false
+     */
+    public function query(string $no, string $type = '')
+    {
+        if (empty($this->appCode)) {
+            return false;
+        }
+
+        $params = ['no' => $no];
+        if (!empty($type)) {
+            $params['type'] = $type;
+        }
+        $url = $this->api . '?' . http_build_query($params);
+
+        $ch = curl_init();
+        curl_setopt_array($ch, [
+            CURLOPT_URL            => $url,
+            CURLOPT_RETURNTRANSFER => true,
+            CURLOPT_HTTPHEADER     => [
+                'Authorization: APPCODE ' . $this->appCode,
+            ],
+            CURLOPT_TIMEOUT        => 30,
+            CURLOPT_SSL_VERIFYPEER => false,
+        ]);
+
+        $result = curl_exec($ch);
+        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+        curl_close($ch);
+
+        if ($httpCode !== 200) {
+            return false;
+        }
+
+        return json_decode($result, true) ?: false;
+    }
+
+    /**
+     * 物流状态码转中文
+     * @param int $status
+     * @return string
+     */
+    public function getStatusText(int $status): string
+    {
+        $map = [
+            0  => '在途',
+            1  => '揽收',
+            2  => '疑难',
+            3  => '签收',
+            4  => '退签',
+            5  => '派件',
+            6  => '退回',
+            7  => '转单',
+            10 => '待清关',
+            11 => '清关中',
+            12 => '已清关',
+            13 => '清关异常',
+            14 => '收件人拒签',
+        ];
+
+        return $map[$status] ?? '未知';
+    }
+}

+ 42 - 0
app/system/controller/Shop.php

@@ -7,6 +7,7 @@ use app\BaseController;
 use app\model\api\StoreOrder;
 use app\model\api\StoreOrderCartInfo;
 use app\Request;
+use app\services\AliyunExpressService;
 use library\services\UtilService;
 use think\facade\Db;
 
@@ -270,6 +271,47 @@ class Shop extends BaseController
         }
     }
 
+    /**
+     * 查询物流信息
+     * @param Request $request
+     */
+    public function express(Request $request)
+    {
+        $post = UtilService::getMore([
+            ['id', '', 'empty', '参数错误'],
+        ], $request);
+
+        $order = Db::name('store_order')
+            ->where('id', $post['id'])
+            ->find();
+
+        if (empty($order)) {
+            return app('json')->fail('订单不存在');
+        }
+
+        if (empty($order['express_num'])) {
+            return app('json')->fail('暂无物流信息');
+        }
+
+        $service = new AliyunExpressService();
+        $result = $service->query($order['express_num'], $order['express_code'] ?? '');
+
+        if ($result === false) {
+            return app('json')->fail('物流查询失败,请检查阿里云物流AppCode配置');
+        }
+
+        $data = [
+            'express_type' => $order['express_type'] ?? '',
+            'express_code' => $order['express_code'] ?? '',
+            'express_num'  => $order['express_num'],
+            'list'         => $result['result']['list'] ?? [],
+            'status'       => $result['result']['deliverystatus'] ?? -1,
+            'status_text'  => $service->getStatusText((int)($result['result']['deliverystatus'] ?? -1)),
+        ];
+
+        return app('json')->success('查询成功', $data);
+    }
+
     /**
      * 订单备注
      * @param Request $request

+ 2 - 0
app/system/route/shop.php

@@ -20,6 +20,8 @@ Route::group('shop', function () {
     Route::rule('info', 'Shop/info');
     //发货
     Route::rule('fahuo', 'Shop/fahuo');
+    //物流查询
+    Route::rule('express', 'Shop/express');
     //订单备注
     Route::rule('mono', 'Shop/mono');
 })->middleware([