用PHP做了一個(gè)領(lǐng)取優(yōu)惠券活動(dòng)的示例代碼
業(yè)務(wù)需求
優(yōu)惠券活動(dòng),具體還是要根據(jù)自己的需求。以下是最近實(shí)現(xiàn)的優(yōu)惠券活動(dòng),主要的業(yè)務(wù)需求:根據(jù)后端設(shè)置優(yōu)惠券模板,用戶類型設(shè)置,優(yōu)惠券活動(dòng)的開始與結(jié)束時(shí)間,最后生成不同的優(yōu)惠券活動(dòng)鏈接。
代碼環(huán)境:
源碼主要laravel5.8,一整個(gè)活動(dòng)要貼的代碼很多,下面主要貼核心代碼,僅供參考。主要還是要根據(jù)自己的業(yè)務(wù)需求來實(shí)現(xiàn)功能吧。
以下是后端截圖,做成模塊化
前端需要做的設(shè)置與限制:
1 判斷優(yōu)惠券是否存在或者停用
2 判斷活動(dòng)開始時(shí)間與優(yōu)惠券開始時(shí)間
接著領(lǐng)取活動(dòng)優(yōu)惠券,需要判斷以下情況:
1 活動(dòng)已結(jié)束
2 活動(dòng)為開始時(shí)
3 活動(dòng)為新用戶領(lǐng)取,而領(lǐng)取的用戶是老用戶
4 活動(dòng)為老用戶領(lǐng)取,而領(lǐng)取的用戶是新用戶
5 優(yōu)惠券是否領(lǐng)取完
6 已領(lǐng)取過優(yōu)惠券提示
7 領(lǐng)取成功
下面核心代碼實(shí)現(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ù)錯(cuò)誤!"); } $preg_phone = '/^1[34578]\d{9}$/ims'; $is_mobile = preg_match ($preg_phone, $params['mobile']); if ($is_mobile == 0) { throw new \Exception("手機(jī)號(hào)碼不正確!"); } //隱藏手機(jī)號(hào)碼中間4位 $str_mobile = substr_replace($params['mobile'],'****',3,4); $activity = $this->find($activity_id); if(empty($activity)){ throw new \Exception("不存在此活動(dòng)"); } $activity_link = $activity->activityLink->where('coupon_status',0); //只選擇不停用的優(yōu)惠券 if(count($activity_link) <= 0){ throw new \Exception("優(yōu)惠券不存在或者已經(jīng)停用"); }else{ //查找注冊(cè)用戶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 } //判斷活動(dòng)開始時(shí)間與優(yōu)惠券開始時(shí)間 $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)取時(shí)間未開始,暫不可領(lǐng)取"); } //領(lǐng)取活動(dòng)有以下幾種情況 //1: 活動(dòng)已結(jié)束 if($activity['end_time'] < date("Y-m-d H:i:s") || $activity['status'] == 1){ $result = [ 'code' => 1, ]; return $result; } //6 活動(dòng)為開始時(shí) 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ù)需求做,這個(gè)方法就不貼了 //2: 活動(dòng)為新用戶領(lǐng)取,而領(lǐng)取的用戶是老用戶 if($activity['user_type'] == 1 && !empty($checkUser)){ $result = [ 'code' => 2, ]; return $result; } //3:活動(dòng)為老用戶領(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']); //這里提示有一個(gè)優(yōu)惠券列表,根據(jù)自己的業(yè)務(wù)需求做,這個(gè)方法就不貼了 //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{ //新用戶,檢查是否注冊(cè)過 $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)取成功 //如果活動(dò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)取活動(dò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)取,如果從來沒注冊(cè)過就要新增用戶 $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)惠券,有多少?gòu)埦吞砑佣嗌購(gòu)垼@里扣除優(yōu)惠券時(shí),主要用不同的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ā)時(shí),優(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; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
實(shí)現(xiàn)PHP框架系列文章(6)mysql數(shù)據(jù)庫方法
這篇文章主要介紹了實(shí)現(xiàn)PHP框架系列文章(6)mysql數(shù)據(jù)庫方法的相關(guān)資料,需要的朋友可以參考下2016-03-03php json轉(zhuǎn)換相關(guān)知識(shí)(小結(jié))
這篇文章主要介紹了php json轉(zhuǎn)換相關(guān)知識(shí)(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12php解析mht文件轉(zhuǎn)換成html的實(shí)例
下面小編就為大家?guī)硪黄猵hp解析mht文件轉(zhuǎn)換成html的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03CI框架AR操作(數(shù)組形式)實(shí)現(xiàn)插入多條sql數(shù)據(jù)的方法
這篇文章主要介紹了CI框架AR操作實(shí)現(xiàn)插入多條sql數(shù)據(jù)的方法,結(jié)合簡(jiǎn)單實(shí)例形式分析了CI框架使用數(shù)組實(shí)現(xiàn)多條數(shù)據(jù)插入的方法,需要的朋友可以參考下2016-05-05PHP開發(fā)制作一個(gè)簡(jiǎn)單的活動(dòng)日程表Calendar
這篇文章主要介紹了PHP開發(fā)制作一個(gè)簡(jiǎn)單的活動(dòng)日程表Calendar,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06使用新浪微博API的OAuth認(rèn)證發(fā)布微博實(shí)例
這篇文章主要介紹了使用新浪微博API的OAuth認(rèn)證發(fā)布微博實(shí)例的相關(guān)資料,需要的朋友可以參考下2015-03-03