Coupon.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\CouponActivity;
  4. use app\common\model\CouponConfig;
  5. use app\common\model\UserCouponList;
  6. use think\Db;
  7. use function Symfony\Component\String\s;
  8. /**
  9. * @title 优惠券管理
  10. * @controller Coupon
  11. * @package app\api\controller
  12. */
  13. class Coupon extends Base
  14. {
  15. protected $need_login = [
  16. 'getUserCouponList',
  17. 'getCouponByCode',
  18. ];
  19. public function initialize(){
  20. parent::initialize();
  21. parent::setUid();
  22. if($this->user_id) UserCouponList::where([['past_at','< time',date('Y-m-d H:i:s')],['user_id','=',$this->user_id]])->update(['status'=>'3']);
  23. }
  24. /**
  25. * @title 自动获取活动优惠券
  26. * @desc 获取失败页面不需要处理!!!仅获取成功处理list数据
  27. * @author qc
  28. * @url /api/Coupon/autoGetActivityCoupon
  29. * @method GET
  30. * @header name:Authorization require:1 desc:Token
  31. * @return name:title type:string default:-- desc:优惠券名称
  32. * @return name:module type:int default:-- desc:应用模块(1所有,2洗鞋,3洗衣)
  33. * @return name:coupon_type type:int default:-- desc:类型(1满减券,2抵扣券【抵一件衣服或是一双鞋价格】)
  34. * @return name:is_new type:int default:-- desc:是否新人券(0否1是)
  35. * @return name:low_amount type:int default:-- desc:最低消费限制(coupon_type=1时用)
  36. * @return name:amount type:int default:-- desc:抵扣金额(coupon_type=1时用)
  37. * @return name:create_at type:string default:-- desc:领取时间
  38. * @return name:past_at type:string default:-- desc:过期时间
  39. * @return name:cover type:string default:-- desc:图片
  40. * @return name:status type:int default:-- desc:状态码(1未使用2已使用3已过期)
  41. */
  42. public function autoGetActivityCoupon()
  43. {
  44. if(!$this->user_id) $this->error('用户未登录');
  45. $where = [];
  46. $where[] = ['status','=',1];
  47. $where[] = ['is_deleted','=',0];
  48. $where[] = ['start_time','< time',date("Y-m-d H:i:s")];
  49. $where[] = ['end_time','> time',date("Y-m-d H:i:s")];
  50. // 进行中的活动
  51. $activity_list = CouponActivity::where($where)->select()->toArray();
  52. $get_ids = [];// 活动的优惠券记录id
  53. foreach ($activity_list as $act_info)
  54. {
  55. if(empty($act_info['coupon_set'])) continue;
  56. $ids = explode('|',trim($act_info['coupon_set'],'|'));// 活动优惠券设置
  57. // 是否已获得该活动的优惠券
  58. $has_get = UserCouponList::where(['user_id'=>$this->user_id,'act_id'=>$act_info['id']])->count();
  59. if($has_get) continue;
  60. foreach ($ids as $coupon_id) {
  61. if(!$coupon_id) continue;
  62. // 优惠券详情
  63. $coupon_info = CouponConfig::where('id',$coupon_id)->find()->toArray();
  64. // 优惠券是否是正常状态
  65. if($coupon_info['status'] !=1 || $coupon_info['is_deleted'] == 1) continue;
  66. // 检查优惠券活动数量
  67. $has_num = UserCouponList::getUserCouponNum($this->user_id,$coupon_id);
  68. if($has_num >= $coupon_info['user_num']) continue;
  69. $user_coupon = UserCouponList::saveUserCoupon($coupon_info,$this->user_id,$act_info['id']);
  70. $get_ids[] = $user_coupon['id'];
  71. }
  72. }
  73. if(empty($get_ids)) $this->error('没有优惠活动或已获得');
  74. $list = UserCouponList::where([['l.user_id','=',$this->user_id],['l.id','in',$get_ids]])
  75. ->alias('l')
  76. ->field('l.*,c.title,c.module,c.coupon_type,c.is_new,c.cover')
  77. ->leftJoin('CouponConfig c','c.id = l.coupon_id')
  78. ->limit($this->off_set,$this->page_num)
  79. ->order('l.id desc')
  80. ->select()->toArray();
  81. $this->success('ok',['list'=>$list]);
  82. }
  83. /**
  84. * @title 获取会员优惠券列表
  85. * @desc 获取会员优惠券列表
  86. * @author qc
  87. * @url /api/Coupon/getUserCouponList
  88. * @method GET
  89. * @header name:Authorization require:1 desc:Token
  90. * @param name:page type:int default:1 desc:页数
  91. * @param name:page_num type:int default:20 desc:每页数
  92. * @param name:status type:int default:1 desc:状态码(1未使用2已使用3已过期)
  93. * @param name:module type:int default:1 desc:应用模块(1所有,2洗鞋,3洗衣)【订单确认页面可用】
  94. * @return name:title type:string default:-- desc:优惠券名称
  95. * @return name:module type:int default:-- desc:应用模块(1所有,2洗鞋,3洗衣)
  96. * @return name:coupon_type type:int default:-- desc:类型(1满减券,2抵扣券【抵一件衣服或是一双鞋价格】)
  97. * @return name:is_new type:int default:-- desc:是否新人券(0否1是)
  98. * @return name:low_amount type:int default:-- desc:最低消费限制(coupon_type=1时用)
  99. * @return name:amount type:int default:-- desc:抵扣金额(coupon_type=1时用)
  100. * @return name:create_at type:string default:-- desc:领取时间
  101. * @return name:past_at type:string default:-- desc:过期时间
  102. * @return name:cover type:string default:-- desc:图片
  103. * @return name:status type:int default:-- desc:状态码(1未使用2已使用3已过期)
  104. *
  105. */
  106. public function getUserCouponList()
  107. {
  108. $status = input('get.status',1);
  109. $module = input('get.module',1);
  110. $list = UserCouponList::where(['l.user_id'=>$this->user_id,'l.status'=>$status])
  111. ->alias('l')
  112. ->field('l.*,c.title,c.module,c.coupon_type,c.is_new,c.cover')
  113. ->when($module,function ($query)use ($module){
  114. if($module > 1 ) $query->where('module','in','1,'. $module);
  115. })->leftJoin('CouponConfig c','c.id = l.coupon_id')
  116. ->limit($this->off_set,$this->page_num)
  117. ->order('l.id desc')
  118. ->select()->toArray();
  119. $this->success('ok',['list'=>$list]);
  120. }
  121. /**
  122. * @title 兑换码兑换优惠券
  123. * @desc 兑换码兑换优惠券
  124. * @author qc
  125. * @url /api/Coupon/getCouponByCode
  126. * @method POST
  127. * @header name:Authorization require:1 desc:Token
  128. * @param name:code type:string default:1 desc:兑换码
  129. *
  130. */
  131. public function getCouponByCode()
  132. {
  133. $code = input('post.code');
  134. if(!$code) $this->error('请输入兑换码');
  135. $coupon_list = CouponConfig::where(['status'=>1,'is_deleted'=>0,'code'=>$code])->select()->toArray();
  136. if(empty($coupon_list))$this->error('兑换码错误');
  137. $exchange_num = 0;
  138. foreach ($coupon_list as $v) {
  139. $has_num = UserCouponList::getUserCouponNum($this->user_id,$v['id']);
  140. if($has_num >= $v['user_num']) continue;
  141. $exchange_num++;
  142. UserCouponList::saveUserCoupon($v,$this->user_id);
  143. }
  144. $exchange_num ? $this->success('兑换成功:'.$exchange_num) : $this->error('兑换失败');
  145. }
  146. }