我是靠谱客的博主 端庄银耳汤,最近开发中收集的这篇文章主要介绍Redis 秒杀添加秒杀入库前台网页设计秒杀接值存入对列中,返回结果生成订单,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

添加秒杀入库

  public function actionAdd_do()

{

$request = Yii::$app->request;

$data = $request->post();
//
print_r($data);die;

//存入redis list

$redis = new redis();

$redis->connect('127.0.0.1',6379);

//商品库存(存key 时 加goods_id 是为了 不被混搅)

$redis->set("kill_num".$data['goods_id'],$data['num']);

//商品价格

$redis->set("kill_price".$data['goods_id'],$data['kill_price']);

//商品开始时间he结束时间

$redis->set("kill_time".$data['goods_id'],$data['status_time'].'_'.$data['die_time']);

//抢购列表(为空)

$redis->rPush("kill_goods".$data['goods_id'],"");

//秒杀商品 入库

$actAdd = Yii::$app->db->createCommand()->insert('man_activity',[

'act_name'=>$data['act_name'],

'status_time'=>$data['status_time'],

'die_time'=>$data['die_time'],

'is_show'=>$data['is_show'],

'content'=>$data['content'],

'goods_id'=>$data['goods_id'],

'kill_price'=>$data['kill_price'],

'num'=>$data['num'],

])->execute();

if($actAdd)

{

return $this->redirect('?r=activity/index');

}else

{

echo "添加失败";

}

}

前台网页设计


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>秒杀活动</title>
</head>
<body>
<script src="./jquery.js"></script>
<script type="text/javascript">
$.extend($.fn,{
//懒人建站 http://www.51xuediannao.com/

fnTimeCountDown:function(d){
this.each(function(){
var $this = $(this);
var o = {
hm: $this.find(".hm"),
sec: $this.find(".sec"),
mini: $this.find(".mini"),
hour: $this.find(".hour"),
day: $this.find(".day"),
month:$this.find(".month"),
year: $this.find(".year")
};
var f = {
haomiao: function(n){
if(n < 10)return "00" + n.toString();
if(n < 100)return "0" + n.toString();
return n.toString();
},
zero: function(n){
var _n = parseInt(n, 10);//解析字符串,返回整数

if(_n > 0){
if(_n <= 9){
_n = "0" + _n

}
return String(_n);
}else{
return "00";
}
},
dv: function(){
//d = d || Date.UTC(2050, 0, 1); //如果未定义时间,则我们设定倒计时日期是2050年1月1日

var _d = $this.data("end") || d;
var now = new Date(),
endDate = new Date(_d);
//现在将来秒差值

//alert(future.getTimezoneOffset());

var dur = (endDate - now.getTime()) / 1000 , mss = endDate - now.getTime() ,pms = {
hm:"000",
sec: "00",
mini: "00",
hour: "00",
day: "00",
month: "00",
year: "0"

};
if(mss > 0){
pms.hm = f.haomiao(mss % 1000);
pms.sec = f.zero(dur % 60);
pms.mini = Math.floor((dur / 60)) > 0? f.zero(Math.floor((dur / 60)) % 60) : "00";
pms.hour = Math.floor((dur / 3600)) > 0? f.zero(Math.floor((dur / 3600)) % 24) : "00";
pms.day = Math.floor((dur / 86400)) > 0? f.zero(Math.floor((dur / 86400)) % 30) : "00";
//月份,以实际平均每月秒数计算

pms.month = Math.floor((dur / 2629744)) > 0? f.zero(Math.floor((dur / 2629744)) % 12) : "00";
//年份,按按回归年365天5时48分46秒算

pms.year = Math.floor((dur / 31556926)) > 0? Math.floor((dur / 31556926)) : "0";
}else{
pms.year=pms.month=pms.day=pms.hour=pms.mini=pms.sec="00";
pms.hm = "000";
//alert('结束了');

return;
}
return pms;
},
ui: function(){
if(o.hm){
o.hm.html(f.dv().hm);
}
if(o.sec){
o.sec.html(f.dv().sec);
}
if(o.mini){
o.mini.html(f.dv().mini);
}
if(o.hour){
o.hour.html(f.dv().hour);
}
if(o.day){
o.day.html(f.dv().day);
}
if(o.month){
o.month.html(f.dv().month);
}
if(o.year){
o.year.html(f.dv().year);
}
if(!parseInt(o.hour.html()) && !parseInt(o.mini.html()) && !parseInt(o.sec.html()))
{
//
alert(123213);

$('#btn').removeAttr('disabled');
$('#box').html(' <P style="font-size:.8em;line-height:2em;">秒杀开始!</P>');
}
setTimeout(f.ui, 1);
}
};
f.ui();
});
}
});
</script>
<div id='box' style="background:rgb(183,17,41); margin-bottom: 50px;
WIDTH: 100%;
color:#fff;FONT-FAMILY: arial; TEXT-ALIGN: center;">
<P style="font-size:.8em;line-height:2em;">距活动开始还有:</P>
<div id="fnTimeCountDown" data-end="<?php echo $list['time']?>">
<span class="hour">00</span>时
<span class="mini">00</span>分
<span class="sec">00</span>秒
</div>
</div>
<script type="text/javascript">
$("#fnTimeCountDown").fnTimeCountDown("2017/03/09 9:00:00");
</script>
<center>
<div><img src="123.jpg" alt="" width="400px" height="300px"><br>
名称:<?php echo $list['goods_name']?><br>
价格:<span id="price"><?php echo $list['price']?></span>元
</div>
<button id="btn" goods="<?php echo $list['id']?>" disabled="">秒杀</button>
</center>
</body>
</html>
<script>
$(function(){
if(!parseInt($('.hour').html()) &&
!parseInt($('.mini').html()) && !parseInt($('.sec').html()))
{
$('#btn').removeAttr('disabled');
$('#box').html(' <P style="font-size:.8em;line-height:2em;">秒杀开始!</P>');
}
$('#btn').click(function(){
var goods_id = $(this).attr('goods');
$.ajax({
type:"post",
url:"miaosha.php",
data:"goods_id="+goods_id,
dataType:'json',
success:function(msgs)
{
//
alert(msgs);

if(msgs.code == 0)
{
alert(msgs.msg)
}
if(msgs.code == 1)
{
alert(msgs.msg)
}
if(msgs.code == 2)
{
alert(msgs.msg)
}
}
})
})
})
</script>

秒杀接值存入对列中,返回结果


public function kill_du(Request $request)

{

//商品id

$goods_id = $request->get('goods_id');

$attr = $request->get('attr');

// print_r($attr);die;


$redis = new redis();

$redis->connect('127.0.0.1',6379);


$kill_info = $redis->lRange('kill_goods'.$goods_id,0,-1);
//
var_dump($kill_info);die;

//new一个session;

$session = new Session();


$user_info = $session->get('users');
//
print_r($user_info);die;

if(empty($user_info) && !isset($user_info))

{

return json_encode(['code'=>3,'msg'=>"请先登录!"]);die;

}

if(isset($kill_info) )

{

//秒杀时间

$kill_time = $redis->get('kill_time'.$goods_id);

//开始时间

$start_time = strtotime(explode('_',$kill_time)[0]);

//结束时间

$end_time = strtotime(explode('_',$kill_time)[1]);

$time = time()+ 8 * 60 * 60;
//
echo $start_time.'-'.$time.'-'.$end_time;

if($time> $start_time && $time < $end_time)

{

$num = $redis->get('kill_num'.$goods_id);

$size = $redis->lSize("kill_goods".$goods_id);
//
echo $num.'-'.$size;

if($size-1 < $num)

{

if(in_array($user_info['user_id'],$kill_info))

{

return json_encode(['code'=>5,'msg'=>"每个人只能秒杀一件商品!"]);

}

$redis->rpush('kill_goods'.$goods_id,$user_info['user_id'].'-'.$attr);

return json_encode(['code'=>1,'msg'=>"秒杀成功,请在活动结束后查看订单!"]);

}

else

{

return json_encode(['code'=>2,'msg'=>"商品已经售完!"]);

}

}

else

{

return json_encode(['code'=>4,'msg'=>"不在活动时间!"]);

}


}

else

{

return json_encode(['code'=>0,'msg'=>"该商品没有加入秒杀活动!"]);

}

}

生成订单

 /**

* 生成唯一订单号

* @return string

*/

public function actionBuild_order_no(){

return 'kill_'.date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);

}

/**

* 订单生成

* @return json

*/

public function actionGetodrer()

{

//获取活动商品id订单

$goods_id = Yii::$app->request->get('goods_id');

$act_id = Yii::$app->request->get('act_id');

$redis = new redis();

$res = $redis->connect('127.0.0.1',6379);

if(!$res)

{

return json_encode(['code'=>0,'msg'=>"服务器错误!"]);

}

$kill_info = $redis->lRange('kill_goods'.$goods_id,0,-1);

$kill_price = $redis->get("kill_price".$goods_id);;
//
var_dump($kill_price);die;

$order_num =count($kill_info)-1;

$num = 0;

if(isset($kill_info) && !empty($kill_info))

{


foreach($kill_info as $k => $v)

{

if(!empty($v))

{

$kill = explode('-',$v);

$user_id = $kill[0];

$goods_attr = $kill[1];

Yii::$app->db->createCommand()->insert('man_order_info',[

'user_id'=>$user_id,

'goods_attr'=>$goods_attr,

'goods_amount'=>$kill_price,

'add_time'=>time(),

'goods_id'=>$goods_id,

'order_sn'=>$this->actionBuild_order_no(),

])->execute();

$num++;

}

}

$actObj = new Man_activity();

$act
= $actObj::find()->where(['act_id'=>$act_id])->one();

$act->order_do = "1";

$result = $act->save();

if($result)

{

$redis->delete('kill_goods'.$goods_id);

return json_encode(['code'=>1,'msgs'=>"应生成".$order_num."条订单,共生成".$num."条订单!"]);

}

else

{

return json_encode(['code'=>2,'msgs'=>"应生成".$order_num."条订单,共生成".$num."条订单!"]);

}


}

else

{

return json_encode(['code'=>0,'msgs'=>"无需生成订单!"]);

}


}



最后

以上就是端庄银耳汤为你收集整理的Redis 秒杀添加秒杀入库前台网页设计秒杀接值存入对列中,返回结果生成订单的全部内容,希望文章能够帮你解决Redis 秒杀添加秒杀入库前台网页设计秒杀接值存入对列中,返回结果生成订单所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部