attract.js 2.9 KB
// pages/attract/attract.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    policyList: [
      // {
      //   "id": "",
      //   "name": "全部",
      //   "weigh": "权重",
      //   "status": "状态"
      // }
    ],
    navScrollLeft:0,
    currentTab: 0,
    page: 1,
    list:[],
    ifData:!1,
    noMore:!1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          pixelRatio: res.pixelRatio,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth,
          tabWidth: res.windowWidth -  39
        })
      }
    }),this.policyType();
  },
  policyType(){
    let url = '/api/Article/category',a = this;
    app.post(url,{header: true}).then((res)=>{
      a.setData({ 
        policyList:res,
        category_id: res[0].id
      })
      a.getList(res[0].id,1)
    })
  },
  chooseLocation(){
    wx.chooseLocation({
      success: function(res) {},
    })
  },
  switchTab(e){
    let b = this,singlewidth = b.data.tabWidth/5,cur = e.currentTarget.dataset.current;
    b.setData({
      navScrollLeft:(cur-2)*singlewidth,
      category_id: e.currentTarget.id,
      page:1,
      list:[]
    }),b.data.currentTab != cur?b.setData({
      currentTab: cur
      }) : '', b.getList(e.currentTarget.id,1)
  },
  getList(id,page){
    let e = this, url = '/api/Article/index',params = {
      category_id: id,
      page: page,
      header: true
    };app.post(url,params).then((res)=>{
      e.setData({
        list: e.data.list.concat(res.data),
        page: res.current_page,
        lastPage: res.last_page,
        ifData: !0,
        noMore: res.last_page==res.per_page?!0:!1
      })
      !e.data.list.concat(res.data).length?e.setData({
        ifData:!1
      }):''
      wx.stopPullDownRefresh();
    })
  },
  bannerDetail(e){
    wx.navigateTo({
      url: '/pages/attract/detail/detail?id='+e.currentTarget.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成  v   
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let t = this;
    t.setData({
      page:1,
      list:[]
    })
    t.getList(t.data.category_id,1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let t = this;
    !t.data.noMore ? (t.setData({
      page:t.dat.page+1
    }),t.getList(t.data.category_id,t.data.page)) : wx.showToast({
      title: '没有更多了',
    }) 
  },

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

  }
})