shop.page.js
5.49 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
/**
* 首页
* @author: bikai<kai.bi@yoho.cn>
* @date: 2016/4/26
*/
var $ = require('yoho-jquery'),
lazyLoad = require('yoho-jquery-lazyload'),
Handlebars = require('yoho-handlebars');
var product = require('./index/product');
var $shopIntro = $('.shop-intro'),
$shopCollect = $('.shop-collect'),
$colloectIcon = $shopCollect.find('.shop-collect-ico'),
$colloectText = $shopCollect.find('.shop-collect-text'),
$searchForm = $('#shop-search-form'),
$sliderLeft = $('.slider-left'),
$allGoods = $('.all-goods'),
$fixedArea = $allGoods.find('.fixed-area'),
fixedAreaTop = $fixedArea.offset() ? $fixedArea.offset().top : 0;
var shopId = $shopCollect.data('id');
var $qrcodeImg = $('#qrcode-img');
// Pjax
require('yoho-jquery-pjax');
require('../common');
require('../plugins/filter');
require('../plugins/sort-pager');
require('../plugins/slider');
require('./index/top-coupon');
require('yoho-jquery-qrcode');
// 生成二维码
$qrcodeImg.qrcode({
render: 'div', // 显示方式,canvas,image和div
text: $qrcodeImg.attr('data-url'), // 二维码的内容
size: parseInt($qrcodeImg.width(), 10) // 大小
});
$('.qrcode-decoration').mouseenter(function() {
window._hmt && window._hmt.push(['_trackEvent', '二维码', '经典店铺', 'shopId', $(this).attr('data-id')]);
});
product.init(4);
lazyLoad($('img.lazy'));
if ($sliderLeft.length) {
$sliderLeft.slider();
}
if ($shopCollect && $shopCollect.length) {
$.ajax({
type: 'POST',
url: '/product/index/isFavoriteShop',
data: {
shopId: shopId
}
}).then(function(data) {
if (data.code === 200 && data.data) {
// 已收藏
$colloectIcon.addClass('on');
$colloectText.html('已收藏');
return;
}
$colloectIcon.removeClass('on');
$colloectText.html('收藏');
});
}
$shopIntro.on('click', function() {
$('.pop-shop-intro').show();
$('.mask').show();
});
$('.close-btn, .mask').on('click', function() {
$('.pop-shop-intro').hide();
$('.mask').hide();
});
$('.shop-query-submit').on('click', function() {
$searchForm.submit();
});
(function() {
var $adBox = $('#brand-shop-ad');
var html = $('#pic-link-tpl').html() || '';
$.ajax({
type: 'POST',
url: '/product/index/getAdnav',
data: {shopId: shopId}
}).then(function(data) {
var $sellRed = $('.sell-recommend');
if (data) {
if ($sellRed && $sellRed.length) {
$sellRed.before($adBox);
} else {
$adBox.parent().append($adBox);
}
$adBox.html(Handlebars.compile(html)({
picLink: [data.series, data.floder]
}));
}
});
}());
// 收藏店铺
function colloectAction() {
var isFavorite = $colloectIcon.hasClass('on'),
needColloect = window.cookie('needColloect');
$.ajax({
type: 'post',
url: '/product/shop/togglecollect',
data: {
isFavorite: isFavorite ? 0 : 1,
needColloect: needColloect,
shopId: shopId
},
success: function(res) {
if (res.code === 200) {
if (isFavorite) {
$colloectIcon.removeClass('on');
$colloectText.html('收藏');
} else {
$colloectIcon.addClass('on');
$colloectText.html('已收藏');
}
if (needColloect) {
$colloectIcon.addClass('on');
$colloectText.html('已收藏');
}
} else if (res.code === 401) {
window.setCookie('needColloect', '1', {
path: '/',
domain: '.yohobuy.com',
expires: 90
});
location.href = res.data.url;
}
}
});
}
$shopCollect.on('click', function() {
colloectAction();
});
if (window.cookie('needColloect') * 1 === 1 && window.getUid()) {
colloectAction();
window.setCookie('needColloect', '', {
path: '/',
domain: '.yohobuy.com',
expires: 1
});
}
// 全部商品使用 pjax 翻页
$allGoods.pjax('a.pjax', '.goods-wrap', {
timeout: 5000,
scrollTo: false // 默认滚动没有动画,禁止掉
}).on('pjax:end', function() {
product.init(4);
lazyLoad($('img.lazy'));
$fixedArea = $allGoods.find('.fixed-area'); // 翻页后 fixed-area 区域有变化,需要更新选择器
// 分页后移动到全部商品
$('html, body').animate({
scrollTop: $allGoods.offset().top - 30
});
}).on('click', '.menu-list li', function() {
var $this = $(this);
$this.siblings().removeClass('on');
$this.addClass('on');
}).on('click', 'a.checked', function(e) {
e.preventDefault();
});
$(window).on('scroll', function() {
var scrollTop = $(this).scrollTop();
if (scrollTop > fixedAreaTop) {
$fixedArea.css({
position: 'fixed'
});
} else {
$fixedArea.css({
position: 'absolute'
});
}
});
// 获取店铺推荐文章
(function() {
var $trendInfo = $('.trend-info');
var brands = $trendInfo.data('brands');
if (brands) {
$.ajax({
type: 'POST',
url: '/product/shop/article',
data: {brands: brands}
}).then(function(data) {
$trendInfo.replaceWith(data);
});
}
}());