Bläddra i källkod

红包相关表的表单验证修改与新增实现

shizhongqi 7 månader sedan
förälder
incheckning
9baa34890a

+ 18 - 3
app-hd/controllers/HbController.php

@@ -11,6 +11,7 @@ use bizHd\recharge\classes\RechargeShbClass;
 use common\components\util;
 use bizHd\hb\classes\HbClass;
 use hd\models\hb\CreateHbForm;
+use hd\models\hb\SendHbRuleItemForm;
 use hd\models\hb\UpdateHbForm;
 use Yii;
 
@@ -107,7 +108,7 @@ class HbController extends BaseController
         }
         $post = $form->attributes;
 
-        $duration = intval($post['days']);
+        $duration = (int)$form->days;
         $count = $post['count'] ?? 1;
         $beginTime = time();
         $endTime = $beginTime + $duration * 86400;
@@ -206,8 +207,22 @@ class HbController extends BaseController
     public function actionSendHbRules()
     {
         $data = Yii::$app->request->post();
-        //var_dump($data); die;
-        $hbRules = RechargeShbClass::setRules($this->shopId, $this->mainId, $data);
+
+        $validatedRows = [];
+        foreach ($data as $item) {
+            if (!is_array($item)) {
+                util::fail('参数格式错误');
+                return;
+            }
+            $form = new SendHbRuleItemForm();
+            $form->load($item, '');
+            if (!$form->validateForm()) {
+                return;
+            }
+            $validatedRows[] = $form->attributes;
+        }
+
+        $hbRules = RechargeShbClass::setRules($this->shopId, $this->mainId, $validatedRows);
         util::success($hbRules);
     }
 

+ 25 - 12
app-hd/models/hb/CreateHbForm.php

@@ -15,10 +15,10 @@ class CreateHbForm extends BaseForm
     /** @var int */
     public $customId;
 
-    /** @var float */
+    /** @var int */
     public $amount;
 
-    /** @var float */
+    /** @var int */
     public $minConsume;
 
     /** @var int 有效天数 */
@@ -45,18 +45,31 @@ class CreateHbForm extends BaseForm
             [['count'], 'default', 'value' => 1],
             [['remark'], 'default', 'value' => ''],
 
-            [['userId', 'customId', 'days', 'count'], 'filter', 'filter' => 'intval'],
-            [['amount', 'minConsume'], 'filter', 'filter' => 'floatval'],
+            // 注意:不要对 'userId', 'customId', 'days' 使用 intval 过滤;否则非数字(如 "ggg")会被转成 0,导致校验绕过
+            //[['userId', 'customId', 'days'], 'filter', 'filter' => 'intval'],
+            [['amount', 'minConsume'], 'number', 'min' => 0.01, 'message' => '{attribute}必须大于等于0.01'],
+            [['userId', 'customId'], 'integer', 'min' => 1, 'tooSmall' => '{attribute}参数错误'],
+            [['days'], 'integer', 'min' => 0, 'max' => 3650, 'tooSmall' => '{attribute}必须大于等于0', 'tooBig' => '{attribute}过大'],
+            [['count'], 'integer', 'min' => 1, 'max' => 1000, 'tooSmall' => '{attribute}必须大于等于1', 'tooBig' => '{attribute}过大'],
+            [['remark'], 'string'],
             [['remark'], 'filter', 'filter' => 'trim'],
+        ];
+    }
 
-            [['userId', 'customId'], 'integer', 'min' => 1, 'tooSmall' => '参数错误'],
-            [['days'], 'integer', 'min' => 1, 'max' => 3650, 'tooSmall' => '有效天数必须大于0', 'tooBig' => '有效天数过大'],
-            [['count'], 'integer', 'min' => 1, 'max' => 1000, 'tooSmall' => '发放张数必须大于0', 'tooBig' => '发放张数过大'],
-
-            [['amount'], 'number', 'min' => 1, 'tooSmall' => '红包金额必须大于0'],
-            [['minConsume'], 'number', 'min' => 0, 'tooSmall' => '最低消费金额不能小于0'],
-
-            [['remark'], 'string'],
+    /**
+     * 获取属性标签
+     * @return array
+     */
+    public function attributeLabels()
+    {
+        return [
+            'userId' => '用户ID',
+            'customId' => '客户ID',
+            'amount' => '红包金额',
+            'minConsume' => '最低消费',
+            'days' => '有效时长',
+            'count' => '红包个数',
+            'remark' => '备注信息',
         ];
     }
 }

+ 100 - 0
app-hd/models/hb/SendHbRuleItemForm.php

@@ -0,0 +1,100 @@
+<?php
+
+namespace hd\models\hb;
+
+use hd\models\BaseForm;
+
+/**
+ * 充值送红包规则项表单模型
+ */
+class SendHbRuleItemForm extends BaseForm
+{
+    /** @var int|string 规则ID:新增传空字符串,更新传正整数 */
+    public $id;
+
+    /** @var int 充值金额 */
+    public $amount;
+
+    /** @var int 红包金额 */
+    public $hbAmount;
+
+    /** @var int 红包张数 */
+    public $num;
+
+    /** @var int 最低消费金额(0 表示不限制) */
+    public $miniCost;
+
+    /** @var int 有效天数(0 表示永不过期) */
+    public $duration;
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id'], 'default', 'value' => ''],
+            [['num'], 'default', 'value' => 1],
+            [['miniCost', 'duration'], 'default', 'value' => 0],
+
+            [['amount'], 'required', 'message' => '请填写充值金额'],
+            [['hbAmount'], 'required', 'message' => '请填写红包金额'],
+
+            // 注意:不要对字段做 intval 过滤,避免 "abc" 被转成 0 导致校验绕过
+            [['amount', 'hbAmount'], 'number', 'min' => 0.01, 'tooSmall' => '{attribute}必须大于等于0.01'],
+            [['num'], 'integer', 'message' => '{attribute}必须为正整数', 'min' => 1, 'tooSmall' => '{attribute}最小不能小于1'],
+            [['miniCost'], 'integer', 'message' => '{attribute}必须为正整数', 'min' => 0, 'tooSmall' => '{attribute}不能小于0'],
+            [['duration'], 'integer', 'min' => 0, 'max' => 3650, 'tooSmall' => '{attribute}不能小于0', 'tooBig' => '{attribute}过大'],
+
+            [['id'], 'validateId'],
+        ];
+    }
+
+    /**
+         * 新增时 id 允许为空字符串;更新时必须为正整数。
+         *
+     * @param string $attribute
+     */
+    public function validateId($attribute)
+    {
+        $value = $this->$attribute;
+        if ($value === '' || $value === null) {
+            return;
+        }
+
+        if (is_int($value)) {
+            if ($value < 1) {
+                $this->addError($attribute, '规则ID参数错误');
+            }
+            return;
+        }
+
+        if (is_string($value) && ctype_digit($value)) {
+            $intVal = (int)$value;
+            if ($intVal < 1) {
+                $this->addError($attribute, '规则ID参数错误');
+                return;
+            }
+            $this->$attribute = $intVal;
+            return;
+        }
+
+        $this->addError($attribute, '规则ID参数错误');
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => '规则ID',
+            'amount' => '充值金额',
+            'hbAmount' => '红包金额',
+            'num' => '红包张数',
+            'miniCost' => '最低消费金额',
+            'duration' => '有效天数',
+        ];
+    }
+}
+

+ 23 - 7
app-hd/models/hb/UpdateHbForm.php

@@ -47,21 +47,37 @@ class UpdateHbForm extends BaseForm
             [['id'], 'required', 'message' => '红包ID不能为空'],
 
             [['remark'], 'default', 'value' => ''],
-
-            [['id', 'status', 'beginTime', 'endTime', 'willTime', 'duration'], 'filter', 'filter' => 'intval'],
-            [['amount', 'minConsume'], 'filter', 'filter' => 'floatval'],
             [['remark'], 'filter', 'filter' => 'trim'],
 
             [['id'], 'integer', 'min' => 1, 'tooSmall' => '红包ID参数错误'],
             [['status'], 'in', 'range' => [-1, 0, 1], 'message' => '红包状态不正确'],
-            [['beginTime', 'endTime', 'willTime'], 'integer', 'min' => 0, 'tooSmall' => '时间参数不正确'],
-            [['duration'], 'integer', 'min' => 0, 'max' => 3650, 'tooSmall' => '有效天数必须大于0', 'tooBig' => '有效天数过大'],
+            [['beginTime', 'endTime', 'willTime'], 'integer', 'min' => 0, 'tooSmall' => '{attribute}必须大于等于0'],
+            [['duration'], 'integer', 'min' => 0, 'max' => 3650, 'tooSmall' => '有效天数必须大于等于0', 'tooBig' => '有效天数过大'],
 
-            [['amount'], 'number', 'min' => 0, 'tooSmall' => '红包金额不能小于0'],
-            [['minConsume'], 'number', 'min' => 0, 'tooSmall' => '最低消费金额不能小于0'],
+            [['amount'], 'number', 'min' => 0.01, 'tooSmall' => '{attribute}不能小于0.01'],
+            [['minConsume'], 'number', 'min' => 0.00, 'tooSmall' => '{attribute}不能小于0'],
 
             [['remark'], 'string'],
         ];
     }
+
+     /**
+     * 获取属性标签
+     * @return array
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => '红包ID',
+            'status' => '红包状态',
+            'amount' => '红包金额',
+            'minConsume' => '最低消费金额',
+            'beginTime' => '生效时间',
+            'endTime' => '失效时间',
+            'willTime' => '预期失效时间',
+            'duration' => '有效天数',
+            'remark' => '备注',
+        ];
+    }
 }
 

+ 0 - 3
biz-hd/recharge/classes/RechargeShbClass.php

@@ -19,9 +19,6 @@ class RechargeShbClass extends BaseClass
      */
     public static function setRules($shopId, $mainId, $data)
     {
-        // 1. 删除旧规则
-        // self::deleteByCondition(['shopId' => $shopId]);
-
         if (empty($data)) {
             return [];
         }