Task(千县名品):产品列表随机算法实现

This commit is contained in:
linxi
2024-10-25 17:47:02 +08:00
parent 1b93ef7909
commit f3f47d236f
+36 -1
View File
@@ -538,6 +538,9 @@
/>
</view>
</view>
<view v-for="(item, index) in leftList" :key="index">
</view>
</view>
<image
:src="webUrl + '/home/no-data-bg.png'"
@@ -744,20 +747,52 @@ export default {
const { success, data } = res
if (success) {
const products = data.products || []
const farmerShops = data.farmerShops || []
// this.leftList = this.sortProductsAndStores(products, farmerShops)
// console.log(this.leftList);
products.map((item, index) => {
item.isOldBrand = 0
item.isLandmarkGoods = 0
item.isCountyFamous = 1
if (index % 2 === 0) {
this.leftList.push(item)
}
if (index % 2 === 1) {
item.isFarmer = 1
this.rightList.push(item)
}
})
}
})
},
shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
},
sortProductsAndStores(products, stores) {
const randomMap = []
const randomProduct = products.length === 0 ? null : products.length === 1 ? products[0] : products[Math.floor(Math.random() * products.length)]
const store = stores.length === 0 ? null : stores.length === 1 ? stores[0] : stores[Math.floor(Math.random() * stores.length)]
if(!randomProduct && !store) {
return []
}
if(!randomProduct && store) {
return stores
}
if(randomProduct && !store) {
return products
}
if(randomProduct && store) {
randomMap[0] = randomProduct
randomMap[1] = store
const _p = products.filter(e => e !== randomProduct)
const _s = stores.filter(e => e !== store)
const concatArr = this.shuffleArray(_p.concat(_s))
return randomMap.concat(concatArr);
}
},
getShopDetailReq() {
const _this = this
getCountyFamousCityShop({ countyFamousDataId: _this.id }).then((res) => {