瀏覽代碼

背包礼物一次赠送多个

lizhen_gitee 1 年之前
父節點
當前提交
4761fd5eae

+ 63 - 1
application/admin/controller/gift/Back.php

@@ -3,7 +3,7 @@
 namespace app\admin\controller\gift;
 
 use app\common\controller\Backend;
-
+use think\Db;
 /**
  * 礼物背包
  *
@@ -77,4 +77,66 @@ class Back extends Backend
         return $this->view->fetch();
     }
 
+    /**
+     * 添加
+     */
+    public function add()
+    {
+        if ($this->request->isPost()) {
+            $params = $this->request->post("row/a");
+            if ($params) {
+                $params = $this->preExcludeFields($params);
+
+                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
+                    $params[$this->dataLimitField] = $this->auth->id;
+                }
+                $result = false;
+                Db::startTrans();
+                try {
+                    //是否采用模型验证
+                    if ($this->modelValidate) {
+                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
+                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
+                        $this->model->validateFailException(true)->validate($validate);
+                    }
+                    //多加几个
+                    $number = intval(input('number',1));
+                    if($number < 1) {$number = 1;}
+
+                    $gift = Db::name('gift')->where('id',$params['gift_id'])->find();
+                    $params['name']      = $gift['name'];
+                    $params['image']     = $gift['image'];
+                    $params['gif_image'] = $gift['special'];
+                    $params['value']     = $gift['price'];
+                    $params['number']    = 1;
+                    $params['is_use']    = 0;
+
+                    $saveall = [];
+                    for($i=1;$i<=$number;$i++){
+                        $saveall[] = $params;
+                    }
+                    $result = Db::name('gift_back')->insertAll($saveall);
+
+                    Db::commit();
+                } catch (ValidateException $e) {
+                    Db::rollback();
+                    $this->error($e->getMessage());
+                } catch (PDOException $e) {
+                    Db::rollback();
+                    $this->error($e->getMessage());
+                } catch (Exception $e) {
+                    Db::rollback();
+                    $this->error($e->getMessage());
+                }
+                if ($result !== false) {
+                    $this->success();
+                } else {
+                    $this->error(__('No rows were inserted'));
+                }
+            }
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        return $this->view->fetch();
+    }
+
 }

+ 0 - 11
application/admin/model/gift/Back.php

@@ -32,17 +32,6 @@ class Back extends Model
 
     protected static function init()
     {
-        self::beforeInsert(function ($row) {
-            $gift = Db::name('gift')->where('id',$row['gift_id'])->find();
-            $row['name']      = $gift['name'];
-            $row['image']     = $gift['image'];
-            $row['gif_image'] = $gift['special'];
-            $row['value']     = $gift['price'];
-            $row['number']    = 1;
-            $row['is_use']    = 0;
-        });
-
-
         self::beforeUpdate(function ($row) {
             $gift = Db::name('gift')->where('id',$row['gift_id'])->find();
             $row['name']      = $gift['name'];

+ 3 - 3
application/admin/view/gift/back/add.html

@@ -52,13 +52,13 @@
         <div class="col-xs-12 col-sm-8">
             <input id="c-value" data-rule="required" readonly class="form-control" name="row[value]" type="number" value="0">
         </div>
-    </div>
+    </div>-->
     <div class="form-group">
         <label class="control-label col-xs-12 col-sm-2">{:__('Number')}:</label>
         <div class="col-xs-12 col-sm-8">
-            <input id="c-number" class="form-control" name="row[number]" type="number" value="1" disabled>
+            <input id="c-number" class="form-control" name="number" type="number" min="1" value="1">
         </div>
-    </div>-->
+    </div>
     <!--<div class="form-group">
         <label class="control-label col-xs-12 col-sm-2">{:__('Is_use')}:</label>
         <div class="col-xs-12 col-sm-8">

+ 2 - 2
public/assets/js/backend/gift/back.js

@@ -33,8 +33,8 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                         {field: 'name', title: __('name')},
                         {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                         {field: 'value', title: __('Value')},
-                        {field: 'is_use', title: __('Is_use'), searchList: {"1":__('Is_use 1'),"0":__('Is_use 0')}, formatter: Table.api.formatter.normal},
-                        {field: 'use_time', title: __('Use_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
+//                        {field: 'is_use', title: __('Is_use'), searchList: {"1":__('Is_use 1'),"0":__('Is_use 0')}, formatter: Table.api.formatter.normal},
+//                        {field: 'use_time', title: __('Use_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                         {field: 'get_way', title: __('Get_way'), searchList: Config.getWayList, formatter: Table.api.formatter.normal},
                         {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                         {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}