index.js 4.88 KB
// pages/tab/mall/index.js
const defaultClassify = ['今日推荐', '新酒预售', '口粮特选', '店铺爆款', '寰球名庄', '红葡萄酒', '白葡萄酒', '雷司令', '起泡酒', '果汁', '西班牙', '南非', '意大利', '德国']
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tagIndex: 0,
    tabList: [],
    goodsList: []
  },
  pageNum: 1,
  total: 0,
  async search(e) {
    let value = e.detail
    let {
      goodsList,
      tagIndex
    } = this.data
    if (tagIndex == 0) {
      if (this.goodsStorageList) this.jumpSearch(this.goodsStorageList, value)
      else if (goodsList.length < this.total) {
        this.getGoodsList(res => {
          this.goodsStorageList = res
          this.jumpSearch(res, value)
        })
      } else this.jumpSearch(goodsList, value)
    } else this.jumpSearch(goodsList, value)
  },
  jumpSearch(goodsList, value) {
    wx.p.router.go('/packageMall/pages/search/search', {
      value,
      goodsList,
      notIntercept: true
    })
    this.setData({
      searchVal: ''
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getMerchantClassify()
    this.getGoodsList()
  },
  async getMerchantClassify() {
    wx.p.wxApi.showLoading('加载中...')
    try {
      const {
        merchantId
      } = wx.p.storage.get('merchant_info'),
        res = await wx.p.api.shopmallApi.getMerchantClassify({
          merchantId
        })
      let tabList = [{
        zoneName: '全部',
        id: '',
        zoneImg: `mall/default-banner.png`
      }]
      res.list.map(e => {
        let tab = {
          zoneName: e.classifyName,
          id: e.id,
          zoneImg: `mall/${e.classifyName}.png`
        }
        if (!defaultClassify.includes(e.classifyName)) tab.zoneImg = `mall/default-banner.png`
        tabList.push(tab)
      })
      this.setData({
        tabList,
        flag: true
      })
      wx.hideLoading()
    } catch (error) {
      wx.p.wxApi.showToast(error)
    }
  },
  getGoodsList(callBack) {
    wx.hideLoading()
    const {
      tagIndex
    } = this.data
    if (tagIndex != 0) return
    wx.p.wxApi.showLoading('加载中...')
    let params = {
      pageSize: callBack ? 99999 : 10,
      pageNum: callBack ? 1 : this.pageNum
    }
    wx.p.api.shopmallApi.getGoodsList(params).then(({
      list,
      total
    }) => {
      if (callBack) callBack(list)
      else {
        let {
          goodsList
        } = this.data
        this.setData({
          goodsList: goodsList.concat(list),
          goodsFlag: true
        })
        this.total = total
      }
      wx.hideLoading()

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let {
      tabList,
      tagIndex
    } = this.data
    if (tabList.length == 0) return
    this.changeData(tagIndex)
  },
  changeData(index) {
    this.setData({
      tagIndex: index,
      goodsList: [],
      goodsFlag: false,
      nothing: false
    })
    this.pageNum = 1
    const {
      tabList
    } = this.data
    if (tabList[index].id) this.getMerchantClassifyGoods(tabList[index].id)
    else this.getGoodsList()
  },
  async getMerchantClassifyGoods(id) {
    wx.p.wxApi.showLoading('加载中...')
    try {
      const {
        merchantId
      } = wx.p.storage.get('merchant_info'), {
        goodsRefList
      } = await wx.p.api.shopmallApi.getMerchantClassifyGoods({
        id,
        merchantId
      })
      let goodsList = []
      if (goodsRefList && goodsRefList.length) {
        goodsRefList.map(e => {
          e.goods.goodsStatistics = e.goodsStatistics
          goodsList.push(e.goods)
        })
      }
      this.setData({
        goodsList,
        goodsFlag: true
      })
      wx.hideLoading()
    } catch (error) {

    }
  },
  goShoppingCart() {
    wx.p.router.go('/packageMall/pages/shoppingCart/shoppingCart')
  },
  tabclick(e) {
    let {
      index
    } = e.detail
    let {
      tagIndex
    } = this.data
    if (index == tagIndex) return
    this.changeData(index)
    this.goodsStorageList = null
  },
  viewScrollTolower() {
    const {
      tagIndex
    } = this.data
    if (tagIndex == 0) {
      let {
        pageNum,
        total
      } = this
      wx.p.util.onReachBottom({
        pageNum,
        total,
        _this: this
      }, () => {
        this.pageNum++
        this.getGoodsList()
      })
    }
  },
  onTabItemTap(e) {
    this.goodsStorageList = null
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})