| const app = getApp(); | 
| var util = require('../../utils/util.js'); | 
| var api = require('../../utils/service-api.js'); | 
| var comReq = require('../../utils/common-request.js'); | 
| var shopcart = require('../../utils/shopcart.js'); | 
| Page({ | 
|   | 
|   /** | 
|    * 页面的初始数据 | 
|    */ | 
|   data: { | 
|     currentIndex: 0, | 
|     tabs: [], | 
|     productList: [], | 
|     offset: 0, | 
|     limit: 8, | 
|     queryKey: '', | 
|     isEnd: false, | 
|     shopId:'', | 
|     toTopIcon:false, | 
|     top_num:0, | 
|     totalNum: 0, | 
|     hasCarts: true, | 
|     hide_good_box: true, | 
|     needAni: -1, | 
|      | 
|   }, | 
|   | 
|   | 
|   | 
|   onLoad: function(options) { | 
|     var _this = this; | 
|      | 
|   | 
|     this.getProductAttribute(); | 
|   | 
|     this.setData({ | 
|       shopId:app.shopInfo.id, | 
|     }); | 
|   | 
|     //  获取页面的有关信息 | 
|   | 
|     wx.getSystemInfo({ | 
|       success: function (res) { | 
|         wx.setStorageSync('systemInfo', res) | 
|         var ww = res.windowWidth; | 
|         var hh = res.windowHeight; | 
|   | 
|         _this.busPos = {}; | 
|         _this.busPos['x'] = ww - 35; | 
|         _this.busPos['y'] = hh - 65; | 
|       } | 
|     }); | 
|   | 
|   | 
|   }, | 
|   onShow(){ | 
|   | 
|     //切换店铺重新加载产品 | 
|     if (this.data.shopId && app.shopInfo.id != this.data.shopId){ | 
|       this.cleanSearchCondition(); | 
|       this.getProductAttribute(); | 
|   | 
|       this.setData({ | 
|         shopId:app.shopInfo.id, | 
|       }); | 
|       //刷新购物车数量 | 
|       this.refreshUserCartCount(); | 
|     } | 
|   | 
|      | 
|   }, | 
|   | 
|   //切换分类 | 
|   titleClick: function(e) { | 
|     let currentPageIndex = e.currentTarget.dataset.idx; | 
|     this.setData({ | 
|       currentIndex: currentPageIndex, | 
|     }) | 
|     this.cleanSearchCondition(); | 
|     this.getProduct(); | 
|   }, | 
|   | 
|   /** | 
|    * 清空数据加载条件 | 
|    */ | 
|   cleanSearchCondition(){ | 
|     this.setData({ | 
|       productList: [], | 
|       offset: 0, | 
|       isEnd: false, | 
|     }) | 
|   }, | 
|   | 
|   //获取产品分类 | 
|   getProductAttribute() { | 
|     var _this = this; | 
|     util.request({ | 
|       api: api.productAttribute.getByCode+app.shopInfo.id+"/cpfl", | 
|       callback: function(data) { | 
|         _this.setData({ | 
|           tabs: data.rows, | 
|         }); | 
|         //加载产品 | 
|         _this.getProduct(); | 
|       } | 
|     }); | 
|   }, | 
|   | 
|   formTitle: function(e) { | 
|     this.setData({ | 
|       queryKey: e.detail.value | 
|     }) | 
|   }, | 
|   | 
|   | 
|   onSearching() { | 
|     if (this.queryKey.length > 0) { | 
|       this.setData({ | 
|         currentIndex: -1 | 
|       }); | 
|       this.cleanSearchCondition(); | 
|       this.getProduct(); | 
|     } | 
|   }, | 
|   | 
|   | 
|   | 
|   | 
|   | 
|   // 加载产品数据 | 
|   getProduct: function() { | 
|   | 
|     var _this = this; | 
|     var limit = _this.data.limit; | 
|     var offset = _this.data.offset; | 
|     var queryKey = _this.data.queryKey; | 
|     var currentIndex = this.data.currentIndex; | 
|     var categoryId = ''; | 
|     if (this.data.tabs.length >0 && currentIndex != -1) { | 
|        categoryId = this.data.tabs[currentIndex].attrId; | 
|     } | 
|   | 
|     util.request({ | 
|       api: api.goods.searchGoods, | 
|       data: { | 
|         "limit": limit, | 
|         "categoryId": categoryId, | 
|         "offset": offset, | 
|         "shopIds": app.shopInfo.id, | 
|         "title": queryKey | 
|       }, | 
|       callback: function(data) { | 
|   | 
|         if (data.rows.length == 0) { | 
|           _this.setData({ | 
|             isEnd: true | 
|           }); | 
|         } else { | 
|           var productList = _this.data.productList; | 
|           for (var i = 0; i < data.rows.length; i++) { | 
|             productList.push(data.rows[i]) | 
|           } | 
|           //更新下次调用的角标 | 
|           _this.setData({ | 
|             productList: productList, | 
|             offset: offset + limit, | 
|           }); | 
|         } | 
|       } | 
|     }); | 
|   | 
|   }, | 
|   | 
|   | 
|   // 进入详情 | 
|   intoDetail(e) { | 
|     var _this = this; | 
|     var id = e.currentTarget.dataset.id; | 
|     var service = e.currentTarget.dataset.service; | 
|     if (service == 1) { | 
|   | 
|       this.checkUserInfoIsBuding() | 
|   | 
|       var index = e.currentTarget.dataset.index; | 
|   | 
|       var product = this.data.productList[index]; | 
|       wx.setStorageSync('chooseService', product); | 
|       wx.switchTab({ | 
|         url: '/pages/yuyue/khyuyue' | 
|       }); | 
|     } else { | 
|       wx.navigateTo({ | 
|         url: '/pages/service/details?id=' + id, | 
|       }) | 
|     } | 
|   }, | 
|   | 
|   | 
|   | 
|   | 
|   //检查用户信息是否绑定 | 
|   checkUserInfoIsBuding() { | 
|     if (util.isBlank(getApp().userInfo.nickName)) { | 
|       wx.navigateTo({ | 
|         url: '/pages/welcome/welcome?url=/pages/yuyue/khyuyue', | 
|       }) | 
|     } else if (util.isBlank(getApp().userInfo.phoneNumber)) { | 
|       wx.navigateTo({ | 
|         url: '/pages/phone/phone?url=/pages/yuyue/khyuyue', | 
|       }) | 
|     } | 
|   }, | 
|   | 
|   //点击购物车事件 | 
|   touchOnGoods: function (e) { | 
|     var _this = this; | 
|     var id = e.currentTarget.dataset.id; | 
|     var stockNum = e.currentTarget.dataset.stockNum; | 
|     if (stockNum < 1) { | 
|       wx.showToast({ | 
|         title: "您来晚了,商品已经抢光了", | 
|         icon: 'none', | 
|       }); | 
|       return; | 
|     } | 
|     var productInfo = {}; | 
|     var _this = this; | 
|     //查询产品信息 | 
|     util.request({method:"GET", | 
|     api: api.goods.productInfo + app.shopInfo.id+"/"+ id, | 
|     callback: function (data) { | 
|       productInfo = data.rows[0]; | 
|       if (productInfo.skus.length == 1) { | 
|         var skuId = productInfo.skus[0].id; | 
|         _this.busAnimation(e); | 
|         shopcart.playAnimation({ | 
|           page: _this, | 
|           event: e, | 
|         }, function () { | 
|           comReq.addShoppingCar(skuId, 1,function(totalNum){ | 
|             | 
|             _this.setData({ | 
|               totalNum: totalNum | 
|             }); | 
|   | 
|           }); | 
|           | 
|         }); | 
|       } else { | 
|         //存在多个库存 | 
|         _this.setData({ | 
|           normsList: data.rows[0].skus, | 
|           currentGoodsInfo: data.rows[0], | 
|           currentSku: data.rows[0].skus[0], | 
|           normspopup: true | 
|         }); | 
|   | 
|   | 
|       } | 
|     }}); | 
|   | 
|   }, | 
|   //刷新购物车数量 | 
|   refreshUserCartCount() { | 
|     var _this = this; | 
|     util.request({method:"POST", | 
|     api: api.goods.getUserCartCount+app.shopInfo.id,  | 
|     callback: function (data) { | 
|         _this.setData({ | 
|           totalNum: data.mapInfo.userCartCount | 
|         }); | 
|       }}); | 
|   }, | 
|   | 
|   | 
|   //购物车放大效果 | 
|   busAnimation: function (e) { | 
|     var id = e.currentTarget.dataset.id; | 
|     var that = this; | 
|     that.setData({ | 
|       needAni: id | 
|     }); | 
|     setTimeout(function () { | 
|       that.setData({ | 
|         needAni: -1 | 
|       }); | 
|     }, 500); | 
|   }, | 
|   | 
|   | 
|   | 
|   | 
|   //一键回到顶部 | 
|   goTap() { | 
|     this.setData({ top_num: 0 }) | 
|   }, | 
|   | 
|   scroll_distance: function (event) { | 
|       | 
|     this.setData({ | 
|       toTopIcon: event.detail.scrollTop > 300 ? true : false | 
|     }) | 
|   }, | 
|   | 
|    | 
|   backtop: function () { | 
|       this.setData({ top_num: 0 }) | 
|   }, | 
|   | 
|   | 
|   | 
|   //转发 | 
|   onShareAppMessage(res) { | 
|   | 
|   }, | 
|   | 
|   | 
| }) |