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']);
}
}
}
php好看主页源码
$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,
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论