Переглянути джерело

增加批量发放优惠券

15954078560 2 роки тому
батько
коміт
12e57bcb7c

+ 12 - 5
application/admin/controller/Couponuser.php

@@ -45,6 +45,7 @@ class Couponuser extends Backend
         if (!$coupon_id) {
             $coupon_id = input('ids', 0, 'intval'); //从优惠券列表第一次跳转过来
         }
+        $this->assign('coupon_id', $coupon_id);
         $this->assignconfig('coupon_id', $coupon_id);
 
         //当前是否为关联查询
@@ -81,7 +82,7 @@ class Couponuser extends Backend
      * 发放优惠券给用户
      */
     public function giveout() {
-        $ids = input('ids', 0, 'intval'); //用户id
+        $ids = input('ids', '', 'trim'); //用户id字符串
         $coupon_id = input('coupon_id', 0, 'intval'); //优惠券id
 
         if (!$ids || !$coupon_id) {
@@ -96,8 +97,9 @@ class Couponuser extends Backend
             $this->error('优惠券类型错误');
         }
 
+        $ids = explode(',', $ids);
         $coupon_data = [
-            'user_id' => $ids,
+//            'user_id' => $ids,
             'coupon_id' => $info['id'],
             'title' => $info['title'],
             'desc' => $info['desc'],
@@ -110,9 +112,14 @@ class Couponuser extends Backend
             'createtime' => time()
         ];
 
-        $coupon_rs = Db::name('user_coupon')->insertGetId($coupon_data);
-        if (!$coupon_rs) {
-            $this->error('发放失败');
+        $user_coupon = Db::name('user_coupon');
+        foreach ($ids as &$v) {
+            $coupon_data['user_id'] = $v;
+
+            $coupon_rs = $user_coupon->insertGetId($coupon_data);
+//            if (!$coupon_rs) {
+//                $this->error('发放失败');
+//            }
         }
 
         $this->success('发放成功');

+ 2 - 0
application/admin/view/couponuser/index.html

@@ -17,6 +17,8 @@
                 <div class="widget-body no-padding">
                     <div id="toolbar" class="toolbar">
                         <a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a>
+                        <a class="btn btn-success btn-disabled disabled btn-selected" data-url="couponuser/giveout/coupon_id/{$coupon_id}" href="javascript:;"><i class="fa fa-pencil"></i> 批量发放</a>
+
                         <!--<a href="javascript:;" class="btn btn-success btn-add {:$auth->check('couponuser/add')?'':'hide'}" title="{:__('Add')}" ><i class="fa fa-plus"></i> {:__('Add')}</a>
                         <a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('couponuser/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-pencil"></i> {:__('Edit')}</a>
                         <a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled {:$auth->check('couponuser/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>

+ 15 - 1
public/assets/js/backend/couponuser.js

@@ -29,7 +29,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                 showExport: false,
                 columns: [
                     [
-                        {checkbox: true},
+                        {field: 'state', checkbox: true},
                         {field: 'id', title: __('Id')},
                         {field: 'username', title: __('Username'), operate: 'LIKE'},
                         {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
@@ -95,6 +95,17 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
 
             // 为表格绑定事件
             Table.api.bindevent(table);
+
+            // 获取选中项
+            $(document).on("click", ".btn-selected", function () {
+                var that = this;
+                var ids = Table.api.selectedids(table);
+                Layer.confirm('确认发放?', function (index) {
+                    // Layer.alert(JSON.stringify(Table.api.selectedids(table)));
+                    Table.api.multi('couponuser/giveout/coupon_id/' + Config.coupon_id, ids.join(","), table, that);
+                    Layer.close(index);
+                });
+            });
         },
         add: function () {
             Controller.api.bindevent();
@@ -102,6 +113,9 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
         edit: function () {
             Controller.api.bindevent();
         },
+        selected: function () {
+            Controller.api.bindevent();
+        },
         api: {
             bindevent: function () {
                 Form.api.bindevent($("form[role=form]"));