From 42cc31916c043f681c271efeb73394260e7d77aa Mon Sep 17 00:00:00 2001
From: litian <C21AF165>
Date: 星期二, 05 三月 2024 18:53:11 +0800
Subject: [PATCH] 合并

---
 pages/cart/index.js |  366 +++++++++++++--------------------------------------
 1 files changed, 96 insertions(+), 270 deletions(-)

diff --git a/pages/cart/index.js b/pages/cart/index.js
index e710106..db4d31b 100644
--- a/pages/cart/index.js
+++ b/pages/cart/index.js
@@ -1,294 +1,120 @@
-import Dialog from 'tdesign-miniprogram/dialog/index';
-import Toast from 'tdesign-miniprogram/toast/index';
-import { fetchCartGroupData } from '../../services/cart/cart';
+const app = getApp();
+// import { getPublicImage } from '@/assets/js/middleGround/tool.js'
+import { getPublicImage } from '../../assets/js/middleGround/tool';
 
 Page({
   data: {
-    cartGroupData: null,
+    imgUrl: [],
+    shoppingCartData: [],
+    checkAll: false,
+    checkedList: [],
+    selectedCount: 0,
+    totalPrice: 0.00
   },
 
-  // 璋冪敤鑷畾涔塼abbar鐨刬nit鍑芥暟锛屼娇椤甸潰涓巘abbar婵�娲荤姸鎬佷繚鎸佷竴鑷�
-  onShow() {
-    this.getTabBar().init();
+  onLoad(options) {
+    wx.setNavigationBarTitle({
+      title: '璐墿杞�'
+    });
+    this.shoppingCartGet();
   },
 
-  onLoad() {
-    this.refreshData();
-  },
-
-  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;
-
-            // 缁熻鏄惁鏈夊姞璐暟澶т簬搴撳瓨鏁扮殑鍟嗗搧
-            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 });
+  onDelete(e) {
+    const item = e.currentTarget.dataset.item;
+    app.MG.store.delShoppingCart({
+      ids: [item.id]
+    }).then(res => {
+      this.shoppingCartGet();
+      wx.showToast({ title: '浣犵偣鍑讳簡鍒犻櫎', icon: 'none' });
     });
   },
 
-  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,
+  shoppingCartGet() {
+    let query = {
+      start: 0,
+      size: 999,
+      filterList: [],
+      searchList: []
     };
-  },
-
-  // 娉細瀹為檯鍦烘櫙鏃跺簲璇ヨ皟鐢ㄦ帴鍙h幏鍙栬喘鐗╄溅鏁版嵁
-  getCartGroupData() {
-    const { cartGroupData } = this.data;
-    if (!cartGroupData) {
-      return fetchCartGroupData();
-    }
-    return Promise.resolve({ data: cartGroupData });
-  },
-
-  // 閫夋嫨鍗曚釜鍟嗗搧
-  // 娉細瀹為檯鍦烘櫙鏃跺簲璇ヨ皟鐢ㄦ帴鍙f洿鏀归�変腑鐘舵��
-  selectGoodsService({ spuId, skuId, isSelected }) {
-    this.findGoods(spuId, skuId).currentGoods.isSelected = isSelected;
-    return Promise.resolve();
-  },
-
-  // 鍏ㄩ�夐棬搴�
-  // 娉細瀹為檯鍦烘櫙鏃跺簲璇ヨ皟鐢ㄦ帴鍙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;
+    app.MG.store.getShoppingCartProductList(query).then(res => {
+      res.datas.forEach(item => {
+        item.imgUrl = getPublicImage(item.productMonWithLinkDto.product.icon, '', '160');
       });
-    });
-    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();
+      this.setData({
+        shoppingCartData: res.datas
       });
     });
   },
 
-  onSelectAll(event) {
-    const { isAllSelected } = event?.detail ?? {};
-    Toast({
-      context: this,
-      selector: '#t-toast',
-      message: `${isAllSelected ? '鍙栨秷' : '鐐瑰嚮'}浜嗗叏閫夋寜閽甡,
+  bottomChange(e) {
+    const checked = e.detail.value.length > 0;
+    const items = this.data.shoppingCartData.map(item => {
+      item.checked = checked;
+      return item;
     });
-    // 璋冪敤鎺ュ彛鏀瑰彉鍏ㄩ��
+    this.setData({
+      shoppingCartData: items,
+      checkAll: checked
+    });
+    this.calculateSelectedCount();
+    this.calculateTotalPrice();
   },
 
-  onToSettle() {
-    const goodsRequestList = [];
-    this.data.cartGroupData.storeGoods.forEach((store) => {
-      store.promotionGoodsList.forEach((promotion) => {
-        promotion.goodsPromotionList.forEach((m) => {
-          if (m.isSelected == 1) {
-            goodsRequestList.push(m);
-          }
-        });
-      });
+  HandelItemChange(e) {
+    const { item } = e.target.dataset;
+    const items = this.data.shoppingCartData
+    items.map(eitem => {
+      if (eitem.id == item.id) {
+        eitem.checked = e.detail.checked
+      }
+
+
+    })
+    const data = items.filter(item => item.checked)
+    const checkAll = data.length == this.data.shoppingCartData.length
+
+    this.setData({
+      shoppingCartData: items,
+      checkAll
     });
-    wx.setStorageSync('order.goodsRequestList', JSON.stringify(goodsRequestList));
-    wx.navigateTo({ url: '/pages/order/order-confirm/index?type=cart' });
+    this.calculateSelectedCount();
+    this.calculateTotalPrice();
   },
-  onGotoHome() {
-    wx.switchTab({ url: '/pages/home/home' });
+
+  calculateSelectedCount() {
+    const selectedItems = this.data.shoppingCartData.filter(item => item.checked);
+    const selectedCount = selectedItems.length;
+    this.setData({
+      selectedCount
+    });
   },
+
+  calculateTotalPrice() {
+    const selectedItems = this.data.shoppingCartData.filter(item => item.checked);
+    const totalPrice = selectedItems.reduce((total, item) => total + parseFloat(item.saleMethod.price), 0);
+    this.setData({
+      totalPrice: totalPrice.toFixed(2)
+    });
+  },
+  goPaymentPage() {
+
+
+    const selectedItems = this.data.shoppingCartData.filter(item => item.checked);
+    console.log(selectedItems, 789);
+    const selectedIds = selectedItems.map(item => item.id);
+    console.log('閫変腑鐨勫晢鍝� id:', selectedIds);
+
+    app.MG.store.shoppingCartCreateOrder({ linkIds: selectedIds }).then(res => {
+      console.log(res.orderNumber, 456);
+      const url = '/pages/cart/paymentPage/index?orderNumber=' + res.orderNumber
+      wx.navigateTo({
+        url
+      });
+
+
+    })
+
+
+  }
 });

--
Gitblit v1.9.1