Coupon.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\store\controller\market;
  3. use app\store\controller\Controller;
  4. use app\store\model\Coupon as CouponModel;
  5. use app\store\model\UserCoupon as UserCouponModel;
  6. /**
  7. * 优惠券管理
  8. * Class Coupon
  9. * @package app\store\controller\market
  10. */
  11. class Coupon extends Controller
  12. {
  13. /* @var CouponModel $model */
  14. private $model;
  15. /**
  16. * 构造方法
  17. * @throws \app\common\exception\BaseException
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. * @throws \think\exception\DbException
  21. */
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new CouponModel;
  26. }
  27. /**
  28. * 优惠券列表
  29. * @return mixed
  30. * @throws \think\exception\DbException
  31. */
  32. public function index()
  33. {
  34. $list = $this->model->getList();
  35. return $this->fetch('index', compact('list'));
  36. }
  37. /**
  38. * 添加优惠券
  39. * @return array|mixed
  40. */
  41. public function add()
  42. {
  43. if (!$this->request->isAjax()) {
  44. return $this->fetch('add');
  45. }
  46. // 新增记录
  47. if ($this->model->add($this->postData('coupon'))) {
  48. return $this->renderSuccess('添加成功', url('market.coupon/index'));
  49. }
  50. return $this->renderError($this->model->getError() ?: '添加失败');
  51. }
  52. /**
  53. * 更新优惠券
  54. * @param $coupon_id
  55. * @return array|mixed
  56. * @throws \think\exception\DbException
  57. */
  58. public function edit($coupon_id)
  59. {
  60. // 优惠券详情
  61. $model = CouponModel::detail($coupon_id);
  62. if (!$this->request->isAjax()) {
  63. return $this->fetch('edit', compact('model'));
  64. }
  65. // 更新记录
  66. if ($model->edit($this->postData('coupon'))) {
  67. return $this->renderSuccess('更新成功', url('market.coupon/index'));
  68. }
  69. return $this->renderError($model->getError() ?: '更新失败');
  70. }
  71. /**
  72. * 删除优惠券
  73. * @param $coupon_id
  74. * @return array|mixed
  75. * @throws \think\exception\DbException
  76. */
  77. public function delete($coupon_id)
  78. {
  79. // 优惠券详情
  80. $model = CouponModel::detail($coupon_id);
  81. // 更新记录
  82. if ($model->setDelete()) {
  83. return $this->renderSuccess('删除成功', url('market.coupon/index'));
  84. }
  85. return $this->renderError($model->getError() ?: '删除成功');
  86. }
  87. /**
  88. * 领取记录
  89. * @return mixed
  90. * @throws \think\exception\DbException
  91. */
  92. public function receive()
  93. {
  94. $model = new UserCouponModel;
  95. $list = $model->getList();
  96. return $this->fetch('receive', compact('list'));
  97. }
  98. }