SaveSeckillForm.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace hd\models\homePageConfig;
  3. use hd\models\BaseForm;
  4. /**
  5. * 保存秒杀专区配置
  6. */
  7. class SaveSeckillForm 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. if ($goodsId <= 0) {
  78. $this->addError($attribute, "请选择第{$no}个秒杀商品");
  79. return;
  80. }
  81. if ($price <= 0) {
  82. $this->addError($attribute, "请填写第{$no}个秒杀价格");
  83. return;
  84. }
  85. if ($stock <= 0) {
  86. $this->addError($attribute, "请填写第{$no}个秒杀库存");
  87. return;
  88. }
  89. if ($limit <= 0) {
  90. $this->addError($attribute, "请填写第{$no}个单人限购");
  91. return;
  92. }
  93. $list[] = [
  94. 'goodsId' => $goodsId,
  95. 'price' => $price,
  96. 'stock' => $stock,
  97. 'limit' => $limit,
  98. 'status' => !empty($item['status']) ? 1 : 0,
  99. 'name' => strval($item['name'] ?? ''),
  100. 'cover' => strval($item['cover'] ?? ''),
  101. 'originPrice' => floatval($item['originPrice'] ?? 0),
  102. ];
  103. }
  104. $this->$attribute = $list;
  105. }
  106. /**
  107. * 活动基础字段
  108. * @return array
  109. */
  110. public function getBasePayload()
  111. {
  112. return [
  113. 'title' => trim(strval($this->title)),
  114. 'subtitle' => trim(strval($this->subtitle)),
  115. 'showCountdown' => !empty($this->showCountdown) ? 1 : 0,
  116. 'expand' => !empty($this->expand) ? 1 : 0,
  117. 'startTime' => intval($this->startTime),
  118. 'endTime' => intval($this->endTime),
  119. 'desc' => trim(strval($this->desc)),
  120. ];
  121. }
  122. /**
  123. * @return array
  124. */
  125. public function getGoods()
  126. {
  127. return is_array($this->goods) ? $this->goods : [];
  128. }
  129. /**
  130. * @return int
  131. */
  132. public function getEnabled()
  133. {
  134. return !empty($this->enabled) ? 1 : 0;
  135. }
  136. }