index.js 2.13 KB
// pages/tab/select/index.js
// import { strategyList } from "./constant/constant";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    strategyList: [],
    categoryList: [],
    currentCateIndex: 0,
    toCate: 'to-0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCategoryList()
    this.shareTitle = "攻略专区"
  },

  // navbarReady(e) {
  //   let {
  //     height
  //   } = e.detail
  //   this.setData({
  //     user_box_style: `padding-top:${height + 15}px`
  //   })
  // },
  // 获取分类列表
  getCategoryList() {
    wx.p.api.strategyApi.getStrategyCategory({}).then(res => {
      if (res && res.list && res.list.length) {
        let setdataobj = { categoryList: res.list }
        if (this.fresh) {
          // 防止下拉刷新时,分类已被置顶乱序
          const currentIndx = res.list.findIndex(item => item.id == this.currentCategoryId)
          setdataobj.currentCateIndex = currentIndx
          setdataobj.toCate = `to-${currentIndx}`
        } else {
          this.currentCategoryId = res.list[0].id
        }
        this.setData(setdataobj, () => {
          this.getStrategyList();
        })
      }
    })
  },
  selectCate(e) {
    const { index, item } = e.currentTarget.dataset
    this.currentCategoryId = item.id
    this.getStrategyList();
    this.setData({
      currentCateIndex: index,
      toCate: `to-${index}`
    })
  },

  getStrategyList() {
    wx.p.api.strategyApi.getStrategyList({
      pageNum: 1,
      pageSize: 99999,
      categoryId: this.currentCategoryId
    }).then(res => {
      this.setData({
        strategyList: res.list,
      })
      if (this.fresh) {
        wx.stopPullDownRefresh()
      }
    });
  },
  selectStrategy(e) {
    const { id } = e.currentTarget.dataset.item
    wx.p.router.go('/packageStrategy/pages/strategyDetail/index', {
      id,
      notIntercept: true
    })
  },
  onShareAppMessage() { },
  onShow() {
    const FIRST = wx.p.storage.get('first_enter_miniprograme')
    this.setData({
      showStore: !FIRST
    })
  },
  onPullDownRefresh() {
    this.fresh = true
    this.getCategoryList()
  },
});