From 15ffeee122ac5c4b0097ae7e931947946cb7a52f Mon Sep 17 00:00:00 2001 From: linxi <957440651@qq.com> Date: Fri, 26 Jun 2026 10:08:12 +0800 Subject: [PATCH] =?UTF-8?q?Fix:5346=20=E3=80=90=E5=95=86=E5=9F=8E=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E3=80=91=E5=B0=86=E5=95=86=E5=93=81=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=E8=B4=AD=E7=89=A9=E8=BD=A6=E5=90=8E=EF=BC=8C=E4=BB=8E?= =?UTF-8?q?=E8=B4=AD=E7=89=A9=E8=BD=A6=E8=BF=9B=E5=85=A5=E5=95=86=E5=93=81?= =?UTF-8?q?=E8=AF=A6=E6=83=85=E9=A1=B5=E5=90=8E=E8=BF=94=E5=9B=9E=EF=BC=8C?= =?UTF-8?q?=E5=8E=9F=E5=B7=B2=E9=80=89=E4=B8=AD=E7=9A=84=E5=95=86=E5=93=81?= =?UTF-8?q?=E5=8F=98=E4=B8=BA=E6=9C=AA=E9=80=89=E4=B8=AD=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 4fbb35ea0d1f53097e7d351398185796e5b5b735) --- pages/cart.vue | 77 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 64 insertions(+), 13 deletions(-) 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) {