item.page.js
8.84 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* 商品详情
* @author: yyqing<yanqing.yang@yoho.cn>
* @date: 2016/7/15
*/
var $ = require('yoho-jquery'),
handlebars = require('yoho-handlebars'),
lazyload = require('yoho-jquery-lazyload'),
Dialog = require('../plugins/dialog').Dialog,
regx = require('../passport/common/mail-phone-regx').phoneRegx,
recProduct = require('./item/recommend-product');
var $main = $('.product-main'),
$mainThumb = $('#main-thumb'),
$checkBtns = $('.check-btns');
var $tradeWrapper = $('.trade-wrapper'),
$balanceWrapper = $('.balance-wrapper'),
$thumbWrap = $('.thumb-wrap'),
$colorText = $('.choose-color .color'),
$sizeText = $('.choose-size .size'),
$colorItem = $('.color-list .round-color'),
$sizeList = $('.size-list'),
$sizeItem = $('.size-list > li');
var $addToBag = $('#add-to-bag'),
$soldOut = $('#sold-out');
var $sizeInfo = $('.size-info');
var $saleReturn = $('#saleReturn');
var $thumbCur = $('.thumb-wrap .cur'); // 当前选中展示图片
var SLIDETIME = 200;
var noticeFn = handlebars.compile($('#arrival-notice-tpl').html() || ''),
noticeSuccFn = handlebars.compile($('#arrival-notice-success-tpl').html() || '');
var thumbsLoaded = [],
id = $main.data('id'),
skn = $main.data('skn'),
introHtml;
require('yoho-jquery-nanoscroller');
require('../plugins/share');
require('../common/header');
require('../common/return-top');
require('../plugins/share');
lazyload($('img.lazy'));
recProduct.init({
url: '/product/getRecommendShop',
dom: '.recommend-product',
isGoodsDetail: true,
params: {
productSkn: skn
}
});
function changeThumb($dom) {
var data;
if ($dom && $dom.length) {
$thumbCur = $dom;
$dom.addClass('cur').siblings('.cur').removeClass('cur');
data = $dom.data();
if (data.shower) {
$mainThumb.attr('src', data.shower);
}
}
}
function chooseDefaultSize() {
var $item = $sizeList.not('.hide').children().not('.disable');
if ($colorItem && $colorItem.length === 1 &&
$item && $item.length === 1) {
$item.trigger('click');
}
}
function getBuyProduct() {
var sku;
$sizeItem.each(function() {
var $this = $(this),
data;
if ($this.hasClass('cur')) {
data = $this.data();
if (+data.num) {
sku = data.sku;
}
}
});
return sku;
}
function chooseColor(index) {
var $thumb = $thumbWrap.eq(index),
$imgs = $thumb.children();
$thumbWrap.not('.hide').addClass('hide');
$thumb.removeClass('hide');
changeThumb($imgs.first());
if (!thumbsLoaded[index]) {
lazyload($imgs, {
event: 'sporty'
});
$imgs.trigger('sporty');
thumbsLoaded[index] = true;
}
// 控制左右切换按钮显示与隐藏
if ($imgs.length > 1) {
$checkBtns.removeClass('hide');
} else {
$checkBtns.addClass('hide');
}
// 控制才显示对应颜色尺码
$sizeList.not('.hide').addClass('hide').children('.cur').removeClass('cur');
$sizeList.eq(index).removeClass('hide');
$sizeText.text('');
}
function arrivalNotice() {
var tip = new Dialog({
className: 'arr-notice-dialog',
content: noticeSuccFn(),
btns: [
{
id: 'sure',
btnClass: ['sure'],
name: '确定',
cb: function() {
// 目前没有到货通知接口,前端只有展示逻辑
tip.close();
}
}
]
}).show();
}
function bagBtnSwitch(num) {
if (num) {
$addToBag.removeClass('hide');
$soldOut.addClass('hide');
} else {
$addToBag.addClass('hide');
$soldOut.removeClass('hide');
}
}
// 缩略图垂直居中
$thumbWrap.each(function() {
var $this = $(this),
$child = $this.children();
if ($child.length) {
$this.css('padding-top', (642 - 105 * $child.length) / 2);
}
});
$thumbWrap.parent().removeClass('hide');
// 展示图
$thumbWrap.on('mouseenter', '.thumb', function() {
changeThumb($(this));
});
$checkBtns.on('click', '.iconfont', function() {
var $dom;
if ($(this).hasClass('pre-thumb')) {
$dom = $thumbCur.prev();
} else {
$dom = $thumbCur.next();
}
if ($dom.length) {
changeThumb($dom);
}
});
// 颜色
$('.color-list').on('click', '.color-item', function() {
var $this = $(this),
index = $this.index(),
data = $this.data();
if (!$this.children().hasClass('cur')) {
$colorItem.removeClass('cur');
$colorText.text(data.color ? data.color : '');
$this.children().addClass('cur');
// 设置图片及尺码
chooseColor(index);
// 加入购物车按钮显示控制
bagBtnSwitch(+data.total);
}
});
// 尺码
$('.size-list').on('click', 'li', function() {
var $this = $(this),
data = $this.data();
$sizeItem.removeClass('cur');
$sizeText.text(data.name ? data.name : '');
$this.addClass('cur');
// 加入购物车按钮显示控制
bagBtnSwitch(+data.num);
});
// 到货通知
$('.arr-notice').click(function() {
var tip = new Dialog({
className: 'arr-notice-dialog',
content: noticeFn({}),
btns: [
{
id: 'apply',
btnClass: ['apply'],
name: '订阅',
cb: function() {
var dom = tip.$el.find('.notice-phone'),
num = dom.val ? dom.val() : '';
if (regx['+86'].test(num)) {
arrivalNotice(num);
tip.close();
} else {
dom.addClass('err');
}
}
},
{
id: 'cancel',
btnClass: ['cancel'],
name: '取消',
cb: function() {
tip.close();
}
}
]
}).show();
});
// 加入购物袋
$addToBag.click(function() {
var sku = getBuyProduct();
if (!sku) {
$sizeText.html('<em class="blue">请选择尺码</em>');
return;
}
$.ajax({
type: 'POST',
url: '/shopping/cart/add',
data: {
productSku: sku,
buyNumber: 1
}
}).then(function(data) {
if (data.code === 200) {
$tradeWrapper.slideUp(SLIDETIME);
$balanceWrapper.slideDown(SLIDETIME);
}
});
});
// 继续购物
$('#keep-shopping').click(function() {
$balanceWrapper.slideUp(SLIDETIME);
$tradeWrapper.slideDown(SLIDETIME);
});
$('#brand-fav').click(function() {
var $this = $(this),
param = {
brandId: $this.data('id'),
type: 'add'
};
if (!param.brandId) {
return;
}
if ($this.hasClass('coll')) {
param.type = 'cancel';
}
$.ajax({
type: 'POST',
url: '/product/brand/togglecollect',
data: param
}).then(function(data) {
if (data.code === 200) {
$this.toggleClass('coll');
} else if (data.code === 400) {
location.href = data.data.refer;
}
});
});
// 商品收藏
$('#collect-product').click(function() {
var $this = $(this),
param = {
productId: id,
type: 'add'
};
if ($this.hasClass('coll')) {
param.type = 'cancel';
}
$.ajax({
type: 'POST',
url: '/product/item/togglecollect',
data: param
}).then(function(data) {
if (data.code === 200) {
$this.toggleClass('coll');
} else if (data.code === 400) {
location.href = data.data.refer;
}
});
});
$('.more-brand-intro').click(function() {
new Dialog({
className: 'item-more-intro-dialog',
content: introHtml,
btns: []
}).show();
$('.full-intro-text').nanoScroller();
});
$sizeInfo.on('click', '.show-img', function() {
var img = $sizeInfo.data('img');
new Dialog({
className: 'item-doubt-dialog',
content: '<img src="' + img + '">',
btns: []
}).show();
});
if ($(window).height() < 800) {
$('.full-intro-text').addClass('min-height');
}
introHtml = $('#intro-full-hide').html();
chooseDefaultSize(); // 如果size只有一个则默认选中
// 特殊商品退换货
function fetchReturn() {
return $.ajax({
type: 'GET',
url: '/product/item/fetchReturn',
data: {
skn: skn
}
}).then(function(result) {
if (result.code === 200) {
if (result.data.result === 'N') {
$saleReturn.removeClass().addClass('not-support-returned-service');
}
}
});
}
fetchReturn();