index.js 3.06 KB
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabBarList: [{
      name: '工作台',
      iconPath: '/packageStoreAssistant/images/icon_activity.png',
      selectedIconPath: '/packageStoreAssistant/images/icon_activity_active.png',
    }, {
      name: '发现',
      iconPath: '/packageStoreAssistant/images/icon_discovery.png',
      selectedIconPath: '/packageStoreAssistant/images/icon_discovery_active.png',
    }, {
      name: '我的',
      iconPath: '/packageStoreAssistant/images/icon_mine.png',
      selectedIconPath: '/packageStoreAssistant/images/icon_mine_active.png',
    }],
    tabIndex: 0,
    pageShow: new Array(3).fill(false)
  },

  onPageScroll({
    scrollTop
  }) {
    this.getComponentPage().onPageScroll && this.getComponentPage().onPageScroll(scrollTop)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onRoute: function (options) {
    if (!wx.$storage.get('userInfo')) wx.$router.go('/pages/Login/Login')
    const { tabIndex } = this.data
    if (options && options.tabIndex) {
      if (tabIndex == 0 && !this.pageShow) {
        this.setData({
          tabIndex: options.tabIndex,
          [`pageShow[${options.tabIndex}]`]: true
        })
        this.pageShow = true
      }
    } else
      this.setData({
        ['pageShow[0]']: true
      })
    this.getComponentPage().onLoad && this.getComponentPage().onLoad(options)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getComponentPage().onShow && this.getComponentPage().onShow()
  },
  getComponentPage() {
    const {
      tabIndex
    } = this.data
    return this.selectComponent(`#page-${tabIndex}`)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () { },

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

  changeTabBar(e) {
    let {
      pageShow
    } = this.data,
      tabIndex = e.detail, {
        loginData,
        options
      } = this
    this.setData({
      tabIndex,
      myTabIdx: e.myTabIdx
    })
    if (!pageShow[tabIndex]) {
      if (!loginData) this.onRoute()
      else this.getComponentPage().onLoad && this.getComponentPage().onLoad(loginData, options)
    }
    this.setData({
      [`pageShow[${tabIndex}]`]: true
    })
    this.onShow()
    // 修复核销扫码后进入此tab不刷新问题
    if (options && options.indexFresh) {
      this.options.indexFresh = null
      this.getComponentPage().onLoad && this.getComponentPage().onLoad(loginData, options)
    }
  },
  onShareAppMessage: function (options) {
    if (options.target && options.target.dataset.tab == 2) {
      const { merchantId } = wx.$storage.get("userInfo");
      const {channel} = options.target.dataset
      let path = '/packageDiscovery/pages/index/index'
      if (merchantId) path = `${path}?merchantId=${merchantId}&channel=${channel.channelType}`
      return {
        path,
        title: channel.promotion,
        imageUrl: channel.publicityImg
      }
    }
  },
})