var currentDate="<s:property value="#request.currentDate" />";//当前时间
var startTimeLong="<s:property value="#request.startTimeLong" />";//本场活动开始时间
var endTimeLong="<s:property value="#request.endTimeLong" />";//本场活动结束时间
var loginFlag="<s:property value="#request.loginFlag"/>";//登陆标识
var alertdiv = new TipBox({target:"alertdiv", closeBtn:"gbdiv"});
var confirmdiv = new TipBox({target:"confirmdiv", closeBtn:"cfqdiv"});
var grabFlag="<s:property value="#request.map.get('mapkeys').flag"/>";//抢单状态 1未支付 2已支付
var grabTimeLong="<s:property value="#request.map.get('mapkeys').createTime"/>";//抢单时间
var ticketPriceId="<s:property value="#request.map.get('mapkeys').ticketPriceId"/>";//门票价格ID
var sale="<s:property value="#request.subscribeFlag"/>";//状态
var activityEndTime="<s:property value="#request.activityEndTime"/>";//秒杀活动结束时间
var time=0;//时间间隔
$(document).ready(function(){
var whole_width =$(window).width();
$(".td_width").css("width",parseInt((whole_width - 20)/2));
});
/*
日期倒计时
*/
function digitalTime(){
var liveClock=document.getElementById("liveClock");
var leave = 0;
var flag = parseInt(startTimeLong)-parseInt(currentDate);//活动开始-当前时间
if(flag > 0){//倒计时
leave= flag;
}else if(flag < 0){//已经进行
leave= parseInt(endTimeLong-currentDate);
}
//Math.floor()返回小于等于数字参数的最大整数,对数字进行下舍入
var day = 0;
var hour = 0;
var minute = 0;
var second = 0;
if(parseInt(endTimeLong)==0 || parseInt(startTimeLong)==0){
sale="4";//抢购结束
}
if(parseInt(endTimeLong)>0 && parseInt(startTimeLong)>0){
day = Math.floor(leave / (1000 * 60 * 60 * 24));
hour = Math.floor(leave / (1000*3600)) - (day * 24);
minute = Math.floor(leave / (1000*60)) - (day * 24 *60) - (hour * 60);
second = Math.floor(leave / (1000)) - (day * 24 *60*60) - (hour * 60 * 60) - (minute*60);
if(flag<0){//活动已开始
sale="1";//订购
liveClock.innerHTML = "距离秒杀结束还有<div class=\"time_icon\">"+hour+"</div>时<div class=\"time_icon\">"+minute+"</div>分<div class=\"time_icon\">"+second+"</div>秒";
}
if(flag>0){//活动未开始
//距离活动开始不足24小时
if (day==0 ){
//距离活动开始不足一小时
if(hour==0){
//距离活动开始不足30min
if(minute<30){
sale="2";//等待抢购
liveClock.innerHTML = "距离秒杀开始还有<div class=\"time_icon\">"+minute+"</div>分<div class=\"time_icon\">"+second+"</div>秒";
}
//距离活动开始超过30min
if(minute>=30){
liveClock.innerHTML = "距离秒杀开始还有<div class=\"time_icon\">"+minute+"</div>分<div class=\"time_icon\">"+second+"</div>秒";
}
}
//距离活动开始超过一小时
if(hour>0){
liveClock.innerHTML = "距离秒杀开始还有<div class=\"time_icon\">"+hour+"</div>时<div class=\"time_icon\">"+minute+"</div>分<div class=\"time_icon\">"+second+"</div>秒";
}
}
//距离活动开始超过24小时
if (day>0){
liveClock.innerHTML="距离秒杀开始还有<div class=\"time_icon\">"+day+"</div>天<div class=\"time_icon\">"+hour+"</div>时<div class=\"time_icon\">"+minute+"</div>分<div class=\"time_icon\">"+second+"</div>秒";
}
}
//当前时间大于活动结束时间
if(parseInt(endTimeLong)<parseInt(currentDate)){
sale="3";
}
}
$(".subscribe").unbind();
$(".subscribe").bind("click",function(){
var id = $(this).attr("id");
if(sale!="2" && sale!="3" && sale!="4"){//等候抢购
sub(id);
}
});
//每个120秒向服务器获取一次当前时间时间戳
if(time>120){
posts();
time=0;
}
//秒杀状态
if(sale==""){
sale="0";
}else if(sale=="0"){//预约
$(".subscribe").text("预约");
}else if(sale=="1"){//订购
$(".subscribe").text("抢购");
}else if(sale=="2"){//等候抢购
$(".subscribe").text("即将开始");
}else if(sale=="3"){//抢购结束
$(".subscribe").text("抢购结束");
}else if(sale=="4"){
$(".subscribe").text("已抢完");
}
if(sale=="3"|| sale=="2" || sale=="4"){
$(".subscribe").attr("href","javascript:void(0)");
}
//抢购结束 隐藏倒计时
if(sale=="3" || sale=="4"){
$(".timer").hide();
}
//抢购结束 而且 当前时间小于活动结束时间
if(parseInt(currentDate) > parseInt(endTimeLong) && parseInt(currentDate) < parseInt(activityEndTime) && sale=="3"){
window.location.href="<s:property value="@common.sysutil.SystemConstants@get('context_root')" />/activity/seckill/toIndex.action?"+params;
}
//当前时间小于活动结束时间 或者 订单状态为未支付
if(parseInt(endTimeLong)> parseInt(currentDate) || grabFlag=="1"){
currentDate=parseInt(currentDate)+1000;
time++;
if(grabFlag=="1"){//抢单状态为1
billCountDown();
}
setTimeout("digitalTime()",1000);
}
}
//去支付 倒计时
function billCountDown(){
if(grabTimeLong==""){
grabTimeLong=currentDate+(1000*60*15);
}
var grabLeave=(1000*60*15)-parseInt(currentDate-grabTimeLong);
var grabDay = Math.floor(grabLeave / (1000 * 60 * 60 * 24));
var grabHour = Math.floor(grabLeave / (1000*3600)) - (grabDay * 24);
var grabMinute = Math.floor(grabLeave / (1000*60)) - (grabDay * 24 *60) - (grabHour * 60);
var grabSecond = Math.floor(grabLeave / (1000)) - (grabDay * 24 *60*60) - (grabHour * 60 * 60) - (grabMinute*60);
if(ticketPriceId!=''){
if((grabLeave)<=(1000*60*15) && (grabLeave>=0)){
$("#"+ticketPriceId).text("支付剩余("+grabMinute+"分"+grabSecond+"秒)");
grabLeave=-1;
}else if(grabLeave<0){ //在秒单期间显示已经超时
grabFlag="-1";
$("#"+ticketPriceId).unbind();
$("#"+ticketPriceId).attr("href","javascript:void(0)");
$("#"+ticketPriceId).text("已超时");
}
}
}
/*异步向服务器获取当前时间戳*/
function posts(){
$.ajax({
type:"POST",
dataType: "json",
url:"<s:property value="@common.sysutil.SystemConstants@get('context_root')" />/activity/seckill/timestampAsync.action?"+params,
data:null,
async:false,
success:function(data){
var time = data.timestamp;
if(time!=null){
currentDate=parseInt(time);
}
}
});
}
/*预约*/
function subscribe(){
$.ajax({
type:"POST",
dataType: "json",
url:"<s:property value="@common.sysutil.SystemConstants@get('context_root')" />/activity/seckill/subscribe.action?"+params,
data:null,
async:false,
success:function(data){
if(data.flag==2){
$("#mge").text("你已经预约过了");
alertdiv.show();
return;
}
if(data.flag==1){
sale="2";
$(".subscribe").text("即将开始");
$("#mge").text("预约成功,请等待通知。");
alertdiv.show();
return;
}
if(data.flag==0){
$("#cfmge").text("你还没有登录,请登录!");
$("#cfddiv").unbind();
$("#cfddiv").bind("click",function(){
window.location.href="<s:property value="@common.sysutil.SystemConstants@get('context_root')" />/activity/seckill/login.action?"+params;
});
confirmdiv.show();
return;
}
}
});
}
/*抢购*/
function grabOrder(id){
$("#mge").text("正在处理中,请稍后...");
alertdiv.show();
$.ajax({
type:"POST",
dataType: "json",
url:"<s:property value="@common.sysutil.SystemConstants@get('context_root')" />/activity/seckill/insertGrabOrder.action?ticketPriceId="+id+params,
data:null,
async:true,
success:function(data){
alertdiv.close();
if(data.flag==2||data.flag==-1){
$("#mge").text("很遗憾你没有抢购成功,再接再厉哦~");
alertdiv.show();
return;
}
if(data.flag==3){
$("#mge").text("活动还没有开始,请耐心等待");
alertdiv.show();
return;
}
if(data.flag==1){
grabFlag="1";
grabTimeLong=currentDate;
ticketPriceId=id;
$("#mge").text("你抢购成功了,请在15分钟内进行支付吧。");
$("#"+id).attr("class","btn_order fr");
$("#"+id).text("去支付");
$("#"+id).unbind();
$("#"+id).bind("click",function(){
window.location.href="<s:property value="@common.sysutil.SystemConstants@get('context_root')" />/activity/seckill/toDingdan.action?ticketPriceId="+id+params;
});
alertdiv.show();
}
if(data.flag==0){
$("#cfmge").text("你还没有登录,请登录!");
$("#cfddiv").unbind();
$("#cfddiv").bind("click",function(){
window.location.href="<s:property value="@common.sysutil.SystemConstants@get('context_root')" />/activity/seckill/login.action?"+params;
});
confirmdiv.show();
return;
}
}
});
}
//提交
function sub(id){
if(sale=="0"){//预约
subscribe();
}else if(sale=="1"){//抢购
grabOrder(id);
}
}
//页面加载完毕进行的方法
function load(){
digitalTime();
if(loginFlag=='nologin' && parseInt(endTimeLong)>0 && parseInt(startTimeLong)>0){
$("#cfmge").text("为了保证您抢购顺利,请先登录!");
$("#cfddiv").unbind();
$("#cfddiv").bind("click",function(){
window.location.href="<s:property value="@common.sysutil.SystemConstants@get('context_root')" />/activity/seckill/login.action?"+params;
});
confirmdiv.show();
}
}