Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation of php coupons?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

php优惠券的实现方式是什么,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

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) 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', '

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report