ソースを参照

添加购车显示数量

skyblued 3 年 前
コミット
e8ca7d9f31
1 ファイル変更16 行追加8 行削除
  1. 16 8
      src/views/shop-mall/modal/add-goods-cart/index.tsx

+ 16 - 8
src/views/shop-mall/modal/add-goods-cart/index.tsx

@@ -31,7 +31,7 @@ export default defineComponent({
       type: String,
       default: 'cart'
     },
-    onGetCartCount:{
+    onGetCartCount: {
       type: Function,
       default: (n: any) => {}
     }
@@ -82,6 +82,7 @@ export default defineComponent({
         index = i > -1 ? i : 0
       }
       const { id, price, pic, stock, spDataJson, skuCode } = skuStockList[index]
+      this.getProductAddCartCount(id)
       this.radio = id
       this.selectItem = {
         id,
@@ -95,10 +96,8 @@ export default defineComponent({
       return skuStockList
     }
   },
-  mounted() {},
   methods: {
     async onAddCart() {
-      // console.log(this.total, this.selectItem, this.item)
       const selectItem = this.selectItem
       const item = this.item
       const body = {
@@ -134,6 +133,16 @@ export default defineComponent({
           }
         }
       } catch (error) {}
+    },
+
+    // 获取购物车当前产品的数量
+    async getProductAddCartCount(id?:any) {
+      id = id ? id : this.skuStockList.find((n: any) => n.id == this.radio)?.id
+      if (!id) return
+      try {
+        let res = await request.get(`/api-mall-portal/product/cart/${id}`)
+        this.total = res.data || 1
+      } catch (err) {}
     }
   },
   render() {
@@ -179,7 +188,6 @@ export default defineComponent({
               <RadioGroup
                 class={styles['radio-group']}
                 modelValue={this.radio}
-                onUpdate:modelValue={val => (this.radio = val)}
               >
                 {this.skuStockList.map((item: any) => {
                   const isActive = item.id === this.radio
@@ -189,11 +197,10 @@ export default defineComponent({
                       class={styles.radio}
                       name={item.id}
                       onClick={() => {
-                        this.radio = item.id
+                        if (this.radio == item.id) return
                         // 处理默认显示
-                        const { id, price, pic, stock, spDataJson, skuCode } =
-                          item
-                        this.radio = id
+                        const { id, price, pic, stock, spDataJson, skuCode } = item
+                        this.radio = item.id
                         this.selectItem = {
                           id,
                           price,
@@ -202,6 +209,7 @@ export default defineComponent({
                           spDataJson,
                           skuCode
                         }
+                        this.getProductAddCartCount(id)
                       }}
                     >
                       <Tag size="large" plain={isActive} type={type}>