php優惠券的實作方式:1、建立一個前端文件,並判斷優惠券是否存在或停用;2、創建PHP範例文件;3、透過「public function doCoupon($params){ ...}」等方式處理優惠券領取狀況即可。
本文操作環境: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['mobile']); //主要是过滤掉领取优惠券为0的,用laravel的同学注意看看 $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'); //去除优惠券剩余数量为0,或者领取优惠券数量-剩余数量 > 0 } //判断活动开始时间与优惠券开始时间 $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("优惠券领取时间未开始,暂不可领取"); } //领取活动有以下几种情况 //1: 活动已结束 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']); //检查是新用户,还是老用户 根据自己的业务需求做,这个方法就不贴了 //2: 活动为新用户领取,而领取的用户是老用户 if($activity['user_type'] == 1 && !empty($checkUser)){ $result = [ 'code' => 2, ]; return $result; } //3:活动为老用户领取,而领取的用户是新用户 if($activity['user_type']==2 && empty($checkUser)){ $result = [ 'code' => 3, ]; return $result; } //4:优惠券是否领取完 $coupon = $this->getCouponExpire($collection_coupon,$params['mobile']); //这里提示有一个优惠券列表,根据自己的业务需求做,这个方法就不贴了 //return $coupon; if($coupon == 1){ $result = [ 'code' => 4, ]; return $result; } //5:已领取过优惠券提示 $userCoupon = ''; $userRate = ''; if(!empty($checkUser)){ //user存在则为老用户,再检查是否领取过 $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:领取成功 //如果活动规定是新老用户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; } }
用戶領取優惠券並發放優惠券
/** * 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 [ 'result' => $res, 'status' => 200 ]; break; case 2: //老用户领取 $res = $this->insertUserCoupon($user,$coupon); return [ 'result' => $res, 'status' => 200 ]; break; default: //新老用户领取,判断是新用户还是老用户,这里的$user是有无配送单,有则为老用户; if(empty($user)){ $res = $this->addUser($mobile,$coupon); }else{ $res = $this->insertUserCoupon($user,$coupon); //老用户,直接发放优惠券 } return [ 'result' => $res, 'status' => 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([ 'config_id'=>$item['config_id'], 'status' => 0, ])->first(); if(empty($res) || (!empty($res) && $res['is_send'] == 0) ){ throw new \Exception("优惠券未发放,暂不可领取"); } //发放优惠券,有多少张就添加多少张,这里扣除优惠券时,主要用不同的coupon_sn来区别 $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); } //扣除相应的优惠券数量,这里用到了锁表,防止并发时,优惠券为-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("优惠券剩余数量不够扣减"); } } $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; }
#推薦學習:《PHP影片教學》
以上是php優惠券的實現方式是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Dreamweaver CS6
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版