| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace hd\models\homePageConfig;
- use hd\models\BaseForm;
- /**
- * 保存秒杀专区配置
- */
- class SaveSeckillForm extends BaseForm
- {
- public $enabled;
- public $title;
- public $subtitle;
- public $showCountdown;
- public $expand;
- public $startTime;
- public $endTime;
- public $desc;
- /** @var array|string */
- public $goods;
- public function rules()
- {
- return [
- ['enabled', 'default', 'value' => 0],
- ['showCountdown', 'default', 'value' => 0],
- ['expand', 'default', 'value' => 0],
- ['goods', 'default', 'value' => []],
- [['title', 'subtitle', 'desc'], 'trim'],
- ['title', 'required', 'message' => '请输入活动标题'],
- ['title', 'string', 'max' => 10, 'tooLong' => '活动标题不能超过10字'],
- ['subtitle', 'required', 'message' => '请输入活动副标题'],
- ['subtitle', 'string', 'max' => 20, 'tooLong' => '活动副标题不能超过20字'],
- ['desc', 'required', 'message' => '请输入活动说明'],
- ['desc', 'string', 'max' => 500, 'tooLong' => '活动说明不能超过500字'],
- [['startTime', 'endTime'], 'required', 'message' => '请选择活动时间'],
- [['startTime', 'endTime'], 'integer', 'min' => 1, 'message' => '请选择活动时间', 'tooSmall' => '请选择活动时间'],
- ['endTime', 'compare', 'compareAttribute' => 'startTime', 'operator' => '>', 'message' => '结束时间必须大于开始时间'],
- [['enabled', 'showCountdown', 'expand'], 'filter', 'filter' => function ($value) {
- return !empty($value) ? 1 : 0;
- }],
- ['goods', 'validateGoods'],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'enabled' => '开关',
- 'title' => '活动标题',
- 'subtitle' => '活动副标题',
- 'showCountdown' => '倒计时',
- 'expand' => '商品展开',
- 'startTime' => '开始时间',
- 'endTime' => '结束时间',
- 'desc' => '活动说明',
- 'goods' => '秒杀商品',
- ];
- }
- public function validateGoods($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;
- }
- $list = [];
- foreach ($rawList as $index => $item) {
- if (!is_array($item)) {
- continue;
- }
- $no = $index + 1;
- $goodsId = intval($item['goodsId'] ?? 0);
- $price = floatval($item['price'] ?? 0);
- $stock = intval($item['stock'] ?? 0);
- $limit = intval($item['limit'] ?? 0);
- if ($goodsId <= 0) {
- $this->addError($attribute, "请选择第{$no}个秒杀商品");
- return;
- }
- if ($price <= 0) {
- $this->addError($attribute, "请填写第{$no}个秒杀价格");
- return;
- }
- if ($stock <= 0) {
- $this->addError($attribute, "请填写第{$no}个秒杀库存");
- return;
- }
- if ($limit <= 0) {
- $this->addError($attribute, "请填写第{$no}个单人限购");
- return;
- }
- $list[] = [
- 'goodsId' => $goodsId,
- 'price' => $price,
- 'stock' => $stock,
- 'limit' => $limit,
- 'status' => !empty($item['status']) ? 1 : 0,
- 'name' => strval($item['name'] ?? ''),
- 'cover' => strval($item['cover'] ?? ''),
- 'originPrice' => floatval($item['originPrice'] ?? 0),
- ];
- }
- $this->$attribute = $list;
- }
- /**
- * 活动基础字段
- * @return array
- */
- public function getBasePayload()
- {
- return [
- 'title' => trim(strval($this->title)),
- 'subtitle' => trim(strval($this->subtitle)),
- 'showCountdown' => !empty($this->showCountdown) ? 1 : 0,
- 'expand' => !empty($this->expand) ? 1 : 0,
- 'startTime' => intval($this->startTime),
- 'endTime' => intval($this->endTime),
- 'desc' => trim(strval($this->desc)),
- ];
- }
- /**
- * @return array
- */
- public function getGoods()
- {
- return is_array($this->goods) ? $this->goods : [];
- }
- /**
- * @return int
- */
- public function getEnabled()
- {
- return !empty($this->enabled) ? 1 : 0;
- }
- }
|