Browse Source

添加购物车

yann 5 tháng trước cách đây
mục cha
commit
21de71103d

+ 2 - 1
components/addCartModel/addCartModel.js

@@ -33,7 +33,8 @@ Component({
             pageNum:1,
             pageSize:1,
             mercId:e.mercId,
-            prodId:e.prodId
+            prodId:e.prodId,
+            mercProdId:e.mercProdId
         }  
         API.getProdDetail(data).then(res=>{
             this.setData({

+ 3 - 1
components/header/header.js

@@ -11,7 +11,9 @@ Component({
       index:0
     },
     methods: {
-        
+        backHome(){
+            wx.navigateBack(-1);
+        },
     },
     ready: function(){
         console.log(this.data.position);

+ 2 - 1
components/header/header.wxml

@@ -2,7 +2,8 @@
 <view class="header">
     <view class="header-bg"></view>
     <view class="logo logo-at-left">
-      <image src="/images/index/logo.png" class="logo-image"></image>
+      <image src="/images/index/logo.png" class="logo-image" wx:if="{{position === 'index'}}" bindtap="backHome"></image>
+      <image src="/images/mall/back.png" mode="" wx:else="{{position != 'index'}}" class="back" bindtap="backHome"/>
     </view>
     <view class="mark-icon" wx:if="{{position != 'index'}}">
        <image class="mark-image" src="/images/mall/{{position}}.png" mode="widthFix"/>

+ 5 - 0
components/header/header.wxss

@@ -22,6 +22,11 @@
     top: 96rpx;
     left: 36rpx;
   }
+  .header .logo.logo-at-left .back{
+    width: 25rpx;
+    height: 40rpx;
+    margin-top: 10rpx;
+  }
   .header .logo-image {
     width: 100%;
     height: 100%;

+ 0 - 9
components/product-list/product-list.js

@@ -76,15 +76,6 @@ Component({
             console.log(prod);
             let prodInfo = prod.currentTarget.dataset.prod;
             this.triggerEvent('addCart',prodInfo);
-            // let data = {
-            //     mercId:prodInfo.mercId,
-            //     prodId:prodInfo.prodId,
-            //     prodAttrId:prodInfo.prodAttrList[0],
-            //     count:1
-            // }
-            // API.addCart(data).then(res=>{
-            //     console.log(res);
-            // })
         },
         // 观察滚动触底事件
         onReachBottom(e){

BIN
images/mall/back.png


+ 14 - 2
pages/Mall/hotCommodity/hotCommodity.js

@@ -5,9 +5,21 @@ Page({
    * 页面的初始数据
    */
   data: {
-
+    showAddCartModal:false,
+    currentProduct:null
+  },
+  // 显示商品属性模态框
+  showAddCartModal(e) {
+    const product = e.detail;
+    this.setData({
+      showAddCartModal: true,
+      currentProduct:product
+    });
+  },
+  // 关闭商品属性模态框
+  handleCloseAddCartModal() {
+    this.setData({ showAddCartModal: false });
   },
-
   /**
    * 生命周期函数--监听页面加载
    */

+ 2 - 1
pages/Mall/hotCommodity/hotCommodity.json

@@ -1,7 +1,8 @@
 {
   "usingComponents": {
       "header":"/components/header/header",
-      "product-list":"/components/product-list/product-list"
+      "product-list":"/components/product-list/product-list",
+      "addCartModel":"/components/addCartModel/addCartModel"
   },
   "navigationStyle":"custom",
   "navigationBarTitleText":"热门商品"

+ 4 - 1
pages/Mall/hotCommodity/hotCommodity.wxml

@@ -4,4 +4,7 @@
 <header position="hot"></header>
 
 <!-- 商品列表 -->
-<product-list position="hot"></product-list>
+<product-list position="hot" bindaddCart="showAddCartModal"></product-list>
+
+<!-- 商品属性模态框 -->
+<addCartModel visible="{{showAddCartModal}}" product="{{currentProduct}}" bindclose="handleCloseAddCartModal"></addCartModel>

+ 14 - 2
pages/Mall/newCommodity/newCommodity.js

@@ -5,9 +5,21 @@ Page({
    * 页面的初始数据
    */
   data: {
-
+    showAddCartModal:false,
+    currentProduct:null
+  },
+  // 显示商品属性模态框
+  showAddCartModal(e) {
+    const product = e.detail;
+    this.setData({
+      showAddCartModal: true,
+      currentProduct:product
+    });
+  },
+  // 关闭商品属性模态框
+  handleCloseAddCartModal() {
+    this.setData({ showAddCartModal: false });
   },
-
   /**
    * 生命周期函数--监听页面加载
    */

+ 2 - 1
pages/Mall/newCommodity/newCommodity.json

@@ -1,7 +1,8 @@
 {
   "usingComponents": {
       "header":"/components/header/header",
-      "product-list":"/components/product-list/product-list"
+      "product-list":"/components/product-list/product-list",
+      "addCartModel":"/components/addCartModel/addCartModel"
   },
   "navigationStyle":"custom",
   "navigationBarTitleText":"上新商品"

+ 4 - 1
pages/Mall/newCommodity/newCommodity.wxml

@@ -4,4 +4,7 @@
 <header position="new"></header>
 
 <!-- 商品列表 -->
-<product-list position="new"></product-list>
+<product-list position="new" bindaddCart="showAddCartModal"></product-list>
+
+<!-- 商品属性模态框 -->
+<addCartModel visible="{{showAddCartModal}}" product="{{currentProduct}}" bindclose="handleCloseAddCartModal"></addCartModel>

+ 14 - 2
pages/Mall/presaleCommodity/presaleCommodity.js

@@ -5,9 +5,21 @@ Page({
    * 页面的初始数据
    */
   data: {
-
+    showAddCartModal:false,
+    currentProduct:null
+  },
+  // 显示商品属性模态框
+  showAddCartModal(e) {
+    const product = e.detail;
+    this.setData({
+      showAddCartModal: true,
+      currentProduct:product
+    });
+  },
+  // 关闭商品属性模态框
+  handleCloseAddCartModal() {
+    this.setData({ showAddCartModal: false });
   },
-
   /**
    * 生命周期函数--监听页面加载
    */

+ 2 - 1
pages/Mall/presaleCommodity/presaleCommodity.json

@@ -1,7 +1,8 @@
 {
   "usingComponents": {
       "header":"/components/header/header",
-      "product-list":"/components/product-list/product-list"
+      "product-list":"/components/product-list/product-list",
+      "addCartModel":"/components/addCartModel/addCartModel"
   },
   "navigationStyle":"custom",
   "navigationBarTitleText":"预售商品"

+ 4 - 1
pages/Mall/presaleCommodity/presaleCommodity.wxml

@@ -4,4 +4,7 @@
 <header position="presale"></header>
 
 <!-- 商品列表 -->
-<product-list position="presale"></product-list>
+<product-list position="presale" bindaddCart="showAddCartModal"></product-list>
+
+<!-- 商品属性模态框 -->
+<addCartModel visible="{{showAddCartModal}}" product="{{currentProduct}}" bindclose="handleCloseAddCartModal"></addCartModel>

+ 2 - 1
pages/Mall/sortCommodity/sortCommodity.json

@@ -1,7 +1,8 @@
 {
     "usingComponents": {
         "header":"/components/header/header",
-        "product-list":"/components/product-list/product-list"
+        "product-list":"/components/product-list/product-list",
+        "addCartModel":"/components/addCartModel/addCartModel"
     },
     "navigationStyle":"custom",
     "navigationBarTitleText":"分类商品"

+ 10 - 14
pages/cart/cart.js

@@ -44,13 +44,13 @@ Page({
     // 选中/取消选中所有
     handleCheckedAll(){
         API.handleCheckedAll().then(res=>{
-          
+          this.handleCartsFn(res);
         })
     },
     // 取消选中所有
     handleUncheckedAll(){
         API.handleUncheckedAll().then(res=>{
-            
+            this.handleCartsFn(res);
         })
     },
     // 删除选中的商品
@@ -100,23 +100,17 @@ Page({
     // 增加数量
     increaseCount(e) {
         const id = e.currentTarget.dataset.id;
-        const cartList = this.data.cartList.map(item => {
-        if (item.id == id) item.quantity++;
-            return item;
-        });
-        this.setData({ cartList });
-        this.calculateTotal();
+        API.addCartProdCount({id}).then(res=>{
+           this.handleCartsFn(res);
+        })
     },
   
     // 减少数量
     decreaseCount(e) {
         const id = e.currentTarget.dataset.id;
-        const cartList = this.data.cartList.map(item => {
-        if (item.id == id && item.quantity > 1) item.quantity--;
-            return item;
-        });
-        this.setData({ cartList });
-        this.calculateTotal();
+        API.subCartProdCount({id}).then(res=>{
+            this.handleCartsFn(res);
+        })
     },
     // 计算总价和选中数量
     calculateTotal() {
@@ -137,6 +131,8 @@ Page({
         })
     },
     onLoad(){
+    },
+    onShow(){
         this.getCartProds();
     }
 })

+ 10 - 2
service/api.js

@@ -247,15 +247,23 @@ export const API = {
         return http.post("/api/store/carts/checked",playload)
     },
     // 选中选中所有商品
-    handleCheckedAll: function(playload){
+    handleCheckedAll: function(playload={}){
         return http.post("/api/store/carts/checkedAll",playload)
     },
     // 取消选中所有商品
-    handleUncheckedAll: function(playload){
+    handleUncheckedAll: function(playload={}){
         return http.post("/api/store/carts/uncheckedAll",playload)
     },
     // 删除购物车选中的商品
     deleteCartProds: function(playload){
         return http.post("/api/store/carts/delete",playload)
     },
+    // 增加商品数量
+    addCartProdCount: function(playload){
+        return http.post("/api/store/carts/addCnt",playload)
+    },
+    // 减少商品数量
+    subCartProdCount: function(playload){
+        return http.post("/api/store/carts/subCnt",playload)
+    }
 }