Coupon.php 2.8 KB

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