let util = require('../../utils/util') let api = require('../../config/api') let app = getApp() Page({ data: { leftMenu: [], menuId: 0, // 左侧目录当前选中 toView: 'menu0', // 滚动到的目录 loading: false, goodsList: [], // 当前分类列表 name: '', page: 1, totalPages: 1 }, onShow() { if (!this.data.leftMenu.length) this.getLeftMenu() else { if (app.globalData.menuId) this.setData({ menuId: app.globalData.menuId, toView: `menu${app.globalData.menuId}` }) this.getCategoryGoods() } }, // 获取左侧目录 getLeftMenu() { util.showLoad('加载中...') util.request(api.CatalogList).then(res => { util.hideLoad() if (res.errno === 0) { let menuId = res.data.categoryList.length && res.data.categoryList[0].id || 0 if (app.globalData.menuId) menuId = app.globalData.menuId this.setData({ leftMenu: res.data.categoryList, menuId, toView: menuId && `menu${menuId}` || "menu0" }) this.getCategoryGoods() } else util.showErrorToast(res.errmsg) }).catch(() => { util.hideLoad() util.showErrorToast('网络连接失败') }) }, // 获取分类商品 getCategoryGoods() { const filter = { page: this.data.page, categoryId: this.data.menuId, name: this.data.name } util.showLoad('加载中...') util.request(api.CatalogGoodsList, filter).then(res => { util.hideLoad() if (res.errno === 0) { const list = res.data.goodsList.map(item => { item.picUrl = item.picUrl.split(',') return item }) const goodsList = this.data.page == 1 ? list : this.data.goodsList.concat(list) this.setData({ totalPages: res.data.totalPages, goodsList }) } else util.showErrorToast(res.errmsg) }).catch(() => { util.hideLoad() util.showErrorToast('网络连接失败') }) }, // 左侧切换分类 changeCategory({ currentTarget }) { const { id } = currentTarget.dataset this.setData({ menuId: id }) app.globalData.menuId = id this.getCategoryGoods() }, nameInput({ detail }) { this.setData({ page: 1, name: detail }) this.getCategoryGoods() }, pullDownRefresh() { this.setData({ page: 1, name: '', loading: true }) this.getCategoryGoods() this.setData({ loading: false }) }, // 加载更多 scrolltolower() { if (this.data.totalPages > this.data.page) { this.setData({ page: this.data.page + 1 }) this.getCategoryGoods() } else util.showToast('已经到底了') }, toDetail({ currentTarget }) { const { id } = currentTarget.dataset wx.navigateTo({ url: `/packageGoods/detail/detail?obj=${JSON.stringify({ id })}` }) } })