Commit ee12eb99 by 李伟康

refactor(OrderPopup): 优化商品列表初始化逻辑

- 在 GoodsList、PackageList 和 SkuGoodsList 组件中,增加了对 paramsData 是否存在的判断
- 修复了 SkuGoodsList 组件中的选择列表初始化问题
- 优化了 SkuGoodsList 组件中的数据处理逻辑,提高了代码的可读性和性能
1 parent 9b228cd8
...@@ -96,7 +96,7 @@ export default { ...@@ -96,7 +96,7 @@ export default {
} }
}) })
let paramsData = dataKey ? data[dataKey] : data let paramsData = dataKey ? data[dataKey] : data
if (paramsData.length) paramsData.map(e => { if (paramsData && paramsData.length) paramsData.map(e => {
this.currentIds.push(e.goods.goodsId) this.currentIds.push(e.goods.goodsId)
this.chooseList.push(e) this.chooseList.push(e)
}) })
......
...@@ -111,7 +111,7 @@ export default { ...@@ -111,7 +111,7 @@ export default {
} }
}) })
let paramsData = dataKey ? data[dataKey] : data let paramsData = dataKey ? data[dataKey] : data
if (paramsData.length) paramsData.map(e => { if (paramsData && paramsData.length) paramsData.map(e => {
this.currentIds.push(e.packageGoods.packageCode) this.currentIds.push(e.packageGoods.packageCode)
this.chooseList.push(e) this.chooseList.push(e)
}) })
......
...@@ -146,7 +146,7 @@ export default { ...@@ -146,7 +146,7 @@ export default {
} }
}, },
initGoodsList (goodsData) { initGoodsList (goodsData) {
let { data,dataKey } = this, let { data, dataKey } = this,
arr = [] arr = []
goodsData.map((val) => { goodsData.map((val) => {
if (val.goodsFlag == 1) { if (val.goodsFlag == 1) {
...@@ -163,7 +163,7 @@ export default { ...@@ -163,7 +163,7 @@ export default {
val.active = false val.active = false
val.key = Date.now() val.key = Date.now()
if (val.goodsFlag == 2) { if (val.goodsFlag == 2) {
if(val.multipleSpecificationsGoods?.goods?.sellPrice) val.multipleSpecificationsGoods.goods.sellPrice = null if (val.multipleSpecificationsGoods?.goods?.sellPrice) val.multipleSpecificationsGoods.goods.sellPrice = null
val.multipleSpecificationsGoods.specificationList.forEach(item => { val.multipleSpecificationsGoods.specificationList.forEach(item => {
item.value = '' item.value = ''
item.specificationSelectValueList = [] item.specificationSelectValueList = []
...@@ -178,7 +178,7 @@ export default { ...@@ -178,7 +178,7 @@ export default {
} }
}) })
let paramsData = dataKey ? data[dataKey] : data let paramsData = dataKey ? data[dataKey] : data
if (paramsData.length) paramsData.map(e => { if (paramsData && paramsData.length) paramsData.map(e => {
if (e.goodsFlag == 1) { if (e.goodsFlag == 1) {
this.currentIds.push(e.goods.goodsId) this.currentIds.push(e.goods.goodsId)
this.chooseList.push(e) this.chooseList.push(e)
...@@ -269,19 +269,9 @@ export default { ...@@ -269,19 +269,9 @@ export default {
item.specificationStr = item.specificationData.replace(/;~;/g, '/') item.specificationStr = item.specificationData.replace(/;~;/g, '/')
}) })
goodsList[index].multipleSpecificationsGoods.skuGoodsList = goodsArr goodsList[index].multipleSpecificationsGoods.skuGoodsList = goodsArr
let chooseList = await this.getChooseList(false); // let chooseList = await this.getChooseList(false);
this.setData({ this.$refs.sukRef.open(goodsList[index])
show: false, this.list = goodsList
goodsList
}, () => {
let thisPage = wx.$router.getPageInstance();
thisPage.setData({
skuGoodsData: goodsList[index],
skuGoodsBox: true,
goodsList: chooseList
})
thisPage?.setGoodsTotal && thisPage.setGoodsTotal(chooseList);
});
} }
} else { } else {
uni.showToast({ uni.showToast({
...@@ -294,6 +284,7 @@ export default { ...@@ -294,6 +284,7 @@ export default {
item.value = '' item.value = ''
}) })
} }
this.list = goodsList
// this.setData({ // this.setData({
// goodsList, // goodsList,
// strTemp: str // strTemp: str
...@@ -308,6 +299,7 @@ export default { ...@@ -308,6 +299,7 @@ export default {
goodsList[index].multipleSpecificationsGoods.specificationList.forEach(item => { goodsList[index].multipleSpecificationsGoods.specificationList.forEach(item => {
item.value = '' item.value = ''
}) })
this.list = goodsList
console.log(33333, goodsList) console.log(33333, goodsList)
// this.setData({ // this.setData({
// goodsList // goodsList
...@@ -320,6 +312,96 @@ export default { ...@@ -320,6 +312,96 @@ export default {
return result return result
}, },
async getChooseList (isValid) {
let {
list: goodsList,
chooseList: chooseGoodsList,
} = this;
let chooseList = [];
goodsList.map((e) => {
if (e.active) {
chooseList.push(e)
}
});
if (isValid) {
if (!chooseList?.length) return uni.showToast({
title: '请勾选商品',
icon: 'none',
})
}
let arr = this.removeDuplicates(
chooseList.concat(chooseGoodsList),
"goodsId"
);
// 太阳膜下单 需保留已选的商品
arr.map(c => {
if (c.active) {
if (c.goods) {
if (!c.count) c.count = 1
c.goods.price = c.goods.sellPrice || 0
if (chooseGoodsList.length > 0) {
chooseGoodsList.map(x => {
if (x.goods && x.goods.goodsId == c.goods.goodsId) {
c.goods.price = x.goods.price || 0
}
})
}
}
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) {
c.multipleSpecificationsGoods.goods.price = x.multipleSpecificationsGoods.goods.price || 0
}
}
})
}
}
}
}
})
return arr;
},
removeDuplicates (arr, property) {
var uniqueArray = [];
for (var i = 0; i < arr.length; i++) {
let goodsType = arr[i].goodsFlag == 1 ? 'goods' : 'multipleSpecificationsGoods.goods'
let value = this.circulation(arr[i], goodsType)[property]
// 兼容套餐
let result = uniqueArray.some(item => {
let key = item.goodsFlag == 1 ? 'goods' : item.goodsFlag == 2 ? 'multipleSpecificationsGoods.goods' : ''
let deepValue = this.circulation(item, key)
return deepValue ? deepValue[property] === value : false
})
if (!result) {
uniqueArray.push(arr[i]);
}
}
return uniqueArray;
},
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
}
},
} }
} }
</script> </script>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!