|
|
@@ -98,6 +98,9 @@ class AddForm extends BaseForm
|
|
|
* @var int
|
|
|
*/
|
|
|
public $autoRise;
|
|
|
+ public $useCaseIdList;
|
|
|
+ public $specEnabled;
|
|
|
+ public $specList;
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -108,15 +111,21 @@ class AddForm extends BaseForm
|
|
|
return [
|
|
|
// 必填字段验证
|
|
|
[['name'], 'required', 'message' => '请填写商品名称'],
|
|
|
- [['priceType'], 'required', 'message' => '请选择价格类型'],
|
|
|
+ [['priceType'], 'required', 'message' => '请选择价格类型', 'when' => function($model) {
|
|
|
+ return (int)$model->specEnabled !== 1;
|
|
|
+ }],
|
|
|
[['catIds'], 'required', 'message' => '请选择商品分类'],
|
|
|
- ['weight', 'required', 'message' => '请填写重量'],
|
|
|
+ ['weight', 'required', 'message' => '请填写重量', 'when' => function($model) {
|
|
|
+ return (int)$model->specEnabled !== 1;
|
|
|
+ }],
|
|
|
|
|
|
// 默认值设置
|
|
|
['price', 'default', 'value' => 0],
|
|
|
['flowerNum', 'default', 'value' => 0],
|
|
|
['sold', 'default', 'value' => 0],
|
|
|
['catIds', 'default', 'value' => []],
|
|
|
+ ['useCaseIdList', 'default', 'value' => []],
|
|
|
+ ['specEnabled', 'default', 'value' => 0],
|
|
|
|
|
|
// 数据类型验证
|
|
|
[['name', 'cover'], 'string'],
|
|
|
@@ -131,6 +140,9 @@ class AddForm extends BaseForm
|
|
|
[['freightType'], 'in', 'range' => [0, 1]],
|
|
|
[['status'], 'in', 'range' => [0, 1]],
|
|
|
[['autoRise'], 'in', 'range' => [0, 1]],
|
|
|
+ [['specEnabled'], 'in', 'range' => [0, 1]],
|
|
|
+ [['useCaseIdList'], 'validateIdList'],
|
|
|
+ [['specList'], 'validateSpecList'],
|
|
|
|
|
|
// 添加条件验证
|
|
|
// ['price', 'required', 'message' => '请填写商品价格', 'when' => function($model) {
|
|
|
@@ -157,6 +169,96 @@ class AddForm extends BaseForm
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public function validateIdList($attribute) {
|
|
|
+ if (!is_array($this->$attribute)) $this->addError($attribute, '使用场景格式不正确');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function validateSpecList($attribute)
|
|
|
+ {
|
|
|
+ if ((int)$this->specEnabled !== 1) {
|
|
|
+ $this->$attribute = [];
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $list = $this->$attribute;
|
|
|
+ if (is_string($list)) {
|
|
|
+ $list = json_decode($list, true);
|
|
|
+ }
|
|
|
+ if (!is_array($list) || empty($list)) {
|
|
|
+ $this->addError($attribute, '请至少填写一个规格');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $formatList = [];
|
|
|
+ foreach ($list as $index => $spec) {
|
|
|
+ $name = '规格' . ($index + 1);
|
|
|
+ if (!is_array($spec)) {
|
|
|
+ $this->addError($attribute, $name . '格式错误');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $specName = trim($spec['specName'] ?? '');
|
|
|
+ if ($specName === '') {
|
|
|
+ $this->addError($attribute, '请填写' . $name . '名称');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $priceType = $spec['priceType'] ?? null;
|
|
|
+ if ($priceType === null || !in_array((int)$priceType, [0, 1], true)) {
|
|
|
+ $this->addError($attribute, $name . '价格类型错误');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $price = $spec['price'] ?? 0;
|
|
|
+ if ((int)$priceType === 1 && (!is_numeric($price) || $price <= 0)) {
|
|
|
+ $this->addError($attribute, '请填写' . $name . '价格');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $stockSet = $spec['stockSet'] ?? 0;
|
|
|
+ if (!in_array((int)$stockSet, [0, 1], true)) {
|
|
|
+ $this->addError($attribute, $name . '库存类型错误');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $stock = $spec['stock'] ?? 0;
|
|
|
+ if ((int)$stockSet === 1 && (!is_numeric($stock) || (int)$stock < 0 || (int)$stock != $stock)) {
|
|
|
+ $this->addError($attribute, $name . '库存数量错误');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $flowerNum = $spec['flowerNum'] ?? 0;
|
|
|
+ if (!is_numeric($flowerNum) || (int)$flowerNum < 0 || (int)$flowerNum != $flowerNum) {
|
|
|
+ $this->addError($attribute, $name . '花支数错误');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $weight = $spec['weight'] ?? 1;
|
|
|
+ if (!is_numeric($weight) || $weight < 0) {
|
|
|
+ $this->addError($attribute, $name . '重量错误');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $sold = $spec['sold'] ?? 0;
|
|
|
+ if (!is_numeric($sold) || (int)$sold < 0 || (int)$sold != $sold) {
|
|
|
+ $this->addError($attribute, $name . '已售数量错误');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $formatList[] = [
|
|
|
+ 'id' => (int)($spec['id'] ?? 0),
|
|
|
+ 'specName' => $specName,
|
|
|
+ 'priceType' => (int)$priceType,
|
|
|
+ 'price' => (int)$priceType === 1 ? (float)$price : 0,
|
|
|
+ 'stockSet' => (int)$stockSet,
|
|
|
+ 'stock' => (int)$stockSet === 1 ? (int)$stock : 9999,
|
|
|
+ 'flowerNum' => (int)$flowerNum,
|
|
|
+ 'weight' => (float)$weight,
|
|
|
+ 'sold' => (int)$sold,
|
|
|
+ 'shopImg' => is_array($spec['shopImg'] ?? null) ? $spec['shopImg'] : [],
|
|
|
+ 'cover' => trim($spec['cover'] ?? ''),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $this->$attribute = $formatList;
|
|
|
+ $firstSpec = $formatList[0];
|
|
|
+ $this->priceType = $firstSpec['priceType'];
|
|
|
+ $this->price = $firstSpec['price'];
|
|
|
+ $this->stockSet = $firstSpec['stockSet'];
|
|
|
+ $this->stock = $firstSpec['stock'];
|
|
|
+ $this->flowerNum = $firstSpec['flowerNum'];
|
|
|
+ $this->weight = $firstSpec['weight'];
|
|
|
+ $this->sold = $firstSpec['sold'];
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取属性标签
|
|
|
* @return array
|
|
|
@@ -176,4 +278,4 @@ class AddForm extends BaseForm
|
|
|
'autoRise' => '自动涨价',
|
|
|
];
|
|
|
}
|
|
|
-}
|
|
|
+}
|