// pages/mine/product/productmanage/productlist.js const app = getApp() Page({ /** * 页面的初始数据 */ data: { CustomBar: app.globalData.CustomBar, productList: [], isRefreshing: false, currentPage: 1, totalSize: 0, shopId: '', isAddPage: false, isCatalogPage: false, activeNames: ['0'], baseImageUrl: app.urls.baseImgUrl, currentCatalogId: '', curIndex: 0, active: 1, curGoodsList: [], contentHeight: app.globalData.windowHeight - app.globalData.CustomBar, isLoadMore: false, hasMore: true }, /** * 切换类目 */ switchRightTap(e) { let curindex = parseInt(e.currentTarget.dataset.curindex); this.data.productList[curindex].goodsList = [] this.setData({ currentPage: 1, hasMore: true, isLoadMore: false, curIndex: curindex }) let item = this.data.productList[curindex] this.getGoodsListByCId(item.categoryId) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.setData({ shopId: options.shopId }) this.getShopCatalogList(this.data.shopId) }, //获取店铺所有商品类目 getShopCatalogList(id) { let _self = this wx.showLoading({ title: '加载中...', }) app.http.get(app.urls.getShopCatalogList.format({ shopId: id }), { hedaer: { token: app.globalData.token } }) .then(res => { wx.hideLoading({}) _self.setData({ isRefreshing: false }) _self.buildDatas(res.data) }) .catch(err => { wx.hideLoading({}) }) }, doAdd(e) { let key = e.currentTarget.dataset.key if ('chooseCatalog' == key) { wx.navigateTo({ url: './choosecatalog?shopId=' + this.data.shopId, }) } else { if (e.currentTarget.dataset.item) { wx.navigateTo({ url: './addproduct?shopId=' + this.data.shopId + '&cataLogId=' + e.currentTarget.dataset.item.categoryId, }) } else { wx.showToast({ title: '请先添加类目', icon: 'error' }) } } }, manageGoods(e) { wx.navigateTo({ url: './productmanage?shopId=' + e.currentTarget.dataset.item.shopId + '&shopGoodsId=' + e.currentTarget.dataset.item.goodsId, }) }, /** * 获取商品列表 */ getGoodsList(shopId) { wx.showLoading({ title: '加载中...', }) app.htt.get(app.urls.getShopGoodsList.format({ shopId: shopId }), { header: { token: app.globalData.token } }) .then(res => { wx.hideLoading({}) this.setData({ isRefreshing: false }) this.buildDatas(res.data) }) .catch(err => { wx.hideLoading({}) }) }, //构建数据 buildDatas(data) { let _self = this if (data.length > 0) { data.forEach(el => { el.goodsList = [] _self.data.productList.push(el) }) _self.setData({ curIndex: 0, productList: _self.data.productList }) _self.getGoodsListByCId(_self.data.productList[0].categoryId) } }, //去除重复的 unique(rows) { let temp = rows.concat() let tempList = [] for (var i = 0; i < temp.length; i++) { for (var j = i + 1; j < temp.length; j++) { if (temp[i].categoryId == temp[j].categoryId) { temp.splice(j, 1); j-- } } } temp.forEach(el => { tempList.push({ name: el.categoryName, id: el.categoryId, goods: [] }) }) return tempList }, //刷新页面 dorefreshList() { this.setData({ currentPage: 1, isLoadMore: false, hasMore: true, productList: [] }) this.getShopCatalogList(this.data.shopId) }, onShow() { if (this.data.isAddPage) { this.setData({ isAddPage: false }) this.dorefreshList() } }, //根据类目ID获取商品 getGoodsListByCId(id) { let _self = this wx.showLoading({ title: '加载中...', }) app.http.get(app.urls.getGoodsByCatalogAndShop.format({ categoryId: id }), { header: { token: app.globalData.token }, data: { page: _self.data.currentPage, shopId: _self.data.shopId } }) .then(res => { wx.hideLoading({}) _self.setData({ isLoadMore: false }) if (res.data.rows.length > 0) { _self.data.productList.forEach(el => { if (el.categoryId == id) { el.goodsList = el.goodsList.concat(res.data.rows) } }) _self.setData({ productList: _self.data.productList }) } else { _self.setData({ hasMore: false }) } }) .catch(err => { wx.hideLoading({}) _self.setData({ isLoadMore: false }) }) }, onChange(e) { let _self = this let item = e.currentTarget.dataset.item console.log(item) this.setData({ activeNames: e.detail, currentCatalogId: item.categoryId }) console.log(e) if (e.detail.length > 1) { //展开 if (item.goodsList || item.goodsList.length <= 0) { _self.getGoodsListByCId(item.categoryId) } } }, doSearchGoods() { wx.navigateTo({ url: './searchproduct?shopId=' + this.data.shopId, }) }, //加载更多 doLoadMore(e) { let _self = this if (_self.data.hasMore) { if (_self.data.isLoadMore) { return } let item = _self.data.productList[_self.data.curIndex] _self.setData({ isLoadMore: true, currentPage: ++_self.data.currentPage }) _self.getGoodsListByCId(item.categoryId) } else { wx.showToast({ title: '暂无更多数据', icon: 'none', duration: 500 }) } }, doDelCategory(e) { let _self = this wx.showLoading({ title: '删除中...', }) app.http.delete(app.urls.doDelShopCategoryById.format({ ids: e.currentTarget.dataset.item.categoryId }), { header: { token: app.globalData.token } }) .then(res => { wx.hideLoading({}) _self.setData({ productList: [] }) wx.showToast({ title: '删除成功', icon: 'success' }) _self.getShopCatalogList(_self.data.shopId) }) .catch(err => { wx.hideLoading({}) }) }, showConfirmDialog(e) { let _self = this wx.showModal({ title: '提示', content: '确定要删除该类目吗?(删除后该类目下所有商品将删除!)', success(res) { if (res.confirm) { _self.doDelCategory(e) } } }) } })