CouponActivity.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\mall\controller;
  3. use library\Controller;
  4. use think\Db;
  5. /**
  6. * 优惠券活动
  7. * Class CouponActivity
  8. * @package app\mall\controller
  9. */
  10. class CouponActivity extends Controller
  11. {
  12. /**
  13. * 绑定数据表
  14. * @var string
  15. */
  16. protected $table = 'CouponActivity';
  17. /**
  18. * 活动列表
  19. * @auth true
  20. * @menu true
  21. * @throws \think\Exception
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. * @throws \think\exception\PDOException
  26. */
  27. public function index()
  28. {
  29. $this->title = '活动列表';
  30. $query = $this->_query($this->table)->where('is_deleted',0);
  31. $query->like('name');
  32. $query->order(' sort desc , id desc')->page();
  33. }
  34. /**
  35. * 数据列表处理
  36. * @auth true
  37. * @menu true
  38. * @param array $data
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. * @throws \think\exception\DbException
  42. */
  43. protected function _index_page_filter(&$data)
  44. {
  45. }
  46. /**
  47. * 添加活动
  48. * @auth true
  49. * @menu true
  50. * @throws \think\Exception
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @throws \think\exception\DbException
  54. * @throws \think\exception\PDOException
  55. */
  56. public function add()
  57. {
  58. $this->title = '添加活动';
  59. $this->_form($this->table, 'form');
  60. }
  61. /**
  62. * 编辑活动
  63. * @auth true
  64. * @menu true
  65. * @throws \think\Exception
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. * @throws \think\exception\DbException
  69. * @throws \think\exception\PDOException
  70. */
  71. public function edit()
  72. {
  73. $this->title = '编辑活动';
  74. $this->_form($this->table, 'form');
  75. }
  76. /**
  77. * 启用
  78. * @auth true
  79. * @menu true
  80. * @throws \think\Exception
  81. * @throws \think\exception\PDOException
  82. */
  83. public function enable()
  84. {
  85. $this->_save($this->table, ['status' => 1]);
  86. }
  87. /**
  88. * 禁用
  89. * @auth true
  90. * @menu true
  91. * @throws \think\Exception
  92. * @throws \think\exception\PDOException
  93. */
  94. public function forbid()
  95. {
  96. $this->_save($this->table, ['status' => 1]);
  97. }
  98. /**
  99. * 删除
  100. * @auth true
  101. * @menu true
  102. * @throws \think\Exception
  103. * @throws \think\exception\PDOException
  104. */
  105. public function del()
  106. {
  107. $this->_save($this->table, ['is_deleted' => 1]);
  108. }
  109. /**
  110. * 表单数据处理
  111. * @auth true
  112. * @menu true
  113. * @param array $data
  114. */
  115. protected function _form_filter(&$data)
  116. {
  117. $data['create_at'] = date('Y-m-d H:i:s');
  118. if($this->request->action() == 'add') {
  119. $this->coupon_list = \app\common\model\CouponConfig::where('is_deleted',0)->column('title','id');
  120. }else{
  121. $this->coupon_list = \app\common\model\CouponConfig::column('title','id');
  122. }
  123. if($this->request->isPost()) {
  124. $select_coupon = [];
  125. if(empty($data['coupon'])) $this->error('请选择优惠券');
  126. if(empty($data['start_time'])) $this->error('请选择活动开始时间');
  127. if(empty($data['end_time'])) $this->error('请选择活动结束时间');
  128. if(strtotime($data['start_time']) >= strtotime($data['end_time'])) $this->error('请选择活动时间有误');
  129. if(isset($data['coupon']) && !empty($data['coupon'])){
  130. foreach ($data['coupon'] as $key=>$value){
  131. if($value) $select_coupon[] = $key;
  132. }
  133. }
  134. $data['coupon_set'] = '|'.implode('|',$select_coupon).'|';
  135. }
  136. if($this->request->isGet() && $this->request->action() =='edit') {
  137. $this->coupon_arr = isset($data['coupon_set']) ? explode('|',trim($data['coupon_set'],'|')) : [];
  138. }
  139. }
  140. }