当前位置: 首页 > 后端技术 > PHP

使用PHP制作优惠券领取活动

时间:2023-03-29 15:03:31 PHP

业务需要优惠券活动,根据自己的需要。以下是最近实现的优惠券活动,主要业务需求:根据后台设置优惠券模板,用户类型设置,优惠券活动起止时间,最终生成不同的优惠券活动链接。代码环境:源码主要是laravel5.8。整个活动有很多代码要贴出来。下面主要贴出核心代码,仅供参考。主要是根据自己的业务需求实现功能。以下是后台截图。制作模块化前端需要做的设置和限制:1.判断优惠券是否存在或禁用。结束2活动开始时间3活动被新用户领取,领取的用户为老用户4活动被老用户领取,领取的用户为新用户5是否优惠券已领取6已领取优惠券提醒7领取成功**核心代码实现如下**/***功能:优惠券领取处理*Author:cyw0413*@param$params*@returnarray*@throws\Exception*/publicfunctiondoCoupon($params){$activity_id=$params['activity_id'];if(!$params){thrownew\Exception("参数错误!");}$preg_phone='/^1[34578]\d{9}$/ims';$is_mobile=preg_match($preg_phone,$params['mobile']);if($is_mobile==0){thrownew\Exception("手机号码不正确!");}//隐藏手机号中间4位$str_mobile=substr_replace($params['mobile'],'****',3,4);$activity=$this->find($activity_id);if(empty($activity)){thrownew\Exception("这个活动不存在");$activity_link=$activity->activityLink->where('coupon_status',0);//只选择不停的优惠券if(count($activity_link)<=0){thrownew\Exception("优惠券不存在或已被禁用");}别的{//查找注册用户的ID$showUser=$this->showUser($params['mobile']);//主要过滤掉领取优惠券的为0,使用laravel的同学注意$detail=$activity_link->each(function($item,$index)use($showUser){$diffCouponQuantity=$this->diffCouponQuantity($item['config_id'],$item['数量'],$item['activity_id'],$showUser);$item->title=$this->getCouponName($item['config_id'])['name'];$item->number=$item['quantity'];$item->msg=$diffCouponQuantity['msg'];$item->diff=$diffCouponQuantity['diff'];$item->code=$diffCouponQuantity['code'];})->toArray();if(count($detail)==1){foreach($detailas$val){if($val['diff']==1&&$val['code']=='400'){thrownew\Exception($detail[0]['msg']);}}}$collection_coupon=collect($detail);$collection_coupon=$collection_coupon->where('diff','<=','0');//取出剩余优惠券数量为0,或领取优惠券-剩余数量>0}//以优惠券开始时间判断活动开始时间$act_coupon=ActivityCouponBaseModel::where('activity_id',$activity['activity_id'])->第一个();$check_time=$this->checkCouponTime($act_coupon['start_time'],$activity_link);if($check_time=='error'){thrownew\Exception("优惠券领取时间尚未开始,无法领取");}//采集活动有几种情况//1:活动已经结束if($activity['end_time']1,];返回$结果;}//6当活动开始时if($activity['start_time']>date("Y-m-dH:i:s")||$activity['status']==1){$result=['code'=>6,];返回$结果;$checkUser=$this->haveUser($params['mobile']);//根据自己的业务需要判断是新用户还是老用户,这个方法是不要再发帖了//2:活动被新用户认领,认领的用户是老用户if($activity['user_type']==1&&!empty($checkUser)){$result=['code'=>2,];返回$结果;}//3:活动被老用户认领,认领的用户是新用户if($activity['user_type']==2&&empty($checkUser)){$result=['code'=>3,];返回$结果;}//4:优惠券是否已经领取$coupon=$this->getCouponExpire($collection_coupon,$params['mobile']);//这里提醒一下,有优惠券列表,根据自己的业务需要做,这个方法不会post//return$coupon;如果($coupon==1){$result=['code'=>4,];返回$结果;}//5:优惠券已领取提醒$userCoupon='';$userRate='';if(!empty($checkUser)){//如果用户存在,则为老用户,然后检查是否已领取$userCoupon=$this->getUserCoupon($collection_coupon,$checkUser['user_id']);$userRate=$this->getUserCouponRate($checkUser['user_id'],$activity['activity_id']);}else{//新用户,检查是否注册$var_user=UserBaseModel::where('user_name',$params['mobile'])->first();如果(!empty($var_user)){$userCoupon=$this->getUserCoupon($collection_coupon,$var_user['user_id']);$userRate=$this->getUserCouponRate($var_user['user_id'],$activity['activity_id']);}}//返回$userRate;如果($userCoupon==1){$result=['code'=>5,'phone'=>$str_mobile,'coupon'=>$userRate,'is_get'=>false,];返回$结果;}//5:接收成功//如果活动规定新老用户为0,新用户为1,老用户为2$getCouponSuccess=$this->getCouponSuccess($activity['user_type'],$checkUser,$collection_coupon,$params['mobile']);//返回$getCouponSuccess;if($getCouponSuccess['status']==200){$result=['code'=>5,'phone'=>$str_mobile,'coupon'=>$getCouponSuccess['result'][0],'is_get'=>真,];返回$结果;}}用户领取优惠券并发放优惠券/***功能:用户领取活动*作者:cyw0413*@param$user_type*/publicfunctiongetCouponSuccess($user_type,$user,$coupon,$mobile){if(count($coupon)>0){switch($user_type){case1://新用户收到,如果如果您从未注册过,则需要添加一个新用户$res=$this->addUser($mobile,$coupon);返回['结果'=>$res,'状态'=>200];休息;case2://老用户收到$res=$this->insertUserCoupon($user,$coupon);返回['结果'=>$res,'状态'=>200];休息;default://新老用户接收,判断yes无论是新用户还是老用户,这里的$user表示是否有发货订单,或者是老用户;if(empty($user)){$res=$this->addUser($mobile,$coupon);}else{$res=$this->insertUserCoupon($user,$coupon);//老用户,直接发优惠券}返回['结果'=>$res,'状态'=>200];休息;}}else{thrownew\Exception("优惠券不存在或已被禁用");}}领取优惠券成功,则发放优惠券/***功能:发放优惠券*Author:cyw0413*@param$user*@param$coupon*/publicfunctioninsertUserCoupon($user,$coupon){$相关=[];foreach($couponas$item){$res=CouponConfigSendBaseModel::where(['config_id'=>$item['config_id'],'status'=>0,])->first();if(empty($res)||(!empty($res)&&$res['is_send']==0)){thrownew\Exception("优惠券尚未发放,暂时无法领取");}//分发优惠券,有多少就加多少,这里扣优惠券的时候主要用不同的coupon_sn来区分$onlyCoupon=$this->getCouponName($item['config_id']);如果($onlyCoupon['expire_type']==0){$start_time=$onlyCoupon['expire_start_time'];$end_time=$onlyCoupon['expire_end_time'];}别的{$start_time=date('Y-m-dH:i:s');$end_time=date('Y-m-dH:i:s',time()+86400*$onlyCoupon['expire_type']);}$result=['user_id'=>$user['user_id'],'config_id'=>$item['config_id'],'name'=>$onlyCoupon['name'],'get_type'=>$onlyCoupon['get_type'],'amount'=>$onlyCoupon['amount'],'require_price'=>$onlyCoupon['require_price'],'status'=>1,'start_time'=>$start_time,'end_time'=>$end_time,];for($i=0;$i<$item['quantity'];$i++){$result['coupon_sn']='B'.mt_rand(1,10000).strtoupper(uniqid(mt_rand(1,10000))));$userCoupon=UserCouponBaseModel::create($result);}//扣除相应的coupon数量,这里使用锁表来防止并发,coupon为-1$couponConfig=CouponConfigBaseModel::where('config_id',$item['config_id'])->lockForUpdate()->first();if($couponConfig->left_quantity>0){if($couponConfig->left_quantity>=$item['quantity']){$couponConfig->left_quantity=$couponConfig->left_quantity-$item['quantity'];$couponConfig->保存();}else{thrownew\Exception("优惠券余数不足扣减");}}$relate=['coupon_id'=>$userCoupon->coupon_id,'user_id'=>$user['user_id'],'config_id'=>$item['config_id'],'activity_id'=>$item['activity_id']];ActivityCouponUserRelateBaseModel::create($relate);$relate[]=$this->getUserCouponRate($user['user_id'],$item['activity_id']);}返回$relate;}