diff --git a/pages/cart.vue b/pages/cart.vue index 353396c..31781ed 100644 --- a/pages/cart.vue +++ b/pages/cart.vue @@ -419,10 +419,12 @@ export default { }, showAttr: false, pageKeyId: Object.freeze('landmarkGoodsIndex'), + cartSelectedIdsCacheKey: Object.freeze('cartSelectedIds'), isEdit: false, addressList:[], addressInfo: {}, userLikeList: [], + selectedCartIds: [], // 是否已登录 isNoLogin: false } @@ -449,6 +451,7 @@ export default { } }, created() { + this.selectedCartIds = this.getSelectedCartIds() this.getCart() this.getAddress() }, @@ -595,22 +598,75 @@ export default { isInvalid: true } }) - this.list = res.data['valid'].concat(invalid) - this.handleAll(false) + this.list = this.restoreCartState(res.data['valid'].concat(invalid)) const _res = await getUserLike() this.userLikeList = _res.data } }, - handleAll(state) { - this.isAll = state + getSelectedCartIds() { + 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 => { - 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 => { - good.state = item.isInvalid && !this.isEdit ? false : state + good.state = canSelect ? state : false return good }) return item }) + this.refreshSelectionState() }, navToStore(id) { if (id === 0) { @@ -629,6 +685,7 @@ export default { if (mark.length === 1) { if (mark[0] === -1) { this.handleAll(state) + return } else { this.list[mark[0]]['state'] = state 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 } - let shopCheckNumber = 0 - this.list.map(item => { - if (item.state) { - shopCheckNumber++ - } - }) - this.isAll = shopCheckNumber === this.list.length + this.refreshSelectionState() }, inputChangeHandle(index, gIndex, good, e) {