啥都不说了,看代码
前台:
<input type="hidden" id="start" value="<?php date_default_timezone_set(‘PRC‘);echo strtotime(date(‘Y-m-d H:i:s‘))-strtotime($goods[‘start_time‘]);?>"><input type="hidden" id="end" value="<?php date_default_timezone_set(‘PRC‘);echo strtotime(date(‘Y-m-d H:i:s‘))-strtotime($goods[‘end_time‘])?>" >
<script type="text/javascript"> //判断时间 var start = document.getElementById("start").value; var end = document.getElementById("end").value; if(start>=0 && end<0) { timer(end*-1); } function timer(intDiff) { window.setInterval(function () { var day = 0, hour = 0, minute = 0, second = 0; //时间默认值 if (intDiff > 0) { day = Math.floor(intDiff / (60 * 60 * 24)); hour = Math.floor(intDiff / (60 * 60)) - (day * 24); minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60); second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60); } if (minute <= 9) minute = ‘0‘ + minute; if (second <= 9) second = ‘0‘ + second; $(‘#t_d‘).html(day + "天"); $(‘#t_h‘).html(‘<s id="h"></s>‘ + hour + ‘时‘); $(‘#t_m‘).html(‘<s></s>‘ + minute + ‘分‘); $(‘#t_s‘).html(‘<s></s>‘ + second + ‘秒‘); intDiff--; }, 1000); } function GetRTime(end){ var EndTime= new Date(end); var NowTime = new Date(); var t =EndTime.getTime() - NowTime.getTime(); var d=0; var h=0; var m=0; var s=0; if(t>=0){ d=Math.floor(t/1000/60/60/24); h=Math.floor(t/1000/60/60%24); m=Math.floor(t/1000/60%60); s=Math.floor(t/1000%60); } document.getElementById("t_d").innerHTML = d + "天"; document.getElementById("t_h").innerHTML = h + "时"; document.getElementById("t_m").innerHTML = m + "分"; document.getElementById("t_s").innerHTML = s + "秒"; } $(function () { $(".ms").click(function () { var id = $(this).attr("ids"); $.ajax({ type: "get", url: "?r=ms/buyms", data: {id:id}, dataType:"json", success: function(msg){ alert(msg[‘message‘]) } }); }) })</script>
后台:
<?phpnamespace frontend\controllers; use think\image\Exception;use Yii;use yii\web\Controller; /** * Site controller */class MsController extends Controller{ public $layout = false; /** * 商品只能购买一件商品 商品ID、当前用户ID、商品数量,存入redis, * 通知当前用户,秒杀成功,或失败 * 并将redis的商品库存队列 递减 * */ public function actionBuyms(){ date_default_timezone_set(‘PRC‘);// 接受商品ID ajax传来的id// $goods_id = yii::$app->request->get(‘goods_id‘); $goods_id=1;// 当前登录用户ID $user_id = 1;// 当前时间 $date = date(‘Y-m-d H:i:s‘);// 首先判断开始时间是否到// 在队列查询开始时间 $start_time = Yii::$app->redis->get(‘start_time‘.$goods_id); if(empty($start_time)||$start_time>$date){ echo json_encode(array(‘code‘=>1002,‘message‘=>‘秒杀时间还未开始‘));exit; }// 判断结束时间是否到 $end_time = Yii::$app->redis->get(‘end_time‘.$goods_id); if(empty($end_time)||$date>=$end_time){ echo json_encode(array(‘code‘=>1002,‘message‘=>‘秒杀已经结束了‘));exit; } //取出储存在redis里的库存 $num = Yii::$app->redis->get(‘num‘.$goods_id); if($num<=0){ echo json_encode(array(‘code‘=>1001,‘message‘=>‘已被抢空了...请等待下次抢购‘));exit; }else{ $msg = json_encode([‘user_id‘=>1,‘goods_id‘=>1,‘buy_num‘=>$goods_id]); //更新库存 decr递减 $u=Yii::$app->redis->decr(‘num‘.$goods_id); if($u) { //储存用户信息到用户的队列 秒杀人员的总队列 Yii::$app->redis->lpush(‘yes_buy‘,$msg); echo json_encode(array(‘code‘=>1000,‘message‘=>‘抢购成功,稍后为您出单,预计时间3分钟‘)); } } } /** * @return string * * 生成订单为定时任务 在服务器每两分钟执行一次,等待1分钟 */ /** * 设置初始值,理论上,应为后台管理员手动设定秒杀商品 * * * 模拟给予 开始时间、结束时间、库存在redis储存的值 */ public function actionSetbuyuser(){ //默认开始时间为 2017-03-28 00:00:00 $start_time = ‘2017-03-28 00:00:00‘; //默认结束时间为 2017-03-28 00:00:00 $end_time = ‘2017-03-28 24:00:00‘; //默认库存为1 $num = 10; //商品id 为了区分商品信息、库存 不会打乱各个商品信息 $goods_id = 1; //设置库存 Yii::$app->redis->set(‘num‘.$goods_id,$num); //设置当前商品的开始时间 Yii::$app->redis->set(‘start_time‘.$goods_id,$start_time); //设置当前商品的结束时间 Yii::$app->redis->set(‘end_time‘.$goods_id,$end_time); }}
时间: 2024-10-18 19:31:49