服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - PHP教程 - 用PHP做了一个领取优惠券活动的示例代码

用PHP做了一个领取优惠券活动的示例代码

2021-08-08 17:51磊丰 PHP教程

这篇文章主要介绍了用PHP做了一个领取优惠券活动的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

业务需求

优惠券活动,具体还是要根据自己的需求。以下是最近实现的优惠券活动,主要的业务需求:根据后端设置优惠券模板,用户类型设置,优惠券活动的开始与结束时间,最后生成不同的优惠券活动链接。

代码环境

源码主要laravel5.8,一整个活动要贴的代码很多,下面主要贴核心代码,仅供参考。主要还是要根据自己的业务需求来实现功能吧。

以下是后端截图,做成模块化

用PHP做了一个领取优惠券活动的示例代码

前端需要做的设置与限制:

1 判断优惠券是否存在或者停用
2 判断活动开始时间与优惠券开始时间

接着领取活动优惠券,需要判断以下情况:
1 活动已结束
2 活动为开始时
3 活动为新用户领取,而领取的用户是老用户
4 活动为老用户领取,而领取的用户是新用户
5 优惠券是否领取完
6 已领取过优惠券提示
7 领取成功

下面核心代码实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
 * 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;
  }
 
 
}

用户领取优惠券并发放优惠券

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
 * 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("优惠券不存在或者已经停用");
  }
 
 
}

领取成功,则发放优惠券

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
 * 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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://mp.weixin.qq.com/s/vt-_Awa0wmhVNMM9CwSGqg

延伸 · 阅读

精彩推荐