Coupon.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace addons\shop\controller;
  3. use addons\shop\model\Coupon as CouponModel;
  4. use addons\shop\model\UserCoupon;
  5. use addons\shop\model\CouponCondition;
  6. use addons\shop\model\Goods;
  7. use think\Db;
  8. use addons\shop\library\IntCode;
  9. class Coupon extends Base
  10. {
  11. protected $noNeedLogin = ['show', 'index'];
  12. protected $noNeedRight = '*';
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. }
  17. //领取优惠券
  18. public function drawCoupon()
  19. {
  20. $coupon_id = $this->request->post('id');
  21. if (!$coupon_id) {
  22. $this->error('参数错误');
  23. }
  24. $coupon_id = IntCode::decode($coupon_id);
  25. if (!$coupon_id) {
  26. $this->error('参数错误');
  27. }
  28. $model = new CouponModel();
  29. try {
  30. $row = $model->getCoupon($coupon_id)
  31. ->checkCoupon()
  32. ->checkOpen()
  33. ->checkNumber()
  34. ->checkMyNumber($this->auth->id)
  35. ->checkReceiveTime();
  36. list($begin_time, $expire_time) = $row->getUseTime();
  37. Db::startTrans();
  38. try {
  39. //检测没问题,可领取
  40. (new UserCoupon())->save([
  41. 'coupon_id' => $coupon_id,
  42. 'user_id' => $this->auth->id,
  43. 'begin_time' => $begin_time,
  44. 'expire_time' => $expire_time
  45. ]);
  46. $row->setInc('received_num');
  47. // 提交事务
  48. Db::commit();
  49. } catch (\Exception $e) {
  50. // 回滚事务
  51. Db::rollback();
  52. throw new \Exception($e->getMessage());
  53. }
  54. } catch (\Exception $e) {
  55. $this->error($e->getMessage());
  56. }
  57. $this->success('领取优惠券成功!');
  58. }
  59. //优惠券列表
  60. public function index()
  61. {
  62. // 判断是否跳转移动端
  63. $this->checkredirect('coupon/list');
  64. $param = $this->request->param();
  65. $param['is_private'] = 'no';
  66. $param['is_open'] = 1;
  67. $param['num'] = 15;
  68. $list = CouponModel::tableList($param);
  69. //已经登录,渲染已领的优惠券
  70. $coupon_ids = [];
  71. if ($this->auth->isLogin()) {
  72. $coupon_ids = UserCoupon::where('user_id', $this->auth->id)->column('coupon_id');
  73. }
  74. foreach ($list as $key => &$item) {
  75. $item = CouponModel::render($item, $coupon_ids);
  76. }
  77. $this->view->assign('result', $this->request->param('result'));
  78. $this->view->assign('couponList', $list);
  79. return $this->view->fetch('/coupon_index');
  80. }
  81. //优惠券详情
  82. public function show()
  83. {
  84. $coupon_id = $this->request->param('coupon');
  85. // 判断是否跳转移动端
  86. $this->checkredirect('coupon/detail', ['id' => $coupon_id]);
  87. $id = IntCode::decode($coupon_id);
  88. if (!$id) {
  89. $this->error('参数错误');
  90. }
  91. $row = CouponModel::where('id', $id)->where('is_open', 1)->find();
  92. if (!$row) {
  93. $this->error('未找到可用的优惠券');
  94. }
  95. //优惠券可使用的商品
  96. $conditions = CouponCondition::where('id', 'IN', $row['condition_ids'])->select();
  97. $goods = Goods::where(function ($query) use ($conditions) {
  98. //组合条件
  99. foreach ($conditions as $item) {
  100. switch ($item['type']) { //1商品
  101. case 1:
  102. $goods_ids = explode(',', $item['content']);
  103. if ($goods_ids) {
  104. $query->where('id', 'IN', $goods_ids);
  105. }
  106. break;
  107. case 4: //指定分类
  108. $category_ids = explode(',', $item['content']);
  109. if ($category_ids) {
  110. $query->whereOr('category_id', 'IN', $category_ids);
  111. }
  112. break;
  113. case 5: //指定品牌
  114. $brand_ids = explode(',', $item['content']);
  115. if ($brand_ids) {
  116. $query->whereOr('brand_id', 'IN', $brand_ids);
  117. }
  118. break;
  119. }
  120. }
  121. })->limit(16)->where('status', 'normal')->select();
  122. $this->view->assign('coupon_goods', $goods);
  123. //已经登录,渲染已领的优惠券
  124. $coupon_ids = [];
  125. if ($this->auth->isLogin()) {
  126. $coupon_ids = UserCoupon::where('user_id', $this->auth->id)->column('coupon_id');
  127. }
  128. CouponModel::render($row, $coupon_ids);
  129. $this->view->assign('__info__', $row);
  130. return $this->view->fetch('/coupon_show');
  131. }
  132. }