Browse Source

feat(backend): 优化阿里云物流扩展

- 重构 Config 类,添加 app_code 属性
- 在 Extension 类中定义 EXTENSION_ID 常量
- 更新 WuLiuClient 类:
  - 修改 kdi 方法返回类型为 object  - 添加 getExpressList 方法获取快递公司列表
- 更新测试用例
runphp 6 months ago
parent
commit
c35beea2ff

+ 37 - 0
database/migrations/20250913071830_extension_wuliu_express.php

@@ -0,0 +1,37 @@
+<?php
+
+use think\migration\db\Column;
+use think\migration\Migrator;
+
+class ExtensionWuliuExpress extends Migrator
+{
+    /**
+     * Change Method.
+     *
+     * Write your reversible migrations using this method.
+     *
+     * More information on writing migrations is available here:
+     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
+     *
+     * The following commands can be used in this method and Phinx will
+     * automatically reverse them when rolling back:
+     *
+     *    createTable
+     *    renameTable
+     *    addColumn
+     *    renameColumn
+     *    addIndex
+     *    addForeignKey
+     *
+     * Remember to call "create()" or "update()" and NOT "save()" when working
+     * with the Table class.
+     */
+    public function change()
+    {
+        $table = $this->table('extension_wuliu_express');
+        $table->addColumn('name', Column::STRING, ['limit' => 255, 'comment' => '快递公司名称'])
+            ->addColumn('code', Column::STRING, ['limit' => 255, 'comment' => '快递公司编码'])
+            ->addIndex('code', ['unique' => true])
+            ->create();
+    }
+}

+ 7 - 0
route/api.php

@@ -0,0 +1,7 @@
+<?php
+declare(strict_types=1);
+
+use SixShop\AliyunWuliu\Controller\IndexController;
+use think\facade\Route;
+
+Route::get('',  [IndexController::class, 'index']);

+ 23 - 0
src/Controller/IndexController.php

@@ -0,0 +1,23 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\AliyunWuliu\Controller;
+
+use SixShop\AliyunWuliu\Entity\ExtensionWuLiuEntity;
+use SixShop\Core\Request;
+use function SixShop\Core\success_response;
+
+class IndexController
+{
+    public function index(Request $request, ExtensionWuLiuEntity $entity)
+    {
+        $params = $request->get([
+            'no/s',
+            'type/s' => ''
+        ]);
+        validate( [
+            'no' => 'require|alphaNum',
+            'type' => 'alphaNum'
+        ])->check($params);
+        return success_response($entity->getWuliuInfo($params['no'], $params['type']));
+    }
+}

+ 38 - 0
src/Entity/ExtensionWuLiuEntity.php

@@ -0,0 +1,38 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\AliyunWuliu\Entity;
+
+use SixShop\AliyunWuliu\Facade\WuLiuClient;
+use SixShop\Core\Entity\BaseEntity;
+use think\Model;
+
+class ExtensionWuLiuEntity extends BaseEntity
+{
+
+    public function getWuliuInfo(string $no, string $type = ''): array
+    {
+        $entity = $this->where('number', $no)->when($type, function (Model $query, $type) {
+            $query->where('type', $type);
+        })->findOrEmpty();
+        if (!$entity->isEmpty() && ($entity->is_sign == 1 || $entity->update_time > time() - 3600)) {
+            // 已签收或一小时内更新过,直接返回
+            return $entity->toArray();
+        }
+        $result = WuLiuClient::kdi($no, $type);
+        $entity->number = $result->number;
+        $entity->type = $result->type;
+        $entity->list = $result->list;
+        $entity->delivery_status = $result->deliverystatus;
+        $entity->is_sign = $result->issign;
+        $entity->exp_name = $result->expName;
+        $entity->exp_site = $result->expSite;
+        $entity->exp_phone = $result->expPhone;
+        $entity->courier = $result->courier;
+        $entity->courier_phone = $result->courierPhone;
+        $entity->take_time = $result->takeTime;
+        $entity->save();
+
+        return $entity->toArray();
+    }
+}

+ 17 - 0
src/Facade/WuLiuClient.php

@@ -0,0 +1,17 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\AliyunWuliu\Facade;
+
+use think\Facade;
+
+/**
+ * @method static array kdi(string $no, string $type = '') 快递查询
+ * @method static array getExpressList() 获取快递公司列表
+ */
+class WuLiuClient extends Facade
+{
+    protected static function getFacadeClass()
+    {
+        return \SixShop\AliyunWuliu\WuLiuClient::class;
+    }
+}

+ 15 - 0
src/Model/ExtensionWuLiuExpressModel.php

@@ -0,0 +1,15 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\AliyunWuliu\Model;
+
+use think\Model;
+
+class ExtensionWuLiuExpressModel extends Model
+{
+    protected function getOptions(): array
+    {
+        return [
+            'name' => 'extension_wuliu_express',
+        ];
+    }
+}

+ 18 - 0
src/Model/ExtensionWuLiuModel.php

@@ -0,0 +1,18 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\AliyunWuliu\Model;
+
+use think\Model;
+
+class ExtensionWuLiuModel extends Model
+{
+    protected function getOptions(): array
+    {
+        return [
+            'name' => 'extension_wuliu',
+            'type' => [
+                'list' => 'array'
+            ],
+        ];
+    }
+}