cart-together.js
6.7 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
/**
* Created by yoho on 2017-01-13.
*/
var lazyLoad = require('yoho-jquery-lazyload');
var dataLazyLoad = require('../plugins/lazy-load')(document);
var capi = require('./cart-api');
var yas = require('../common/data-yas');
var togetherTpl = require('hbs/cart/cart-together-item.hbs');
var $togetherSlide = $('[data-role=together-slide]');
var $fineSlide = $('[data-role=fine-slide]');
var $latestWalkSlide = $('[data-role=latestWalk-slide]');
var productInfoTpl = require('hbs/cart/cart-product-info-tpl.hbs');
var $goodsSelWin = $('#Y_goodsSelectWin');
function randomString(len) {
var $chars = 'ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-';
var maxPos = $chars.length,
pwd = '',
i;
for (i = 0; i < len; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
function loadTogetherProduct(page) {
capi.getTogetherProduct(page).done(function(togetherInfo) {
if (togetherInfo && togetherInfo.code === 200) {
$togetherSlide.html(togetherTpl(togetherInfo.data));
}
});/* .fail(function() {
});*/
}
function loadFineProduct(page) {
capi.getFineProduct(page).done(function(fineInfo) {
if (fineInfo && fineInfo.code === 200) {
$fineSlide.html(togetherTpl($.extend({
showAdd2Cart: true
}, fineInfo.data)));
$fineSlide.find('li').eq(4).css('margin-right', 0);
// 若无数据,请隐藏容器
if (!fineInfo.data || !fineInfo.data.item || fineInfo.data.item.length === 0) {
$fineSlide.remove();
$('[data-role=fine-tab]').remove();
$('.bottom-tab-slide .change').remove();
$('.bottom-tab-slide .bottom-title').eq(0).trigger('click');
return;
} else {
$('.bottom-tab-slide').removeClass('none');
$('.bottom-slide-content').removeClass('none');
}
lazyLoad($fineSlide.find('.lazy'));
$fineSlide.find('.good').on('click', function() {
var $this = $(this);
var proId = $this.data('id');
var loadYas = {
REC_POSE: 120003,
REC_ID: randomString(40),
PRD_ID: proId,
PRD_NUM: $this.data('index') + 1,
ACTION_ID: 1,
PAGE_NUM: 1
};
yas.givePoint('YB_CHOOSE_FOR_YOU_Y', loadYas);
});
yas.givePoint('YB_CHOOSE_FOR_YOU_Y', {
REC_POSE: 120003,
REC_ID: randomString(40),
PRD_ID: $.map(fineInfo.data.item, function(value) {
return value.id;
}).join(','),
PRD_NUM: fineInfo.data.item.length,
ACTION_ID: 0,
PAGE_NUM: 1
});
}
});/* .fail(function() {
});*/
}
function fetchLatestWalk(num) {
$.ajax({
url: '//www.yohobuy.com/common/recentReview',
dataType: 'jsonp',
data: {
limit: num || 15
},
success: function(data) {
var latestWalk = [],
res, i, cur;
if (data.code === 200) {
res = data.data;
// 若无最近浏览,请隐藏容器
if (res.length === 0) {
$latestWalkSlide.hide();
$('[data-role=latestWalk-tab]').hide();
return;
} else {
$('.bottom-tab-slide').removeClass('none');
$('.bottom-slide-content').removeClass('none');
}
for (i = 0; i < res.length; i++) {
cur = res[i];
latestWalk.push({
href: cur.url,
img: cur.pic_url,
title: cur.product_name,
price: cur.price,
marketPrice: cur.market_price
});
}
$latestWalkSlide.html(togetherTpl({
item: latestWalk,
hasNext: latestWalk && latestWalk.length > 5,
hasPrev: latestWalk && latestWalk.length > 5
}));
lazyLoad($latestWalkSlide.find('.lazy'));
$latestWalkSlide.find('.individual-item').slider2({
shownum: 5,
isCircle: true
});
}
}
});
}
// 凑单商品翻页
$togetherSlide.on('click', '.pagenext, .pageprev', function() {
loadTogetherProduct(Math.ceil(Math.random() * 100) + 1);
});
$('.bottom-tab-slide .change').on('click', function() {
var pageNum = Number($fineSlide.data('pagenum') || 1);
if (pageNum !== 3) {
pageNum++;
} else {
pageNum = 1;
}
$fineSlide.data('pagenum', pageNum);
loadFineProduct(pageNum);
});
// 为您优选翻页
/* $fineSlide.on('click', '.pagenext, .pageprev', function() {
var pageNum = Number($fineSlide.data('pagenum') || 1);
if ($(this).hasClass('pagenext')) {
if (pageNum !== 5) {
pageNum++;
} else {
pageNum = 1;
}
} else {
--pageNum > 0 || (pageNum = 1);
}
$fineSlide.data('pagenum', pageNum);
loadFineProduct(pageNum);
});*/
// 推荐、优选、浏览切换
$('.bottom-tab-slide').on('click', '.bottom-title', function() {
var $this = $(this),
$triggers = $('.bottom-tab-slide .bottom-title'),
index = $this.index(),
$panels = $('.bottom-slide-content .slide-panel'),
$curPanel = $panels.eq(index),
$chgBtn = $('.bottom-tab-slide .change');
$panels.hide();
$panels.eq(index).show();
if ($this.hasClass('cur')) {
return;
}
$triggers.removeClass('cur');
$this.addClass('cur');
if ($this.data('type') === 'fine') {
$chgBtn.show();
} else {
$chgBtn.hide();
}
$panels.hide();
$curPanel.slideDown();
});
function showTogetherSelWin(id, skn) {
capi.getProductInfo(id, skn).then(function(res) {
$goodsSelWin.find('.detail-body').empty().append(productInfoTpl(res));
});
capi.showMDialog('#Y_goodsSelectWinDialog');
}
$('.bottom-slide-content').on('click', '[data-role=add2cart]', function() {
var $this = $(this);
var id = $this.data('id');
var skn = $this.data('skn');
showTogetherSelWin(id, skn);
});
window.loadTogetherProduct = loadTogetherProduct;
window.loadFineProduct = loadFineProduct;
window.fetchLatestWalk = fetchLatestWalk;
// 数据懒加载
dataLazyLoad.init({cls: '.lazy-load-object', threshold: 700});