佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 用PHP做了一個領(lǐng)取優(yōu)惠券活動的示例代碼

用PHP做了一個領(lǐng)取優(yōu)惠券活動的示例代碼

熱門標(biāo)簽:Mysql連接數(shù)設(shè)置 銀行業(yè)務(wù) 阿里云 團購網(wǎng)站 科大訊飛語音識別系統(tǒng) 服務(wù)器配置 電子圍欄 Linux服務(wù)器

業(yè)務(wù)需求

優(yōu)惠券活動,具體還是要根據(jù)自己的需求。以下是最近實現(xiàn)的優(yōu)惠券活動,主要的業(yè)務(wù)需求:根據(jù)后端設(shè)置優(yōu)惠券模板,用戶類型設(shè)置,優(yōu)惠券活動的開始與結(jié)束時間,最后生成不同的優(yōu)惠券活動鏈接。

代碼環(huán)境

源碼主要laravel5.8,一整個活動要貼的代碼很多,下面主要貼核心代碼,僅供參考。主要還是要根據(jù)自己的業(yè)務(wù)需求來實現(xiàn)功能吧。

以下是后端截圖,做成模塊化

前端需要做的設(shè)置與限制:

1 判斷優(yōu)惠券是否存在或者停用
2 判斷活動開始時間與優(yōu)惠券開始時間

接著領(lǐng)取活動優(yōu)惠券,需要判斷以下情況:
1 活動已結(jié)束
2 活動為開始時
3 活動為新用戶領(lǐng)取,而領(lǐng)取的用戶是老用戶
4 活動為老用戶領(lǐng)取,而領(lǐng)取的用戶是新用戶
5 優(yōu)惠券是否領(lǐng)取完
6 已領(lǐng)取過優(yōu)惠券提示
7 領(lǐng)取成功

下面核心代碼實現(xiàn)

/**
 * Function:優(yōu)惠券領(lǐng)取處理
 * Author:cyw0413
 * @param $params
 * @return array
 * @throws \Exception
 */
public function doCoupon($params)
{
  $activity_id = $params['activity_id'];
  if(!$params){
    throw new \Exception("參數(shù)錯誤!");
  }

  $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); //只選擇不停用的優(yōu)惠券
  if(count($activity_link) = 0){
    throw new \Exception("優(yōu)惠券不存在或者已經(jīng)停用");

  }else{

    //查找注冊用戶ID
    $showUser = $this->showUser($params['mobile']);
    //主要是過濾掉領(lǐng)取優(yōu)惠券為0的,用laravel的同學(xué)注意看看
    $detail = $activity_link->each(function($item,$index) use ($showUser) {

      $diffCouponQuantity = $this->diffCouponQuantity($item['config_id'],$item['quantity'],$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($detail as $val){
        if($val['diff'] == 1  $val['code'] == '400'){
          throw new \Exception($detail[0]['msg']);
        }
      }

    }

    $collection_coupon = collect($detail);
    $collection_coupon = $collection_coupon->where('diff', '=' ,'0');  //去除優(yōu)惠券剩余數(shù)量為0,或者領(lǐng)取優(yōu)惠券數(shù)量-剩余數(shù)量 > 0

  }
  //判斷活動開始時間與優(yōu)惠券開始時間
  $act_coupon = ActivityCouponBaseModel::where('activity_id',$activity['activity_id'])->first();
  $check_time = $this-> checkCouponTime($act_coupon['start_time'],$activity_link);
  if($check_time == 'error'){
    throw new \Exception("優(yōu)惠券領(lǐng)取時間未開始,暫不可領(lǐng)取");
  }

  //領(lǐng)取活動有以下幾種情況
  //1: 活動已結(jié)束
  if($activity['end_time']  date("Y-m-d H:i:s") || $activity['status'] == 1){
    $result = [
      'code' => 1,
    ];
    return $result;
  }

  //6 活動為開始時
  if($activity['start_time'] > date("Y-m-d H:i:s") || $activity['status'] == 1){
    $result = [
      'code' => 6,
    ];
    return $result;

  }

  $checkUser = $this->haveUser($params['mobile']); //檢查是新用戶,還是老用戶 根據(jù)自己的業(yè)務(wù)需求做,這個方法就不貼了
  //2: 活動為新用戶領(lǐng)取,而領(lǐng)取的用戶是老用戶
  if($activity['user_type'] == 1  !empty($checkUser)){
    $result = [
      'code' => 2,
    ];
    return $result;
  }

  //3:活動為老用戶領(lǐng)取,而領(lǐng)取的用戶是新用戶
  if($activity['user_type']==2  empty($checkUser)){
    $result = [
      'code' => 3,
    ];
    return $result;
  }


  //4:優(yōu)惠券是否領(lǐng)取完
  $coupon = $this->getCouponExpire($collection_coupon,$params['mobile']); //這里提示有一個優(yōu)惠券列表,根據(jù)自己的業(yè)務(wù)需求做,這個方法就不貼了
  //return $coupon;
  if($coupon == 1){
    $result = [
      'code' => 4,
    ];
    return $result;
  }

  //5:已領(lǐng)取過優(yōu)惠券提示
  $userCoupon = '';
  $userRate = '';
  if(!empty($checkUser)){
    //user存在則為老用戶,再檢查是否領(lǐng)取過
    $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();
    if(!empty($var_user)){
      $userCoupon = $this->getUserCoupon($collection_coupon,$var_user['user_id']);
      $userRate = $this->getUserCouponRate($var_user['user_id'],$activity['activity_id']);
    }
  }

  //return $userRate;

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

  //5:領(lǐng)取成功
  //如果活動規(guī)定是新老用戶0,新用戶1,老用戶2
  $getCouponSuccess = $this->getCouponSuccess($activity['user_type'],$checkUser,$collection_coupon,$params['mobile']);
  //return $getCouponSuccess;
  if($getCouponSuccess['status'] == 200){
    $result = [
      'code' => 5,
      'phone'=> $str_mobile,
      'coupon' => $getCouponSuccess['result'][0],
      'is_get' => true,
    ];
    return $result;
  }


}

用戶領(lǐng)取優(yōu)惠券并發(fā)放優(yōu)惠券

/**
 * Function:用戶領(lǐng)取活動
 * Author:cyw0413
 * @param $user_type
 */
public function getCouponSuccess($user_type,$user,$coupon,$mobile)
{
  if(count($coupon) > 0){

    switch ($user_type){
      case 1:
        //新用戶領(lǐng)取,如果從來沒注冊過就要新增用戶
        $res = $this->addUser($mobile,$coupon); 
        return [
          'result' => $res,
          'status' => 200
        ];
        break;
      case 2:
        //老用戶領(lǐng)取
        $res = $this->insertUserCoupon($user,$coupon);
        return [
          'result' => $res,
          'status' => 200
        ];
        break;
      default:
        //新老用戶領(lǐng)取,判斷是新用戶還是老用戶,這里的$user是有無配送單,有則為老用戶;
        if(empty($user)){
          $res = $this->addUser($mobile,$coupon);
        }else{

          $res = $this->insertUserCoupon($user,$coupon); //老用戶,直接發(fā)放優(yōu)惠券
        }
        return [
          'result' => $res,
          'status' => 200
        ];
        break;
    }
  }else{
    throw new \Exception("優(yōu)惠券不存在或者已經(jīng)停用");
  }


}

領(lǐng)取成功,則發(fā)放優(yōu)惠券

/**
 * Function:發(fā)放優(yōu)惠券
 * Author:cyw0413
 * @param $user
 * @param $coupon
 */
public function insertUserCoupon($user,$coupon)
{
  $relate = [];
  foreach($coupon as $item){

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

    if(empty($res) || (!empty($res)  $res['is_send'] == 0) ){
      throw new \Exception("優(yōu)惠券未發(fā)放,暫不可領(lǐng)取");
    }

    //發(fā)放優(yōu)惠券,有多少張就添加多少張,這里扣除優(yōu)惠券時,主要用不同的coupon_sn來區(qū)別
    $onlyCoupon = $this->getCouponName($item['config_id']);
    if ($onlyCoupon['expire_type'] == 0) {
      $start_time = $onlyCoupon['expire_start_time'];
      $end_time = $onlyCoupon['expire_end_time'];
    } else {
      $start_time = date('Y-m-d H:i:s');
      $end_time = date('Y-m-d H: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);
    }

    //扣除相應(yīng)的優(yōu)惠券數(shù)量,這里用到了鎖表,防止并發(fā)時,優(yōu)惠券為-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->save();
      }else{
        throw new \Exception("優(yōu)惠券剩余數(shù)量不夠扣減");
      }

    }


    $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']);


  }

  return $relate;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Android 自定義View之邊緣凹凸的優(yōu)惠券效果的開發(fā)過程
  • 優(yōu)惠券優(yōu)惠的思路以及實踐
  • 使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能

標(biāo)簽:大理 衡水 棗莊 廣元 萍鄉(xiāng) 衢州 蚌埠 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《用PHP做了一個領(lǐng)取優(yōu)惠券活動的示例代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    东辽县| 绍兴市| 鹤岗市| 赫章县| 伊宁县| 且末县| 绥阳县| 那坡县| 齐河县| 兰溪市| 桃园市| 伊春市| 永康市| 凤山县| 瓦房店市| 蒲城县| 固镇县| 永川市| 肥城市| 岑溪市| 台安县| 新田县| 银川市| 盐山县| 大庆市| 洛宁县| 湖南省| 岢岚县| 灵璧县| 苏州市| 大庆市| 新竹市| 岗巴县| 罗甸县| 寿光市| 黄大仙区| 黎川县| 石河子市| 明水县| 什邡市| 云南省|