業(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)的開(kāi)始與結(jié)束時(shí)間,最后生成不同的優(yōu)惠券活動(dòng)鏈接。
代碼環(huán)境:
源碼主要laravel5.8,一整個(gè)活動(dòng)要貼的代碼很多,下面主要貼核心代碼,僅供參考。主要還是要根據(jù)自己的業(yè)務(wù)需求來(lái)實(shí)現(xiàn)功能吧。
以下是后端截圖,做成模塊化
前端需要做的設(shè)置與限制:
1 判斷優(yōu)惠券是否存在或者停用
2 判斷活動(dòng)開(kāi)始時(shí)間與優(yōu)惠券開(kāi)始時(shí)間
接著領(lǐng)取活動(dòng)優(yōu)惠券,需要判斷以下情況:
1 活動(dòng)已結(jié)束
2 活動(dòng)為開(kāi)始時(shí)
3 活動(dòng)為新用戶領(lǐng)取,而領(lǐng)取的用戶是老用戶
4 活動(dòng)為老用戶領(lǐng)取,而領(lǐng)取的用戶是新用戶
5 優(yōu)惠券是否領(lǐng)取完
6 已領(lǐng)取過(guò)優(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']); //主要是過(guò)濾掉領(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)開(kāi)始時(shí)間與優(yōu)惠券開(kāi)始時(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í)間未開(kāi)始,暫不可領(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)為開(kāi)始時(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)取過(guò)優(yōu)惠券提示 $userCoupon = ''; $userRate = ''; if(!empty($checkUser)){ //user存在則為老用戶,再檢查是否領(lǐng)取過(guò) $userCoupon = $this->getUserCoupon($collection_coupon,$checkUser['user_id']); $userRate = $this->getUserCouponRate($checkUser['user_id'],$activity['activity_id']); }else{ //新用戶,檢查是否注冊(cè)過(guò) $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)取,如果從來(lái)沒(méi)注冊(cè)過(guò)就要新增用戶 $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是有無(wú)配送單,有則為老用戶; 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來(lái)區(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í)有所幫助,也希望大家多多支持腳本之家。
標(biāo)簽:重慶 十堰 銅陵 巴彥淖爾 衡陽(yáng) 鷹潭 臨沂 麗江
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《用PHP做了一個(gè)領(lǐng)取優(yōu)惠券活動(dòng)的示例代碼》,本文關(guān)鍵詞 用,PHP,做了,一個(gè),領(lǐng)取,優(yōu)惠券,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。