From b1f7bc97fa7f7be33dbb0c336693a2fac05ce361 Mon Sep 17 00:00:00 2001 From: macbook <418170129@qq.com> Date: Mon, 18 Jan 2021 00:35:50 +0800 Subject: [PATCH] 开通会员赠送优惠券 --- addons/shopro/model/Coupons.php | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------ application/admin/controller/package/Coupons.php | 193 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ application/admin/lang/zh-cn/package/coupons.php | 12 ++++++++++++ application/admin/model/package/Coupons.php | 44 ++++++++++++++++++++++++++++++++++++++++++++ application/admin/validate/package/Coupons.php | 27 +++++++++++++++++++++++++++ application/admin/view/package/coupons/add.html | 34 ++++++++++++++++++++++++++++++++++ application/admin/view/package/coupons/edit.html | 34 ++++++++++++++++++++++++++++++++++ application/admin/view/package/coupons/index.html | 26 ++++++++++++++++++++++++++ application/api/controller/PackageOrder.php | 19 +++++++++++++++++++ public/assets/js/backend/package/coupons.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ public/assets/js/backend/package/package.js | 21 +++++++++++++++++++-- 11 files changed, 522 insertions(+), 26 deletions(-) create mode 100644 application/admin/controller/package/Coupons.php create mode 100644 application/admin/lang/zh-cn/package/coupons.php create mode 100644 application/admin/model/package/Coupons.php create mode 100644 application/admin/validate/package/Coupons.php create mode 100644 application/admin/view/package/coupons/add.html create mode 100644 application/admin/view/package/coupons/edit.html create mode 100644 application/admin/view/package/coupons/index.html create mode 100644 public/assets/js/backend/package/coupons.js diff --git a/addons/shopro/model/Coupons.php b/addons/shopro/model/Coupons.php index 448d825..d025c9a 100644 --- a/addons/shopro/model/Coupons.php +++ b/addons/shopro/model/Coupons.php @@ -41,7 +41,7 @@ class Coupons extends Model if (!$coupon) { throw new Exception('未找到优惠券'); } - + if ($coupon['gettimestart'] > time() || $coupon['gettimeend'] < time()) { throw new Exception('优惠券领取已结束'); } @@ -85,11 +85,11 @@ class Coupons extends Model $coupon = self::get($id); // 查询并返回用户的优惠券状态 - if ($coupon && $user_coupons_id && $user) { + if ($coupon && $user_coupons_id && $user) { $userCoupons = UserCoupons::where('id', $user_coupons_id) ->where('coupons_id', $coupon->id) ->where('user_id', $user->id)->find(); - + if ($userCoupons) { $coupon->user_coupons_id = $userCoupons->id; @@ -97,17 +97,30 @@ class Coupons extends Model $coupon->status_code = 'used'; $coupon->status_name = '已使用'; } else { - if ($coupon->usetimestart <= time() && $coupon->usetimeend >= time()) { - $coupon->status_code = 'no_use'; - $coupon->status_name = '未使用'; - } else if ($coupon->usetimeend <= time()) { - $coupon->status_code = 'expired'; - $coupon->status_name = '已过期'; - } else { - // 未到使用日期 - $coupon->status_code = 'no_can_use'; - $coupon->status_name = '不可使用'; + //开通会员后赠送的优惠券 + if($userCoupons->package_coupons_time > 0){ + if ($userCoupons->package_coupons_time >= time()) { + $coupon->status_code = 'no_use'; + $coupon->status_name = '未使用'; + } else { + $coupon->status_code = 'expired'; + $coupon->status_name = '已过期'; + } + }else{ + //正常领取的优惠券 + if ($coupon->usetimestart <= time() && $coupon->usetimeend >= time()) { + $coupon->status_code = 'no_use'; + $coupon->status_name = '未使用'; + } else if ($coupon->usetimeend <= time()) { + $coupon->status_code = 'expired'; + $coupon->status_name = '已过期'; + } else { + // 未到使用日期 + $coupon->status_code = 'no_can_use'; + $coupon->status_name = '不可使用'; + } } + } } } @@ -138,14 +151,25 @@ class Coupons extends Model $userCoupons = UserCoupons::where(['user_id' => $user->id,'usetime' => null])->select(); foreach ($userCoupons as $u) { $coupon = self::get($u->coupons_id); - if ($coupon && $coupon->usetimestart <= time() && $coupon->usetimeend >= time()) { - $coupon->user_coupons_id = $u->id; - $coupon->status_code = 'no_use'; - $coupon->status_name = '未使用'; - $couponsList[] = $coupon; + if ($coupon) { + if($u->package_coupons_time > 0){ + // 开通会员后赠送的优惠券 + if($u->package_coupons_time > time()){ + $coupon->user_coupons_id = $u->id; + $coupon->status_code = 'no_use'; + $coupon->status_name = '未使用'; + $couponsList[] = $coupon; + } + }else if($coupon->usetimestart <= time() && $coupon->usetimeend >= time()){ + // 正常领取的优惠券 + $coupon->user_coupons_id = $u->id; + $coupon->status_code = 'no_use'; + $coupon->status_name = '未使用'; + $couponsList[] = $coupon; + } } } - + break; case self::COUPONS_USED: $userCoupons = UserCoupons::where('user_id', $user->id)->where('usetime', 'not null')->select(); @@ -163,11 +187,22 @@ class Coupons extends Model $userCoupons = UserCoupons::where(['user_id' => $user->id,'usetime' => null])->select(); foreach ($userCoupons as $u) { $coupon = self::get($u->coupons_id); - if ($coupon && $coupon->usetimeend <= time()) { - $coupon->user_coupons_id = $u->id; - $coupon->status_code = 'expired'; - $coupon->status_name = '已过期'; - $couponsList[] = $coupon; + if($coupon){ + if($u->package_coupons_time > 0){ + // 开通会员后赠送的优惠券 + if($u->package_coupons_time <= time()){ + $coupon->user_coupons_id = $u->id; + $coupon->status_code = 'expired'; + $coupon->status_name = '已过期'; + $couponsList[] = $coupon; + } + }else if($coupon->usetimeend <= time()){ + // 正常领取的优惠券 + $coupon->user_coupons_id = $u->id; + $coupon->status_code = 'expired'; + $coupon->status_name = '已过期'; + $couponsList[] = $coupon; + } } } break; diff --git a/application/admin/controller/package/Coupons.php b/application/admin/controller/package/Coupons.php new file mode 100644 index 0000000..b55f6de --- /dev/null +++ b/application/admin/controller/package/Coupons.php @@ -0,0 +1,193 @@ +<?php + +namespace app\admin\controller\package; + +use app\common\controller\Backend; +use think\Db; + +/** + * 会员套餐优惠券管理 + * + * @icon fa fa-circle-o + */ +class Coupons extends Backend +{ + + /** + * Coupons模型对象 + * @var \app\admin\model\package\Coupons + */ + protected $model = null; + + public function _initialize() + { + parent::_initialize(); + $this->model = new \app\admin\model\package\Coupons; + + } + + public function import() + { + parent::import(); + } + + /** + * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 + * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 + * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 + */ + + + /** + * 查看 + */ + public function index() + { + //当前是否为关联查询 + $this->relationSearch = true; + //设置过滤方法 + $this->request->filter(['strip_tags', 'trim']); + if ($this->request->isAjax()) { + //如果发送的来源是Selectpage,则转发到Selectpage + if ($this->request->request('keyField')) { + return $this->selectpage(); + } + list($where, $sort, $order, $offset, $limit) = $this->buildparams(); + + $list = $this->model + ->with(['coupon']) + ->where('package_id',$this->request->request('package_id')) + ->where($where) + ->order($sort, $order) + ->paginate($limit); + + foreach ($list as $row) { + $row->visible(['id','give_num','expire_days','createtime','updatetime']); + $row->visible(['coupon']); + $row->getRelation('coupon')->visible(['name']); + } + + $result = array("total" => $list->total(), "rows" => $list->items()); + + return json($result); + } + return $this->view->fetch(); + } + + /** + * 添加 + */ + public function add() + { + $package_id = $this->request->request('package_id'); + 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; + } + + $package_coupons = $this->model + ->where('coupons_id',$params['coupons_id']) + ->where('package_id',$package_id) + ->field('id') + ->find(); + !empty($package_coupons) && $this->error('该优惠券已被设置'); + $params['package_id'] = $package_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); + } + $result = $this->model->allowField(true)->save($params); + 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(); + } + + /** + * 编辑 + */ + public function edit($ids = null) + { + $row = $this->model->get($ids); + if (!$row) { + $this->error(__('No Results were found')); + } + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds)) { + if (!in_array($row[$this->dataLimitField], $adminIds)) { + $this->error(__('You have no permission')); + } + } + if ($this->request->isPost()) { + $params = $this->request->post("row/a"); + if ($params) { + $params = $this->preExcludeFields($params); + + $package_coupons = $this->model + ->where('coupons_id',$params['coupons_id']) + ->where('id','neq',$row['id']) + ->where('package_id',$row['package_id']) + ->field('id') + ->find(); + !empty($package_coupons) && $this->error('该优惠券已被设置'); + + $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 . '.edit' : $name) : $this->modelValidate; + $row->validateFailException(true)->validate($validate); + } + $result = $row->allowField(true)->save($params); + 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 updated')); + } + } + $this->error(__('Parameter %s can not be empty', '')); + } + $this->view->assign("row", $row); + return $this->view->fetch(); + } + +} diff --git a/application/admin/lang/zh-cn/package/coupons.php b/application/admin/lang/zh-cn/package/coupons.php new file mode 100644 index 0000000..8b78ce7 --- /dev/null +++ b/application/admin/lang/zh-cn/package/coupons.php @@ -0,0 +1,12 @@ +<?php + +return [ + 'Id' => 'ID', + 'Package_id' => '会员包ID', + 'Coupons_id' => '优惠券ID', + 'Give_num' => '赠送数量', + 'Expire_days' => '有效期(天)', + 'Createtime' => '创建时间', + 'Updatetime' => '更新时间', + 'Coupon.name' => '优惠券' +]; diff --git a/application/admin/model/package/Coupons.php b/application/admin/model/package/Coupons.php new file mode 100644 index 0000000..87cd8e7 --- /dev/null +++ b/application/admin/model/package/Coupons.php @@ -0,0 +1,44 @@ +<?php + +namespace app\admin\model\package; + +use think\Model; + + +class Coupons extends Model +{ + + + + + + // 表名 + protected $name = 'package_coupons'; + + // 自动写入时间戳字段 + protected $autoWriteTimestamp = 'int'; + + // 定义时间戳字段名 + protected $createTime = 'createtime'; + protected $updateTime = 'updatetime'; + protected $deleteTime = false; + + // 追加属性 + protected $append = [ + + ]; + + + + + + + + + + + public function coupon() + { + return $this->belongsTo('app\admin\model\shopro\Coupons', 'coupons_id', 'id', [], 'LEFT')->setEagerlyType(0); + } +} diff --git a/application/admin/validate/package/Coupons.php b/application/admin/validate/package/Coupons.php new file mode 100644 index 0000000..47abfb2 --- /dev/null +++ b/application/admin/validate/package/Coupons.php @@ -0,0 +1,27 @@ +<?php + +namespace app\admin\validate\package; + +use think\Validate; + +class Coupons extends Validate +{ + /** + * 验证规则 + */ + protected $rule = [ + ]; + /** + * 提示消息 + */ + protected $message = [ + ]; + /** + * 验证场景 + */ + protected $scene = [ + 'add' => [], + 'edit' => [], + ]; + +} diff --git a/application/admin/view/package/coupons/add.html b/application/admin/view/package/coupons/add.html new file mode 100644 index 0000000..65d3aaf --- /dev/null +++ b/application/admin/view/package/coupons/add.html @@ -0,0 +1,34 @@ +<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action=""> + + <!--<<div class="form-group"> + <label class="control-label col-xs-12 col-sm-2">{:__('Package_id')}:</label> + <div class="col-xs-12 col-sm-8"> + <input id="c-package_id" data-rule="required" data-source="package/index" class="form-control selectpage" name="row[package_id]" type="text" value=""> + </div> + </div>--> + <div class="form-group"> + <label class="control-label col-xs-12 col-sm-2">{:__('Coupons_id')}:</label> + <div class="col-xs-12 col-sm-8"> + <input id="c-coupons_id" data-rule="required" data-source="shopro/coupons/index" class="form-control selectpage" name="row[coupons_id]" type="text" value=""> + </div> + </div> + <div class="form-group"> + <label class="control-label col-xs-12 col-sm-2">{:__('Give_num')}:</label> + <div class="col-xs-12 col-sm-8"> + <input id="c-give_num" data-rule="required" class="form-control" name="row[give_num]" type="number" value="0"> + </div> + </div> + <div class="form-group"> + <label class="control-label col-xs-12 col-sm-2">{:__('Expire_days')}:</label> + <div class="col-xs-12 col-sm-8"> + <input id="c-expire_days" data-rule="required" class="form-control" name="row[expire_days]" type="number" value="0"> + </div> + </div> + <div class="form-group layer-footer"> + <label class="control-label col-xs-12 col-sm-2"></label> + <div class="col-xs-12 col-sm-8"> + <button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button> + <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button> + </div> + </div> +</form> diff --git a/application/admin/view/package/coupons/edit.html b/application/admin/view/package/coupons/edit.html new file mode 100644 index 0000000..8b5e6da --- /dev/null +++ b/application/admin/view/package/coupons/edit.html @@ -0,0 +1,34 @@ +<form id="edit-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action=""> + + <!--<div class="form-group"> + <label class="control-label col-xs-12 col-sm-2">{:__('Package_id')}:</label> + <div class="col-xs-12 col-sm-8"> + <input id="c-package_id" data-rule="required" data-source="package/index" class="form-control selectpage" name="row[package_id]" type="text" value="{$row.package_id|htmlentities}"> + </div> + </div>--> + <div class="form-group"> + <label class="control-label col-xs-12 col-sm-2">{:__('Coupons_id')}:</label> + <div class="col-xs-12 col-sm-8"> + <input id="c-coupons_id" data-rule="required" data-source="shopro/coupons/index" class="form-control selectpage" name="row[coupons_id]" type="text" value="{$row.coupons_id|htmlentities}"> + </div> + </div> + <div class="form-group"> + <label class="control-label col-xs-12 col-sm-2">{:__('Give_num')}:</label> + <div class="col-xs-12 col-sm-8"> + <input id="c-give_num" data-rule="required" class="form-control" name="row[give_num]" type="number" value="{$row.give_num|htmlentities}"> + </div> + </div> + <div class="form-group"> + <label class="control-label col-xs-12 col-sm-2">{:__('Expire_days')}:</label> + <div class="col-xs-12 col-sm-8"> + <input id="c-expire_days" data-rule="required" class="form-control" name="row[expire_days]" type="number" value="{$row.expire_days|htmlentities}"> + </div> + </div> + <div class="form-group layer-footer"> + <label class="control-label col-xs-12 col-sm-2"></label> + <div class="col-xs-12 col-sm-8"> + <button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button> + <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button> + </div> + </div> +</form> diff --git a/application/admin/view/package/coupons/index.html b/application/admin/view/package/coupons/index.html new file mode 100644 index 0000000..ba8b827 --- /dev/null +++ b/application/admin/view/package/coupons/index.html @@ -0,0 +1,26 @@ +<div class="panel panel-default panel-intro"> + {:build_heading()} + + <div class="panel-body"> + <div id="myTabContent" class="tab-content"> + <div class="tab-pane fade active in" id="one"> + <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 href="javascript:;" class="btn btn-success btn-add {:$auth->check('package/coupons/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('package/coupons/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('package/coupons/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a> + + + </div> + <table id="table" class="table table-striped table-bordered table-hover table-nowrap" + data-operate-edit="{:$auth->check('package/coupons/edit')}" + data-operate-del="{:$auth->check('package/coupons/del')}" + width="100%"> + </table> + </div> + </div> + + </div> + </div> +</div> diff --git a/application/api/controller/PackageOrder.php b/application/api/controller/PackageOrder.php index d6d76cc..2c9ba1a 100644 --- a/application/api/controller/PackageOrder.php +++ b/application/api/controller/PackageOrder.php @@ -268,6 +268,25 @@ class PackageOrder extends Base } } + // 赠送优惠券 + $package_coupons = Db::name('package_coupons') + ->alias('pc') + ->join('shopro_coupons sc','pc.coupons_id = sc.id') + ->where('pc.package_id',$order['package_id']) + ->field('pc.coupons_id,') + ->select(); + $user_coupons_data = []; + foreach ($package_coupons as $v) { + for ($i=0; $i < $v['give_num']; $i++) { + $user_coupons_data[] = [ + 'user_id' => $order['user_id'], + 'coupons_id' => $v['coupons_id'], + 'package_coupons_time' => strtotime('+'.$v['exipre_days'].'days') + ]; + } + } + (new \addons\shopro\model\UserCoupons)->saveAll($user_coupons_data); + // 变更会员到期时间 if($user['vip_start_time'] <= 0){ $user->vip_start_time = time(); diff --git a/public/assets/js/backend/package/coupons.js b/public/assets/js/backend/package/coupons.js new file mode 100644 index 0000000..94bdf65 --- /dev/null +++ b/public/assets/js/backend/package/coupons.js @@ -0,0 +1,55 @@ +define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) { + + var Controller = { + index: function () { + // 初始化表格参数配置 + Table.api.init({ + extend: { + index_url: 'package/coupons/index' + location.search, + add_url: 'package/coupons/add' + location.search, + edit_url: 'package/coupons/edit', + del_url: 'package/coupons/del', + multi_url: 'package/coupons/multi', + import_url: 'package/coupons/import', + table: 'package_coupons', + } + }); + + var table = $("#table"); + + // 初始化表格 + table.bootstrapTable({ + url: $.fn.bootstrapTable.defaults.extend.index_url, + pk: 'id', + sortName: 'id', + columns: [ + [ + {checkbox: true}, + {field: 'id', title: __('Id')}, + {field: 'coupon.name', title: __('Coupon.name'), operate: 'LIKE'}, + {field: 'give_num', title: __('Give_num')}, + {field: 'expire_days', title: __('Expire_days')}, + {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime}, + {field: 'updatetime', title: __('Updatetime'), 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} + ] + ] + }); + + // 为表格绑定事件 + Table.api.bindevent(table); + }, + add: function () { + Controller.api.bindevent(); + }, + edit: function () { + Controller.api.bindevent(); + }, + api: { + bindevent: function () { + Form.api.bindevent($("form[role=form]")); + } + } + }; + return Controller; +}); diff --git a/public/assets/js/backend/package/package.js b/public/assets/js/backend/package/package.js index cca7c7a..db3342a 100644 --- a/public/assets/js/backend/package/package.js +++ b/public/assets/js/backend/package/package.js @@ -30,7 +30,24 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin {field: 'package_price', title: __('Package_price'), operate:'BETWEEN'}, {field: 'month', title: __('Month')}, {field: 'weigh', title: __('Weigh'), operate: false}, - {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate} + //操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮 + { + field: 'operate', + title: __('Operate'), + table: table, + events: Table.api.events.operate, + buttons: [ + { + name: 'coupons', + text: __('优惠券'), + title: __('优惠券'), + classname: 'btn btn-xs btn-primary btn-dialog', + icon: 'fa fa-list', + url: 'package/coupons?package_id={id}', + } + ], + formatter: Table.api.formatter.operate + } ] ] }); @@ -51,4 +68,4 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin } }; return Controller; -}); \ No newline at end of file +}); -- libgit2 0.24.0