favorite.js
12.4 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/**
* 我的收藏
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/2/23
*/
var $ = require('yoho.jquery'),
Handlebars = require('yoho.handlebars');
var phoneReg = require('../passport/mail-phone-regx').phoneRegx['+86'];
var clockTxt = {
on: '[ 降价通知 ]',
off: '[ 取消通知 ]'
};
var param = location.href.match(/type=(.*)[&]*/),
favType;
var curSkn;
var $curClock;
require('yoho.dotdotdot');
//商品收藏
(function() {
var $defaultSorts,
$allSorts;
var $bodyMask,
$noticeNum,
$priceNotice,
$noticeContent,
$noticeSuccess,
$noticeErrorMsg,
$noticeSubOrCancel,
$phone;
var reduction = {},
noticeNum;
// 同步订阅数量
function syncSubscribeNum(num) {
noticeNum = num * 1;
$noticeNum.text(num);
$noticeSuccess.find('.notice-num').text(5 - num);
}
//关闭价格订阅弹窗和蒙层
function closeNoticeBox() {
$bodyMask.addClass('hide');
$priceNotice.addClass('hide');
//重置头部文字
$noticeSubOrCancel.text('订阅成功');
}
//显示价格订阅弹窗和蒙层
function showNoticeBox() {
var top = $(document).scrollTop() + $(window).height() / 2;
if (typeof $bodyMask === 'undefined') {
$('body').append('<div class="body-mask hide"></div>');
$bodyMask = $('.body-mask');
}
$bodyMask.css({
height: $(document).height(),
width: $(document).width()
}).removeClass('hide');
$priceNotice.removeClass('error');
if ($curClock.hasClass('noticed')) {
//取消降价通知
$.ajax({
type: 'GET',
url: '/home/favorite/cancelnotice',
data: {
id: curSkn
}
}).then(function(data) {
if (data.code === 200) {
syncSubscribeNum(data.data.num);
$noticeContent.addClass('hide');
$noticeSuccess.removeClass('hide');
$noticeSubOrCancel.text('取消成功');
$priceNotice.removeClass('hide').css({
top: top
});
if ($curClock.closest('.reduction-products').length > 0) {
$curClock.closest('.fav-good').remove();
} else {
$curClock.removeClass('noticed').text(clockTxt.on);
}
} else {
if (data.message) {
$noticeErrorMsg.text(data.message);
$priceNotice.removeClass('hide').addClass('error').css({
top: top
});
}
}
});
} else {
if (noticeNum >= 5) {
$noticeErrorMsg.text('您的订阅数已经到达上限');
$priceNotice.removeClass('hide').addClass('error').css({
top: top
});
} else {
$noticeContent.removeClass('hide');
$noticeSuccess.addClass('hide');
$priceNotice.removeClass('hide').css({
top: top
});
}
}
}
if ($('.fav-products').length === 0) {
return;
}
$defaultSorts = $('.default-sorts');
$allSorts = $('.all-sorts');
$noticeNum = $('.reduction-notice span');
$priceNotice = $('.price-notice');
$noticeContent = $('#price-notice .content');
$noticeSuccess = $('#price-notice .success');
$noticeErrorMsg = $('#price-notice .error-message');
$noticeSubOrCancel = $noticeSuccess.find('.subscribe-or-cancel');
$phone = $('#notice-phone-num');
reduction.phone = $phone.val();
reduction.change = false;
noticeNum = $noticeNum.text() * 1;
//展开分类
$('#spread-sort').click(function() {
$defaultSorts.slideUp(function() {
$allSorts.slideDown();
});
});
//收起分类
$('#retract-sort').click(function() {
$allSorts.slideUp(function() {
$defaultSorts.slideDown();
});
});
//降价通知
$('.price-down-clock').click(function() {
$curClock = $(this);
curSkn = $curClock.closest('.fav-good').data('id');
showNoticeBox();
});
//checkbox【手动触发checkbox点击后跳转链接的地址】
$('.reduction-filter input[type="checkbox"]').change(function() {
location.href = $(this).parent().attr('href');
});
//降价通知确定按钮
$('#price-notice').on('click', '.close', function() {
closeNoticeBox();
}).on('click', '.price-notice-sure', function() {
var phone,
errTxt;
if (!$priceNotice.hasClass('error') && !$noticeContent.hasClass('hide')) {
//订阅到手机号
phone = $.trim($phone.val());
if (phone === '' || !phoneReg.test(phone)) {
errTxt = phone === '' ? '请输入手机号码' : '手机号码格式不正确';
$phone.addClass('error');
$('#price-notice .err-text').removeClass('hide').find('em').html(errTxt);
return;
} else {
$phone.removeClass('error');
$('#price-notice .err-text').addClass('hide');
}
if (reduction.phone !== '' && reduction.phone !== phone && !reduction.change) {
$('#price-notice .phone-diff').removeClass('hide');
reduction.change = true;
return;
}
$.ajax({
type: 'GET',
url: '/home/favorite/notice',
data: {
mobile: phone,
id: curSkn
}
}).then(function(data) {
if (data.code === 200) {
syncSubscribeNum(data.data.num);
$noticeSuccess.removeClass('hide');
$noticeContent.addClass('hide');
reduction.phone = phone;
reduction.change = false;
$('#price-notice .phone-diff').addClass('hide');
//切换商品通知文字
$curClock.toggleClass('noticed').text(clockTxt.off);
} else {
if (data.message) {
$noticeErrorMsg.text(data.message);
$priceNotice.removeClass('hide').addClass('error');
}
}
});
} else {
//关闭窗口
closeNoticeBox();
}
});
//商品可参加活动
$('.has-activity').click(function() {
var $this = $(this),
$activites = $this.next('.activites'),
$li = $this.closest('li');
if ($activites.hasClass('hide')) {
//显示
$activites.removeClass('hide');
$li.css('padding-bottom', $activites.outerHeight());
} else {
//隐藏
$activites.addClass('hide');
$li.css('padding-bottom', '');
}
});
}());
//品牌收藏
(function() {
var naTpl;
var naCache = {};
/**
* 翻页控制
* @param $li 当前品牌容器
* @param flag 翻页方向/-1-pre 1-next
*/
function pageControll($li, flag) {
var id = $li.data('id'),
$ul = $li.find('.na-goods-ul'),
theCache = naCache[id],
curPage = theCache.cur ? theCache.cur : 1,
page = Math.ceil(theCache.total / 5);
var $pre = $li.find('.na-pre');
var $next = $li.find('.na-next');
curPage += flag;
//初始
$pre.removeClass('hidden');
$next.removeClass('hidden');
//第一页
if (curPage === 1) {
$pre.addClass('hidden');
}
//最后一页
if (curPage === page) {
$next.addClass('hidden');
}
$ul.animate({
marginLeft: -(curPage - 1) * $ul.parent('.na-goods').width()
}, 200);
theCache.cur = curPage;
}
function setListPadding($li, $naList) {
$li.css('padding-bottom', $naList.outerHeight() + 10);
}
if ($('.fav-brands').length === 0) {
return;
}
naTpl = Handlebars.compile($('#na-tpl').html());
// 新品到着【重要信息:最多显示20个新品到着商品】
$('.na-trigger').click(function() {
var $this = $(this),
$naList = $this.next('.na-list'),
$li = $this.closest('li'),
id;
if (!$naList.hasClass('hide')) {
$naList.addClass('hide');
$li.css('padding-bottom', '');
return;
}
id = $li.data('id');
if (naCache[id] && naCache[id].cache) {
$naList.removeClass('hide');
setListPadding($li, $naList);
return;
}
$.ajax({
type: 'GET',
url: '/home/favorite/newproduct',
data: {
id: id
}
}).then(function(data) {
var html,
res,
tplData;
if (data.code === 200) {
res = data.data;
tplData = res.length === 0 ? {} : {
naGoods: res
};
html = naTpl(tplData);
$naList.removeClass('hide').find('.na-content').html(html);
setListPadding($li, $naList);
//初始,判断是否显示Next翻页
if (res.length > 5) {
$li.find('.na-next').removeClass('hidden');
}
if (!naCache[id]) {
naCache[id] = {};
}
naCache[id].cache = true;//set cache
naCache[id].total = res.length;
}
});
});
$('.na-content').on('click', '.na-pre, .na-next', function() {
var $this = $(this);
var flag = $this.hasClass('na-pre') ? -1 : 1;
pageControll($this.closest('.fav-brand'), flag);
});
}());
//文章收藏
(function() {
if ($('.fav-articles').length < 0) {
return;
}
$('.fav-articles .desc').dotdotdot();
}());
if (param && param.length) {
favType = param[1];
}
//全选【通用】
$('#me-checkall').click(function() {
$('.checkbox input[type="checkbox"]').prop('checked', $(this).prop('checked'));
});
//删除收藏
$('.del-favorite').click(function(e) {
var id = $(this).closest('.fav-row').data('id');
e.preventDefault();
$.ajax({
type: 'GET',
url: '/home/favorite/cancel',
data: {
id: id,
type: favType
}
}).then(function(data) {
if (data.code === 200) {
history.go(0);
}
});
});
//删除多个商品/品牌/文章【通用】
$('#me-del-checked').click(function() {
var ids = [],
name = '商品';
if (favType === 'brand') {
name = '品牌';
} else if (favType === 'article') {
name = '文章';
}
if (confirm('您确定要删除您收藏的' + name + '吗?')) {
$('.checkbox input[type="checkbox"]:checked').each(function() {
ids.push($(this).closest('.fav-row').data('id'));
});
if (ids.length === 0) {
alert('请选中您要移除的' + name);
return;
}
$.ajax({
type: 'GET',
url: '/home/favorite/cancel',
data: {
id: ids.join(','),
type: favType
}
}).then(function(data) {
if (data.code === 200) {
history.go(0);
}
});
}
});
if (location.href.match(/is_reduction=(.*)[&]*/)) {
$('.reductionChecked').attr('checked', true);
}
if (location.href.match(/is_promotion=(.*)[&]*/)) {
$('.activityChecked').attr('checked', true);
}