// pages/tabBar/salesallGoods/allGoods.js const app = getApp() const api = require('../../../request/api'); const http = require('../../../request/http'); const storeKeys = require('../../../utils/storageKeys.js'); const utils = require('../../../utils/util'); Page({ data: { statusBarHeight: app.globalData.statusBarHeight, list: [], //页面数据 dataObj: {}, //页面对象 isLoading: true, //是否处于加载中 sortStyle: 'all', //排序类型,all:综合,sales:销量,price:价格,create_time:新品 sortSort: true, //排序是否升序 page: 1, //分页 }, onLoad: function (options) { this.onPullDownRefresh(); app.countRecord() }, onShow: function () { if(this.data.list.length <= 0 && !this.data.isLoading){ this.onPullDownRefresh(); } app.changeTabBarBadge(utils.getStorageSync(storeKeys.CARETOTAl)); app.countRecordPublic(5); }, onPullDownRefresh: function () { wx.stopPullDownRefresh(); this.data.page = 1; this.getPageData(); }, onReachBottom: function () { if (this.data.dataObj.current_page < this.data.dataObj.last_page){ this.data.page++; this.getPageData(); } }, //筛选切换 changeNav: utils.throttle(function (e) { let type = e.currentTarget.dataset.type; let sortStyle = this.data.sortStyle; if(type == sortStyle){ if(type == "price"){ let sortSort = !this.data.sortSort this.setData({ sortSort }) } }else{ let sortSort = true; if(type == "price"){ sortSort = false; } this.setData({ sortStyle: type, sortSort }) } this.onPullDownRefresh(); },500), navigateTo: utils.throttle(function (e) { app.navigateTo(e); }), //添加购物车 addCart: utils.throttle(function (e) { if (!utils.getStorageSync(storeKeys.TOKEN)) { wx.navigateTo({ url: "/pages/tabBar/login/login" }) return; } let id = e.currentTarget.dataset.id; let stock_total = e.currentTarget.dataset.stock || 0; //库存不足 if(stock_total <= 0){ utils.toast("当前商品暂时缺货") return; } let staffUserId = utils.getStorageSync(storeKeys.SHAREID) || utils.getStorageSync(storeKeys.APPLESID) || 0; let data = { "goodsId": id, //商品ID "goodsSkuId": "", //商品规格ID "goodsNum": 1, //购买数量,不能小于1 staffUserId: parseInt(staffUserId) } wx.showLoading({ title: "加载中" }); http.request({ url: api.URL + '/api/cart/add', method: 'POST', data, token: utils.getStorageSync(storeKeys.TOKEN), success: (res) => { wx.hideLoading(); utils.setStorageSync(storeKeys.CARETOTAl, res.data.data.cartTotal); app.changeTabBarBadge(utils.getStorageSync(storeKeys.CARETOTAl)); utils.toast(res.data.message) }, error: (res) => { wx.hideLoading(); utils.toast(res.data.message) }, }) }), //获取页面数据 getPageData(noLoading) { wx.showLoading({ title: "加载中", mask: true }); http.request({ url: api.URL + "/api/goods/list?sortStyle=" + this.data.sortStyle + "&sortSort=" + this.data.sortSort + "&page=" + this.data.page, token: utils.getStorageSync(storeKeys.TOKEN), method: 'GET', success: (res) => { let list = []; let tmpList = res.data.data.list.data; if (this.data.page == 1) { list = tmpList; } else { list = this.data.list.concat(tmpList); } this.setData({ list, dataObj: res.data.data.list }); setTimeout(() => { this.setData({ isLoading: false }); }, 300) wx.hideLoading(); }, error: (res) => { this.setData({ isLoading: false }); wx.hideLoading(); } }) }, onShareAppMessage: function (e) { return { title: app.globalData.shareTitle, imageUrl: app.globalData.shareImage } }, onShareTimeline: function (e) { return { title: app.globalData.shareTitle, imageUrl: app.globalData.shareImage } } })