Fix:5346 【商城小程序】将商品加入购物车后,从购物车进入商品详情页后返回,原已选中的商品变为未选中状态
This commit is contained in:
+64
-13
@@ -419,10 +419,12 @@ export default {
|
|||||||
},
|
},
|
||||||
showAttr: false,
|
showAttr: false,
|
||||||
pageKeyId: Object.freeze('landmarkGoodsIndex'),
|
pageKeyId: Object.freeze('landmarkGoodsIndex'),
|
||||||
|
cartSelectedIdsCacheKey: Object.freeze('cartSelectedIds'),
|
||||||
isEdit: false,
|
isEdit: false,
|
||||||
addressList:[],
|
addressList:[],
|
||||||
addressInfo: {},
|
addressInfo: {},
|
||||||
userLikeList: [],
|
userLikeList: [],
|
||||||
|
selectedCartIds: [],
|
||||||
// 是否已登录
|
// 是否已登录
|
||||||
isNoLogin: false
|
isNoLogin: false
|
||||||
}
|
}
|
||||||
@@ -449,6 +451,7 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
|
this.selectedCartIds = this.getSelectedCartIds()
|
||||||
this.getCart()
|
this.getCart()
|
||||||
this.getAddress()
|
this.getAddress()
|
||||||
},
|
},
|
||||||
@@ -595,22 +598,75 @@ export default {
|
|||||||
isInvalid: true
|
isInvalid: true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
this.list = res.data['valid'].concat(invalid)
|
this.list = this.restoreCartState(res.data['valid'].concat(invalid))
|
||||||
this.handleAll(false)
|
|
||||||
const _res = await getUserLike()
|
const _res = await getUserLike()
|
||||||
this.userLikeList = _res.data
|
this.userLikeList = _res.data
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
handleAll(state) {
|
getSelectedCartIds() {
|
||||||
this.isAll = state
|
const ids = uni.getStorageSync(this.cartSelectedIdsCacheKey)
|
||||||
|
return Array.isArray(ids) ? ids : []
|
||||||
|
},
|
||||||
|
syncSelectedCartIds() {
|
||||||
|
const ids = []
|
||||||
|
this.list.forEach(item => {
|
||||||
|
item.carts.forEach(good => {
|
||||||
|
if (good.state) {
|
||||||
|
ids.push(good.id)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
this.selectedCartIds = ids
|
||||||
|
uni.setStorageSync(this.cartSelectedIdsCacheKey, ids)
|
||||||
|
},
|
||||||
|
refreshSelectionState() {
|
||||||
|
let hasSelectableGoods = false
|
||||||
|
let isAll = true
|
||||||
this.list = this.list.map(item => {
|
this.list = this.list.map(item => {
|
||||||
item.state = state
|
const selectableCarts = item.carts.filter(() => !item.isInvalid || this.isEdit)
|
||||||
|
if (selectableCarts.length > 0) {
|
||||||
|
hasSelectableGoods = true
|
||||||
|
}
|
||||||
|
item.state = selectableCarts.length > 0 && selectableCarts.every(good => good.state)
|
||||||
|
if (selectableCarts.length > 0 && !item.state) {
|
||||||
|
isAll = false
|
||||||
|
}
|
||||||
|
return item
|
||||||
|
})
|
||||||
|
this.isAll = hasSelectableGoods && isAll
|
||||||
|
this.syncSelectedCartIds()
|
||||||
|
},
|
||||||
|
restoreCartState(list) {
|
||||||
|
const selectedCartIds = new Set(this.selectedCartIds)
|
||||||
|
const nextList = list.map(item => {
|
||||||
|
const canSelect = !item.isInvalid || this.isEdit
|
||||||
|
const carts = item.carts.map(good => {
|
||||||
|
return {
|
||||||
|
...good,
|
||||||
|
state: canSelect ? selectedCartIds.has(good.id) : false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return {
|
||||||
|
...item,
|
||||||
|
carts,
|
||||||
|
state: false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
this.list = nextList
|
||||||
|
this.refreshSelectionState()
|
||||||
|
return this.list
|
||||||
|
},
|
||||||
|
handleAll(state) {
|
||||||
|
this.list = this.list.map(item => {
|
||||||
|
const canSelect = !item.isInvalid || this.isEdit
|
||||||
|
item.state = canSelect ? state : false
|
||||||
item.carts = item.carts.map(good => {
|
item.carts = item.carts.map(good => {
|
||||||
good.state = item.isInvalid && !this.isEdit ? false : state
|
good.state = canSelect ? state : false
|
||||||
return good
|
return good
|
||||||
})
|
})
|
||||||
return item
|
return item
|
||||||
})
|
})
|
||||||
|
this.refreshSelectionState()
|
||||||
},
|
},
|
||||||
navToStore(id) {
|
navToStore(id) {
|
||||||
if (id === 0) {
|
if (id === 0) {
|
||||||
@@ -629,6 +685,7 @@ export default {
|
|||||||
if (mark.length === 1) {
|
if (mark.length === 1) {
|
||||||
if (mark[0] === -1) {
|
if (mark[0] === -1) {
|
||||||
this.handleAll(state)
|
this.handleAll(state)
|
||||||
|
return
|
||||||
} else {
|
} else {
|
||||||
this.list[mark[0]]['state'] = state
|
this.list[mark[0]]['state'] = state
|
||||||
this.list[mark[0]]['carts'] = this.list[mark[0]]['carts'].map(good => {
|
this.list[mark[0]]['carts'] = this.list[mark[0]]['carts'].map(good => {
|
||||||
@@ -648,13 +705,7 @@ export default {
|
|||||||
})
|
})
|
||||||
this.list[mark[0]]['state'] = count === length
|
this.list[mark[0]]['state'] = count === length
|
||||||
}
|
}
|
||||||
let shopCheckNumber = 0
|
this.refreshSelectionState()
|
||||||
this.list.map(item => {
|
|
||||||
if (item.state) {
|
|
||||||
shopCheckNumber++
|
|
||||||
}
|
|
||||||
})
|
|
||||||
this.isAll = shopCheckNumber === this.list.length
|
|
||||||
},
|
},
|
||||||
inputChangeHandle(index, gIndex, good, e) {
|
inputChangeHandle(index, gIndex, good, e) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user