SaveGroupBuyForm.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace hd\models\homePageConfig;
  3. use hd\models\BaseForm;
  4. /**
  5. * 保存团购专区配置
  6. */
  7. class SaveGroupBuyForm extends BaseForm
  8. {
  9. public $enabled;
  10. public $title;
  11. public $subtitle;
  12. public $showCountdown;
  13. public $expand;
  14. public $startTime;
  15. public $endTime;
  16. public $desc;
  17. /** @var array|string */
  18. public $goods;
  19. public function rules()
  20. {
  21. return [
  22. ['enabled', 'default', 'value' => 0],
  23. ['showCountdown', 'default', 'value' => 0],
  24. ['expand', 'default', 'value' => 0],
  25. ['goods', 'default', 'value' => []],
  26. [['title', 'subtitle', 'desc'], 'trim'],
  27. ['title', 'required', 'message' => '请输入活动标题'],
  28. ['title', 'string', 'max' => 10, 'tooLong' => '活动标题不能超过10字'],
  29. ['subtitle', 'required', 'message' => '请输入活动副标题'],
  30. ['subtitle', 'string', 'max' => 20, 'tooLong' => '活动副标题不能超过20字'],
  31. ['desc', 'required', 'message' => '请输入活动说明'],
  32. ['desc', 'string', 'max' => 500, 'tooLong' => '活动说明不能超过500字'],
  33. [['startTime', 'endTime'], 'required', 'message' => '请选择活动时间'],
  34. [['startTime', 'endTime'], 'integer', 'min' => 1, 'message' => '请选择活动时间', 'tooSmall' => '请选择活动时间'],
  35. ['endTime', 'compare', 'compareAttribute' => 'startTime', 'operator' => '>', 'message' => '结束时间必须大于开始时间'],
  36. [['enabled', 'showCountdown', 'expand'], 'filter', 'filter' => function ($value) {
  37. return !empty($value) ? 1 : 0;
  38. }],
  39. ['goods', 'validateGoods'],
  40. ];
  41. }
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'enabled' => '开关',
  46. 'title' => '活动标题',
  47. 'subtitle' => '活动副标题',
  48. 'showCountdown' => '倒计时',
  49. 'expand' => '商品展开',
  50. 'startTime' => '开始时间',
  51. 'endTime' => '结束时间',
  52. 'desc' => '活动说明',
  53. 'goods' => '团购商品',
  54. ];
  55. }
  56. public function validateGoods($attribute)
  57. {
  58. $rawList = $this->$attribute;
  59. if (is_string($rawList)) {
  60. $decoded = json_decode($rawList, true);
  61. $rawList = is_array($decoded) ? $decoded : null;
  62. }
  63. if (!is_array($rawList)) {
  64. $this->addError($attribute, '团购商品数据格式错误');
  65. return;
  66. }
  67. $list = [];
  68. foreach ($rawList as $index => $item) {
  69. if (!is_array($item)) {
  70. continue;
  71. }
  72. $no = $index + 1;
  73. $goodsId = intval($item['goodsId'] ?? 0);
  74. $price = floatval($item['price'] ?? 0);
  75. $stock = intval($item['stock'] ?? 0);
  76. $limit = intval($item['limit'] ?? 0);
  77. $groupSize = intval($item['groupSize'] ?? 3);
  78. if (!in_array($groupSize, [2, 3, 5], true)) {
  79. $this->addError($attribute, "第{$no}个成团人数无效");
  80. return;
  81. }
  82. if ($goodsId <= 0 || $price <= 0 || $stock <= 0 || $limit <= 0) {
  83. $this->addError($attribute, "请完善第{$no}个团购商品");
  84. return;
  85. }
  86. $virtualGroup = !empty($item['virtualGroup']) ? 1 : 0;
  87. $virtualMinutes = intval($item['virtualMinutes'] ?? 0);
  88. if ($virtualGroup && $virtualMinutes <= 0) {
  89. $this->addError($attribute, "请填写第{$no}个虚拟成团时间");
  90. return;
  91. }
  92. $list[] = [
  93. // 透传活动商品版本 id,便于后端判断更新现有版本还是新建版本
  94. 'id' => intval($item['id'] ?? 0),
  95. 'goodsId' => $goodsId,
  96. 'price' => $price,
  97. 'stock' => $stock,
  98. 'limit' => $limit,
  99. 'groupSize' => $groupSize,
  100. 'virtualGroup' => $virtualGroup,
  101. 'virtualMinutes' => $virtualMinutes,
  102. 'autoRefund' => !empty($item['autoRefund']) ? 1 : 0,
  103. 'status' => !empty($item['status']) ? 1 : 0,
  104. 'name' => strval($item['name'] ?? ''),
  105. 'cover' => strval($item['cover'] ?? ''),
  106. 'originPrice' => floatval($item['originPrice'] ?? 0),
  107. 'specName' => strval($item['specName'] ?? ''),
  108. ];
  109. }
  110. $this->$attribute = $list;
  111. }
  112. /**
  113. * @return array
  114. */
  115. public function getBasePayload()
  116. {
  117. return [
  118. 'title' => trim(strval($this->title)),
  119. 'subtitle' => trim(strval($this->subtitle)),
  120. 'showCountdown' => !empty($this->showCountdown) ? 1 : 0,
  121. 'expand' => !empty($this->expand) ? 1 : 0,
  122. 'startTime' => intval($this->startTime),
  123. 'endTime' => intval($this->endTime),
  124. 'desc' => trim(strval($this->desc)),
  125. ];
  126. }
  127. /**
  128. * @return array
  129. */
  130. public function getGoods()
  131. {
  132. return is_array($this->goods) ? $this->goods : [];
  133. }
  134. /**
  135. * @return int
  136. */
  137. public function getEnabled()
  138. {
  139. return !empty($this->enabled) ? 1 : 0;
  140. }
  141. }