Bläddra i källkod

feat(shipping-template): 初始化运费模板功能模块

- 创建运费模板数据库迁移文件,包含模板表和规则表- 添加运费模板相关的模型、实体和枚举类
- 实现前端新建和编辑运费模板组件
- 支持按件数、重量、体积三种计费方式- 支持默认区域和特殊区域的运费规则配置
- 提供完整的地区选择和单位切换功能- 增加模板信息校验和数据保存逻辑
- 更新 composer.json 自动加载配置
- 编写详细的 README 文档说明使用方法
runphp 5 månader sedan
förälder
incheckning
fc0ec06fbf

+ 77 - 0
README.md

@@ -0,0 +1,77 @@
+# 运费模板扩展
+
+## 简介
+
+运费模板扩展为 SixShop 商城系统提供灵活的运费计算功能,支持按件数、重量和体积三种计费方式。
+
+## 数据库设计
+
+### 1. shipping_templates 表(运费模板表)
+
+| 字段名 | 类型 | 说明 |
+|--------|------|------|
+| id | int(11) | 主键 |
+| name | varchar(100) | 模板名称 |
+| calc_method | varchar(20) | 计费方式: piece(按件), weight(按重量), volume(按体积) |
+| unit | varchar(10) | 单位: 件,g,kg,cm3,m3 |
+| status | tinyint(1) | 状态: 0(禁用), 1(启用) |
+| sort | int(11) | 排序 |
+| create_time | datetime | 创建时间 |
+| update_time | datetime | 更新时间 |
+| delete_time | datetime | 删除时间 |
+
+### 2. shipping_template_rules 表(运费模板规则表)
+
+| 字段名 | 类型 | 说明 |
+|--------|------|------|
+| id | int(11) | 主键 |
+| template_id | int(11) | 运费模板ID |
+| first | decimal(10,3) | 首件/首重/首体积 |
+| first_price | decimal(10,2) | 首件/首重/首体积费用 |
+| next_price | decimal(10,2) | 续件/续重/续体积费用 |
+| area_type | varchar(20) | 区域类型: default(默认), special(特殊区域) |
+| regions | json | 地区信息(JSON格式,省级行政区划,包含地区编码和名称) |
+| create_time | datetime | 创建时间 |
+| update_time | datetime | 更新时间 |
+
+### 3. 表关系
+
+- 一个运费模板可以有多个规则(1:N)
+- 每个模板必须有一个默认规则(area_type = 'default')
+- 可选多个特殊区域规则(area_type = 'special')
+
+## 地区层级说明
+
+在运费模板中,地区信息限定为省级层级:
+
+1. **省级层级**(唯一支持的层级)
+   - 适用于所有地区运费设置
+   - 管理简单,维护成本低
+   - 满足大多数业务场景需求
+   - 例如:北京市、上海市、广东省等
+
+这种设计简化了系统复杂度,同时满足了核心业务需求。
+
+## 数据库迁移
+
+扩展包含以下迁移文件:
+
+1. `20251010100000_create_shipping_templates_table.php` - 创建运费模板表
+2. `20251010100100_create_shipping_template_rules_table.php` - 创建运费模板规则表
+
+## 数据结构示例
+
+### regions 字段 JSON 格式示例:
+
+```json
+[
+  {
+    "code": "110000",
+    "name": "北京市"
+  },
+  {
+    "code": "120000",
+    "name": "天津市"
+  }
+]
+```

+ 3 - 2
composer.json

@@ -19,13 +19,14 @@
   "license": "MIT",
   "autoload": {
     "psr-4": {
-      "SixShop\\ShippingTemplate\\": "src"
+      "SixShop\\ShippingTemplate\\": "src/"
     }
   },
   "extra": {
     "sixshop": {
       "id": "shipping_template",
-      "class": "SixShop\\ShippingTemplate\\Extension"    }
+      "class": "SixShop\\ShippingTemplate\\Extension"
+    }
   },
   "minimum-stability": "dev",
   "prefer-stable": true

+ 24 - 0
database/migrations/20251010100000_create_shipping_templates_table.php

@@ -0,0 +1,24 @@
+<?php
+
+use think\migration\Migrator;
+use think\migration\db\Column;
+
+class CreateShippingTemplatesTable extends Migrator
+{
+    public function change()
+    {
+        $table = $this->table('shipping_templates');
+        $table->setId('id')
+            ->addColumn('name', 'string', ['limit' => 100, 'comment' => '模板名称'])
+            ->addColumn('calc_method', 'string', ['limit' => 20, 'comment' => '计费方式: piece(按件), weight(按重量), volume(按体积)'])
+            ->addColumn('unit', 'string', ['limit' => 10, 'comment' => '单位: 件,g,kg,cm3,m3', 'default' => 'piece'])
+            ->addColumn('status', 'integer', ['limit' => 1, 'comment' => '状态: 0(禁用), 1(启用)', 'default' => 1])
+            ->addColumn('sort', 'integer', ['comment' => '排序', 'default' => 100])
+            ->addTimestamps('create_time', 'update_time')
+            ->addColumn('delete_time', 'timestamp', ['comment' => '删除时间', 'null' => true])
+            ->addIndex(['name'])
+            ->addIndex(['status'])
+            ->addIndex(['sort'])
+            ->create();
+    }
+}

+ 23 - 0
database/migrations/20251010100100_create_shipping_template_rules_table.php

@@ -0,0 +1,23 @@
+<?php
+
+use think\migration\Migrator;
+use think\migration\db\Column;
+
+class CreateShippingTemplateRulesTable extends Migrator
+{
+    public function change()
+    {
+        $table = $this->table('shipping_template_rules');
+        $table->setId('id')
+            ->addColumn('template_id', 'integer', ['comment' => '运费模板ID'])
+            ->addColumn('first', 'decimal', ['precision' => 10, 'scale' => 3, 'comment' => '首件/首重/首体积'])
+            ->addColumn('first_price', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '首件/首重/首体积费用'])
+            ->addColumn('next_price', 'decimal', ['precision' => 10, 'scale' => 2, 'comment' => '续件/续重/续体积费用'])
+            ->addColumn('area_type', 'string', ['limit' => 20, 'comment' => '区域类型: default(默认), special(特殊区域)'])
+            ->addColumn('regions', 'json', ['comment' => '地区信息(JSON格式,省级行政区划,包含地区编码和名称)', 'null' => true])
+            ->addTimestamps('create_time', 'update_time')
+            ->addIndex(['template_id'])
+            ->addIndex(['area_type'])
+            ->create();
+    }
+}

+ 15 - 0
src/Entity/ShippingTemplateEntity.php

@@ -0,0 +1,15 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\ShippingTemplate\Entity;
+
+use SixShop\Core\Entity\BaseEntity;
+use SixShop\ShippingTemplate\Model\ShippingTemplateModel;
+
+/**
+ * @mixin ShippingTemplateModel
+ */
+class ShippingTemplateEntity extends BaseEntity
+{
+
+}

+ 15 - 0
src/Entity/ShippingTemplateRuleEntity.php

@@ -0,0 +1,15 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\ShippingTemplate\Entity;
+
+use SixShop\Core\Entity\BaseEntity;
+use SixShop\ShippingTemplate\Model\ShippingTemplateRuleModel;
+
+/**
+ * @mixin ShippingTemplateRuleModel
+ */
+class ShippingTemplateRuleEntity extends BaseEntity
+{
+
+}

+ 27 - 0
src/Enum/ShippingTemplateAreaTypeEnum.php

@@ -0,0 +1,27 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\ShippingTemplate\Enum;
+
+use SixShop\Core\Enum\BaseEnum;
+
+/**
+ * 运费模板区域类型枚举
+ */
+class ShippingTemplateAreaTypeEnum extends BaseEnum
+{
+    /**
+     * @var string 默认区域
+     */
+    public const string DEFAULT = 'default';
+
+    /**
+     * @var string 特殊区域
+     */
+    public const string SPECIAL = 'special';
+
+    protected static array $texts = [
+        self::DEFAULT => '默认区域',
+        self::SPECIAL => '特殊区域',
+    ];
+}

+ 33 - 0
src/Enum/ShippingTemplateCalcMethodEnum.php

@@ -0,0 +1,33 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\ShippingTemplate\Enum;
+
+use SixShop\Core\Enum\BaseEnum;
+
+/**
+ * 运费模板计费方式枚举
+ */
+class ShippingTemplateCalcMethodEnum extends BaseEnum
+{
+    /**
+     * @var string 按件数计算
+     */
+    public const string PIECE = 'piece';
+
+    /**
+     * @var string 按重量计算
+     */
+    public const string WEIGHT = 'weight';
+
+    /**
+     * @var string 按体积计算
+     */
+    public const string VOLUME = 'volume';
+
+    protected static array $texts = [
+        self::PIECE => '按件数',
+        self::WEIGHT => '按重量',
+        self::VOLUME => '按体积',
+    ];
+}

+ 27 - 0
src/Enum/ShippingTemplateStatusEnum.php

@@ -0,0 +1,27 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\ShippingTemplate\Enum;
+
+use SixShop\Core\Enum\BaseEnum;
+
+/**
+ * 运费模板状态枚举
+ */
+class ShippingTemplateStatusEnum extends BaseEnum
+{
+    /**
+     * @var int 禁用
+     */
+    public const int DISABLED = 0;
+
+    /**
+     * @var int 启用
+     */
+    public const int ENABLED = 1;
+
+    protected static array $texts = [
+        self::DISABLED => '禁用',
+        self::ENABLED => '启用',
+    ];
+}

+ 41 - 0
src/Model/ShippingTemplateModel.php

@@ -0,0 +1,41 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\ShippingTemplate\Model;
+
+use SixShop\ShippingTemplate\Enum\ShippingTemplateCalcMethodEnum;
+use SixShop\ShippingTemplate\Enum\ShippingTemplateStatusEnum;
+use think\Model;
+use think\model\concern\SoftDelete;
+
+/**
+ * Class SixShop\ShippingTemplate\Model\ShippingTemplateModel
+ *
+ * @property int $id 主键
+ * @property string $name 模板名称
+ * @property ShippingTemplateCalcMethodEnum $calc_method 计费方式
+ * @property string $unit 单位
+ * @property ShippingTemplateStatusEnum $status 状态
+ * @property int $sort 排序
+ * @property string $create_time 创建时间
+ * @property string $update_time 更新时间
+ * @property string $delete_time 删除时间
+ * @method static \think\db\Query onlyTrashed()
+ * @method static \think\db\Query withTrashed()
+ */
+class ShippingTemplateModel extends Model
+{
+    use SoftDelete;
+
+    protected function getOptions(): array
+    {
+        return [
+            'name' => 'shipping_templates',
+            'pk' => 'id',
+            'type' => [
+                'calc_method' => ShippingTemplateCalcMethodEnum::class,
+                'status' => ShippingTemplateStatusEnum::class,
+            ]
+        ];
+    }
+}

+ 34 - 0
src/Model/ShippingTemplateRuleModel.php

@@ -0,0 +1,34 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\ShippingTemplate\Model;
+
+use SixShop\ShippingTemplate\Enum\ShippingTemplateAreaTypeEnum;
+use think\Model;
+
+/**
+ * Class SixShop\ShippingTemplate\Model\ShippingTemplateRuleModel
+ *
+ * @property int $id 主键
+ * @property int $template_id 运费模板ID
+ * @property float $first 首件/首重/首体积
+ * @property float $first_price 首件/首重/首体积费用
+ * @property float $next_price 续件/续重/续体积费用
+ * @property ShippingTemplateAreaTypeEnum $area_type 区域类型
+ * @property string $regions 地区信息(JSON格式)
+ * @property string $create_time 创建时间
+ * @property string $update_time 更新时间
+ */
+class ShippingTemplateRuleModel extends Model
+{
+    protected function getOptions(): array
+    {
+        return [
+            'name' => 'shipping_template_rules',
+            'pk' => 'id',
+            'type' => [
+                'area_type' => ShippingTemplateAreaTypeEnum::class,
+            ]
+        ];
+    }
+}

+ 73 - 0
src/README.md

@@ -0,0 +1,73 @@
+# 运费模板扩展 - 源代码说明
+
+## 目录结构
+
+```
+src/
+├── Entity/                    # 实体类
+│   ├── ShippingTemplateEntity.php     # 运费模板实体类
+│   └── ShippingTemplateRuleEntity.php # 运费模板规则实体类
+├── Enum/                      # 枚举类
+│   ├── ShippingTemplateAreaTypeEnum.php    # 区域类型枚举
+│   ├── ShippingTemplateCalcMethodEnum.php  # 计费方式枚举
+│   └── ShippingTemplateStatusEnum.php      # 模板状态枚举
+├── Model/                     # 模型类
+│   ├── ShippingTemplateModel.php      # 运费模板模型类
+│   └── ShippingTemplateRuleModel.php  # 运费模板规则模型类
+└── Extension.php              # 扩展入口文件
+```
+
+## 类说明
+
+### 枚举类 (Enum)
+
+1. **ShippingTemplateStatusEnum** - 运费模板状态枚举
+   - DISABLED (0): 禁用
+   - ENABLED (1): 启用
+
+2. **ShippingTemplateCalcMethodEnum** - 计费方式枚举
+   - PIECE ('piece'): 按件数计算
+   - WEIGHT ('weight'): 按重量计算
+   - VOLUME ('volume'): 按体积计算
+
+3. **ShippingTemplateAreaTypeEnum** - 区域类型枚举
+   - DEFAULT ('default'): 默认区域
+   - SPECIAL ('special'): 特殊区域
+
+### 模型类 (Model)
+
+1. **ShippingTemplateModel** - 运费模板模型
+   - 对应数据库表: shipping_templates
+   - 主要字段: id, name, calc_method, unit, status, sort 等
+
+2. **ShippingTemplateRuleModel** - 运费模板规则模型
+   - 对应数据库表: shipping_template_rules
+   - 主要字段: id, template_id, first, first_price, next_price, area_type, regions 等
+
+### 实体类 (Entity)
+
+1. **ShippingTemplateEntity** - 运费模板实体
+   - 继承 BaseEntity
+   - 关联 ShippingTemplateModel
+
+2. **ShippingTemplateRuleEntity** - 运费模板规则实体
+   - 继承 BaseEntity
+   - 关联 ShippingTemplateRuleModel
+
+## 使用示例
+
+```php
+use SixShop\ShippingTemplate\Entity\ShippingTemplateEntity;
+
+// 创建运费模板
+$template = new ShippingTemplateEntity();
+$template->name = '全国包邮模板';
+$template->calc_method = 'piece';
+$template->unit = '件';
+$template->status = 1;
+$template->save();
+
+// 查询运费模板
+$template = ShippingTemplateEntity::find(1);
+echo $template->name;
+```