Эх сурвалжийг харах

refactor: 简化购物车商品更新后的视图刷新逻辑

移除手动查找并更新购物车列表项的复杂逻辑,改为在商品信息更新后直接调用 loadData 重新加载数据。这确保了视图的正确刷新,避免了潜在的更新不一致问题,并使代码更简洁易维护。
ylong 1 сар өмнө
parent
commit
72d85141f4

+ 6 - 25
pages-car/pages/index.vue

@@ -195,14 +195,14 @@
                 // 获取购物车列表
                 this.$u.api.getShopCartListAjax({}).then(res => {
                     this.loading = false;
-                    
+
                     // 获取本地缓存的选中状态,和当前列表的选中状态合并
                     let storageCheckedIds = uni.getStorageSync('cartCheckedIds') || [];
                     const currentCheckedIds = this.cartList.filter(i => i.checked).map(i => i.id);
-                    
+
                     // 合并并去重
                     let checkedIds = [...new Set([...storageCheckedIds, ...currentCheckedIds])];
-                    
+
                     // 本地添加选中状态属性
                     const list = res.rows || [];
                     this.cartList = list.map(item => {
@@ -212,11 +212,11 @@
                             show: false
                         };
                     });
-                    
+
                     // 更新缓存中存在的且当前在购物车里的项
                     const finalCheckedIds = this.cartList.filter(i => i.checked).map(i => i.id);
                     uni.setStorageSync('cartCheckedIds', finalCheckedIds);
-                    
+
                 }).catch(() => {
                     this.loading = false;
                 });
@@ -401,26 +401,7 @@
                     uni.hideLoading();
                     this.$u.toast('更新成功');
 
-                    // 在列表中查找并更新,确保视图刷新
-                    const index = this.cartList.findIndex(i => i.id === this.currentItem.id);
-                    if (index > -1) {
-                        const targetItem = this.cartList[index];
-                        // 确保转换为数字类型
-                        const newType = Number(sku.conditionType);
-
-                        this.$set(targetItem, 'conditionType', newType);
-                        this.$set(targetItem, 'productPrice', sku.price);
-
-                        if (sku.reduceMoney !== undefined) {
-                            this.$set(targetItem, 'reduceMoney', sku.reduceMoney);
-                        }
-
-                        // 强制更新 currentItem 引用(虽然它指向同一个对象,但为了保险)
-                        this.currentItem = targetItem;
-                    } else {
-                        // 如果找不到(极少情况),重新加载
-                        this.loadData();
-                    }
+                    this.loadData();
                 }).catch(() => {
                     uni.hideLoading();
                 });