red-shop.js
3.86 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
/*
* @Author: Targaryen
* @Date: 2017-03-23 11:31:51
* @Last Modified by: Targaryen
* @Last Modified time: 2017-05-02 11:17:31
*/
/** *****************
* 红人店铺首页
********************/
const Swiper2 = require('yoho-swiper2');
const lazyLoad = require('yoho-jquery-lazyload');
let tip = require('../../plugin/tip');
let $goodsContainer = $('.index-goods-container');
let $indexGoodsContaniner = $('#indexGoodsContainer');
let $collect = $('#collect');
const shopId = $('#shopId').val();
lazyLoad($('.lazy'));
/**
* 异步检测是否已经收藏
*/
$.ajax({
type: 'GET',
url: location.protocol + '//m.yohobuy.com/product/index/shopFav',
xhrFields: {
withCredentials: true
},
data: {
shopId: shopId
},
success: function(data) {
if (data.collect) {
$collect.attr('class', 'already-collect pull-left');
}
},
error: function() {
tip.show('网络断开连接了~');
}
});
/**
* 店铺轮播图
*/
if ($('.shop-swiper')) {
let num = $('.shop-swiper').length;
for (let i = 1; i <= num; i++) {
new Swiper2('.shop-swiper-' + i, {
lazyLoading: true,
lazyLoadingInPrevNext: true,
loop: true,
autoplay: 3000,
slideElement: 'li',
paginationClickable: true,
pagination: $(this).closest('.shop-swiper-' + i).find('.pagination-inner').get(0)
});
}
}
/**
* 异步加载推荐商品
*/
$.each($goodsContainer, function(index, elem) {
let url = '/product/new/shop/hotlist';
let data = {};
switch (parseInt($(elem).data('type'), 10)) {
case 0:
url = '/product/search/search';
Object.assign(data, {
shop_id: shopId,
order: $(elem).data('order')
});
break;
case 1:
Object.assign(data, {
skns: $(elem).data('skns')
});
break;
default:
break;
}
$.ajax({
type: 'GET',
url: url,
xhrFields: {
withCredentials: true
},
data: data,
success: function(result) {
let $result = $(result);
lazyLoad($result.find('img[class=lazy]'));
$(elem).html($result);
}
});
});
/**
* 异步加载首页全部商品
*/
$.ajax({
type: 'GET',
url: '/product/search/search',
xhrFields: {
withCredentials: true
},
data: {
shop_id: shopId
},
success: function(result) {
let $result = $(result);
lazyLoad($result.find('img[class=lazy]'));
$indexGoodsContaniner.find('.container').html($result);
}
});
/**
* 店铺收藏取消收藏操作
*/
$collect.on('click', function() {
let options = {
id: shopId,
opt: 'ok',
type: 'shop'
};
if ($collect.hasClass('already-collect')) {
options.opt = 'cancel';
}
$.ajax({
method: 'get',
url: location.protocol + '//m.yohobuy.com/product/opt/favoriteBrand',
xhrFields: {
withCredentials: true
},
data: options,
success: function(result) {
if (result.code === 200) {
if ($collect.hasClass('already-collect')) {
$collect.attr('class', 'not-collect pull-left');
tip.show('店铺取消收藏成功');
} else {
$collect.attr('class', 'already-collect pull-left');
tip.show('店铺收藏成功');
}
}
if (result.code === 400) {
if ($('#jump-login').length <= 0) {
$('body').append('<a href=\'' + result.data + '\'><span id="jump-login"><span></a>');
}
$('#jump-login').click();
}
}
});
});