123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace app\api\controller;
- use app\common\model\CouponConfig;
- use app\common\model\UserCouponList;
- use think\Db;
- /**
- * @title 会员优惠券
- * @controller UserCoupon
- * @group base
- */
- class UserCoupon extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::checkLogin();
- }
- /**
- * @title 获取所有的优惠券
- * @desc 获取所有的优惠券
- * @author qc
- * @method GET
- * @url /api/User_coupon/getAllCouponList
- * @param name:status type:string require:1 default:-1 desc:状态 (-1全部,0:未使用 1:已使用 2:已到期)
- * @return name:id type:int default:-- desc:会员优惠券记录id
- * @return name:coupon_type type:int default:-- desc:优惠券类型0全场券1商品券
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:title type:int default:-- desc:标题
- * @return name:low_amount type:float default:-- desc:最低消费限制
- * @return name:amount type:float default:-- desc:券额
- * @return name:type type:int default:-- desc:0不限制使用时间1限制使用时间
- * @return name:start type:string default:-- desc:开始使用时间
- * @return name:end type:string default:-- desc:结束使用时间
- * @return name:status type:int default:-- desc:状态 (0:未使用 1:已使用 2:已到期)
- * @return name:create_at type:string default:-- desc:时间
- * @return name:coupon_set type:array default:-- desc:券详情
- * @return name:coupon_set.status type:string default:-- desc:0禁用1正常
- * @return name:coupon_set.is_deleted type:string default:-- desc:删除
- */
- public function getAllCouponList()
- {
- $status = input('get.status',-1);
- $where = [];
- $where['user_id']=$this->user_id;
- if($status >=0) $where['status']=$status;
- $list = UserCouponList::with('couponSet')
- ->where($where)->order('id desc')
- ->select();
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @title 领取优惠券
- * @desc 领取优惠券
- * @author qc
- * @method POST
- * @url /api/User_coupon/getCoupon
- * @param name:coupon_id type:string require:1 default:-- desc:券id
- * @return name:id type:int require:1 default:-- desc:会员优惠券记录id
- */
- public function getCoupon()
- {
- $coupon_id = input('post.coupon_id');
- $coupon_info = CouponConfig::where(['id'=>$coupon_id,'is_deleted'=>'0'])->find();
- if(!$coupon_info) $this->error('优惠券不存在');
- $coupon_info = $coupon_info->toArray();
- if($coupon_info['status'] != 1 ) $this->error('优惠券已禁用');
- $count = UserCouponList::where('config_id',$coupon_id)->where('user_id',$this->user_id)->count();
- $all_send = UserCouponList::where('config_id',$coupon_id)->count();// 总共发放了多少
- if($coupon_info['num'] > 0 && $all_send >= $coupon_info['num']) $this->error('已发放完');
- if($count + 1 > $coupon_info['user_num']) $this->error('已领取');
- // 领取的详情
- $data['config_id']=$coupon_info['id'];
- $data['user_id']=$this->user_id;
- $data['goods_id']= $coupon_info['goods_id'];
- $data['title'] = $coupon_info['title'];
- $data['low_amount']=$coupon_info['low_amount'];
- $data['amount']=$coupon_info['amount'];
- if($coupon_info['time_type']==0){
- if($coupon_info['low_day']==0){
- $data['type']=0;
- }else{
- $data['type']=1;
- $data['start']=date('Y-m-d');
- $data['end']=date('Y-m-d', strtotime('+'.$coupon_info['low_day'].' days') );;
- }
- }else{
- $data['type']=1;
- $data['start']=$coupon_info['start_tm'];
- $data['end']=$coupon_info['end_tm'];
- }
- $data['status']=0;
- $data['create_at']=time();
- $data['coupon_type']=$coupon_info['coupon_type'];
- $res = UserCouponList::create($data);
- $this->success('领取成功',['id'=>$res->id]);
- }
- /**
- * @title 我的优惠券记录详情
- * @desc 我的优惠券记录详情
- * @author qc
- * @method GET
- * @url /api/User_coupon/getCouponInfo
- * @param name:id type:string require:1 default:-- desc:记录id
- * @return name:id type:int default:-- desc:会员优惠券记录id
- * @return name:coupon_type type:int default:-- desc:优惠券类型0全场券1商品券
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:title type:int default:-- desc:标题
- * @return name:low_amount type:float default:-- desc:最低消费限制
- * @return name:amount type:float default:-- desc:券额
- * @return name:type type:int default:-- desc:0不限制使用时间1限制使用时间
- * @return name:start type:string default:-- desc:开始使用时间
- * @return name:end type:string default:-- desc:结束使用时间
- * @return name:status type:int default:-- desc:状态 (0:未使用 1:已使用 2:已到期)0:未使用 1:已使用 2:已到期
- * @return name:create_at type:string default:-- desc:时间
- * @return name:coupon_set type:array default:-- desc:券详情
- * @return name:coupon_set.status type:string default:-- desc:0禁用1正常
- * @return name:coupon_set.is_deleted type:string default:-- desc:删除
- */
- public function getCouponInfo()
- {
- $id = input('get.id');
- $detail = UserCouponList::with('couponSet')->where('id',$id)->where('user_id',$this->user_id)->find();
- $this->success('ok',['detail'=>$detail]);
- }
- }
|