SaveNavGridForm.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. $allowedTypes = [
  65. HomePageModuleClass::LINK_CATEGORY,
  66. HomePageModuleClass::LINK_USE_CASE,
  67. HomePageModuleClass::LINK_ITEM_CLASS,
  68. ];
  69. if (!in_array($type, $allowedTypes, true)) {
  70. $this->addError($attribute, "第{$no}个导航关联类型无效");
  71. return;
  72. }
  73. // 花材分类只能关联一个分类 ID,避免商城首页无法定位到具体花材分类
  74. if ($type === HomePageModuleClass::LINK_ITEM_CLASS) {
  75. $itemClassIds = array_values(array_filter(array_map('intval', explode(',', $value))));
  76. if (count($itemClassIds) !== 1 || $itemClassIds[0] <= 0) {
  77. $this->addError($attribute, "第{$no}个导航请选择一个花材分类");
  78. return;
  79. }
  80. $value = strval($itemClassIds[0]);
  81. }
  82. $list[] = [
  83. 'id' => strval($item['id'] ?? ('nav_' . ($index + 1))),
  84. 'name' => mb_substr($name, 0, 20),
  85. 'icon' => $icon,
  86. 'iconType' => intval($item['iconType'] ?? 1) === 2 ? 2 : 1,
  87. 'enabled' => !empty($item['enabled']) ? 1 : 0,
  88. 'type' => $type,
  89. 'value' => $value,
  90. ];
  91. }
  92. $this->$attribute = $list;
  93. }
  94. /**
  95. * @return array
  96. */
  97. public function getPayload()
  98. {
  99. return [
  100. 'cols' => intval($this->cols),
  101. 'list' => is_array($this->list) ? $this->list : [],
  102. ];
  103. }
  104. /**
  105. * @return int
  106. */
  107. public function getEnabled()
  108. {
  109. return !empty($this->enabled) ? 1 : 0;
  110. }
  111. }