var util = require('../../utils/util.js'); var api = require('../../config/api.js'); //获取应用实例 const app = getApp(); Page({ data: { navList: [], categoryList: [], currentCategory: {}, brandList:[], scrollLeft: 0, scrollTop: 0, goodsCount: 0, scrollHeight: 0, // filterCategory: [], goodsList: [], categoryId: 0, currentSortType: 'default', currentSortOrder: 'desc', filterDiscount: 0,// 0不限 1特价 2活动 goodsBizType: app.globalData.appGoodsBizType,//业务类型 page: 1, size: 4, showNavList: false, footCart: {}, referrer: 0, sourceKey: '', openCoupon: false, refresh: false, hidden: false }, toggleNav() { this.setData({ showNavList: !this.data.showNavList }) }, switchNav(event) { let name = event.currentTarget.dataset.name; wx.switchTab({ url: `/pages/${name}/${name}`, }); }, onLoad: function (options) { let that = this; // 页面初始化 options为页面跳转所带来的参数 if (options.referrer){ that.setData({ referrer: parseInt(options.referrer), sourceKey: options.sourceKey, openCoupon: true }); } // that.setData({ // goodsBizType: app.globalData.appGoodsBizType // }); wx.setNavigationBarTitle({ title: '分类' }); }, onReady: function () { // 页面渲染完成 }, onShow: function () { let that = this; that.setData({ goodsBizType: app.globalData.appGoodsBizType }); console.log(wx.getStorageSync('storeId')) console.log(wx.getStorageSync('merchSn')) // 页面显示 if (wx.getStorageSync('storeId') && wx.getStorageSync('merchSn')) { util.request(api.ChooseStoreId, { storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn'), isRefusedLogin: wx.getStorageSync('isRefusedLogin') }, 'POST').then(function (res) { if (res.errno === 0) { wx.setStorageSync('storeId', wx.getStorageSync('storeId')); wx.setStorageSync('merchSn', wx.getStorageSync('merchSn')); // that.reLoad(); } }); that.getCatalog(); } if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) { if (wx.getStorageSync('storeId') && wx.getStorageSync('merchSn')) { that.reLoad(); that.getFootCart(); } } else { // console.log('拒绝授权:' + wx.getStorageSync('isRefusedLogin')) // if (wx.getStorageSync('isRefusedLogin') != 'true') { // wx.navigateTo({ // url: '/pages/auth/btnAuth/btnAuth', // }) // } } }, onHide: function () { // 页面隐藏 this.setData({ showNavList: false }) }, onUnload: function () { // 页面关闭 }, reLoad: function () { let that = this; if (wx.getStorageSync('storeId')) { if (wx.getStorageSync('userId')) { wx.request({ url: api.updateLoginUser, data: { userId: wx.getStorageSync('userId'), storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn') }, method: 'POST', header: { 'Content-Type': 'application/json' }, success: function (wxRes) { if (wxRes.data.errno === 0) { // console.log("用户信息更新成功"); } }, fail: function (err) { console.log("failed"); } }); } } }, getCatalog: function () { wx.showLoading({ title: '加载中...', }) //CatalogList let that = this; util.request(api.CatalogList, { storeId: wx.getStorageSync('storeId') }).then(function (res) { if (!wx.getStorageSync('currentCategory')) { console.log(res.data.currentCategory); console.log(res.data.categoryList); that.setData({ navList: res.data.categoryList, currentCategory: res.data.currentCategory, brandList: res.data.brandList }); wx.setStorageSync('currentCategory', res.data.currentCategory); } else { if (that.data.currentCategory && that.data.currentCategory.id > 0) { that.setData({ navList: res.data.categoryList }); // console.log(res.data.categoryList); // console.log(that.data.currentCategory); // console.log(that.data.currentCategory.id); } else { that.setData({ navList: res.data.categoryList, currentCategory: res.data.currentCategory }); if (res.data.brandList) { that.setData({ brandList: res.data.brandList }); } console.log(res.data.currentCategory); // console.log(res.data.currentCategory.id); } } if (that.data.goodsList.length == 0 || wx.getStorageSync('storeId') != wx.getStorageSync('mapCatalogStoreId') || wx.getStorageSync('isSwitchCatalog') == 'true') { wx.setStorageSync('mapCatalogStoreId', wx.getStorageSync('storeId')); that.setData({ page: 1 }); that.getCategoryData(); wx.setStorageSync('isSwitchCatalog', 'false'); } // if (wx.getStorageSync('storeId') != wx.getStorageSync('mapCatalogStoreId')) { // wx.setStorageSync('mapCatalogStoreId', wx.getStorageSync('storeId')); // that.setData({ // page: 1 // }); // that.getCategoryData(); // } wx.hideLoading(); }); util.request(api.GoodsCount).then(function (res) { that.setData({ goodsCount: res.data.goodsCount }); }); }, getCurrentCategory: function (id) { let that = this; util.request(api.CatalogCurrent, { id: id }) .then(function (res) { that.setData({ currentCategory: res.data.currentCategory, brandList: res.data.brandList, goodsList: [], page: 1 }); // console.log(res.data.brandList); that.getGoodsList(); }); }, getList: function () { var that = this; util.request(api.ApiRootUrl + 'api/catalog/' + that.data.currentCategory.id) .then(function (res) { that.setData({ categoryList: res.data, }); }); }, switchCate: function (event) { var that = this; var currentTarget = event.currentTarget; if (this.data.currentCategory.id == event.currentTarget.dataset.id) { return false; } this.getCurrentCategory(event.currentTarget.dataset.id); }, // getCategoryData: function () { let that = this; that.setData({ goodsList: [] }); that.getGoodsList(); }, getGoodsList() { // wx.showLoading({ // title: '加载中...', // }); var that = this; if (that.data.currentCategory != null && that.data.currentCategory.id > 0){ util.request(api.CatalogProductList, { page: that.data.page, size: that.data.size, order: that.data.currentSortOrder, sort: that.data.currentSortType, // discount: that.data.filterDiscount, goodsBizType: that.data.goodsBizType, categoryId: that.data.currentCategory.id }) .then(function (res) { if (res.errno === 0) { let goodsList = that.data.goodsList.concat(res.data.data); that.setData({ goodsList: goodsList, filterCategory: res.data.filterCategory, refresh: true }); if(that.data.page == 4){ that.setData({refresh: true}); }else{ that.setData({hidden: true }) } } }); } wx.hideLoading(); }, getFootCart: function () { let that = this; util.request(api.GetFootCart).then(function (res) { if (res.errno === 0) { that.setData({ footCart: res.data, }); } }); }, //购物车减少 cutNumber: function (e) { let that = this; var goodsId = e.currentTarget.dataset.goodsId; var productId = e.currentTarget.dataset.productId; var goodsList = that.data.goodsList; // goodsList.forEach(function (val, index, arr) { // if (val.product_id == productId) { // val.cart_num = val.cart_num - 1; // if (val.cart_num >= 0) { // goodsList[index] = val; // that.setData({goodsList: goodsList}); // } // } // }); // that.setData({goodsList: goodsList}); util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) { if (res.errno === 0 && null != res.data) { var goodsList = that.data.goodsList; that.getFootCart(); goodsList.forEach(function (val, index, arr) { if (val.product_id == productId) { val.cart_num = res.data; goodsList[index] = val; if(val.cart_num == 0){ util.showErrorToast('不能再减了'); } } }, that); that.setData({ goodsList: goodsList }); } }); }, //购物车增加 addNumber: function (e) { let that = this; var goodsId = e.currentTarget.dataset.goodsId; var productId = e.currentTarget.dataset.productId; var goodsList = that.data.goodsList; // goodsList.forEach(function (val, index, arr) { // if (val.product_id == productId) { // val.cart_num = val.cart_num + 1; // goodsList[index] = val; // } // }); // that.setData({goodsList: goodsList}); util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) { if (res.errno === 0 && null != res.data) { that.getFootCart(); goodsList.forEach(function (val, index, arr) { res.data.cartList.forEach(function (cartVal, cartIndex, cartArr) { if (val.product_id == cartVal.product_id) { val.cart_num = cartVal.number; goodsList[index] = val; } }); }, that); that.setData({ goodsList: goodsList }); } }); }, openSortFilter: function (event) { let that = this; let currentId = event.currentTarget.id; console.log('currentId:'+currentId); switch (currentId) { case 'defaultActivity': that.setData({ 'goodsBizType': '00', goodsList: [], page: 1 }); app.globalData.appGoodsBizType = '00'; // this.getGoodsList(); console.log(that.data.currentCategory.id); that.getCurrentCategory(that.data.currentCategory.id); break; case 'discountActivity': that.setData({ 'goodsBizType': '02', goodsList: [], page: 1 }); app.globalData.appGoodsBizType = '02'; // this.getGoodsList(); console.log(that.data.currentCategory.id); that.getCurrentCategory(that.data.currentCategory.id); break; case 'groupActivity': that.setData({ 'goodsBizType': '10', goodsList: [], page: 1 }); app.globalData.appGoodsBizType = '10'; // this.getGoodsList(); console.log(that.data.currentCategory.id); that.getCurrentCategory(that.data.currentCategory.id); break; case 'ordActivity': that.setData({ 'goodsBizType': '11', goodsList: [], page: 1 }); app.globalData.appGoodsBizType = '11'; // this.getGoodsList(); console.log(that.data.currentCategory.id); that.getCurrentCategory(that.data.currentCategory.id); break; case 'sellSort': let tmpSortOrder = 'asc'; if (this.data.currentSortOrder == 'asc') { tmpSortOrder = 'desc'; } this.setData({ 'currentSortType': 'sell', 'currentSortOrder': tmpSortOrder, goodsList: [], page: 1 }); // this.getGoodsList(); console.log(that.data.currentCategory.id); that.getCurrentCategory(that.data.currentCategory.id); break; case 'priceSort': tmpSortOrder = 'asc'; if (this.data.currentSortOrder == 'asc') { tmpSortOrder = 'desc'; } this.setData({ 'currentSortType': 'price', 'currentSortOrder': tmpSortOrder, goodsList: [], page: 1 }); // this.getGoodsList(); console.log(that.data.currentCategory.id); that.getCurrentCategory(that.data.currentCategory.id); break; default: //综合排序 this.setData({ 'currentSortType': 'default', 'currentSortOrder': 'desc', goodsList: [], page: 1 }); // this.getGoodsList(); console.log(that.data.currentCategory.id); that.getCurrentCategory(that.data.currentCategory.id); } }, /** * 转发成功领取转发优惠券,暂时无此优惠券 */ takeShareCoupon() { let that = this; // util.request(api.CouponTransActivit, { // referrer: that.data.sourceKey, // sourceKey: that.data.sourceKey // }).then(function (res) { // if (res.errno === 0) { // util.showErrorToast("领取成功"); // that.setData({ // couponList: res.data, // openCoupon: false // }); // } else if (res.errno === 2) { // util.showErrorToast(res.errmsg) // that.setData({ // couponList: res.data, // openCoupon: false // }); // } // }); }, closeCoupon: function () { var that = this; that.setData({ openCoupon: false }); }, bindtapGoodsDetail: function (e) { var that = this; let url = ''; var itemId = e.currentTarget.dataset.itemId; if (that.data.filterDiscount != 2) { url = '/pages/goods/goods?id=' + itemId; } else { url = '/pages/groupDetail/groupDetail?id=' + itemId; } wx.navigateTo({ url: url, }) }, sercherCategory: function (e) { var that = this; let url = ''; var replyType = e.currentTarget.dataset.replyType; var currentIndex = e.currentTarget.dataset.currentIndex; // console.log('replyType:' + e.currentTarget.dataset.replyType); // console.log('goodsBizType:' + that.data.goodsBizType); // 跳转页面 wx.navigateTo({ url: '/pages/category/category?id=' + replyType + '&&goodsBizType=' + that.data.goodsBizType + '&¤tIndex=' + currentIndex }) }, sercherCategoryByBrand: function (e) { var that = this; let url = ''; var replyType = e.currentTarget.dataset.replyType; var currentIndex = e.currentTarget.dataset.currentIndex; console.log('replyType:' + replyType); console.log('goodsBizType:' + that.data.goodsBizType); // 跳转页面 wx.navigateTo({ url: '/pages/categoryBrand/categoryBrand?brandId=' + replyType + '&&goodsBizType=' + that.data.goodsBizType + '&¤tIndex=' + currentIndex }) }, // onReachBottom() { // var that = this; // wx.showLoading({ // title: '加载中...', // }) // that.setData({ // page: that.data.page + 1 // }); // if(that.data.page > that.data.total){ // that.setData({ // hidden: false // }); // } // that.getGoodsList(); // }, onLoaderMoreMovies: function () { var that = this; wx.showNavigationBarLoading(); if (that.data.goodsList.length > 0) { wx.showLoading({ title: '加载中...', }); that.setData({ page: that.data.page + 1 }); } console.log('加载'); that.getGoodsList(); wx.hideNavigationBarLoading() } })