From f20325b22014668327ae1d903b12f468919d968f Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Wed, 12 Feb 2025 17:58:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9=E4=BA=A7?= =?UTF-8?q?=E5=93=81=E5=B1=9E=E6=80=A7=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/v1/ProductAttr.php | 20 +++++++++++-------- app/admin/validate/v1/ProductAttrValidate.php | 8 ++++---- app/common/model/ProductAttrBaseModel.php | 1 + app/common/model/ProductAttrPropBaseModel.php | 1 - .../20241219033236_create_product_attr.php | 2 ++ ...0241219054243_create_product_attr_prop.php | 2 -- 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/app/admin/controller/v1/ProductAttr.php b/app/admin/controller/v1/ProductAttr.php index 1516ae99..998479c4 100644 --- a/app/admin/controller/v1/ProductAttr.php +++ b/app/admin/controller/v1/ProductAttr.php @@ -74,6 +74,7 @@ class ProductAttr public function save() { $post = request()->post([ + 'attr_type' => 2, 'attr_name' => '', 'is_system' => 0, ]); @@ -100,13 +101,15 @@ class ProductAttr } // 添加属性特征 - foreach ($props as &$prop) { - $prop['attr_id'] = $attr_ret->id; - } - unset($prop); - $props_ret = (new ProductAttrPropModel)->saveAll($props); - if ($props_ret->isEmpty()) { - throw new \Exception("操作失败"); + if (!empty($props)) { + foreach ($props as &$prop) { + $prop['attr_id'] = $attr_ret->id; + } + unset($prop); + $props_ret = (new ProductAttrPropModel)->saveAll($props); + if ($props_ret->isEmpty()) { + throw new \Exception("操作失败"); + } } ProductAttrModel::commit(); @@ -125,7 +128,8 @@ class ProductAttr { $id = request()->param('id'); $put = request()->put([ - 'attr_name' => '', + 'attr_type', + 'attr_name', 'is_system' => 0, ]); $attr = array_merge($put, ['language_id' => request()->lang_id]); diff --git a/app/admin/validate/v1/ProductAttrValidate.php b/app/admin/validate/v1/ProductAttrValidate.php index 7d0ad233..e1b7830f 100644 --- a/app/admin/validate/v1/ProductAttrValidate.php +++ b/app/admin/validate/v1/ProductAttrValidate.php @@ -15,11 +15,11 @@ class ProductAttrValidate extends Validate */ protected $rule = [ 'language_id' => 'require|number', + 'attr_type' => 'in:1,2', 'attr_name' => 'require|max:64', 'is_system' => 'in:0,1', - 'props.*.prop_type' => 'in:1,2', - 'props.*.prop_name' => 'require|max:64', - 'props.*.prop_value' => 'require|max:64', + 'props.*.prop_name' => 'requireIf:attr_type,1|max:64', + 'props.*.prop_value' => 'requireIf:attr_type,1|max:64', ]; /** @@ -31,10 +31,10 @@ class ProductAttrValidate extends Validate protected $message = [ 'language_id.require' => '语言ID不能为空', 'language_id.number' => '语言ID必须是数字', + 'attr_type.in' => '属性类型只能是1或2', 'attr_name.require' => '属性名称不能为空', 'attr_name.max' => '属性名称不能超过64个字符', 'is_system.in' => '是否系统属性只能是0或1', - 'props.*.prop_type.in' => '属性特征类型只能是1或2', 'props.*.prop_name.require' => '属性特征名称不能为空', 'props.*.prop_name.max' => '属性特征名称不能超过64个字符', 'props.*.prop_value.require' => '属性特征值不能为空', diff --git a/app/common/model/ProductAttrBaseModel.php b/app/common/model/ProductAttrBaseModel.php index 9b83d4f9..fe4d49c7 100644 --- a/app/common/model/ProductAttrBaseModel.php +++ b/app/common/model/ProductAttrBaseModel.php @@ -19,6 +19,7 @@ class ProductAttrBaseModel extends BaseModel protected $schema = [ 'id' => 'int', 'language_id' => 'int', + 'attr_type' => 'string', 'attr_name' => 'string', 'is_system' => 'string', 'created_at' => 'datetime', diff --git a/app/common/model/ProductAttrPropBaseModel.php b/app/common/model/ProductAttrPropBaseModel.php index 9d7d67c0..2764f44d 100644 --- a/app/common/model/ProductAttrPropBaseModel.php +++ b/app/common/model/ProductAttrPropBaseModel.php @@ -18,7 +18,6 @@ class ProductAttrPropBaseModel extends BaseModel // 字段信息 protected $schema = [ 'attr_id' => 'int', - 'prop_type' => 'int', 'prop_name' => 'string', 'prop_value' => 'string', 'created_at' => 'datetime', diff --git a/database/migrations/20241219033236_create_product_attr.php b/database/migrations/20241219033236_create_product_attr.php index f8dd0f9d..42ad1abd 100644 --- a/database/migrations/20241219033236_create_product_attr.php +++ b/database/migrations/20241219033236_create_product_attr.php @@ -1,5 +1,6 @@ table('product_attr', ['engine' => 'InnoDB', 'comment' => '商品属性表']); $table->addColumn('language_id', 'integer', ['signed' => false, 'null' => false, 'comment' => '语言ID']) + ->addColumn('attr_type', MysqlAdapter::INT_TINY, ['limit' => 3, 'null' => false, 'default' => 2, 'comment' => '类型:1为选项类型,2为文本输入类型']) ->addColumn('attr_name', 'string', ['limit' => 64, 'signed' => false, 'null' => false, 'comment' => '属性ID']) ->addColumn('is_system', 'boolean', ['null' => false, 'default' => 0, 'comment' => '是否系统属性:1是,0否']) ->addColumn('created_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP', 'comment' => '新增时间']) diff --git a/database/migrations/20241219054243_create_product_attr_prop.php b/database/migrations/20241219054243_create_product_attr_prop.php index 9c60acc9..e730b8fe 100644 --- a/database/migrations/20241219054243_create_product_attr_prop.php +++ b/database/migrations/20241219054243_create_product_attr_prop.php @@ -1,6 +1,5 @@ table('product_attr_prop', ['id' => false, 'engine' => 'InnoDB', 'comment' => '商品属性特征表']); $table->addColumn('attr_id', 'integer', ['signed' => false, 'null' => false, 'comment' => '属性ID']) - ->addColumn('prop_type', MysqlAdapter::INT_TINY, ['limit' => 3, 'null' => false, 'default' => 2, 'comment' => '类型:1为选项类型,2为文本输入类型']) ->addColumn('prop_name', 'string', ['limit' => 64, 'null' => false, 'comment' => '特征名']) ->addColumn('prop_value', 'string', ['limit' => 64, 'null' => false, 'comment' => '特征值']) ->addColumn('created_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP', 'comment' => '新增时间'])