Goods.js
7.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
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
Component({
options: {
styleIsolation: 'isolated',//表示样式隔离,默认隔离
},
/**
* 组件的属性列表
*/
properties: {
show: {
type: Boolean,
observer: function (n) {
if (n) {
this.setData({
keyWord: ''
})
this.setGoodsActive();
}
},
},
goodsData: {
type: Array,
observer: function (n) {
if (n.length > 0) {
this.getOrderBaseGoodsList(n);
}
},
},
chooseGoodsList: {
type: Array,
observer: function (n) {
this.setGoodsActive();
},
},
minPrice: {
type: Number,
// value:0
},
isSolarFilm: Boolean,
baseConfigData: Object,
},
/**
* 组件的初始数据
*/
data: {
goodsList: [],
keyWord: ''
},
/**
* 组件的方法列表
*/
methods: {
setGoodsActive() {
let { chooseGoodsList, goodsList } = this.data;
if (chooseGoodsList.length == 0) {
goodsList.map((e) => {
e.upgradeGoodsList.map((c) => (c.active = false));
});
} else {
let indexList = [];
goodsList.map((e, index) => {
e.upgradeGoodsList.map((c, ind) => {
c.active = false;
chooseGoodsList.map((s) => {
if (s.merchantGoodsId == c.id || s.id == c.id) {
c.count = s.count;
indexList.push({
index,
ind,
});
}
});
});
});
indexList.map((e) => {
goodsList[e.index].upgradeGoodsList[e.ind].active = true;
});
}
this.setData({
goodsList,
});
},
getOrderBaseGoodsList(data) {
this.setData({
goodsList: data,
});
},
// 删除数组重复项 都不保留
removeDuplicates(arr, property) {
console.log(arr, 'packageCode');
var uniqueArray = [];
for (var i = 0; i < arr.length; i++) {
var value = arr[i][property];
// 兼容套餐
if ((!uniqueArray.some((obj) => obj[property] === value) || arr[i]?.packageCode)) {
uniqueArray.push(arr[i]);
}
}
return uniqueArray;
},
// 去重保留第二项
arrayUnique(arr, name, packageCode = 'packageCode') {
console.log(arr, 'arrayUnique');
var hash = {};
return arr.reduce(function (acc, cru, index) {
// 兼容套餐逻辑
if (!hash[cru[name]] || !hash[cru[packageCode]]) {
hash[cru[name]] = { index: index };
acc.push(cru);
} else {
acc.splice(hash[cru[name]]["index"], 1, cru);
}
return acc;
}, []);
},
getChooseList() {
let { goodsList, chooseGoodsList, isSolarFilm } = this.data;
console.log(goodsList, chooseGoodsList, 'goodsList');
let chooseList = [];
// debugger
// 太阳膜下单 需保留已选的商品
if (isSolarFilm) {
let upgradeGoodsList = [];
goodsList.map((e) => {
upgradeGoodsList = upgradeGoodsList.concat(e.upgradeGoodsList);
});
let selectActiveArr = [];
for (var i = 0; i < upgradeGoodsList?.length; i++) {
let val = upgradeGoodsList[i];
if (val.active) selectActiveArr.push(val);
}
if (!selectActiveArr?.length) return wx.$util.showToast("请勾选商品");
// 将已选商品与当前弹窗数据对比处理数据
console.log(upgradeGoodsList, chooseGoodsList, 'chooseGoodsList');
let arr = this.removeDuplicates(
upgradeGoodsList.concat(chooseGoodsList),
"id"
);
console.log(arr, 'arr');
arr?.map((c) => {
if (c.active) {
if (!c.count) c.count = 1;
if (chooseGoodsList.length > 0) {
chooseGoodsList.map((x) => {
if (x.id == c.id) {
c.price = x.price || 0;
}
});
} else {
c.price = c.upgradePrice || 0;
}
if (!c.price) c.price = c.upgradePrice;
chooseList.push(c);
}
});
} else {
goodsList.map((e) => {
e.upgradeGoodsList.map((c) => {
if (c.active) {
if (!c.count) c.count = 1;
if (chooseGoodsList.length > 0) {
chooseGoodsList.map((x) => {
if (x.id == c.id) {
c.price = x.price || 0;
}
});
} else {
c.price = c.upgradePrice || 0;
}
if (!c.price) c.price = c.upgradePrice;
console.log(c, 'isSolarFilm');
chooseList.push(c);
}
});
});
}
return this.arrayUnique(chooseList, "id");
},
choose(e) {
let { item, index, ind } = e.currentTarget.dataset;
let { active } = item;
let { minPrice } = this.data;
// item
// console.log(item)
// console.log(minPrice)
// console.log(item.goodsInfo.shopMinPrice)
// console.log(minPrice && minPrice < item.shopMinPrice)
// 如果有设置订单价格 那就不能选单个成本价比总价还高的
if (minPrice && minPrice / 100 < item.shopMinPrice) return false;
// 太阳膜类商品 未配置部位不可选择
// if (item?.goodsInfo?.goodsType == 1 && !item.merchantGoodsPosition) return false
let target = `goodsList[${index}].upgradeGoodsList[${ind}].active`;
this.setData({
[target]: !active,
});
},
addCount(e) {
let { index, count, item, ind } = e.currentTarget.dataset;
let target = `goodsList[${index}].upgradeGoodsList[${ind}].count`;
if (!item.count) item.count = 1;
item.count += count;
if (item.count < 1) item.count = 1;
if (item.count > 999) item.count = 999;
this.setData({
[target]: item.count,
});
},
input(e) {
let { index, ind } = e.currentTarget.dataset;
let target = `goodsList[${index}].upgradeGoodsList[${ind}].count`;
let { value } = e.detail;
if (value < 1) value = 1;
if (value > 999) value = 999;
this.setData({
[target]: Number(value),
});
},
searchGoods(e) {
let { goodsData } = this.data;
let { value } = e.detail;
if (!value) {
this.getOrderBaseGoodsList(goodsData);
setTimeout(() => {
this.setGoodsActive();
}, 500);
return;
}
let _DATA_ = JSON.parse(JSON.stringify(goodsData));
let arr = [];
_DATA_.map((i) => {
let upgradeGoodsList = [];
i.upgradeGoodsList.map((c) => {
// console.log(c);
let name =
c.upgradeGoodsAlias || c.upgradeGoodsName || c.goodsInfo.goodsName;
if (name.indexOf(value) != -1) {
upgradeGoodsList.push(c);
}
});
if (
i.goodsTypeName.indexOf(value) != -1 ||
upgradeGoodsList.length > 0
) {
i.upgradeGoodsList =
upgradeGoodsList.length > 0 ? upgradeGoodsList : i.upgradeGoodsList;
arr.push(i);
}
});
console.log(arr);
this.setData({
goodsList: arr,
});
},
cancel() {
this.setData({
show: false,
});
},
confirm() {
let chooseList = this.getChooseList();
let { goodsList } = this.data;
if (goodsList.length < 1) return;
if (!chooseList.length) return wx.$util.showToast("请勾选商品");
let thisPage = wx.$router.getPageInstance();
thisPage?.setGoodsTotal && thisPage.setGoodsTotal(chooseList);
// console.log(chooseList)
thisPage.setData({
goodsList: chooseList,
});
this.triggerEvent("confirm", chooseList);
this.setData({
show: false,
});
},
},
});