Coupon.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use Think\Db;
  5. /**
  6. * 优惠券管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Coupon extends Backend
  11. {
  12. /**
  13. * Coupon模型对象
  14. * @var \app\admin\model\Coupon
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Coupon;
  21. $this->view->assign("typeList", $this->model->getTypeList());
  22. $this->view->assign("purposeList", $this->model->getPurposeList());
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 添加
  32. */
  33. public function add() {
  34. if ($this->request->isPost()) {
  35. $params = $this->request->post("row/a");
  36. if ($params) {
  37. $params = $this->preExcludeFields($params);
  38. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  39. $params[$this->dataLimitField] = $this->auth->id;
  40. }
  41. if ($params['type'] == 1) { //折扣券
  42. $params['money'] = (int)$params['money'];
  43. if ($params['money'] <= 0 || $params['money'] >= 100) {
  44. $this->error('输入正确打折券(百分比)折数');
  45. }
  46. }
  47. if ($params['type'] == 2) { //抵扣券
  48. if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $params['money']) || $params['money'] <= 0) {
  49. $this->error('请输入正确抵扣券面值金额');
  50. }
  51. if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $params['minmoney']) || $params['minmoney'] <= 0) {
  52. $this->error('请输入正确抵扣券最低消费金额');
  53. }
  54. if ($params['minmoney'] <= $params['money']) {
  55. $this->error('抵扣券最低消费金额必须大于抵扣金额');
  56. }
  57. }
  58. $params['effectiveday'] = (int)$params['effectiveday']; //有效天数
  59. if ($params['effectiveday'] <= 0) {
  60. $this->error('请输入正确有效天数');
  61. }
  62. $result = false;
  63. Db::startTrans();
  64. try {
  65. //是否采用模型验证
  66. if ($this->modelValidate) {
  67. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  68. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  69. $this->model->validateFailException(true)->validate($validate);
  70. }
  71. $result = $this->model->allowField(true)->save($params);
  72. Db::commit();
  73. } catch (ValidateException $e) {
  74. Db::rollback();
  75. $this->error($e->getMessage());
  76. } catch (PDOException $e) {
  77. Db::rollback();
  78. $this->error($e->getMessage());
  79. } catch (Exception $e) {
  80. Db::rollback();
  81. $this->error($e->getMessage());
  82. }
  83. if ($result !== false) {
  84. $this->success();
  85. } else {
  86. $this->error(__('No rows were inserted'));
  87. }
  88. }
  89. $this->error(__('Parameter %s can not be empty', ''));
  90. }
  91. return $this->view->fetch();
  92. }
  93. /**
  94. * 修改优惠券状态
  95. */
  96. public function chanagestatus() {
  97. $ids = input('ids', 0, 'intval'); //优惠券id
  98. $status = input('status', 0, 'intval'); //状态:1=发布,2=下架
  99. if (!$ids) {
  100. $this->error('参数缺失');
  101. }
  102. if (!in_array($status, [1, 2])) {
  103. $this->error('参数错误');
  104. }
  105. $info = Db::name('coupon')->find($ids);
  106. if (!$info) {
  107. $this->error('优惠券不存在');
  108. }
  109. if ($status == 1 && $info['status'] != 0) {
  110. $this->error('优惠券状态已改变');
  111. }
  112. if ($status == 2) {
  113. if ($info['status'] != 1) {
  114. $this->error('优惠券状态已改变');
  115. }
  116. if ($info['purpose'] == 0) { //获得方式:0=充值,1=注册,2=推广,3=后台发放,4=轮播图,5=报名活动
  117. //判断是否有绑定充值
  118. $count = Db::name('recharge')->where(['coupon_id' => $ids])->find();
  119. if ($count) {
  120. $this->error('请先取消该优惠券与充值列表的绑定关系');
  121. }
  122. }
  123. }
  124. $rs = Db::name('coupon')->where(['id' => $ids, 'status' => $info['status']])->setField('status', $status);
  125. if (!$rs) {
  126. $this->error('操作失败');
  127. }
  128. $this->success('操作成功');
  129. }
  130. /**
  131. * 删除
  132. */
  133. public function delcoupon($ids = "")
  134. {
  135. if (!$this->request->isPost()) {
  136. $this->error(__("Invalid parameters"));
  137. }
  138. $ids = $ids ? $ids : $this->request->post("ids");
  139. $info = Db::name('coupon')->find($ids);
  140. if (!$info) {
  141. $this->error('操作成功');
  142. }
  143. if ($info['status'] != 2) {
  144. $this->error('请先下架优惠券');
  145. }
  146. Db::name('coupon')->delete($ids);
  147. $this->success('操作成功');
  148. }
  149. }