| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace hd\models\homePageConfig;
- use bizHd\homePageConfig\classes\HomePageModuleClass;
- use hd\models\BaseForm;
- /**
- * 保存金刚区导航配置
- */
- class SaveNavGridForm extends BaseForm
- {
- public $enabled;
- public $cols;
- /** @var array|string */
- public $list;
- public function rules()
- {
- return [
- ['enabled', 'default', 'value' => 0],
- ['cols', 'default', 'value' => 5],
- ['list', 'default', 'value' => []],
- ['enabled', 'filter', 'filter' => function ($value) {
- return !empty($value) ? 1 : 0;
- }],
- ['cols', 'in', 'range' => [4, 5], 'message' => '排列规则无效'],
- ['list', 'validateList'],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'enabled' => '开关',
- 'cols' => '排列规则',
- '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) > HomePageModuleClass::MAX_NAV_COUNT) {
- $this->addError($attribute, '最多可配置' . HomePageModuleClass::MAX_NAV_COUNT . '个导航');
- return;
- }
- $list = [];
- foreach ($rawList as $index => $item) {
- if (!is_array($item)) {
- continue;
- }
- $name = trim(strval($item['name'] ?? ''));
- $icon = trim(strval($item['icon'] ?? ''));
- $type = intval($item['type'] ?? 0);
- $value = trim(strval($item['value'] ?? ''));
- $no = $index + 1;
- if ($name === '' || $icon === '' || $value === '') {
- $this->addError($attribute, "请完善第{$no}个导航项");
- return;
- }
- if (!in_array($type, [HomePageModuleClass::LINK_CATEGORY, HomePageModuleClass::LINK_USE_CASE], true)) {
- $this->addError($attribute, "第{$no}个导航关联类型无效");
- return;
- }
- $list[] = [
- 'id' => strval($item['id'] ?? ('nav_' . ($index + 1))),
- 'name' => mb_substr($name, 0, 20),
- 'icon' => $icon,
- 'iconType' => intval($item['iconType'] ?? 1) === 2 ? 2 : 1,
- 'enabled' => !empty($item['enabled']) ? 1 : 0,
- 'type' => $type,
- 'value' => $value,
- ];
- }
- $this->$attribute = $list;
- }
- /**
- * @return array
- */
- public function getPayload()
- {
- return [
- 'cols' => intval($this->cols),
- 'list' => is_array($this->list) ? $this->list : [],
- ];
- }
- /**
- * @return int
- */
- public function getEnabled()
- {
- return !empty($this->enabled) ? 1 : 0;
- }
- }
|