1 登录判断
用户在未登录状态下可以查看商品列别以及秒杀商品详情,但不可以在未登录状态进行秒杀商品的操作,当用户点击开始秒杀时,进行登陆验证
1.1 编写前端登录判断
没有收货地址的提示。。。
| 商品名称 | |||
| 商品图片 | |||
| 秒杀开始时间 |
|
||
| 商品原价 | |||
| 秒杀价 | |||
| 库存数量 | |||
var seckillId;
$(function () {
seckillId = getQueryString("seckillId");
initGood();
initUser();
});
function initGood(){
$(function () {
$.ajax({
url: "http://localhost:9000/seckill/seckillGood/find?seckillId="+seckillId,
type: "get",
xhrFields: {withCredentials: true}, //启用cookie
success:function (data) {
if(data.code==200){
//填充表格中的数据
renderGood(data.data);
}else{
layer.msg(data.msg)
}
}
});
});
}
function renderGood(good) {
$("#goodName").html(good.goodName);
$("#goodImg").prop("src",good.goodImg);
$("#startDate").html(good.startDate);
$("#goodPrice").html(good.goodPrice);
$("#stockCount").html(good.stockCount);
$("#seckillPrice").html(good.seckillPrice);
//调用时间
renderDate(good.startDate,good.endDate);
}
//定义秒杀的三个阶段
var timer; //计时器
//距离抢购开始还有多久
var remainStartSeconds;
//距离结束还有多久
var remainEndSeconds;
function renderDate(sDate,eDate) {
var startTime = new Date(sDate); // 2023-11-25 16:00
var endTime = new Date(eDate); // 2023-11-25 18:00
var now = new Date(); // 2023-11-25 14:37
remainStartSeconds=parseInt((startTime.getTime()-now.getTime())/1000);//秒
remainEndSeconds=parseInt((endTime.getTime()-now.getTime())/1000);//秒
timer=window.setInterval(showSeckillTip,1000);
}
function showSeckillTip() {
remainStartSeconds--;
remainEndSeconds--;
if(remainStartSeconds>0){
$("#seckillTip").html("距离本场秒杀开始还有"+remainStartSeconds+"秒");
//禁用按钮
$("#buyButton").prop("disabled",true);
}else{
if(remainEndSeconds>0){
//秒杀中
$("#seckillTip").html("秒杀进行中....");
//禁用按钮
$("#buyButton").prop("disabled",false);
}else{
$("#seckillTip").html("秒杀结束了");
//禁用按钮
$("#buyButton").prop("disabled",true);
window.clearInterval(timer);//取消计时器
}
}
}
function initUser(){
$(function () {
$.ajax({
url: "http://localhost:9000/member/token/getCurrent",
type: "get",
xhrFields: {withCredentials: true}, //启用cookie
success:function (data) {
if(data.code==200){
//填充表格中的数据
renderUser(data.data);
}else{
layer.msg(data.msg)
}
}
});
});
}
var user;
function renderUser(u){
if(u){
user=u;
}else{
//没有数据
$("#userTip").show();
}
}