bundle.js
4.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
164
165
166
167
168
169
170
171
172
173
174
/*
* @Author: Targaryen
* @Date: 2017-04-06 16:51:52
* @Last Modified by: Targaryen
* @Last Modified time: 2017-04-14 18:32:07
*/
const $ = require('yoho-jquery');
let tip = require('plugin/tip');
let loading = require('plugin/loading');
let bundle = {
init(handle) {
let self = this;
let $bundle = $('.bundle'); // 限定作用域
self.handle = handle;
/**
* 减少套餐数量
*/
$bundle.on('click', '.bundle-nums .num-opt .btn-opt-minus', function(e) {
let skuIdsArray = [];
$(e.delegateTarget).find('.good-item').each(function() {
skuIdsArray.push($(this).data('id'));
});
self.decrBundle({
activity_id: $(e.delegateTarget).data('activityid'),
batch_no: $(e.delegateTarget).data('poolbatchno'),
sku_ids: skuIdsArray.join(',')
}, e);
});
/**
* 增加套餐数量
*/
$bundle.on('click', '.bundle-nums .num-opt .btn-opt-plus', function(e) {
let skuIdsArray = [];
$(e.delegateTarget).find('.good-item').each(function() {
skuIdsArray.push($(this).data('id'));
});
if ($(e.delegateTarget).data('buynum') >= $(e.delegateTarget).data('maxnum')) {
tip.show('库存不足');
return false;
}
self.incrBundle({
activity_id: $(e.delegateTarget).data('activityid'),
batch_no: $(e.delegateTarget).data('poolbatchno'),
sku_ids: skuIdsArray.join(',')
}, e);
});
/**
* 套餐选中/取消
*/
$bundle.on('click', '.bundle-title .opt .select', function(e) {
self.bundleCheckHandle(e);
});
/**
* 编辑状态下套餐选中/取消
*/
$bundle.on('click', '.bundle-title .opt .edit', function(e) {
self.bundleEditCheckHandle(e);
});
},
/**
* 增加套餐数量
* @param {*} params
*/
incrBundle(params, e) {
let self = this;
$.post('/cart/index/new/incrbundle', params, function(result) {
let bundleNum = $(e.delegateTarget).find('.good-num');
let defaultValue = parseInt(bundleNum.val(), 10);
if (result.code === 200) {
bundleNum.val(defaultValue + 1);
self.handle.refreshPage();
} else {
tip.show(result.message);
}
});
},
/**
* 减少套餐数量
* @param {*} params
*/
decrBundle(params, e) {
let self = this;
$.post('/cart/index/new/decrbundle', params, function(result) {
let bundleNum = $(e.delegateTarget).find('.good-num');
let defaultValue = parseInt(bundleNum.val(), 10);
if (result.code === 200) {
bundleNum.val(defaultValue - 1);
self.handle.refreshPage();
} else {
tip.show(result.message);
}
});
},
/**
* 套餐选中
* @param {*} e
*/
bundleCheckHandle(e) {
let self = this;
let selectCheck = $(e.delegateTarget).find('.select');
let goodItem = $(e.delegateTarget).find('.good-item');
let skuData = [];
let selected = 'Y';
if (selectCheck.hasClass('checked')) {
selected = 'N';
}
goodItem.each(function(index, element) {
skuData.push({
product_sku: $(element).data('id'),
buy_number: $(e.delegateTarget).data('buynum'),
activity_id: $(e.delegateTarget).data('activityid'),
batch_no: $(e.delegateTarget).data('poolbatchno'),
selected: selected
});
});
loading.showLoading();
$.ajax({
type: 'post',
url: '/cart/index/new/select',
data: {
skuList: JSON.stringify(skuData)
},
success: function(result) {
if (result.code === 200) {
selectCheck.toggleClass('checked');
self.handle.refreshPage(result.data);
}
loading.hideLoading();
}
});
},
/**
* 编辑状态下套餐选中/取消
* @param {*} e
*/
bundleEditCheckHandle(e) {
let editCheck = $(e.delegateTarget).find('.edit');
let goodItem = $(e.delegateTarget).find('.good-item');
if (editCheck.hasClass('checked')) {
editCheck.removeClass('checked');
goodItem.find('.opt .edit').removeClass('checked');
} else {
editCheck.addClass('checked');
goodItem.find('.opt .edit').addClass('checked');
}
},
};
module.exports = bundle;