Coupon.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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['money'] <= 0) {
  42. $this->error('请输入正确抵扣券面值金额或者打折券(百分比)折数');
  43. }
  44. if ($params['type'] == 2) { //抵扣券
  45. if ($params['minmoney'] <= $params['money']) {
  46. $this->error('抵扣券最低消费金额必须大于抵扣金额');
  47. }
  48. }
  49. $result = false;
  50. Db::startTrans();
  51. try {
  52. //是否采用模型验证
  53. if ($this->modelValidate) {
  54. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  55. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  56. $this->model->validateFailException(true)->validate($validate);
  57. }
  58. $result = $this->model->allowField(true)->save($params);
  59. Db::commit();
  60. } catch (ValidateException $e) {
  61. Db::rollback();
  62. $this->error($e->getMessage());
  63. } catch (PDOException $e) {
  64. Db::rollback();
  65. $this->error($e->getMessage());
  66. } catch (Exception $e) {
  67. Db::rollback();
  68. $this->error($e->getMessage());
  69. }
  70. if ($result !== false) {
  71. $this->success();
  72. } else {
  73. $this->error(__('No rows were inserted'));
  74. }
  75. }
  76. $this->error(__('Parameter %s can not be empty', ''));
  77. }
  78. return $this->view->fetch();
  79. }
  80. /**
  81. * 修改优惠券状态
  82. */
  83. public function chanagestatus() {
  84. $ids = input('ids', 0, 'intval'); //优惠券id
  85. $status = input('status', 0, 'intval'); //状态:1=发布,2=下架
  86. if (!$ids) {
  87. $this->error('参数缺失');
  88. }
  89. if (!in_array($status, [1, 2])) {
  90. $this->error('参数错误');
  91. }
  92. $info = Db::name('coupon')->find($ids);
  93. if (!$info) {
  94. $this->error('优惠券不存在');
  95. }
  96. if ($status == 1 && $info['status'] != 0) {
  97. $this->error('优惠券状态已改变');
  98. }
  99. if ($status == 2) {
  100. if ($info['status'] != 1) {
  101. $this->error('优惠券状态已改变');
  102. }
  103. if ($info['purpose'] == 0) { //获得方式:0=充值,1=注册,2=推广,3=后台发放,4=轮播图,5=报名活动
  104. //判断是否有绑定充值
  105. $count = Db::name('recharge')->where(['coupon_id' => $ids])->find();
  106. if ($count) {
  107. $this->error('请先取消该优惠券与充值列表的绑定关系');
  108. }
  109. }
  110. }
  111. $rs = Db::name('coupon')->where(['id' => $ids, 'status' => $info['status']])->setField('status', $status);
  112. if (!$rs) {
  113. $this->error('操作失败');
  114. }
  115. $this->success('操作成功');
  116. }
  117. /**
  118. * 删除
  119. */
  120. public function delcoupon($ids = "")
  121. {
  122. if (!$this->request->isPost()) {
  123. $this->error(__("Invalid parameters"));
  124. }
  125. $ids = $ids ? $ids : $this->request->post("ids");
  126. $info = Db::name('coupon')->find($ids);
  127. if (!$info) {
  128. $this->error('操作成功');
  129. }
  130. if ($info['status'] != 2) {
  131. $this->error('请先下架优惠券');
  132. }
  133. Db::name('coupon')->delete($ids);
  134. $this->success('操作成功');
  135. }
  136. }