index.js 3.86 KB
import {
  kfMixin,
} from '~/mixins/index'
Page({
  behaviors: [kfMixin],
  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    goodsIndex: 0,
    customerlist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getHomeConfig()
    this.getActivityList()
  },
  getActivityList() {
    wx.p.wxApi.showLoading()
    let total = 0
    let {
      activityList,
      pageNum
    } = this.data
    wx.p.api.homeApi.getActivityList({
        currentPage: pageNum,
        type: 2,
        activityStatusList: [1, 2]
      })
      .then(data => {
        total = data.total
        this.setData({
          total,
          activityList: pageNum > 1 ? [...activityList, ...data.list] : data.list // 防止多次请求 数据重复 page不变不concat数据
        })
        this.getClassifyList(pageNum > 1 ? [...activityList, ...data.list] : data.list)
      })
  },
  getClassifyList(list) {
    wx.p.api.homeApi.getClassifyList().then(data => {
      let tab = []
      let classifyList = []
      if (list.length) {
        data.unshift({
          classifyName: '秒杀',
          goodsList: list
        })
      }
      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)
              })
              obj = {
                goodsData: arr
              }
            } else {
              val.classifyName = e.classifyName
              arr.push(val)
              obj = {
                activityData: arr
              }
            }
          })
          classifyList.push(obj)
          tab.push(e.classifyName)
        }
      })
      this.setData({
        goodsFlag: true,
        classifyList,
        tab
      })
      wx.hideLoading()
    })
  },
  //获取banner
  getHomeConfig(refresh) {
    wx.p.api.homeApi.getHomeConfig().then(data => {
      let {
        noticeContent
      } = data
      //在线客服
      let customerlist = []
      if (data.customerServerName && data.customerServerOrCode) {
        customerlist.push({
          staffName: data.customerServerName,
          qrCode: data.customerServerOrCode,
          thumbAvatar: data.customerServerAvatar,
          staffPhone: data.customerServerPhone
        })
      }
      let bannerList = []
      if (data.bannerList && data.bannerList.length) bannerList = data.bannerList
      this.setData({
        noticeContent,
        bannerList
      })
      this.filterKfList(data)
      if (refresh) this.selectComponent('#channels').init(false)
    })
  },
  changeTab(e) {
    this.setData({
      goodsIndex: e.detail
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

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

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      noticeContent: ''
    })
    this.getHomeConfig(true)
    this.getActivityList()
    this.selectComponent('#storeQuity').onPullDownRefresh()
  },
  onReachBottom() {
    let {
      pageNum,
      total
    } = this.data
    wx.p.util.onReachBottom({
      pageNum,
      total,
    }, () => {
      this.getActivityList()
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
})