SaveNavGridForm.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace hd\models\homePageConfig;
  3. use bizHd\homePageConfig\classes\HomePageModuleClass;
  4. use hd\models\BaseForm;
  5. /**
  6. * 保存金刚区导航配置
  7. */
  8. class SaveNavGridForm extends BaseForm
  9. {
  10. public $enabled;
  11. public $cols;
  12. /** @var array|string */
  13. public $list;
  14. public function rules()
  15. {
  16. return [
  17. ['enabled', 'default', 'value' => 0],
  18. ['cols', 'default', 'value' => 5],
  19. ['list', 'default', 'value' => []],
  20. ['enabled', 'filter', 'filter' => function ($value) {
  21. return !empty($value) ? 1 : 0;
  22. }],
  23. ['cols', 'in', 'range' => [4, 5], 'message' => '排列规则无效'],
  24. ['list', 'validateList'],
  25. ];
  26. }
  27. public function attributeLabels()
  28. {
  29. return [
  30. 'enabled' => '开关',
  31. 'cols' => '排列规则',
  32. 'list' => '导航列表',
  33. ];
  34. }
  35. public function validateList($attribute)
  36. {
  37. $rawList = $this->$attribute;
  38. if (is_string($rawList)) {
  39. $decoded = json_decode($rawList, true);
  40. $rawList = is_array($decoded) ? $decoded : null;
  41. }
  42. if (!is_array($rawList)) {
  43. $this->addError($attribute, '导航数据格式错误');
  44. return;
  45. }
  46. if (count($rawList) > HomePageModuleClass::MAX_NAV_COUNT) {
  47. $this->addError($attribute, '最多可配置' . HomePageModuleClass::MAX_NAV_COUNT . '个导航');
  48. return;
  49. }
  50. $list = [];
  51. foreach ($rawList as $index => $item) {
  52. if (!is_array($item)) {
  53. continue;
  54. }
  55. $name = trim(strval($item['name'] ?? ''));
  56. $icon = trim(strval($item['icon'] ?? ''));
  57. $type = intval($item['type'] ?? 0);
  58. $value = trim(strval($item['value'] ?? ''));
  59. $no = $index + 1;
  60. if ($name === '' || $icon === '' || $value === '') {
  61. $this->addError($attribute, "请完善第{$no}个导航项");
  62. return;
  63. }
  64. if (!in_array($type, [HomePageModuleClass::LINK_CATEGORY, HomePageModuleClass::LINK_USE_CASE], true)) {
  65. $this->addError($attribute, "第{$no}个导航关联类型无效");
  66. return;
  67. }
  68. $list[] = [
  69. 'id' => strval($item['id'] ?? ('nav_' . ($index + 1))),
  70. 'name' => mb_substr($name, 0, 20),
  71. 'icon' => $icon,
  72. 'iconType' => intval($item['iconType'] ?? 1) === 2 ? 2 : 1,
  73. 'enabled' => !empty($item['enabled']) ? 1 : 0,
  74. 'type' => $type,
  75. 'value' => $value,
  76. ];
  77. }
  78. $this->$attribute = $list;
  79. }
  80. /**
  81. * @return array
  82. */
  83. public function getPayload()
  84. {
  85. return [
  86. 'cols' => intval($this->cols),
  87. 'list' => is_array($this->list) ? $this->list : [],
  88. ];
  89. }
  90. /**
  91. * @return int
  92. */
  93. public function getEnabled()
  94. {
  95. return !empty($this->enabled) ? 1 : 0;
  96. }
  97. }