搜索
首页后端开发PHP问题php优惠券的实现方式是什么

php优惠券的实现方式是什么

Dec 01, 2021 am 09:54 AM
php领取优惠券

php优惠券的实现方式:1、创建一个前端文件,并判断优惠券是否存在或者停用;2、创建一个PHP示例文件;3、通过“public function doCoupon($params){...}”等方法处理优惠券领取情况即可。

php优惠券的实现方式是什么

本文操作环境:windows7系统、PHP7.4版、DELL G3电脑

php优惠券的实现方式是什么?

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

业务需求

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

代码环境

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

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

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

1 判断优惠券是否存在或者停用
2 判断活动开始时间与优惠券开始时间

接着领取活动优惠券,需要判断以下情况:
1 活动已结束
2 活动为开始时
3 活动为新用户领取,而领取的用户是老用户
4 活动为老用户领取,而领取的用户是新用户
5 优惠券是否领取完
6 已领取过优惠券提示
7 领取成功

下面核心代码实现

/**
 * Function:优惠券领取处理
 * Author:cyw0413
 * @param $params
 * @return array
 * @throws \Exception
 */
public function doCoupon($params)
{
  $activity_id = $params['activity_id'];
  if(!$params){
    throw new \Exception("参数错误!");
  }

  $preg_phone = '/^1[34578]\d{9}$/ims';
  $is_mobile = preg_match ($preg_phone, $params['mobile']);
  if ($is_mobile == 0) {
    throw new \Exception("手机号码不正确!");
  }

  //隐藏手机号码中间4位
  $str_mobile = substr_replace($params['mobile'],'****',3,4);

  $activity = $this->find($activity_id);
  if(empty($activity)){
    throw new \Exception("不存在此活动");
  }

  $activity_link = $activity->activityLink->where('coupon_status',0); //只选择不停用的优惠券
  if(count($activity_link) <= 0){
    throw new \Exception("优惠券不存在或者已经停用");

  }else{

    //查找注册用户ID
    $showUser = $this->showUser($params[&#39;mobile&#39;]);
    //主要是过滤掉领取优惠券为0的,用laravel的同学注意看看
    $detail = $activity_link->each(function($item,$index) use ($showUser) {

      $diffCouponQuantity = $this->diffCouponQuantity($item[&#39;config_id&#39;],$item[&#39;quantity&#39;],$item[&#39;activity_id&#39;],$showUser);
      $item->title = $this->getCouponName($item[&#39;config_id&#39;])[&#39;name&#39;];
      $item->number = $item[&#39;quantity&#39;];
      $item->msg  = $diffCouponQuantity [&#39;msg&#39;];
      $item->diff   = $diffCouponQuantity [&#39;diff&#39;];
      $item->code   = $diffCouponQuantity [&#39;code&#39;];
    })->toArray();

    if(count($detail) == 1){
      foreach($detail as $val){
        if($val[&#39;diff&#39;] == 1 && $val[&#39;code&#39;] == &#39;400&#39;){
          throw new \Exception($detail[0][&#39;msg&#39;]);
        }
      }

    }

    $collection_coupon = collect($detail);
    $collection_coupon = $collection_coupon->where(&#39;diff&#39;, &#39;<=&#39; ,&#39;0&#39;);  //去除优惠券剩余数量为0,或者领取优惠券数量-剩余数量 > 0

  }
  //判断活动开始时间与优惠券开始时间
  $act_coupon = ActivityCouponBaseModel::where(&#39;activity_id&#39;,$activity[&#39;activity_id&#39;])->first();
  $check_time = $this-> checkCouponTime($act_coupon[&#39;start_time&#39;],$activity_link);
  if($check_time == &#39;error&#39;){
    throw new \Exception("优惠券领取时间未开始,暂不可领取");
  }

  //领取活动有以下几种情况
  //1: 活动已结束
  if($activity[&#39;end_time&#39;] < date("Y-m-d H:i:s") || $activity[&#39;status&#39;] == 1){
    $result = [
      &#39;code&#39; => 1,
    ];
    return $result;
  }

  //6 活动为开始时
  if($activity[&#39;start_time&#39;] > date("Y-m-d H:i:s") || $activity[&#39;status&#39;] == 1){
    $result = [
      &#39;code&#39; => 6,
    ];
    return $result;

  }

  $checkUser = $this->haveUser($params[&#39;mobile&#39;]); //检查是新用户,还是老用户 根据自己的业务需求做,这个方法就不贴了
  //2: 活动为新用户领取,而领取的用户是老用户
  if($activity[&#39;user_type&#39;] == 1 && !empty($checkUser)){
    $result = [
      &#39;code&#39; => 2,
    ];
    return $result;
  }

  //3:活动为老用户领取,而领取的用户是新用户
  if($activity[&#39;user_type&#39;]==2 && empty($checkUser)){
    $result = [
      &#39;code&#39; => 3,
    ];
    return $result;
  }


  //4:优惠券是否领取完
  $coupon = $this->getCouponExpire($collection_coupon,$params[&#39;mobile&#39;]); //这里提示有一个优惠券列表,根据自己的业务需求做,这个方法就不贴了
  //return $coupon;
  if($coupon == 1){
    $result = [
      &#39;code&#39; => 4,
    ];
    return $result;
  }

  //5:已领取过优惠券提示
  $userCoupon = &#39;&#39;;
  $userRate = &#39;&#39;;
  if(!empty($checkUser)){
    //user存在则为老用户,再检查是否领取过
    $userCoupon = $this->getUserCoupon($collection_coupon,$checkUser[&#39;user_id&#39;]);
    $userRate = $this->getUserCouponRate($checkUser[&#39;user_id&#39;],$activity[&#39;activity_id&#39;]);
  }else{
    //新用户,检查是否注册过
    $var_user = UserBaseModel::where(&#39;user_name&#39;,$params[&#39;mobile&#39;])->first();
    if(!empty($var_user)){
      $userCoupon = $this->getUserCoupon($collection_coupon,$var_user[&#39;user_id&#39;]);
      $userRate = $this->getUserCouponRate($var_user[&#39;user_id&#39;],$activity[&#39;activity_id&#39;]);
    }
  }

  //return $userRate;

  if($userCoupon == 1){
    $result = [
      &#39;code&#39; => 5,
      &#39;phone&#39;=> $str_mobile,
      &#39;coupon&#39; => $userRate,
      &#39;is_get&#39; => false,
    ];
    return $result;
  }

  //5:领取成功
  //如果活动规定是新老用户0,新用户1,老用户2
  $getCouponSuccess = $this->getCouponSuccess($activity[&#39;user_type&#39;],$checkUser,$collection_coupon,$params[&#39;mobile&#39;]);
  //return $getCouponSuccess;
  if($getCouponSuccess[&#39;status&#39;] == 200){
    $result = [
      &#39;code&#39; => 5,
      &#39;phone&#39;=> $str_mobile,
      &#39;coupon&#39; => $getCouponSuccess[&#39;result&#39;][0],
      &#39;is_get&#39; => true,
    ];
    return $result;
  }


}

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

/**
 * Function:用户领取活动
 * Author:cyw0413
 * @param $user_type
 */
public function getCouponSuccess($user_type,$user,$coupon,$mobile)
{
  if(count($coupon) > 0){

    switch ($user_type){
      case 1:
        //新用户领取,如果从来没注册过就要新增用户
        $res = $this->addUser($mobile,$coupon); 
        return [
          &#39;result&#39; => $res,
          &#39;status&#39; => 200
        ];
        break;
      case 2:
        //老用户领取
        $res = $this->insertUserCoupon($user,$coupon);
        return [
          &#39;result&#39; => $res,
          &#39;status&#39; => 200
        ];
        break;
      default:
        //新老用户领取,判断是新用户还是老用户,这里的$user是有无配送单,有则为老用户;
        if(empty($user)){
          $res = $this->addUser($mobile,$coupon);
        }else{

          $res = $this->insertUserCoupon($user,$coupon); //老用户,直接发放优惠券
        }
        return [
          &#39;result&#39; => $res,
          &#39;status&#39; => 200
        ];
        break;
    }
  }else{
    throw new \Exception("优惠券不存在或者已经停用");
  }


}

领取成功,则发放优惠券

/**
 * Function:发放优惠券
 * Author:cyw0413
 * @param $user
 * @param $coupon
 */
public function insertUserCoupon($user,$coupon)
{
  $relate = [];
  foreach($coupon as $item){

    $res = CouponConfigSendBaseModel::where([
      &#39;config_id&#39;=>$item[&#39;config_id&#39;],
      &#39;status&#39;  => 0,
    ])->first();

    if(empty($res) || (!empty($res) && $res[&#39;is_send&#39;] == 0) ){
      throw new \Exception("优惠券未发放,暂不可领取");
    }

    //发放优惠券,有多少张就添加多少张,这里扣除优惠券时,主要用不同的coupon_sn来区别
    $onlyCoupon = $this->getCouponName($item[&#39;config_id&#39;]);
    if ($onlyCoupon[&#39;expire_type&#39;] == 0) {
      $start_time = $onlyCoupon[&#39;expire_start_time&#39;];
      $end_time = $onlyCoupon[&#39;expire_end_time&#39;];
    } else {
      $start_time = date(&#39;Y-m-d H:i:s&#39;);
      $end_time = date(&#39;Y-m-d H:i:s&#39;, time()+86400*$onlyCoupon[&#39;expire_type&#39;]);
    }

    $result = [
      &#39;user_id&#39;  => $user[&#39;user_id&#39;],
      &#39;config_id&#39; => $item[&#39;config_id&#39;],
      &#39;name&#39;   => $onlyCoupon[&#39;name&#39;],
      &#39;get_type&#39; => $onlyCoupon[&#39;get_type&#39;],
      &#39;amount&#39;  => $onlyCoupon[&#39;amount&#39;],
      &#39;require_price&#39; => $onlyCoupon[&#39;require_price&#39;],
      &#39;status&#39;    => 1,
      &#39;start_time&#39;  => $start_time,
      &#39;end_time&#39;   => $end_time,
    ];
    for($i=0; $i < $item[&#39;quantity&#39;];$i++){
      $result[&#39;coupon_sn&#39;] = &#39;B&#39;.mt_rand(1, 10000) . strtoupper(uniqid(mt_rand(1, 10000)));
      $userCoupon = UserCouponBaseModel::create($result);
    }

    //扣除相应的优惠券数量,这里用到了锁表,防止并发时,优惠券为-1
    $couponConfig = CouponConfigBaseModel::where(&#39;config_id&#39;,$item[&#39;config_id&#39;])->lockForUpdate()->first();
    if($couponConfig->left_quantity > 0 ){
      if($couponConfig->left_quantity >= $item[&#39;quantity&#39;]){
        $couponConfig->left_quantity = $couponConfig->left_quantity-$item[&#39;quantity&#39;];
        $couponConfig->save();
      }else{
        throw new \Exception("优惠券剩余数量不够扣减");
      }

    }


    $relate = [
      &#39;coupon_id&#39; => $userCoupon->coupon_id,
      &#39;user_id&#39;  => $user[&#39;user_id&#39;],
      &#39;config_id&#39; => $item[&#39;config_id&#39;],
      &#39;activity_id&#39; => $item[&#39;activity_id&#39;]
    ];

    ActivityCouponUserRelateBaseModel::create($relate);

    $relate[] = $this->getUserCouponRate($user[&#39;user_id&#39;],$item[&#39;activity_id&#39;]);


  }

  return $relate;
}

推荐学习:《PHP视频教程

以上是php优惠券的实现方式是什么的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
酸与基本数据库:差异和何时使用。酸与基本数据库:差异和何时使用。Mar 26, 2025 pm 04:19 PM

本文比较了酸和基本数据库模型,详细介绍了它们的特征和适当的用例。酸优先确定数据完整性和一致性,适合财务和电子商务应用程序,而基础则侧重于可用性和

PHP安全文件上传:防止与文件相关的漏洞。PHP安全文件上传:防止与文件相关的漏洞。Mar 26, 2025 pm 04:18 PM

本文讨论了确保PHP文件上传的确保,以防止诸如代码注入之类的漏洞。它专注于文件类型验证,安全存储和错误处理以增强应用程序安全性。

PHP输入验证:最佳实践。PHP输入验证:最佳实践。Mar 26, 2025 pm 04:17 PM

文章讨论了PHP输入验证以增强安全性的最佳实践,重点是使用内置功能,白名单方法和服务器端验证等技术。

PHP API率限制:实施策略。PHP API率限制:实施策略。Mar 26, 2025 pm 04:16 PM

本文讨论了在PHP中实施API速率限制的策略,包括诸如令牌桶和漏水桶等算法,以及使用Symfony/Rate-limimiter之类的库。它还涵盖监视,动态调整速率限制和手

php密码哈希:password_hash和password_verify。php密码哈希:password_hash和password_verify。Mar 26, 2025 pm 04:15 PM

本文讨论了使用password_hash和pyspasswify在PHP中使用密码的好处。主要论点是,这些功能通过自动盐,强大的哈希算法和SECH来增强密码保护

OWASP前10 php:描述并减轻常见漏洞。OWASP前10 php:描述并减轻常见漏洞。Mar 26, 2025 pm 04:13 PM

本文讨论了OWASP在PHP和缓解策略中的十大漏洞。关键问题包括注射,验证损坏和XSS,并提供用于监视和保护PHP应用程序的推荐工具。

PHP XSS预防:如何预防XSS。PHP XSS预防:如何预防XSS。Mar 26, 2025 pm 04:12 PM

本文讨论了防止PHP中XSS攻击的策略,专注于输入消毒,输出编码以及使用安全增强的库和框架。

PHP接口与抽象类:何时使用。PHP接口与抽象类:何时使用。Mar 26, 2025 pm 04:11 PM

本文讨论了PHP中接口和抽象类的使用,重点是何时使用。界面定义了无实施的合同,适用于无关类和多重继承。摘要类提供常见功能

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具