当前位置:首页 > PHP教程 > php应用 > 列表

用PHP做了一个领取优惠券活动的示例代码

发布:smiling 来源: PHP粉丝网  添加日期:2021-12-02 15:16:10 浏览: 评论:0 

这篇文章主要介绍了用PHP做了一个领取优惠券活动的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

业务需求

优惠券活动,具体还是要根据自己的需求。以下是最近实现的优惠券活动,主要的业务需求:根据后端设置优惠券模板,用户类型设置,优惠券活动的开始与结束时间,最后生成不同的优惠券活动链接。

代码环境:

源码主要laravel5.8,一整个活动要贴的代码很多,下面主要贴核心代码,仅供参考。主要还是要根据自己的业务需求来实现功能吧。

以下是后端截图,做成模块化

PHP领取优惠券

前端需要做的设置与限制:

1 判断优惠券是否存在或者停用

2 判断活动开始时间与优惠券开始时间

接着领取活动优惠券,需要判断以下情况:

1 活动已结束

2 活动为开始时

3 活动为新用户领取,而领取的用户是老用户

4 活动为老用户领取,而领取的用户是新用户

5 优惠券是否领取完

6 已领取过优惠券提示

7 领取成功

下面核心代码实现

  1. /** 
  2.  * Function:优惠券领取处理 
  3.  * Author:cyw0413 
  4.  * @param $params 
  5.  * @return array 
  6.  * @throws \Exception 
  7.  */ 
  8. public function doCoupon($params) 
  9. { 
  10.   $activity_id = $params['activity_id']; 
  11.   if(!$params){ 
  12.     throw new \Exception("参数错误!"); 
  13.   } 
  14.  
  15.   $preg_phone = '/^1[34578]\d{9}$/ims'; 
  16.   $is_mobile = preg_match ($preg_phone, $params['mobile']); 
  17.   if ($is_mobile == 0) { 
  18.     throw new \Exception("手机号码不正确!"); 
  19.   } 
  20.  
  21.   //隐藏手机号码中间4位 
  22.   $str_mobile = substr_replace($params['mobile'],'****',3,4); 
  23.  
  24.   $activity = $this->find($activity_id); 
  25.   if(emptyempty($activity)){ 
  26.     throw new \Exception("不存在此活动"); 
  27.   } 
  28.  
  29.   $activity_link = $activity->activityLink->where('coupon_status',0); //只选择不停用的优惠券 
  30.   if(count($activity_link) <= 0){ 
  31.     throw new \Exception("优惠券不存在或者已经停用"); 
  32.  
  33.   }else{ 
  34.  
  35.     //查找注册用户ID 
  36.     $showUser = $this->showUser($params['mobile']); 
  37.     //主要是过滤掉领取优惠券为0的,用laravel的同学注意看看 
  38.     $detail = $activity_link->each(function($item,$index) use ($showUser) { 
  39.  
  40.       $diffCouponQuantity = $this->diffCouponQuantity($item['config_id'],$item['quantity'],$item['activity_id'],$showUser); 
  41.       $item->title = $this->getCouponName($item['config_id'])['name']; 
  42.       $item->number = $item['quantity']; 
  43.       $item->msg  = $diffCouponQuantity ['msg']; 
  44.       $item->diff   = $diffCouponQuantity ['diff']; 
  45.       $item->code   = $diffCouponQuantity ['code']; 
  46.     })->toArray(); 
  47.  
  48.     if(count($detail) == 1){ 
  49.       foreach($detail as $val){ 
  50.         if($val['diff'] == 1 && $val['code'] == '400'){ 
  51.           throw new \Exception($detail[0]['msg']); 
  52.         } 
  53.       } 
  54.  
  55.     } 
  56.  
  57.     $collection_coupon = collect($detail); 
  58.     $collection_coupon = $collection_coupon->where('diff', '<=' ,'0');  //去除优惠券剩余数量为0,或者领取优惠券数量-剩余数量 > 0 
  59.  
  60.   } 
  61.   //判断活动开始时间与优惠券开始时间 
  62.   $act_coupon = ActivityCouponBaseModel::where('activity_id',$activity['activity_id'])->first(); 
  63.   $check_time = $this-> checkCouponTime($act_coupon['start_time'],$activity_link); 
  64.   if($check_time == 'error'){ 
  65.     throw new \Exception("优惠券领取时间未开始,暂不可领取"); 
  66.   } 
  67.  
  68.   //领取活动有以下几种情况 
  69.   //1: 活动已结束 
  70.   if($activity['end_time'] < date("Y-m-d H:i:s") || $activity['status'] == 1){ 
  71.     $result = [ 
  72.       'code' => 1, 
  73.     ]; 
  74.     return $result; 
  75.   } 
  76.  
  77.   //6 活动为开始时 
  78.   if($activity['start_time'] > date("Y-m-d H:i:s") || $activity['status'] == 1){ 
  79.     $result = [ 
  80.       'code' => 6, 
  81.     ]; 
  82.     return $result; 
  83.  
  84.   } 
  85.  
  86.   $checkUser = $this->haveUser($params['mobile']); //检查是新用户,还是老用户 根据自己的业务需求做,这个方法就不贴了 
  87.   //2: 活动为新用户领取,而领取的用户是老用户 
  88.   if($activity['user_type'] == 1 && !emptyempty($checkUser)){ 
  89.     $result = [ 
  90.       'code' => 2, 
  91.     ]; 
  92.     return $result; 
  93.   } 
  94.  
  95.   //3:活动为老用户领取,而领取的用户是新用户 
  96.   if($activity['user_type']==2 && emptyempty($checkUser)){ 
  97.     $result = [ 
  98.       'code' => 3, 
  99.     ]; 
  100.     return $result; 
  101.   } 
  102.  
  103.  
  104.   //4:优惠券是否领取完 
  105.   $coupon = $this->getCouponExpire($collection_coupon,$params['mobile']); //这里提示有一个优惠券列表,根据自己的业务需求做,这个方法就不贴了 
  106.   //return $coupon; 
  107.   if($coupon == 1){ 
  108.     $result = [ 
  109.       'code' => 4, 
  110.     ]; 
  111.     return $result; 
  112.   } 
  113.  
  114.   //5:已领取过优惠券提示 
  115.   $userCoupon = ''; 
  116.   $userRate = ''; 
  117.   if(!emptyempty($checkUser)){ 
  118.     //user存在则为老用户,再检查是否领取过 
  119.     $userCoupon = $this->getUserCoupon($collection_coupon,$checkUser['user_id']); 
  120.     $userRate = $this->getUserCouponRate($checkUser['user_id'],$activity['activity_id']); 
  121.   }else{ 
  122.     //新用户,检查是否注册过 
  123.     $var_user = UserBaseModel::where('user_name',$params['mobile'])->first(); 
  124.     if(!emptyempty($var_user)){ 
  125.       $userCoupon = $this->getUserCoupon($collection_coupon,$var_user['user_id']); 
  126.       $userRate = $this->getUserCouponRate($var_user['user_id'],$activity['activity_id']); 
  127.     } 
  128.   } 
  129.  
  130.   //return $userRate; 
  131.  
  132.   if($userCoupon == 1){ 
  133.     $result = [ 
  134.       'code' => 5, 
  135.       'phone'=> $str_mobile, 
  136.       'coupon' => $userRate, 
  137.       'is_get' => false, 
  138.     ]; 
  139.     return $result; 
  140.   } 
  141.  
  142.   //5:领取成功 
  143.   //如果活动规定是新老用户0,新用户1,老用户2 
  144.   $getCouponSuccess = $this->getCouponSuccess($activity['user_type'],$checkUser,$collection_coupon,$params['mobile']); 
  145.   //return $getCouponSuccess; 
  146.   if($getCouponSuccess['status'] == 200){ 
  147.     $result = [ 
  148.       'code' => 5, 
  149.       'phone'=> $str_mobile, 
  150.       'coupon' => $getCouponSuccess['result'][0], 
  151.       'is_get' => true, 
  152.     ]; 
  153.     return $result; 
  154.   } 
  155.  
  156.  
  157. } 

用户领取优惠券并发放优惠券

  1. /** 
  2.  * Function:用户领取活动 
  3.  * Author:cyw0413 
  4.  * @param $user_type 
  5.  */ 
  6. public function getCouponSuccess($user_type,$user,$coupon,$mobile) 
  7. { 
  8.   if(count($coupon) > 0){ 
  9.  
  10.     switch ($user_type){ 
  11.       case 1: 
  12.         //新用户领取,如果从来没注册过就要新增用户 
  13.         $res = $this->addUser($mobile,$coupon);  
  14.         return [ 
  15.           'result' => $res, 
  16.           'status' => 200 
  17.         ]; 
  18.         break; 
  19.       case 2: 
  20.         //老用户领取 
  21.         $res = $this->insertUserCoupon($user,$coupon); 
  22.         return [ 
  23.           'result' => $res, 
  24.           'status' => 200 
  25.         ]; 
  26.         break; 
  27.       default: 
  28.         //新老用户领取,判断是新用户还是老用户,这里的$user是有无配送单,有则为老用户; 
  29.         if(emptyempty($user)){ 
  30.           $res = $this->addUser($mobile,$coupon); 
  31.         }else{ 
  32.  
  33.           $res = $this->insertUserCoupon($user,$coupon); //老用户,直接发放优惠券 
  34.         } 
  35.         return [ 
  36.           'result' => $res, 
  37.           'status' => 200 
  38.         ]; 
  39.         break; 
  40.     } 
  41.   }else{ 
  42.     throw new \Exception("优惠券不存在或者已经停用"); 
  43.   } 
  44. } 

领取成功,则发放优惠券

  1. /** 
  2.  * Function:发放优惠券 
  3.  * Author:cyw0413 
  4.  * @param $user 
  5.  * @param $coupon 
  6.  */ 
  7. public function insertUserCoupon($user,$coupon) 
  8. { 
  9.   $relate = []; 
  10.   foreach($coupon as $item){ 
  11.  
  12.     $res = CouponConfigSendBaseModel::where([ 
  13.       'config_id'=>$item['config_id'], 
  14.       'status'  => 0, 
  15.     ])->first(); 
  16.  
  17.     if(emptyempty($res) || (!emptyempty($res) && $res['is_send'] == 0) ){ 
  18.       throw new \Exception("优惠券未发放,暂不可领取"); 
  19.     } 
  20.  
  21.     //发放优惠券,有多少张就添加多少张,这里扣除优惠券时,主要用不同的coupon_sn来区别 
  22.     $onlyCoupon = $this->getCouponName($item['config_id']); 
  23.     if ($onlyCoupon['expire_type'] == 0) { 
  24.       $start_time = $onlyCoupon['expire_start_time']; 
  25.       $end_time = $onlyCoupon['expire_end_time']; 
  26.     } else { 
  27.       $start_time = date('Y-m-d H:i:s'); 
  28.       $end_time = date('Y-m-d H:i:s', time()+86400*$onlyCoupon['expire_type']); 
  29.     } 
  30.  
  31.     $result = [ 
  32.       'user_id'  => $user['user_id'], 
  33.       'config_id' => $item['config_id'], 
  34.       'name'   => $onlyCoupon['name'], 
  35.       'get_type' => $onlyCoupon['get_type'], 
  36.       'amount'  => $onlyCoupon['amount'], 
  37.       'require_price' => $onlyCoupon['require_price'], 
  38.       'status'    => 1, 
  39.       'start_time'  => $start_time, 
  40.       'end_time'   => $end_time, 
  41.     ]; 
  42.     for($i=0; $i < $item['quantity'];$i++){ 
  43.       $result['coupon_sn'] = 'B'.mt_rand(1, 10000) . strtoupper(uniqid(mt_rand(1, 10000))); 
  44.       $userCoupon = UserCouponBaseModel::create($result); 
  45.     } 
  46.  
  47.     //扣除相应的优惠券数量,这里用到了锁表,防止并发时,优惠券为-1 
  48.     $couponConfig = CouponConfigBaseModel::where('config_id',$item['config_id'])->lockForUpdate()->first(); 
  49.     if($couponConfig->left_quantity > 0 ){ 
  50.       if($couponConfig->left_quantity >= $item['quantity']){ 
  51.         $couponConfig->left_quantity = $couponConfig->left_quantity-$item['quantity']; 
  52.         $couponConfig->save(); 
  53.       }else{ 
  54.         throw new \Exception("优惠券剩余数量不够扣减"); 
  55.       } 
  56.  
  57.     } 
  58.  
  59.  
  60.     $relate = [ 
  61.       'coupon_id' => $userCoupon->coupon_id, 
  62.       'user_id'  => $user['user_id'], 
  63.       'config_id' => $item['config_id'], 
  64.       'activity_id' => $item['activity_id'] 
  65.     ]; 
  66.  
  67.     ActivityCouponUserRelateBaseModel::create($relate); 
  68.  
  69.     $relate[] = $this->getUserCouponRate($user['user_id'],$item['activity_id']); 
  70.  
  71.  
  72.   } 
  73.  
  74.   return $relate; 
  75. }

Tags: PHP领取优惠券

分享到: