'商品ID不能为空'], [['name'], 'required', 'message' => '请填写商品名称'], [['categoryIdList'], 'required', 'message' => '请选择商品分类'], // 数据类型验证 [['id', 'status', 'delStatus', 'setItemNum'], 'integer'], [['name', 'cover'], 'string'], [['price'], 'number', 'min' => 0, 'message' => '商品价格必须大于或等于0'], [['sold'], 'integer', 'min' => 0, 'message' => '已售数量必须大于或等于0'], ['flowerNum', 'integer', 'min' => 0, 'message' => '花朵数量必须大于或等于0'], ['weight', 'number', 'min' => 0, 'message' => '重量必须大于或等于0'], [['autoRise'], 'in', 'range' => [0, 1]], [['stockSet'], 'in', 'range' => [0, 1]], [['stock'], 'integer', 'min' => 0, 'message' => '库存不能小于0'], [['priceType'], 'in', 'range' => [0, 1]], [['delStatus'], 'in', 'range' => [0, 1]], [['needSend'], 'in', 'range' => [0, 1]], [['freightType'], 'in', 'range' => [0, 1]], [['status'], 'in', 'range' => [0, 1]], // 数组类型验证 [['shopImg'], 'validateShopImg'], [['categoryIdList'], 'validateCategoryIdList'], // 默认值设置 ['flowerNum', 'default', 'value' => 0], ['weight', 'default', 'value' => 1],//当没有重量时,默认给1千克 ['setItemNum', 'default', 'value' => 0],// 在更新时,没传值,会为 NULL 而导致报错 ['status', 'default', 'value' => 1], ['delStatus', 'default', 'value' => 0], ['categoryIdList', 'default', 'value' => []], ['stock', 'default', 'value' => 0], ['useCaseIdList', 'default', 'value' => []], ['specEnabled', 'default', 'value' => 0], [['specEnabled'], 'in', 'range' => [0, 1]], [['useCaseIdList'], 'validateUseCaseIdList'], [['specList'], 'validateSpecList'], ]; } /** * 验证商品图片 * @param string $attribute 属性名 * @param array $params 参数 */ public function validateShopImg($attribute, $params) { if (!empty($this->$attribute) && !is_array($this->$attribute)) { $this->addError($attribute, '商品图片格式不正确'); } } /** * 验证分类ID列表 * @param string $attribute 属性名 * @param array $params 参数 */ public function validateCategoryIdList($attribute, $params) { if (!empty($this->$attribute) && !is_array($this->$attribute)) { $this->addError($attribute, '商品分类格式不正确'); } } public function validateUseCaseIdList($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 */ public function attributeLabels() { return [ 'id' => '商品ID', 'name' => '商品名称', 'price' => '商品价格', 'priceType' => '价格类型', 'flowerNum' => '花朵数量', 'shopImg' => '商品图片', 'cover' => '商品封面图', 'stock' => '商品库存', 'categoryIdList' => '商品分类', 'status' => '商品状态', 'delStatus' => '删除状态', 'setItemNum' => '商品子项数量', 'itemList' => '商品子项列表', ]; } }