tick.js
7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* [秒抢页面js]
* author: 陈峰<feng.chen@yoho.cn>
* date: 2016/09/08
*/
var $ = require('yoho-jquery'),
tip = require('../plugin/tip');
var seckillObj = {};
var timeObj = '';
var offsetTime = 0;
var nowTime = Date.parse(new Date()) / 1000;
var startTime = 0;
var endTime = 0;
var dateText = 0,
newDate = 0,
newMonth = 0,
newDay = 0,
newHour = 0,
newMinus = 0;
var skuFlag;
require('../common');
seckillObj = {
el: {
iScroll: null,
currentTick: null
},
startTick: function(elem, offsetTime) {
var that = this,
$el = this.el,
hour = parseInt(offsetTime / (60 * 60), 10),
minute = parseInt(offsetTime % (60 * 60) / 60, 10),
second = offsetTime % 60;
if (offsetTime) {
$(elem).find('.tick.hour').text(hour < 0 ? '00' : (hour < 10 ? ('0' + hour) : hour));
$(elem).find('.tick.minute').text(minute < 0 ? '00' : (minute < 10 ? ('0' + minute) : minute));
$(elem).find('.tick.second').text(second < 0 ? '00' : (second < 10 ? ('0' + second) : second));
if (offsetTime <= 0) { // 结束倒计时刷新状态
window.location.reload();
} else {
$el.currentTick = setTimeout(function() {
that.startTick(elem, --offsetTime);
}, 1000);
}
}
}
};
$(
function() {
$('.cart-bar').hide();
$('.current-price').hide();
var ajaxUrl = '/product/detail/seckillData/' + $('#productSkn').val();
$.ajax({
type: 'GET',
url: ajaxUrl,
success: function(data) {
// 秒杀是否结束
if (data.status !== 1 && data.status !== 2) {
$('.cart-bar').show();
$('.current-price').show();
} else {
// 秒杀开始前
if (data.status === 1) {
$('.cart-bar').before(
'<div class="seckill-count">' +
'<div class="seckill-count-bg"></div>' +
'<div class="seckill-count-num">距秒杀开始:' +
'<i class="tick hour">00</i>时' +
'<i class="tick minute">00</i>分' +
'<i class="tick second">00</i>秒' +
'</div>' +
'</div>'
);
$('.current-price').text('¥' + data.secKillPrice).show();
$('.price-date').append(
'<div class="seckill-time notStart">' +
'<span class="seckill-time-pic">秒杀预告</span>' +
'<span class="seckill-time-c">月日</span>' +
'</div>'
);
$('.cart-bar a:first').append('<a href="javascript:;" class="sold-out">即将开抢</a>');
$('.addto-cart').hide();
$('.cart-bar').show();
$('.current-price').show();
}
// 秒杀进行中
if (data.status === 2) {
$('.chose-panel .size-list').find('li[data-skuid != \'\']').each(
function() {
skuFlag = 0;
for (var i = 0; i < data.secKillSku.length; i++) {
if (data.secKillSku[i].productSku === Number($(this).attr('data-skuid'))) {
skuFlag++;
// 如果秒杀库存为0
if (data.secKillSku[i].storageNum === 0) {
skuFlag = 0;
}
} else {
// alert("没有匹配");
}
}
if (skuFlag === 0) {
$(this).addClass('zero-stock').attr('data-num', '1');
var oneIndex = $(this).index() + 1;
var twoIndex = $(this).parent('ul').index() - 2;
$('.chose-panel .color-list ul:eq(' + oneIndex + ') li:eq(' + twoIndex + ')').addClass('zero-stock');
}
}
);
$('.price-date').append(
'<div class="seckill-time seckill-time-border">' +
'<span>距结束 </span>' +
'<span class="end-time">' +
'<i class="tick hour">00</i>:' +
'<i class="tick minute">00</i>:' +
'<i class="tick second">00</i>' +
'</span>' +
'</div>'
);
$('.text-info').append(
'<div class="seckill-time seckill-time-border seckill-chose">' +
'<span>距结束 </span>' +
'<span class="end-time">' +
'<i class="tick hour">00</i>:' +
'<i class="tick minute">00</i>:' +
'<i class="tick second">00</i>' +
'</span>' +
'</div>'
);
$('.current-price').text('¥' + data.secKillPrice).show();
$('.sale-price').text('¥' + data.secKillPrice).show();
$('.chose-items .num').find('.clearfix').append(
'<span class="limit-num-text">限购1件</span>'
);
$('.left-num').hide();
$('.btn-plus').removeClass('btn-plus');
$('.addto-cart').text('立即购买');
$('#chose-btn-sure').text('立即抢购').addClass('isSecKill');
$('.cart-bar').show();
var seckillNum = 0;
for (var i = 0; i < data.secKillSku.length; i++) {
seckillNum = seckillNum + data.secKillSku[i].storageNum;
}
$('.current-price').show();
}
startTime = data.startTime;
endTime = data.endTime;
if (startTime > nowTime) {
offsetTime = startTime - nowTime;
timeObj = $('.seckill-count-num');
} else if (nowTime > startTime && nowTime < endTime) {
offsetTime = endTime - nowTime;
timeObj = $('.end-time');
}
dateText = Number(data.startTime * 1000);
newDate = new Date(dateText);
newMonth = newDate.getMonth() + 1;
newDay = newDate.getDate();
newHour = newDate.getHours();
newMinus = newDate.getMinutes();
$('.notStart').find('.seckill-time-c').text(newMonth + '月' + newDay + '日' + newHour + ':' + newMinus);
seckillObj.startTick(timeObj, offsetTime);
}
},
error: function() {
tip.show('网络异常~');
}
});
}
);