index.js 4.41 KB
// pages/tab/mall/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tagIndex: 0,
    tabList: []
  },
  navbarReady(e) {
    console.log(e)
    let {
      height
    } = e.detail
    this.setData({
      navHeight: height,
    })
  },
  search(e) {
    let value = e.detail
    let {
      goodsList
    } = this.data
    wx.p.router.go('/packageMall/pages/search/search', {
      value,
      goodsList
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.getZoneInfo()
    this.getClassifyList()
  },
  getClassifyList(tagIndex) {
    wx.p.wxApi.showLoading('加载中...')
    wx.p.api.homeApi.getClassifyList().then(data => {
      let tabList = [{
        zoneName: '全部',
        id: '',
        zoneImg: `mall/default-banner.png`
      }]
      let classifyList = []
      data.map(e => {
        e.goodsList &&
          e.goodsList.length &&
          e.goodsList.map(i => {
            i &&
              i.length &&
              i.map(c => {
                c.goods.goodsStatistics = c.goodsStatistics
                c.goods.id = c.goodsId
              })
          })
        if (e.goodsList && e.goodsList.length) {
          e.count = 0
          // 处理数据
          let [arr, obj] = [
            [], {}
          ]
          e.goodsList.forEach((val, id) => {
            if (val instanceof Array) {
              val.forEach((item, index) => {
                arr.push(item.goods)
              })
              obj = {
                goodsData: arr,
                id: e.id
              }
            } else {
              val.classifyName = e.classifyName
              arr.push(val)
              obj = {
                activityData: arr,
                id: e.id
              }
            }
          })
          classifyList.push(obj)
          tabList.push({
            zoneName: e.classifyName,
            id: e.id
          })
        }
      })
      this.setData({
        classifyList,
        tabList,
        flag: true
      })
      if (tagIndex) this.changeData(tagIndex)
      this.getGoodsList()
    })
  },
  getZoneInfo() {
    wx.p.api.discoveryApi.getZoneInfo(2).then(data => {
      let tabList = [{
        zoneName: '门店自营',
        id: '',
        zoneImg: `mall/default-banner.png`
      }]
      data.map(e => tabList.push(e))
      this.setData({
        tabList,
        flag: true
      })
      this.getGoodsList()
    })
  },
  getGoodsList(zoneId) {
    wx.hideLoading()
    const {
      tagIndex
    } = this.data
    if (tagIndex != 0) return
    wx.p.wxApi.showLoading('加载中...')
    let params = {}
    if (zoneId) params.zoneId = zoneId
    wx.p.api.shopmallApi.getGoodsList(params).then(({
      list
    }) => {
      this.setData({
        goodsList: list,
        goodsFlag: true
      })
      wx.hideLoading()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let {
      tabList,
      tagIndex
    } = this.data
    this.setData({
      goodsList: [],
      goodsFlag: false
    })
    if (tabList.length == 0) return
    this.getClassifyList(tagIndex)
  },
  changeData(index) {
    const {
      classifyList,
      tabList
    } = this.data
    if (tabList[index].id) {
      classifyList.map(i => {
        if (i.id == tabList[index].id) this.setData({
          goodsList: i.goodsData,
          goodsFlag: true
        })
      })
    } else this.getGoodsList()
  },
  goShoppingCart() {
    wx.p.router.go('/packageMall/pages/shoppingCart/shoppingCart')
  },
  tabclick(e) {
    let {
      index
    } = e.detail
    let {
      tagIndex
    } = this.data
    if (index == tagIndex) return
    this.setData({
      tagIndex: index,
      goodsList: [],
      goodsFlag: false
    })
    this.changeData(index)
  },
  search(e) {
    let value = e.detail
    let {
      goodsList
    } = this.data
    wx.p.router.go('/packageMall/pages/search/search', {
      value,
      goodsList
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  },

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

  }
})