123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace app\api\controller;
- use app\common\model\CouponActivity;
- use app\common\model\CouponConfig;
- use app\common\model\UserCouponList;
- use think\Db;
- use function Symfony\Component\String\s;
- /**
- * @title 优惠券管理
- * @controller Coupon
- * @package app\api\controller
- */
- class Coupon extends Base
- {
- protected $need_login = [
- 'getUserCouponList',
- 'getCouponByCode',
- ];
- public function initialize(){
- parent::initialize();
- parent::setUid();
- if($this->user_id) UserCouponList::where([['past_at','< time',date('Y-m-d H:i:s')],['user_id','=',$this->user_id]])->update(['status'=>'3']);
- }
- /**
- * @title 自动获取活动优惠券
- * @desc 获取失败页面不需要处理!!!仅获取成功处理list数据
- * @author qc
- * @url /api/Coupon/autoGetActivityCoupon
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @return name:title type:string default:-- desc:优惠券名称
- * @return name:module type:int default:-- desc:应用模块(1所有,2洗鞋,3洗衣)
- * @return name:coupon_type type:int default:-- desc:类型(1满减券,2抵扣券【抵一件衣服或是一双鞋价格】)
- * @return name:is_new type:int default:-- desc:是否新人券(0否1是)
- * @return name:low_amount type:int default:-- desc:最低消费限制(coupon_type=1时用)
- * @return name:amount type:int default:-- desc:抵扣金额(coupon_type=1时用)
- * @return name:create_at type:string default:-- desc:领取时间
- * @return name:past_at type:string default:-- desc:过期时间
- * @return name:cover type:string default:-- desc:图片
- * @return name:status type:int default:-- desc:状态码(1未使用2已使用3已过期)
- */
- public function autoGetActivityCoupon()
- {
- if(!$this->user_id) $this->error('用户未登录');
- $where = [];
- $where[] = ['status','=',1];
- $where[] = ['is_deleted','=',0];
- $where[] = ['start_time','< time',date("Y-m-d H:i:s")];
- $where[] = ['end_time','> time',date("Y-m-d H:i:s")];
- // 进行中的活动
- $activity_list = CouponActivity::where($where)->select()->toArray();
- $get_ids = [];// 活动的优惠券记录id
- foreach ($activity_list as $act_info)
- {
- if(empty($act_info['coupon_set'])) continue;
- $ids = explode('|',trim($act_info['coupon_set'],'|'));// 活动优惠券设置
- // 是否已获得该活动的优惠券
- $has_get = UserCouponList::where(['user_id'=>$this->user_id,'act_id'=>$act_info['id']])->count();
- if($has_get) continue;
- foreach ($ids as $coupon_id) {
- if(!$coupon_id) continue;
- // 优惠券详情
- $coupon_info = CouponConfig::where('id',$coupon_id)->find()->toArray();
- // 优惠券是否是正常状态
- if($coupon_info['status'] !=1 || $coupon_info['is_deleted'] == 1) continue;
- // 检查优惠券活动数量
- $has_num = UserCouponList::getUserCouponNum($this->user_id,$coupon_id);
- if($has_num >= $coupon_info['user_num']) continue;
- $user_coupon = UserCouponList::saveUserCoupon($coupon_info,$this->user_id,$act_info['id']);
- $get_ids[] = $user_coupon['id'];
- }
- }
- if(empty($get_ids)) $this->error('没有优惠活动或已获得');
- $list = UserCouponList::where([['l.user_id','=',$this->user_id],['l.id','in',$get_ids]])
- ->alias('l')
- ->field('l.*,c.title,c.module,c.coupon_type,c.is_new,c.cover')
- ->leftJoin('CouponConfig c','c.id = l.coupon_id')
- ->limit($this->off_set,$this->page_num)
- ->order('l.id desc')
- ->select()->toArray();
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @title 获取会员优惠券列表
- * @desc 获取会员优惠券列表
- * @author qc
- * @url /api/Coupon/getUserCouponList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int default:1 desc:页数
- * @param name:page_num type:int default:20 desc:每页数
- * @param name:status type:int default:1 desc:状态码(1未使用2已使用3已过期)
- * @param name:module type:int default:1 desc:应用模块(1所有,2洗鞋,3洗衣)【订单确认页面可用】
- * @return name:title type:string default:-- desc:优惠券名称
- * @return name:module type:int default:-- desc:应用模块(1所有,2洗鞋,3洗衣)
- * @return name:coupon_type type:int default:-- desc:类型(1满减券,2抵扣券【抵一件衣服或是一双鞋价格】)
- * @return name:is_new type:int default:-- desc:是否新人券(0否1是)
- * @return name:low_amount type:int default:-- desc:最低消费限制(coupon_type=1时用)
- * @return name:amount type:int default:-- desc:抵扣金额(coupon_type=1时用)
- * @return name:create_at type:string default:-- desc:领取时间
- * @return name:past_at type:string default:-- desc:过期时间
- * @return name:cover type:string default:-- desc:图片
- * @return name:status type:int default:-- desc:状态码(1未使用2已使用3已过期)
- *
- */
- public function getUserCouponList()
- {
- $status = input('get.status',1);
- $module = input('get.module',1);
- $list = UserCouponList::where(['l.user_id'=>$this->user_id,'l.status'=>$status])
- ->alias('l')
- ->field('l.*,c.title,c.module,c.coupon_type,c.is_new,c.cover')
- ->when($module,function ($query)use ($module){
- if($module > 1 ) $query->where('module','in','1,'. $module);
- })->leftJoin('CouponConfig c','c.id = l.coupon_id')
- ->limit($this->off_set,$this->page_num)
- ->order('l.id desc')
- ->select()->toArray();
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @title 兑换码兑换优惠券
- * @desc 兑换码兑换优惠券
- * @author qc
- * @url /api/Coupon/getCouponByCode
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:code type:string default:1 desc:兑换码
- *
- */
- public function getCouponByCode()
- {
- $code = input('post.code');
- if(!$code) $this->error('请输入兑换码');
- $coupon_list = CouponConfig::where(['status'=>1,'is_deleted'=>0,'code'=>$code])->select()->toArray();
- if(empty($coupon_list))$this->error('兑换码错误');
- $exchange_num = 0;
- foreach ($coupon_list as $v) {
- $has_num = UserCouponList::getUserCouponNum($this->user_id,$v['id']);
- if($has_num >= $v['user_num']) continue;
- $exchange_num++;
- UserCouponList::saveUserCoupon($v,$this->user_id);
- }
- $exchange_num ? $this->success('兑换成功:'.$exchange_num) : $this->error('兑换失败');
- }
- }
|