Coupon.php 5.3 KB

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