UserCoupon.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\CouponConfig;
  4. use app\common\model\UserCouponList;
  5. use think\Db;
  6. /**
  7. * @title 会员优惠券
  8. * @controller UserCoupon
  9. * @group base
  10. */
  11. class UserCoupon extends Base
  12. {
  13. public function initialize()
  14. {
  15. parent::initialize();
  16. parent::checkLogin();
  17. }
  18. /**
  19. * @title 获取所有的优惠券
  20. * @desc 获取所有的优惠券
  21. * @author qc
  22. * @method GET
  23. * @url /api/User_coupon/getAllCouponList
  24. * @param name:status type:string require:1 default:-1 desc:状态 (-1全部,0:未使用 1:已使用 2:已到期)
  25. * @return name:id type:int default:-- desc:会员优惠券记录id
  26. * @return name:coupon_type type:int default:-- desc:优惠券类型0全场券1商品券
  27. * @return name:goods_id type:int default:-- desc:商品id
  28. * @return name:title type:int default:-- desc:标题
  29. * @return name:low_amount type:float default:-- desc:最低消费限制
  30. * @return name:amount type:float default:-- desc:券额
  31. * @return name:type type:int default:-- desc:0不限制使用时间1限制使用时间
  32. * @return name:start type:string default:-- desc:开始使用时间
  33. * @return name:end type:string default:-- desc:结束使用时间
  34. * @return name:status type:int default:-- desc:状态 (0:未使用 1:已使用 2:已到期)
  35. * @return name:create_at type:string default:-- desc:时间
  36. * @return name:coupon_set type:array default:-- desc:券详情
  37. * @return name:coupon_set.status type:string default:-- desc:0禁用1正常
  38. * @return name:coupon_set.is_deleted type:string default:-- desc:删除
  39. */
  40. public function getAllCouponList()
  41. {
  42. $status = input('get.status',-1);
  43. $where = [];
  44. $where['user_id']=$this->user_id;
  45. if($status >=0) $where['status']=$status;
  46. $list = UserCouponList::with('couponSet')
  47. ->where($where)->order('id desc')
  48. ->select();
  49. $this->success('ok',['list'=>$list]);
  50. }
  51. /**
  52. * @title 领取优惠券
  53. * @desc 领取优惠券
  54. * @author qc
  55. * @method POST
  56. * @url /api/User_coupon/getCoupon
  57. * @param name:coupon_id type:string require:1 default:-- desc:券id
  58. * @return name:id type:int require:1 default:-- desc:会员优惠券记录id
  59. */
  60. public function getCoupon()
  61. {
  62. $coupon_id = input('post.coupon_id');
  63. $coupon_info = CouponConfig::where(['id'=>$coupon_id,'is_deleted'=>'0'])->find();
  64. if(!$coupon_info) $this->error('优惠券不存在');
  65. $coupon_info = $coupon_info->toArray();
  66. if($coupon_info['status'] != 1 ) $this->error('优惠券已禁用');
  67. $count = UserCouponList::where('config_id',$coupon_id)->where('user_id',$this->user_id)->count();
  68. $all_send = UserCouponList::where('config_id',$coupon_id)->count();// 总共发放了多少
  69. if($coupon_info['num'] > 0 && $all_send >= $coupon_info['num']) $this->error('已发放完');
  70. if($count + 1 > $coupon_info['user_num']) $this->error('已领取');
  71. // 领取的详情
  72. $data['config_id']=$coupon_info['id'];
  73. $data['user_id']=$this->user_id;
  74. $data['goods_id']= $coupon_info['goods_id'];
  75. $data['title'] = $coupon_info['title'];
  76. $data['low_amount']=$coupon_info['low_amount'];
  77. $data['amount']=$coupon_info['amount'];
  78. if($coupon_info['time_type']==0){
  79. if($coupon_info['low_day']==0){
  80. $data['type']=0;
  81. }else{
  82. $data['type']=1;
  83. $data['start']=date('Y-m-d');
  84. $data['end']=date('Y-m-d', strtotime('+'.$coupon_info['low_day'].' days') );;
  85. }
  86. }else{
  87. $data['type']=1;
  88. $data['start']=$coupon_info['start_tm'];
  89. $data['end']=$coupon_info['end_tm'];
  90. }
  91. $data['status']=0;
  92. $data['create_at']=time();
  93. $data['coupon_type']=$coupon_info['coupon_type'];
  94. $res = UserCouponList::create($data);
  95. $this->success('领取成功',['id'=>$res->id]);
  96. }
  97. /**
  98. * @title 我的优惠券记录详情
  99. * @desc 我的优惠券记录详情
  100. * @author qc
  101. * @method GET
  102. * @url /api/User_coupon/getCouponInfo
  103. * @param name:id type:string require:1 default:-- desc:记录id
  104. * @return name:id type:int default:-- desc:会员优惠券记录id
  105. * @return name:coupon_type type:int default:-- desc:优惠券类型0全场券1商品券
  106. * @return name:goods_id type:int default:-- desc:商品id
  107. * @return name:title type:int default:-- desc:标题
  108. * @return name:low_amount type:float default:-- desc:最低消费限制
  109. * @return name:amount type:float default:-- desc:券额
  110. * @return name:type type:int default:-- desc:0不限制使用时间1限制使用时间
  111. * @return name:start type:string default:-- desc:开始使用时间
  112. * @return name:end type:string default:-- desc:结束使用时间
  113. * @return name:status type:int default:-- desc:状态 (0:未使用 1:已使用 2:已到期)0:未使用 1:已使用 2:已到期
  114. * @return name:create_at type:string default:-- desc:时间
  115. * @return name:coupon_set type:array default:-- desc:券详情
  116. * @return name:coupon_set.status type:string default:-- desc:0禁用1正常
  117. * @return name:coupon_set.is_deleted type:string default:-- desc:删除
  118. */
  119. public function getCouponInfo()
  120. {
  121. $id = input('get.id');
  122. $detail = UserCouponList::with('couponSet')->where('id',$id)->where('user_id',$this->user_id)->find();
  123. $this->success('ok',['detail'=>$detail]);
  124. }
  125. }