Skip to content

Commit

Permalink
前端完善
Browse files Browse the repository at this point in the history
  • Loading branch information
tkst1993 committed Aug 10, 2018
1 parent 19d9ec8 commit 9960a26
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/seckill/web/SeckillController.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ public SeckillResult<Exposer> export(@PathVariable("seckillId")Long seckillId){
res = new SeckillResult<SecKillExecution>(true,execution);
}catch (RepeatKillException e2){
SecKillExecution execution = new SecKillExecution(seckillId, SecKillStateEnum.REPEAT_KILL);
res = new SeckillResult<SecKillExecution>(false,execution);
res = new SeckillResult<SecKillExecution>(true,execution);
}catch(SecKillCloseException e1){
SecKillExecution execution = new SecKillExecution(seckillId, SecKillStateEnum.END);
res = new SeckillResult<SecKillExecution>(false,execution);
res = new SeckillResult<SecKillExecution>(true,execution);
}catch(Exception e){
SecKillExecution execution = new SecKillExecution(seckillId, SecKillStateEnum.INNER_ERROR);
res = new SeckillResult<SecKillExecution>(false,execution);
res = new SeckillResult<SecKillExecution>(true,execution);

}
return res;
Expand Down
60 changes: 60 additions & 0 deletions src/main/webapp/jsp/detail.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,76 @@
<h2>${seckill.name}</h2>
</div>
<div class="panel-body">
<h2 class="text-danger">
<%--显示time图标--%>
<span class="glyphicon glyphicon-time"></span>
<%--展示倒计时--%>
<span class="glyphicon" id="seckill-box"></span>
</h2>

</div>
</div>
</div>

<%--登录弹出层,输入电话--%>
<div id="killPhoneModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title text-center">
<span class="glyphicon glyphicon-phone"></span>秒杀电话:
</h3>
</div>

<div class="modal-body">
<div class="row">
<div class="col-xs-8 col-xs-offset-2">
<input type="text" name="killPhone" id="killPhoneKey"
placeholder="填手机号^0^" class="form-control">
</div>
</div>
</div>

<div class="modal-footer">
<%--验证信息--%>
<span id="killPhoneMessage" class="glyphicon"></span>
<button type="button" id="killPhoneBtn" class="btn btn-success">
<span class="glyphicon glyphicon-phone"></span>
Submit
</button>
</div>

</div>

</div>

</div>

<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="http://apps.bdimg.com/libs/jquery/2.0.0/jquery.min.js"></script>

<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>

<%--使用CDN 获取公共js--%>
<%--jQuery cookie操作插件--%>
<script src="http://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<%--jQuery countdown的倒计时插件--%>
<script src="http://cdn.bootcss.com/jquery.countdown/2.1.0/jquery.countdown.min.js"></script>

<%--开始编写交互逻辑--%>
<script src="/resource/script/seckill.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
//使用EL表达式传入参数
seckill.detail.init({
seckillId : ${seckill.seckillId},
startTime : ${seckill.startTime.time},//毫秒时间
endTime : ${seckill.endTime.time}
})
})
</script>
</body>
</html>
172 changes: 172 additions & 0 deletions src/main/webapp/resource/script/seckill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/**
* Created by tangke on 2018/8/6.
*/
//存放主要交互逻辑js代码
//javascript 模块化


var seckill = {
//封装秒杀相关ajax地址
URL : {
now : function () {
return '/seckill/time/now';
},
exposer : function (seckillId) {
return '/seckill/'+seckillId+'/exposer';
},
execution : function (seckillId,md5) {
return '/seckill/' + seckillId + '/' + md5 + '/execution';
}

},
handleSeckill : function (seckillId,node) {
//处理秒杀逻辑
//获取秒杀地址,控制显示逻辑,执行秒杀
node.hide()
.html('<button class="btn btn-primary btn-lg" id="killBtn">开始秒杀</button>')
$.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);

$('#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'];
//显示秒杀结果
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:'+result);


}

})

},
validatePhone : function (phone) {
if(phone && phone.length == 11 && !isNaN(phone)){
return true;
}else{
return false;
}
},
countdown:function (seckillId,nowTime,startTime,endTime) {
var seckillBox = $('#seckill-box');
//时间判断
if(nowTime > endTime){
//秒杀结束
seckillBox.html('秒杀结束');
}else if(nowTime < startTime){
//秒杀未开始,计时事件绑定
var killTime = new Date(startTime + 1000);
seckillBox.countdown(killTime,function (event) {
//时间格式
var format = event.strftime('秒杀倒计时: %D天 %H时 %M分 %S秒');
seckillBox.html(format);
}).on('finish.countdown',function () {
//时间完成后回调事件
//获取秒杀地址,控制显示逻辑,执行秒杀
seckill.handleSeckill(seckillId,seckillBox);

});

}else{
//秒杀开始
seckill.handleSeckill(seckillId,seckillBox);

}
}

,
//详情页秒杀逻辑
detail:{
//详情页初始化
init:function (params) {
//手机验证和登录,计时交互
//规划我们的交互流程
//在cookie中查找手机号
var killPhone = $.cookie('killPhone');

//验证手机号
//控制输出
if(!seckill.validatePhone(killPhone)){
//绑定手机号
var killPhoneModal = $('#killPhoneModal');
killPhoneModal.modal({
show : true,//显示弹出层
backdrop:'static',//禁止位置关闭
keyboard:false//关闭键盘事件

});
$('#killPhoneBtn').click(function () {
var inputPhone = $('#killPhoneKey').val();
if(seckill.validatePhone(inputPhone)){
//电话写入cookie
$.cookie('killPhone',inputPhone,{expires:7,path:'/seckill'});
//刷新页面
window.location.reload();
}else{
$('#killPhoneMessage').hide()
.html('<label class="label label-danger">手机号错误!</label>')
.show(300);
}

});


}

var startTime = params['startTime'];
var endTime = params['endTime'];
var seckillId = params['seckillId'];
//已经登录
//计时交互
$.get(seckill.URL.now(),{},function (result) {
if(result && result['success']){
var nowTime = result['data'];
//时间判断
seckill.countdown(seckillId,nowTime,startTime,endTime);


}else{
console.log('result:'+result);

}

})


}

}

}

0 comments on commit 9960a26

Please sign in to comment.