Goods.js
19.5 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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import { ensureCatalogUpgradePrice, seedMarketingActivitySellPrice } from '~/utils/orderSubOrderExtras'
import { normalizeMarketingFlagOnItem } from '~/utils/marketingFlag'
Component({
options: {
styleIsolation: 'isolated' //表示样式隔离,默认隔离
},
/**
* 组件的属性列表
*/
properties: {
show: {
type: Boolean,
observer: function (n) {
if (n) {
this.setData({
keyWord: '',
strTemp: ''
})
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: {
//规格选择
handlePickerChange(e) {
let value = e.detail.value, //下标
{ index, specindex, item } = e.currentTarget.dataset,
{ goodsList } = this.data,
selectValue
selectValue =
goodsList[index].multipleSpecificationsGoods.specificationList[
specindex
].specificationSelectValueList[value]
let target = `goodsList[${index}].multipleSpecificationsGoods.specificationList[${specindex}].value`
let targetIndex = `goodsList[${index}].multipleSpecificationsGoods.specificationList[${specindex}].selectIndex`
this.setData(
{
[target]: selectValue,
[targetIndex]: value
},
() => {
//监听规格是否全部选中
this.watchAllChose(index)
}
)
},
/*
index:索引
cancel:点击选择按钮的操作;只有选中后,取消才可以触发
*/
async watchAllChose(index, cancel) {
let { goodsList, strTemp } = this.data
let result = goodsList[
index
].multipleSpecificationsGoods.specificationList.every(item => item.value)
if (result) {
console.log(result,'所有规格都选中了')
if (!cancel) {
let arr = []
goodsList[
index
].multipleSpecificationsGoods.specificationList.forEach(spec => {
arr.push(spec.value)
})
let str = arr.join(';~;')
console.log(str,'str')
//获取该规格的商品是否存在
goodsList[index].multipleSpecificationsGoods.skuGoodsList = []
if (
goodsList[index].multipleSpecificationsGoods.specificationGoodsMap[
str
]
) {
let goodsArr =
goodsList[index].multipleSpecificationsGoods
.specificationGoodsMap[str]
console.log(goodsArr,'goodsArr')
//当多规格商品只有一个时,直接选中
if (goodsArr.length == 1) {
goodsList[index].active = true
goodsList[index].multipleSpecificationsGoods.goods = goodsArr[0]
seedMarketingActivitySellPrice(
goodsList[index].multipleSpecificationsGoods.goods
)
ensureCatalogUpgradePrice(
goodsList[index].multipleSpecificationsGoods.goods
)
goodsList[index].multipleSpecificationsGoods.goods.goodsId =
goodsList[
index
].multipleSpecificationsGoods.goods.merchantGoodsId
goodsList[
index
].multipleSpecificationsGoods.goods.specificationStr = goodsList[
index
].multipleSpecificationsGoods.goods.specificationData.replace(
/;~;/g,
'/'
)
} else {
//多个多规格商品时出现弹窗
goodsList[index].active = false
goodsArr.forEach(item => {
seedMarketingActivitySellPrice(item)
ensureCatalogUpgradePrice(item)
item.count = 1
item.active = false
item.goodsId = item.merchantGoodsId
item.specificationStr = item.specificationData.replace(
/;~;/g,
'/'
)
})
goodsList[index].multipleSpecificationsGoods.skuGoodsList =
goodsArr
let chooseList = await this.getChooseList(false)
this.setData(
{
show: false,
goodsList
},
() => {
let thisPage = wx.$router.getPageInstance()
thisPage.setData({
skuGoodsData: goodsList[index],
skuGoodsBox: true,
goodsList: chooseList
})
thisPage?.setGoodsTotal && thisPage.setGoodsTotal(chooseList)
}
)
}
} else {
console.log(88888)
wx.$util.showToast('暂无该规格商品')
goodsList[index].active = false
goodsList[index].multipleSpecificationsGoods.goods = ''
goodsList[
index
].multipleSpecificationsGoods.specificationList.forEach(item => {
item.value = ''
item.selectIndex = 0
})
}
this.setData(
{
goodsList,
strTemp: str
},
() => {
//同步更新
this.synchronousChange(goodsList[index])
}
)
} else {
if (goodsList[index].active) {
goodsList[index].active = false
goodsList[index].multipleSpecificationsGoods.goods = ''
goodsList[
index
].multipleSpecificationsGoods.specificationList.forEach(item => {
item.value = ''
item.selectIndex = 0
})
console.log(33333, goodsList)
this.setData(
{
goodsList
},
() => {
this.synchronousChange(goodsList[index])
}
)
}
}
}
return result
},
setGoodsActive(isFlag) {
if (this.data.show) {
let {
chooseGoodsList,
goodsList,
goodsData
// recordList
} = this.data
console.log(chooseGoodsList, goodsList, goodsData, 'goodsList')
let chooseList = []
goodsData.map(e => {
if (e.active) {
console.log(e, e.active, '8888')
chooseList.push(e)
}
})
console.log(chooseList, 'chooseList')
let arr = this.removeDuplicates(
chooseList.concat(chooseGoodsList),
'goodsId'
)
console.log(arr, arr.length, 'arr')
// console.log(chooseGoodsList, 'chooseGoodsList')
if (arr.length == 0) {
goodsList.forEach(e => {
e.active = false
e.count = 1
})
} else {
goodsList.map((e, index) => {
// e.active = false
arr.map(s => {
if (s.goods && e.goods) {
if (s.goods.goodsId == e.goods.goodsId) {
e.active = true
e.count = s.count
// e.goods.sellPrice = s.goods.sellPrice
Object.assign(e.goods, s.goods)
}
}
//初始化进来不显示标识
// if (!s.isFlag) {
if (
s.multipleSpecificationsGoods &&
e.multipleSpecificationsGoods
) {
if (
s.multipleSpecificationsGoods.id ==
e.multipleSpecificationsGoods.id
) {
e.count = s.count
if (
s.multipleSpecificationsGoods.goods &&
e.multipleSpecificationsGoods.goods
) {
Object.assign(
e.multipleSpecificationsGoods.goods,
s.multipleSpecificationsGoods.goods
)
}
// e.active=s.acitve
}
}
})
})
}
console.log(goodsList, 'goodsList999')
this.setData({
goodsList
// recordList
})
}
},
getOrderBaseGoodsList(data) {
const goodsList = (data || []).map((item) => {
const next = { ...item }
normalizeMarketingFlagOnItem(next)
return next
})
this.setData({
goodsList
})
},
circulation(data, str) {
if (str) {
let arr = str.split('.'),
result
if (arr.length) {
result = arr.reduce((pre, key) => {
return pre[key] ? pre[key] : {}
}, data)
}
return result
} else {
return false
}
},
// 删除数组重复项 都不保留
removeDuplicates(arr, property) {
console.log(arr, 'removeDuplicates')
var uniqueArray = []
for (var i = 0; i < arr.length; i++) {
let goodsType = ''
if (arr[i].goodsFlag == 1) {
goodsType = 'goods'
} else if (arr[i].goodsFlag == 2) {
goodsType = 'multipleSpecificationsGoods.goods'
} else if (arr[i].goodsFlag == 3) {
goodsType = 'packageGoods'
}
let value = this.circulation(arr[i], goodsType)[property]
// 兼容套餐
let result = uniqueArray.some(item => {
let key = ''
if (item.goodsFlag == 1) {
key = 'goods'
} else if (item.goodsFlag == 2) {
key = 'multipleSpecificationsGoods.goods'
} else if (item.goodsFlag == 3) {
key = 'packageGoods'
}
let deepValue = this.circulation(item, key)
return deepValue ? deepValue[property] === value : false
})
if (!result) {
uniqueArray.push(JSON.parse(JSON.stringify(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
}, [])
},
async getChooseList(isValid) {
let { goodsList, chooseGoodsList, isSolarFilm } = this.data
console.log(goodsList, chooseGoodsList, 'goodsList')
let chooseList = []
goodsList.map(e => {
if (e.active) {
chooseList.push(e)
}
})
if (isValid) {
if (!chooseList?.length) return wx.$util.showToast('请勾选商品')
}
// 处理多规格选择重复商品时,数量相加逻辑
// if (chooseGoodsList.length > 0) {
// await this.addMMultipleSpecificationsGoods(chooseList, chooseGoodsList)
// }
console.log(chooseList,chooseGoodsList, 'chooseList')
// debugger
let arr = this.removeDuplicates(
chooseList.concat(chooseGoodsList),
'goodsId'
)
// 太阳膜下单 需保留已选的商品
arr.map(c => {
if (c.active) {
if (c.goods) {
if (!c.count) c.count = 1
ensureCatalogUpgradePrice(c.goods)
seedMarketingActivitySellPrice(c.goods)
c.goods.price = c.goods.sellPrice || 0
if (chooseGoodsList.length > 0) {
chooseGoodsList.map(x => {
if (x.goods && x.goods.goodsId == c.goods.goodsId) {
Object.assign(c.goods, x.goods)
}
})
}
}
if (c.multipleSpecificationsGoods) {
c.isFlag = true //初始化进来不显示标识
if (c.multipleSpecificationsGoods.goods) {
if (!c.count) c.count = 1
c.multipleSpecificationsGoods.goods.price =
c.multipleSpecificationsGoods.goods.sellPrice || 0
if (chooseGoodsList.length > 0) {
chooseGoodsList.map(x => {
if (x.goodsFlag == 2) {
if (
x.multipleSpecificationsGoods.goods &&
x.multipleSpecificationsGoods.goods.goodsId ==
c.multipleSpecificationsGoods.goods.goodsId
) {
Object.assign(
c.multipleSpecificationsGoods.goods,
x.multipleSpecificationsGoods.goods
)
}
}
})
}
}
}
if (c.packageGoods) {
if (chooseGoodsList.length > 0) {
chooseGoodsList.map(x => {
if (x.goodsFlag == 3 && x.packageGoods) {
if (x.packageGoods.goodsId == c.packageGoods.goodsId) {
Object.assign(c.packageGoods, x.packageGoods)
}
}
})
}
}
}
})
return arr
},
addMMultipleSpecificationsGoods(newData, oldData) {
oldData.forEach(oldVal => {
newData.forEach(newVal => {
if (
oldVal.multipleSpecificationsGoods &&
newVal.multipleSpecificationsGoods
) {
if (
oldVal.multipleSpecificationsGoods.goods &&
newVal.multipleSpecificationsGoods.goods
) {
if (
oldVal.multipleSpecificationsGoods.goods.goodsId ==
newVal.multipleSpecificationsGoods.goods.goodsId
) {
oldVal.count =
Number(newVal.count ? newVal.count : 1) + Number(oldVal.count)
newVal.count = oldVal.count
newVal.multipleSpecificationsGoods.goods.sellPrice =
oldVal.multipleSpecificationsGoods.goods.sellPrice
newVal.multipleSpecificationsGoods.goods.price =
oldVal.multipleSpecificationsGoods.goods.price
}
}
}
})
})
},
async choose(e) {
let { item, ind } = e.currentTarget.dataset
let { active } = item
let { minPrice } = this.data
//单独处理多规格商品点击事件
if (item.goodsFlag == 2) {
if (!(await this.watchAllChose(ind, true))) {
wx.$util.showToast('请先选择该商品规格')
return
}
}
console.log(item)
let goodsType =
item.goodsFlag == 1 ? 'goods' : 'multipleSpecificationsGoods.goods'
let target = `goodsList[${ind}].active`
item.active = !active
// 如果有设置订单价格 那就不能选单个成本价比总价还高的
let value = this.circulation(item, goodsType).minSellPrice
if (minPrice && minPrice / 100 < value) return false
this.setData(
{
[target]: !active
},
() => {
//清空规格选择值
this.synchronousChange(item)
}
)
},
synchronousChange(item) {
console.log(item, '9999')
this.data.goodsData.forEach(i => {
if (i.goodsFlag == 1 && item.goods) {
if (i.goods.goodsId == item.goods.goodsId) {
i.active = item.active
i.count = item.count || 1
i.goods.sellPrice = item.goods.sellPrice
}
} else if (i.goodsFlag == 2 && item.goodsFlag == 2) {
if (
i.multipleSpecificationsGoods.id ==
item.multipleSpecificationsGoods.id
) {
i.active = item.active
i.count = item.count || 1
i.multipleSpecificationsGoods = item.multipleSpecificationsGoods
if (!item.active) {
i.multipleSpecificationsGoods.goods = ''
i.multipleSpecificationsGoods.specificationList.forEach(m => {
m.value = ''
})
}
}
}
})
this.setData({
goodsData: this.data.goodsData
})
console.log(this.data.goodsData)
},
addCount(e) {
let { count, item, ind } = e.currentTarget.dataset
let target = `goodsList[${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
console.log(target)
this.setData(
{
[target]: item.count
},
() => {
this.synchronousChange(this.data.goodsList[ind])
}
)
},
input(e) {
let { ind, item } = e.currentTarget.dataset
console.log(item)
// let goodsType = item.goodsFlag == 1 ? 'goods' : 'multipleSpecificationsGoods'
let target = `goodsList[${ind}].count`
let { value } = e.detail
if (value < 1) value = 1
if (value > 999) value = 999
this.setData(
{
[target]: Number(value)
},
() => {
this.synchronousChange(this.data.goodsList[ind])
}
)
},
searchGoods(e) {
let { goodsData } = this.data
let { value } = e.detail
console.log(this.data.goodsData, 'goodsData')
if (!value) {
this.getOrderBaseGoodsList(goodsData)
setTimeout(() => {
this.setGoodsActive('second', true)
}, 500)
return
}
let _DATA_ = JSON.parse(JSON.stringify(goodsData))
let arr = []
_DATA_.map(c => {
console.log(c)
// let nameList = []
let name
// debugger
if (c.goodsFlag == 1 && c.goods) {
// nameList.push(c.goods.goodsAlias,c.goods.upgradeGoodsName,c.goods.goodsName)
name =
c.goods.goodsAlias ||
c.goods.upgradeGoodsName ||
c.goods.goodsName ||
''
}
if (c.goodsFlag == 3 && c.packageGoods) {
name = c.packageGoods.packageAlias || c.packageGoods.packageName
}
if (c.goodsFlag == 2 && c.multipleSpecificationsGoods) {
name = c.multipleSpecificationsGoods.baseGoodsName
}
console.log(name, c, 'name')
if (name.indexOf(value) != -1) {
arr.push(c)
}
})
console.log(arr)
this.setData({
goodsList: arr
})
},
/** 取消时通知父页面关闭弹框;内部 show 兼容未绑 cancel 的引用页 */
cancel() {
this.setData({ show: false })
this.triggerEvent('cancel')
},
async confirm() {
let chooseList = await this.getChooseList(true)
console.log(chooseList, '99999')
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
})
thisPage?.syncMarketingFlags?.(thisPage.data.orderObj?.merchantId, {
goodsList: chooseList
})
this.triggerEvent('confirm', chooseList)
this.setData({ show: false })
}
}
})