From c899ddfe49c5dedf6e78fc136dd0efdbe3951dfd Mon Sep 17 00:00:00 2001
From: yiming <m13691596795@163.com>
Date: 星期五, 01 三月 2024 09:38:20 +0800
Subject: [PATCH] feat(提交): 代码

---
 pages/cart/index.js |  326 ++++++++----------------------------------------------
 1 files changed, 49 insertions(+), 277 deletions(-)

diff --git a/pages/cart/index.js b/pages/cart/index.js
index e710106..7622fcd 100644
--- a/pages/cart/index.js
+++ b/pages/cart/index.js
@@ -1,294 +1,66 @@
-import Dialog from 'tdesign-miniprogram/dialog/index';
-import Toast from 'tdesign-miniprogram/toast/index';
-import { fetchCartGroupData } from '../../services/cart/cart';
-
+// pages/cart/index.js
 Page({
+
+  /**
+   * 椤甸潰鐨勫垵濮嬫暟鎹�
+   */
   data: {
-    cartGroupData: null,
+
   },
 
-  // 璋冪敤鑷畾涔塼abbar鐨刬nit鍑芥暟锛屼娇椤甸潰涓巘abbar婵�娲荤姸鎬佷繚鎸佷竴鑷�
+  /**
+   * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鍔犺浇
+   */
+  onLoad(options) {
+
+  },
+
+  /**
+   * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鍒濇娓叉煋瀹屾垚
+   */
+  onReady() {
+
+  },
+
+  /**
+   * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鏄剧ず
+   */
   onShow() {
-    this.getTabBar().init();
+
   },
 
-  onLoad() {
-    this.refreshData();
+  /**
+   * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰闅愯棌
+   */
+  onHide() {
+
   },
 
-  refreshData() {
-    this.getCartGroupData().then((res) => {
-      let isEmpty = true;
-      const cartGroupData = res.data;
-      // 涓�浜涚粍浠朵腑闇�瑕佺殑瀛楁鍙兘鎺ュ彛骞舵病鏈夎繑鍥烇紝鎴栬�呰繑鍥炵殑鏁版嵁缁撴瀯涓庨鏈熶笉涓�鑷达紝闇�瑕佸湪姝ゅ厛瀵规暟鎹仛涓�浜涘鐞�
-      // 缁熻闂ㄥ簵涓嬪姞璐殑鍟嗗搧鏄惁鍏ㄩ�夈�佹槸鍚﹀瓨鍦ㄧ己璐�/鏃犺揣
-      for (const store of cartGroupData.storeGoods) {
-        store.isSelected = true; // 璇ラ棬搴楀凡鍔犺喘鍟嗗搧鏄惁鍏ㄩ��
-        store.storeStockShortage = false; // 璇ラ棬搴楀凡鍔犺喘鍟嗗搧鏄惁瀛樺湪搴撳瓨涓嶈冻
-        if (!store.shortageGoodsList) {
-          store.shortageGoodsList = []; // 璇ラ棬搴楀凡鍔犺喘鍟嗗搧濡傛灉搴撳瓨涓�0闇�鍗曠嫭鍒嗙粍
-        }
-        for (const activity of store.promotionGoodsList) {
-          activity.goodsPromotionList = activity.goodsPromotionList.filter((goods) => {
-            goods.originPrice = undefined;
+  /**
+   * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鍗歌浇
+   */
+  onUnload() {
 
-            // 缁熻鏄惁鏈夊姞璐暟澶т簬搴撳瓨鏁扮殑鍟嗗搧
-            if (goods.quantity > goods.stockQuantity) {
-              store.storeStockShortage = true;
-            }
-            // 缁熻鏄惁鍏ㄩ��
-            if (!goods.isSelected) {
-              store.isSelected = false;
-            }
-            // 搴撳瓨涓�0锛堟棤璐э級鐨勫晢鍝佸崟鐙垎缁�
-            if (goods.stockQuantity > 0) {
-              return true;
-            }
-            store.shortageGoodsList.push(goods);
-            return false;
-          });
-
-          if (activity.goodsPromotionList.length > 0) {
-            isEmpty = false;
-          }
-        }
-        if (store.shortageGoodsList.length > 0) {
-          isEmpty = false;
-        }
-      }
-      cartGroupData.invalidGoodItems = cartGroupData.invalidGoodItems.map((goods) => {
-        goods.originPrice = undefined;
-        return goods;
-      });
-      cartGroupData.isNotEmpty = !isEmpty;
-      this.setData({ cartGroupData });
-    });
   },
 
-  findGoods(spuId, skuId) {
-    let currentStore;
-    let currentActivity;
-    let currentGoods;
-    const { storeGoods } = this.data.cartGroupData;
-    for (const store of storeGoods) {
-      for (const activity of store.promotionGoodsList) {
-        for (const goods of activity.goodsPromotionList) {
-          if (goods.spuId === spuId && goods.skuId === skuId) {
-            currentStore = store;
-            currentActivity = currentActivity;
-            currentGoods = goods;
-            return {
-              currentStore,
-              currentActivity,
-              currentGoods,
-            };
-          }
-        }
-      }
-    }
-    return {
-      currentStore,
-      currentActivity,
-      currentGoods,
-    };
+  /**
+   * 椤甸潰鐩稿叧浜嬩欢澶勭悊鍑芥暟--鐩戝惉鐢ㄦ埛涓嬫媺鍔ㄤ綔
+   */
+  onPullDownRefresh() {
+
   },
 
-  // 娉細瀹為檯鍦烘櫙鏃跺簲璇ヨ皟鐢ㄦ帴鍙h幏鍙栬喘鐗╄溅鏁版嵁
-  getCartGroupData() {
-    const { cartGroupData } = this.data;
-    if (!cartGroupData) {
-      return fetchCartGroupData();
-    }
-    return Promise.resolve({ data: cartGroupData });
+  /**
+   * 椤甸潰涓婃媺瑙﹀簳浜嬩欢鐨勫鐞嗗嚱鏁�
+   */
+  onReachBottom() {
+
   },
 
-  // 閫夋嫨鍗曚釜鍟嗗搧
-  // 娉細瀹為檯鍦烘櫙鏃跺簲璇ヨ皟鐢ㄦ帴鍙f洿鏀归�変腑鐘舵��
-  selectGoodsService({ spuId, skuId, isSelected }) {
-    this.findGoods(spuId, skuId).currentGoods.isSelected = isSelected;
-    return Promise.resolve();
-  },
+  /**
+   * 鐢ㄦ埛鐐瑰嚮鍙充笂瑙掑垎浜�
+   */
+  onShareAppMessage() {
 
-  // 鍏ㄩ�夐棬搴�
-  // 娉細瀹為檯鍦烘櫙鏃跺簲璇ヨ皟鐢ㄦ帴鍙f洿鏀归�変腑鐘舵��
-  selectStoreService({ storeId, isSelected }) {
-    const currentStore = this.data.cartGroupData.storeGoods.find((s) => s.storeId === storeId);
-    currentStore.isSelected = isSelected;
-    currentStore.promotionGoodsList.forEach((activity) => {
-      activity.goodsPromotionList.forEach((goods) => {
-        goods.isSelected = isSelected;
-      });
-    });
-    return Promise.resolve();
-  },
-
-  // 鍔犺喘鏁伴噺鍙樻洿
-  // 娉細瀹為檯鍦烘櫙鏃跺簲璇ヨ皟鐢ㄦ帴鍙�
-  changeQuantityService({ spuId, skuId, quantity }) {
-    this.findGoods(spuId, skuId).currentGoods.quantity = quantity;
-    return Promise.resolve();
-  },
-
-  // 鍒犻櫎鍔犺喘鍟嗗搧
-  // 娉細瀹為檯鍦烘櫙鏃跺簲璇ヨ皟鐢ㄦ帴鍙�
-  deleteGoodsService({ spuId, skuId }) {
-    function deleteGoods(group) {
-      for (const gindex in group) {
-        const goods = group[gindex];
-        if (goods.spuId === spuId && goods.skuId === skuId) {
-          group.splice(gindex, 1);
-          return gindex;
-        }
-      }
-      return -1;
-    }
-    const { storeGoods, invalidGoodItems } = this.data.cartGroupData;
-    for (const store of storeGoods) {
-      for (const activity of store.promotionGoodsList) {
-        if (deleteGoods(activity.goodsPromotionList) > -1) {
-          return Promise.resolve();
-        }
-      }
-      if (deleteGoods(store.shortageGoodsList) > -1) {
-        return Promise.resolve();
-      }
-    }
-    if (deleteGoods(invalidGoodItems) > -1) {
-      return Promise.resolve();
-    }
-    return Promise.reject();
-  },
-
-  // 娓呯┖澶辨晥鍟嗗搧
-  // 娉細瀹為檯鍦烘櫙鏃跺簲璇ヨ皟鐢ㄦ帴鍙�
-  clearInvalidGoodsService() {
-    this.data.cartGroupData.invalidGoodItems = [];
-    return Promise.resolve();
-  },
-
-  onGoodsSelect(e) {
-    const {
-      goods: { spuId, skuId },
-      isSelected,
-    } = e.detail;
-    const { currentGoods } = this.findGoods(spuId, skuId);
-    Toast({
-      context: this,
-      selector: '#t-toast',
-      message: `${isSelected ? '閫夋嫨' : '鍙栨秷'}"${
-        currentGoods.title.length > 5 ? `${currentGoods.title.slice(0, 5)}...` : currentGoods.title
-      }"`,
-      icon: '',
-    });
-    this.selectGoodsService({ spuId, skuId, isSelected }).then(() => this.refreshData());
-  },
-
-  onStoreSelect(e) {
-    const {
-      store: { storeId },
-      isSelected,
-    } = e.detail;
-    this.selectStoreService({ storeId, isSelected }).then(() => this.refreshData());
-  },
-
-  onQuantityChange(e) {
-    const {
-      goods: { spuId, skuId },
-      quantity,
-    } = e.detail;
-    const { currentGoods } = this.findGoods(spuId, skuId);
-    const stockQuantity = currentGoods.stockQuantity > 0 ? currentGoods.stockQuantity : 0; // 閬垮厤鍚庣杩斿洖鐨勬槸-1
-    // 鍔犺喘鏁伴噺瓒呰繃搴撳瓨鏁伴噺
-    if (quantity > stockQuantity) {
-      // 鍔犺喘鏁伴噺绛変簬搴撳瓨鏁伴噺鐨勬儏鍐典笅缁х画鍔犺喘
-      if (currentGoods.quantity === stockQuantity && quantity - stockQuantity === 1) {
-        Toast({
-          context: this,
-          selector: '#t-toast',
-          message: '褰撳墠鍟嗗搧搴撳瓨涓嶈冻',
-        });
-        return;
-      }
-      Dialog.confirm({
-        title: '鍟嗗搧搴撳瓨涓嶈冻',
-        content: `褰撳墠鍟嗗搧搴撳瓨涓嶈冻锛屾渶澶у彲璐拱鏁伴噺涓�${stockQuantity}浠禶,
-        confirmBtn: '淇敼涓烘渶澶у彲璐拱鏁伴噺',
-        cancelBtn: '鍙栨秷',
-      })
-        .then(() => {
-          this.changeQuantityService({
-            spuId,
-            skuId,
-            quantity: stockQuantity,
-          }).then(() => this.refreshData());
-        })
-        .catch(() => {});
-      return;
-    }
-    this.changeQuantityService({ spuId, skuId, quantity }).then(() => this.refreshData());
-  },
-
-  goCollect() {
-    /** 娲诲姩鑲畾鏈変竴涓椿鍔↖D锛岀敤鏉ヨ幏鍙栨椿鍔╞anner锛屾椿鍔ㄥ晢鍝佸垪琛ㄧ瓑 */
-    const promotionID = '123';
-    wx.navigateTo({
-      url: `/pages/promotion-detail/index?promotion_id=${promotionID}`,
-    });
-  },
-
-  goGoodsDetail(e) {
-    const { spuId, storeId } = e.detail.goods;
-    wx.navigateTo({
-      url: `/pages/goods/details/index?spuId=${spuId}&storeId=${storeId}`,
-    });
-  },
-
-  clearInvalidGoods() {
-    // 瀹為檯鍦烘櫙鏃跺簲璇ヨ皟鐢ㄦ帴鍙f竻绌哄け鏁堝晢鍝�
-    this.clearInvalidGoodsService().then(() => this.refreshData());
-  },
-
-  onGoodsDelete(e) {
-    const {
-      goods: { spuId, skuId },
-    } = e.detail;
-    Dialog.confirm({
-      content: '纭鍒犻櫎璇ュ晢鍝佸悧?',
-      confirmBtn: '纭畾',
-      cancelBtn: '鍙栨秷',
-    }).then(() => {
-      this.deleteGoodsService({ spuId, skuId }).then(() => {
-        Toast({ context: this, selector: '#t-toast', message: '鍟嗗搧鍒犻櫎鎴愬姛' });
-        this.refreshData();
-      });
-    });
-  },
-
-  onSelectAll(event) {
-    const { isAllSelected } = event?.detail ?? {};
-    Toast({
-      context: this,
-      selector: '#t-toast',
-      message: `${isAllSelected ? '鍙栨秷' : '鐐瑰嚮'}浜嗗叏閫夋寜閽甡,
-    });
-    // 璋冪敤鎺ュ彛鏀瑰彉鍏ㄩ��
-  },
-
-  onToSettle() {
-    const goodsRequestList = [];
-    this.data.cartGroupData.storeGoods.forEach((store) => {
-      store.promotionGoodsList.forEach((promotion) => {
-        promotion.goodsPromotionList.forEach((m) => {
-          if (m.isSelected == 1) {
-            goodsRequestList.push(m);
-          }
-        });
-      });
-    });
-    wx.setStorageSync('order.goodsRequestList', JSON.stringify(goodsRequestList));
-    wx.navigateTo({ url: '/pages/order/order-confirm/index?type=cart' });
-  },
-  onGotoHome() {
-    wx.switchTab({ url: '/pages/home/home' });
-  },
-});
+  }
+})
\ No newline at end of file

--
Gitblit v1.9.1