//存放主要的交互逻辑js代码
//javascript模块化 json
//seckill.detail.init(params);
var seckill = {
//封装秒杀相关ajax的url
URL: {
now: function () {
return '/seckill/time/now';
},
exposer: function (seckillId) {
return '/seckill/' + seckillId + '/exposer';
},
execution: function (seckillId, md5) {
return '/seckill/' + seckillId + '/' + md5 + '/execution';
}
},
handleSeckillkill: function (seckillId, node) {
//获取秒杀地址,控制现实逻辑,时间完成后回调
node.hide()
.html("<button class='btn btn-primary btn-lg' id='killBtn'>开始秒杀</button>").show();
$.post(seckill.URL.exposer(seckillId), {}, function (result) {
if (result && result['success']) {
var exposer = result['data'];
if (exposer['exposed']) {
//开启秒杀
//获取秒杀地址
var md5 = exposer['md5'];
var killUrl = seckill.URL.execution(seckillId, md5);
console.log('killUrl=' + killUrl);
//绑定一次点击事件
$('#killBtn').one('click', function () {
//执行秒杀请求
//1、禁用按钮
$(this).addClass('disabled');
//2、发送秒杀请求执行秒杀
$.post(killUrl, {}, function (result) {
if (result && result['success']) {
var killResult = result['data'];
var state = killResult['state'];
var stateInfo = killResult['stateInfo'];
//3;显示秒杀结果
node.html('<span class="label label-success">' + stateInfo + '</span>');
} else {
//console.log("执行操作异常结果:" + JSON.stringify(result));
var killResult = result['data'];
var stateInfo = killResult['stateInfo'];
node.html('<span class="label label-success">' + stateInfo + '</span>');
}
}
);
});
node.show();
} else {
//未开始秒杀
var now = exposer['now'];
var start = exposer['start'];
var end = exposer['end'];
//重新计时
seckill.countdown(seckillId, now, start, end);//每个人机器的时间有偏差,纠正一下
}
} else {
console.log("result=" + JSON.stringify(result));
}
}
);
},
countdown: function (seckillId, nowTime, startTime, endTime) {
var seckillBox = $('#seckill-box');
//时间的判断
if (nowTime > endTime) {
//秒杀结束
console.log('秒杀结束');
seckillBox.html('秒杀结束!');
} else if (nowTime < startTime) {
//秒杀未开始,计时事件绑定
console.log('秒杀未开始');
var killTime = new Date(startTime + 1000);//+1000防止计时偏移
seckillBox.countdown(killTime, function (event) {
//时间格式
var format = event.strftime("秒杀计时: %D天 %H时 %M分 %S秒");
seckillBox.html(format);
}).on('finish.countdown', function () {
//获取秒杀地址,控制现实逻辑,时间完成后回调
seckill.handleSeckillkill(seckillId, seckillBox);
});
} else {
//秒杀开始
console.log('秒杀进行中');
seckill.handleSeckillkill(seckillId, seckillBox);
}
}
,
//验证手机号
validatePhone: function (phone) {
if (phone && phone.length == 11 && !isNaN(phone)) {//isNaN()判断是否是非数字
return true;
} else {
return false;
}
}
,
//详情页秒杀逻辑
detail: {
//详情页初始化
init: function (params) {
//手机验证和登录,计时交互
//规划我们的交互流程
var killPhone = $.cookie('killPhone');
//验证手机号
if (!seckill.validatePhone(killPhone)) {
//绑定phone
//控制输出
var killPhoneModal = $('#killPhoneModal');
killPhoneModal.modal({
show: true,//显示弹出层
backdrop: 'static',//禁止位置关闭
keyboard: false//关闭键盘事件
});//modal是jquery的一个组件
$('#killPhoneBtn').click(
function () {//
var inputPhone = $('#killPhoneKey').val();
console.log('inputPhone=' + inputPhone);
if (seckill.validatePhone(inputPhone)) {
$.cookie('killPhone', inputPhone, {expires: 7, path: "/seckill"});//cookie会发送到后端
//刷新页面
window.location.reload();
} else {
$('#killPhoneMessage').hide().html("<label class='label label-danger'>手机号错误</label>").show(300);
}
});
}
//已经登录
//计时交互
var startTime = params['startTime'];//js中可以像数据一样访问json
var endTime = params['endTime'];
var seckillId = params['seckillId'];
$.get(seckill.URL.now(), {}, function (result) {
if (result && result['success']) {
console.log('result=' + JSON.stringify(result));
var nowTime = result['data'];
//时间判断
seckill.countdown(seckillId, nowTime, startTime, endTime);
} else {
console.log('result=' + result);
}
});
}
}
}