0], ['interval', 'default', 'value' => 3], ['list', 'default', 'value' => []], ['enabled', 'filter', 'filter' => function ($value) { return !empty($value) ? 1 : 0; }], ['interval', 'integer', 'min' => 1, 'max' => 60, 'message' => '轮播间隔时间格式错误', 'tooSmall' => '轮播间隔时间至少1秒', 'tooBig' => '轮播间隔时间不能超过60秒', ], ['list', 'validateList'], ]; } public function attributeLabels() { return [ 'enabled' => '开关', 'interval' => '轮播间隔', 'list' => '轮播图列表', ]; } public function validateList($attribute) { $rawList = $this->$attribute; if (is_string($rawList)) { $decoded = json_decode($rawList, true); $rawList = is_array($decoded) ? $decoded : null; } if (!is_array($rawList)) { $this->addError($attribute, '轮播图数据格式错误'); return; } if (count($rawList) > HomePageConfigClass::MAX_BANNER_COUNT) { $this->addError($attribute, '最多可配置' . HomePageConfigClass::MAX_BANNER_COUNT . '张轮播图'); return; } $list = []; $types = HomePageConfigClass::getBannerTypes(); $ossRoot = HomePageConfigClass::BANNER_OSS_ROOT . '/'; foreach ($rawList as $index => $item) { if (!is_array($item)) { $this->addError($attribute, '轮播图数据格式错误'); return; } $img = isset($item['img']) ? trim(strval($item['img'])) : ''; $type = isset($item['type']) ? intval($item['type']) : 0; $value = isset($item['value']) ? trim(strval($item['value'])) : ''; $no = $index + 1; if ($img === '' && $value === '') { continue; } if ($img === '') { $this->addError($attribute, "请上传轮播图{$no}的图片"); return; } if ($value === '') { $this->addError($attribute, "请完善轮播图{$no}的关联内容"); return; } if (!in_array($type, $types, true)) { $this->addError($attribute, "轮播图{$no}关联类型无效"); return; } if (strpos($img, $ossRoot) !== 0) { $this->addError($attribute, "轮播图{$no}图片路径无效"); return; } $list[] = [ 'img' => $img, 'type' => $type, 'value' => $value, ]; } $this->$attribute = $list; } /** * @return array */ public function getPayload() { return [ 'interval' => intval($this->interval), 'list' => is_array($this->list) ? $this->list : [], ]; } /** * @return int */ public function getEnabled() { return !empty($this->enabled) ? 1 : 0; } }