Ver código fonte

添加商品规格

skyblued 3 anos atrás
pai
commit
656920a803

+ 1 - 1
src/views/cart/index.tsx

@@ -222,7 +222,7 @@ export default defineComponent({
                                 inputWidth="50px"
                                 buttonSize="24px"
                                 min={1}
-                                max={item.stock}
+                                max={item.stock > 200 ? 200 : item.stock}
                                 integer
                               />
                             ),

+ 33 - 10
src/views/shop-mall/modal/add-goods-cart/index.tsx

@@ -51,9 +51,10 @@ export default defineComponent({
     selectItem() {
       const select = this.skuStockList.find((n: any) => n.id == this.radio) as any
       if (select) {
+        let stock : number = select.stock - select.lockStock - select.cartNum
         return {
           ...select,
-          stock: select.stock - (select.lockStock < 0 ? 0 : select.lockStock)
+          stock
         }
       }
       return {
@@ -69,6 +70,7 @@ export default defineComponent({
       if (Array.isArray(item.skuStockList)) {
         skuStockList = item.skuStockList.map((n: any) => {
           n.pic = n.pic || item.pic
+          n.cartNum = 0
           if (n.spData) {
             const spData = JSON.parse(n.spData)
             let str = ''
@@ -79,7 +81,7 @@ export default defineComponent({
           } else {
             n.spDataJson = '默认'
           }
-          n.lock = n.stock - n.lockStock
+          n.lockStock = n.lockStock > 0 ? n.lockStock : 0
           return {
             ...n
           }
@@ -92,9 +94,9 @@ export default defineComponent({
         let i = skuStockList.findIndex(n => n.id == this.defaultRadio)
         index = i > -1 ? i : 0
       }
-      this.getProductAddCartCount(skuStockList[index].id)
       this.radio = skuStockList[index].id
       this.skuStockList = skuStockList
+      this.getProductAddCartCount(skuStockList[index].id)
     },
     async onAddCart() {
       const selectItem = this.selectItem
@@ -112,7 +114,7 @@ export default defineComponent({
           data: body
         })
         if (code === 200) {
-          this.totalData[selectItem.id] = this.total
+          this.getProductAddCartCount(selectItem.id, true)
           if (this.showType === 'cart') {
             this.onGetCartCount()
             this.$nextTick(() => {
@@ -132,21 +134,42 @@ export default defineComponent({
             })
           }
         }
-      } catch (error) {}
+      } catch (error) {
+        let msg : string = (error as any).message
+        if (msg === "库存不足") {
+          for(let i = 0; i < this.skuStockList.length; i++){
+            if ((this.skuStockList[i] as any).id === this.selectItem.id){
+              (this.skuStockList[i] as any).stock = 0
+            }
+          }
+          console.log(this.skuStockList)
+        }
+      }
     },
 
     // 获取购物车当前产品的数量
-    async getProductAddCartCount(id?: any) {
+    async getProductAddCartCount(id?: any, isRest = false) {
       id = id ? id : this.selectItem.id
-      if (this.totalData[id]) {
-        this.total = this.totalData[id]
+      if (this.totalData[id] && !isRest) {
+        this.setProductStock(this.totalData[id])
         return
       }
       if (!id) return
       try {
         let res = await request.get(`/api-mall-portal/product/cart/${id}`)
-        this.totalData[id] = this.total = res.data || 1
+        this.setProductStock(res.data || 0)
+        this.totalData[id] = res.data || 0
       } catch (err) {}
+    },
+
+    // 更新产品规格的库存
+    setProductStock(n: number){
+      // 根据当前用户的购物车,当前产品规格的数量,限制库存
+      for(let i = 0; i < this.skuStockList.length; i++){
+        if ((this.skuStockList[i] as any).id === this.radio) {
+          (this.skuStockList[i] as any).cartNum = n
+        }
+      }
     }
   },
   render() {
@@ -224,7 +247,7 @@ export default defineComponent({
             inputWidth="50px"
             theme="round"
             buttonSize="24px"
-            max={this.selectItem.stock}
+            max={this.selectItem.stock > 200 ? 200 : this.selectItem.stock}
             min={1}
             disabled={this.selectItem.stock <= 0}
             integer