seckill-detail.page.js
11.9 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// 调用秒杀js
$(window).on('seckill', function(event, detailData) {
/**
* [秒抢页面js]
* author: 李靖<jing.li@yoho.cn>
* date: 2016/09/08
*/
let $ = require('yoho-jquery'),
tip = require('plugin/tip');
let seckillObj = {};
let offsetTime = 0;
let nowTime = 0;
let startTime = 0;
let endTime = 0,
diffTime = 0; // 误差时间
let dateText = 0,
newDate = 0,
newMonth = 0,
newDay = 0,
newHour = 0,
newMinus = 0;
let secKillPrice = 0;
// 隐藏折扣信息
$('.good-detail-page').addClass('hide-discount');
require('common');
seckillObj = {
el: {
iScroll: null,
currentTick: null
},
startTick: function(status, offTime) {
let that = this,
$el = this.el,
day = parseInt(offTime / (60 * 60 * 24), 10),
hour = parseInt(offTime % (60 * 60 * 24) / (60 * 60), 10),
minute = parseInt(offTime % (60 * 60) / 60, 10),
second = offTime % 60;
let $elem = status === 0 ? $('.seckill-count-num') : $('.end-time');
if (offTime >= -1) {
if (day <= 0) {
$('.day-c').addClass('hide');
}
$elem.find('.tick.day').text(that.formatNumber(day));
$elem.find('.tick.hour').text(that.formatNumber(hour));
$elem.find('.tick.minute').text(that.formatNumber(minute));
$elem.find('.tick.second').text(that.formatNumber(second));
if (offTime <= -1) { // 结束倒计时刷新状态
window.location.reload();
} else {
$el.currentTick = setTimeout(function() {
that.startTick(status, --offTime);
}, 1000);
}
}
},
formatNumber: (number) => {
if (number < 0) {
return '00';
} else if (number < 10) {
return '0' + number;
} else {
return number;
}
},
};
$(
function() {
$('#goodsDiscount').hide(); // 隐藏折扣楼层
$('.cart-bar').hide();
$('.current-price').hide();
let ajaxUrl = '/product/seckillDetail/seckillData/' + $('#productSkn').val();
let timestamp = Date.parse(new Date());
$.ajax({
type: 'GET',
url: ajaxUrl + '?tamp=' + timestamp,
success: function(data) {
// 秒杀是否结束
if (data === '' || data.status === 0 || data.status === 3) {
window.location.replace('/product/' + $('#productSkn').val() + '.html'); // 商品url改版
// $('.sold-out').hide();
// $('.cart-bar a:first').append('<a href="javascript:;" class="sold-out">已售罄</a>');
// $('.addto-cart').hide();
// $('.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">距秒杀开始:' +
'<span class="day-c"><i class="tick day">00</i>天</span>' +
'<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').eq(0).append(
'<div class="seckill-time notStart">' +
'<span class="seckill-time-pic">秒杀预告</span>' +
'<span class="seckill-time-c">月日</span>' +
'</div>'
);
$('.sold-out').hide();
$('.btn-c').append('<a href="javascript:;" class="sold-out">即将开抢</a>');
$('.addto-cart').hide();
$('.cart-bar').show();
$('.current-price').show();
timeInit(data); // eslint-disable-line
}
// 秒杀进行中
if (data.status === 2) {
$('#addtoCart').off('touchstart');
let chosePanel = require('common/chose-panel-new');
let seckillData = data.secKillSku;
detailData.cartInfo.skus.forEach(
function(val) {
seckillData.forEach(
function(item) {
if (val.skuId === item.productSku) {
val.storage = item.storageNum;
}
}
);
}
);
$('#addtoCart').on('touchstart', function() {
$('.cart-bar').hide();
chosePanel.show({
data: detailData,
disableNum: true
}).then(result => {
$('.cart-bar').show();
if (result && result.sku) {
window.location.href = '/cart/index/seckill?skn=' +
$('#productSkn').val() + '&sku=' + result.sku.skuId;
}
}, () => {
$('.cart-bar').show();
});
$('.text-info').append($('.seckill-time').clone());
$('.chose-items .num').find('.clearfix').append(
'<span class="limit-num-text">限购1件</span>'
);
$('.sale-price').text('¥' + secKillPrice).show();
return false;
});
// 插入倒计时
addTimeout($('.price-date').eq(0)); // eslint-disable-line
secKillPrice = toDecimal2(data.secKillPrice); // eslint-disable-line
$('.current-price').text('¥' + secKillPrice).show();
$('.left-num').hide();
$('.btn-plus').removeClass('btn-plus');
$('.addto-cart').text('立即购买');
$('#chose-btn-sure').text('立即购买').addClass('isSecKill');
$('.cart-bar').show();
let seckillNum = 0;
for (let i = 0; i < data.secKillSku.length; i++) {
seckillNum = seckillNum + data.secKillSku[i].storageNum;
}
if (seckillNum === 0) {
$('.sold-out').hide();
$('.btn-c').append('<a href="javascript:;" class="sold-out">已售罄</a>');
$('.addto-cart').hide();
}
$('.current-price').show();
timeInit(data); // eslint-disable-line
}
}
},
error: function() {
tip.show('网络异常~');
}
});
}
);
// function seckHide() {
// $('.chose-panel .size-list ul').each(
// function() {
// hideNum = 0;
// $(this).find('li').each(
// function() {
// if ($(this).hasClass('hide')) {
// oneIndex = $(this).index() + 1;
// twoIndex = $(this).parent('ul').index() - 2;
// $('.color-list ul:eq(' + oneIndex + ') li:eq(' + twoIndex + ')').hide();
// } else {
// hideNum++;
// }
// }
// );
// if (hideNum === 0) {
// hideIndex = $(this).index() - 2;
// $('.color-list ul:first li:eq(' + hideIndex + ')').hide();
// }
// }
// );
// $('.chose-panel .color-list ul').each(
// function() {
// hideNum = 0;
// $(this).find('li').each(
// function() {
// if ($(this).hasClass('hide')) {
// oneIndex = $(this).index() + 1;
// twoIndex = $(this).parent('ul').index() - 2;
// $('.size-list ul:eq(' + oneIndex + ') li:eq(' + twoIndex + ')').hide();
// } else {
// hideNum++;
// }
// }
// );
// if (hideNum === 0) {
// hideIndex = $(this).index() - 2;
// $('.size-list ul:first li:eq(' + hideIndex + ')').hide();
// }
// }
// );
// }
function addTimeout(obj) {
obj.append(
'<div class="seckill-time seckill-time-border">' +
'<span>距结束 </span>' +
'<span class="end-time">' +
'<i class="tick day hide">00</i>' +
'<i class="tick hour">00</i>:' +
'<i class="tick minute">00</i>:' +
'<i class="tick second">00</i>' +
'</span>' +
'</div>'
);
}
// 强制保留2位小数点
function toDecimal2(num) {
let f = parseFloat(num);
if (isNaN(f)) {
return false;
}
f = Math.round(num * 100) / 100;
let s = f.toString();
let rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + 2) {
s += '0';
}
return s;
}
function timeInit(data) {
let status = 0;
startTime = data.startTime;
endTime = data.endTime;
diffTime = data.currentTime;
nowTime = diffTime;
if (startTime > nowTime) {
offsetTime = startTime - nowTime;
} else if (nowTime > startTime && nowTime < endTime) {
offsetTime = endTime - nowTime;
status = 1;
}
dateText = Number(data.startTime * 1000);
newDate = new Date(dateText);
newMonth = newDate.getMonth() + 1;
newDay = newDate.getDate();
newHour = newDate.getHours();
newMinus = newDate.getMinutes();
if (newMinus === 0) {
newMinus = '00';
}
$('.notStart').find('.seckill-time-c').text(newMonth + '月' + newDay + '日' + newHour + ':' + newMinus);
seckillObj.startTick(status, offsetTime);
}
});
// 调用新商品详情页js
require('./new-detail');