brand-coupon.js
2.99 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
'use strict';
/**
* 商品详情: 品牌券
*/
var tip = require('plugin/tip');
var $ = require('yoho-jquery');
var $body = $(document.body);
var brandCoupon = {
skn: null,
brandId: null,
$entry: null,
$couponDrawer: null,
template: require('product/detail/coupon-list.hbs'),
init: function(skn, brandId) {
var self = this;
this.skn = skn;
this.brandId = brandId;
if (!(skn && brandId)) {
return;
}
this.fetchCoupons(this.skn, this.brandId)
.done(function(data) {
if (data.length) {
self.render(data);
self.domInit();
self.bindEvents();
self.$entry.removeClass('hide');
}
})
.fail();
},
domInit: function() {
this.$entry = $('.brand-coupon').removeClass('hide');
},
bindEvents: function() {
var self = this;
this.$entry.on('click', function() {
self.toggleDrawer(true);
});
this.$couponDrawer
.on('click', '.coupon-drawer-mask', $.proxy(this.toggleDrawer, this, false))
.on('click', '.coupon-btn-valid', $.proxy(this.saveCouponHandler, this));
},
render: function(data) {
this.$couponDrawer = $(this.template({
coupons: data
}));
this.$couponDrawer.appendTo('.good-detail-page');
return this;
},
// 获取 品牌券
fetchCoupons: function(skn, brandId) {
return $.get('/product/detail/coupon.json', {
skn: skn,
brandId: brandId
});
},
saveCoupon: function(couponId, callback) {
$.post('/product/detail/coupon/save.json', {
couponId: couponId
}).done(function(res) {
tip.show(res.message);
if (res.code === 200) {
callback(); // eslint-disable-line
} else {
tip.show(
res.message || '抱歉,您不符合领用条件'
);
if (res.redirect) {
setTimeout(function() {
location.href = res.redirect;
}, 1000);
}
}
}).fail(function() {
tip.show('网络异常,请稍后再试');
});
},
// 收藏 品牌券
saveCouponHandler: function(event) {
var $btn = $(event.target);
var couponId = $btn.closest('.coupon').data('coupon');
this.saveCoupon(couponId, function() {
$btn.prop('disabled', true)
.removeClass('coupon-btn-valid')
.text('已领取');
});
event.stopPropagation();
},
toggleDrawer: function(bool) {
this.$couponDrawer.toggleClass('open', bool);
$body.toggleClass('coupon-drawer-open', bool);
}
};
$(function() {
brandCoupon.init(
$('#productSkn').val(),
$('#brand-id').val()
);
});