Browse Source

小程序:商品详情接口对接

panyong 2 years ago
parent
commit
c9450daa76

+ 16 - 0
htmldev/wxMini/pages/goodsDetail/api/index.js

@@ -12,3 +12,19 @@ export const getProductDetail = (productId) => request({
   },
   showLoading: true
 })
+
+/**
+ * 收藏/取消收藏商品
+ * @param productId 商品ID
+ * @param type 类型 1收藏2取消收藏
+ * @returns {Promise<*>}
+ */
+export const postProductCollect = (productId, type) => request({
+  url: '/api/user/caigou/product/collect',
+  method: 'POST',
+  data: {
+    product_id: productId,
+    type: type
+  },
+  showLoading: true
+})

+ 35 - 4
htmldev/wxMini/pages/goodsDetail/goodsDetail.js

@@ -1,5 +1,5 @@
 const { getShopDetail } = require('../../api/common')
-const { getProductDetail } = require('./api/index')
+const { getProductDetail, postProductCollect } = require('./api/index')
 
 Page({
 
@@ -8,7 +8,8 @@ Page({
    */
   data: {
     searchForm: {
-      product_id: ''
+      product_id: '',
+      type: -1
     },
     nav: [
       {
@@ -25,7 +26,8 @@ Page({
       product_rotation_img_list: [],
       product_detail_img_list: []
     },
-    objShopDetail: { shop_address: {} }
+    objShopDetail: { shop_address: {} },
+    booLock: false
   },
 
   /**
@@ -100,7 +102,8 @@ Page({
             product_rotation_img_list: JSON.parse(product_rotation_img_list),
             product_detail_img_list: JSON.parse(product_detail_img_list)
           },
-          'searchForm.shop_id': shop_id
+          'searchForm.shop_id': shop_id,
+          'searchForm.type': 1 // todo
         }, () => {
           this.fetchShopDetail()
         })
@@ -130,6 +133,34 @@ Page({
       }
     } catch (err) {}
   },
+  async productCollect() {
+    const { type, product_id } = this.data.searchForm
+    const _type = type === 1 ? 2 : 1
+
+    this.setData({
+      booLock: true
+    })
+    try {
+      const {
+        status,
+        msg
+      } = await postProductCollect(product_id, _type)
+      if (status) {
+        this.setData({
+          'searchForm.type': _type
+        })
+      } else {
+        wx.showToast({
+          title: msg,
+          icon: 'none'
+        })
+      }
+    } catch (err) {}
+
+    this.setData({
+      booLock: false
+    })
+  },
   handleNav(e) {
     const { value } = e.detail
 

+ 3 - 1
htmldev/wxMini/pages/goodsDetail/goodsDetail.wxml

@@ -84,7 +84,9 @@
   <view>
     <button
       class="btn-collection"
-      type="primary">添加收藏夹
+      type="primary"
+      bind:tap="productCollect"
+      wx:if="{{searchForm.type >= 1}}">{{searchForm.type === 1 ? '取消收藏' : '添加收藏'}}
     </button>
   </view>
 </view>