瀏覽代碼

1.红包功能补表单验证 2.订单中使用了红包要把红包记录并消耗掉

shizhongqi 6 月之前
父節點
當前提交
ca2578a9a9

+ 25 - 22
app-hd/controllers/HbController.php

@@ -10,6 +10,8 @@ use bizHd\hb\classes\HbManageClass;
 use bizHd\recharge\classes\RechargeShbClass;
 use common\components\util;
 use bizHd\hb\classes\HbClass;
+use hd\models\hb\CreateHbForm;
+use hd\models\hb\UpdateHbForm;
 use Yii;
 
 class HbController extends BaseController
@@ -98,14 +100,12 @@ class HbController extends BaseController
      */
     public function actionCreateHb()
     {
-        // TODO
-        // $form = new \hd\models\hb\CreateHbForm();
-        // $form->load(Yii::$app->request->post(), '');
-        // if (!$form->validateForm()) {
-        //     return;
-        // }
-        // $data = $form->attributes;
-        $post = Yii::$app->request->post();
+        $form = new CreateHbForm();
+        $form->load(Yii::$app->request->post(), '');
+        if (!$form->validateForm()) {
+            return;
+        }
+        $post = $form->attributes;
 
         $duration = intval($post['days']);
         $count = $post['count'] ?? 1;
@@ -124,7 +124,7 @@ class HbController extends BaseController
             'willTime' => $endTime,
             'duration' => $duration,
             'getType' => 1, //取得方式 0新人领取 1门店发放 2充值赠送
-            'remark' => $post['remark'],
+            'remark' => $post['remark'] ?? '',
         ];
 
         $manageData = [
@@ -152,16 +152,13 @@ class HbController extends BaseController
      */
     public function actionUpdateHb()
     {
-        // TODO
-        // $hbId = Yii::$app->request->get('hbId');
-        // $form = new \hd\models\hb\UpdateHbForm();
-        // $form->load(Yii::$app->request->post(), '');
-        // if (!$form->validateForm()) {
-        //     return;
-        // }
-        // $data = $form->attributes;
-        $data = Yii::$app->request->post();
-        $id = $data['id'];
+        $rawPost = Yii::$app->request->post();
+        $form = new UpdateHbForm();
+        $form->load($rawPost, '');
+        if (!$form->validateForm()) {
+            return;
+        }
+        $id = (int)$form->id;
         $hb = HbClass::getById($id, true);
 
         //检测是否是本门店的红包
@@ -169,8 +166,15 @@ class HbController extends BaseController
             util::fail('不是本门店的红包');
         }
 
-        foreach($data as $key => $val){
-            $hb->$key = $val;
+        // 仅更新允许字段,且只更新请求中实际传入的字段(避免用 null 覆盖原值)
+        $allowFields = array_keys($form->attributes);
+        foreach ($allowFields as $field) {
+            if ($field === 'id') {
+                continue;
+            }
+            if (array_key_exists($field, $rawPost)) {
+                $hb->$field = $form->$field;
+            }
         }
         // 当操作作废时,status改为-1,同时将endTime改为当前时间
         if($hb->status == -1){
@@ -213,7 +217,6 @@ class HbController extends BaseController
     public function actionDeleteHbRule()
     {
         $id = Yii::$app->request->post('id');
-        var_dump($id); die;
         $re = RechargeShbClass::deleteById($id);
         if($re == 1){
             util::complete();

+ 18 - 0
app-hd/controllers/OrderController.php

@@ -2,6 +2,7 @@
 
 namespace hd\controllers;
 
+use bizHd\hb\classes\HbClass;
 use Yii;
 use biz\shop\classes\ShopExtClass;
 use biz\sj\services\MerchantExtendService;
@@ -813,6 +814,23 @@ class OrderController extends BaseController
                 $order->save();
             }
 
+            //红包使用
+            $hbId = $post['hbId'] ?? 0;
+            if ($hbId != 0) {
+                $hb = HbClass::getById($hbId, true);
+                if (!empty($hb)) {
+                    if ($hb->status == 1 || $hb->status == -1) {
+                        util::fail($hb->status == 1 ? '红包已使用' : '红包已作废');
+                    }
+                    $hb->status = 1;
+                    $hb->orderId = $id;
+                    $hb->save();
+                }else{
+                    Yii::error('没有找到红包,hbId: ', $hbId . ' ' . __METHOD__);
+                    util::fail('没有找到红包,hbId: ' . $hbId);
+                }
+            }
+
             $transaction->commit();
 
             if ($order->fromType == 3 || $order->fromType == 4) {

+ 1 - 7
app-hd/controllers/RefundController.php

@@ -268,13 +268,7 @@ class RefundController extends BaseController
             $refundHb = $post['refundHb'] ?? 0;
             $hbId = $order->hbId;
             if ($refundHb == 1 && $hbId != 0 && bccomp($post['price'], $order->orderPrice, 2) == 0) {
-                $hb = HbClass::getById($hbId, true);
-                if (empty($hb)) {
-                    util::fail('没有找到红包');
-                }
-                $hb->status = 0;
-                $hb->orderId = 0;
-                $hb->save();
+                hbClass::hbBack($order['hbId']);
             }
 
             //制作单取消通知

+ 63 - 0
app-hd/models/hb/CreateHbForm.php

@@ -0,0 +1,63 @@
+<?php
+
+namespace hd\models\hb;
+
+use hd\models\BaseForm;
+
+/**
+ * 创建红包表单模型
+ */
+class CreateHbForm extends BaseForm
+{
+    /** @var int */
+    public $userId;
+
+    /** @var int */
+    public $customId;
+
+    /** @var float */
+    public $amount;
+
+    /** @var float */
+    public $minConsume;
+
+    /** @var int 有效天数 */
+    public $days;
+
+    /** @var int 发放张数 */
+    public $count;
+
+    /** @var string 备注 */
+    public $remark;
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['userId'], 'required', 'message' => '请选择用户'],
+            [['customId'], 'required', 'message' => '请选择客户'],
+            [['amount'], 'required', 'message' => '请填写红包金额'],
+            [['minConsume'], 'required', 'message' => '请填写最低消费金额'],
+            [['days'], 'required', 'message' => '请填写有效天数'],
+
+            [['count'], 'default', 'value' => 1],
+            [['remark'], 'default', 'value' => ''],
+
+            [['userId', 'customId', 'days', 'count'], 'filter', 'filter' => 'intval'],
+            [['amount', 'minConsume'], 'filter', 'filter' => 'floatval'],
+            [['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'],
+        ];
+    }
+}
+

+ 67 - 0
app-hd/models/hb/UpdateHbForm.php

@@ -0,0 +1,67 @@
+<?php
+
+namespace hd\models\hb;
+
+use hd\models\BaseForm;
+
+/**
+ * 更新红包表单模型
+ *
+ * 仅暴露允许更新的字段,避免被恶意修改 shopId/mainId 等敏感字段。
+ */
+class UpdateHbForm extends BaseForm
+{
+    /** @var int 红包ID */
+    public $id;
+
+    /** @var int 红包状态(-1 作废,0 未使用,1 已使用) */
+    public $status;
+
+    /** @var float 红包金额 */
+    public $amount;
+
+    /** @var float 最低消费金额 */
+    public $minConsume;
+
+    /** @var int 生效时间 */
+    public $beginTime;
+
+    /** @var int 失效时间 */
+    public $endTime;
+
+    /** @var int 预期失效时间 */
+    public $willTime;
+
+    /** @var int 有效天数 */
+    public $duration;
+
+    /** @var string 备注 */
+    public $remark;
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['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' => '有效天数过大'],
+
+            [['amount'], 'number', 'min' => 0, 'tooSmall' => '红包金额不能小于0'],
+            [['minConsume'], 'number', 'min' => 0, 'tooSmall' => '最低消费金额不能小于0'],
+
+            [['remark'], 'string'],
+        ];
+    }
+}
+

+ 17 - 0
app-mall/controllers/OrderController.php

@@ -7,6 +7,7 @@ use biz\shop\classes\ShopClass;
 use biz\wx\classes\WxMessageClass;
 use bizHd\custom\classes\HdClass;
 use bizHd\express\classes\HdDeliveryOrderClass;
+use bizHd\hb\classes\HbClass;
 use bizHd\order\classes\ScanPayClass;
 use bizHd\wx\classes\WxOpenClass;
 use bizMall\custom\classes\CustomClass;
@@ -429,6 +430,22 @@ class OrderController extends BaseController
                     util::fail('本店暂不能使用跑腿呢');
                 }
             }
+            //红包使用
+            $hbId = $post['hbId'] ?? 0;
+            if ($hbId != 0) {
+                $hb = HbClass::getById($hbId, true);
+                if (!empty($hb)) {
+                    if ($hb->status == 1 || $hb->status == -1) {
+                        util::fail($hb->status == 1 ? '红包已使用' : '红包已作废');
+                    }
+                    $hb->status = 1;
+                    $hb->orderId = $return->id;
+                    $hb->save();
+                }else{
+                    Yii::error('没有找到红包,hbId', $hbId . ' ' . __METHOD__);
+                    util::fail('没有找到红包,hbId: ' . $hbId);
+                }
+            }
             $transaction->commit();
             
             $orderSn = $return->orderSn ?? '';