attract.js 4.5 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,
    imgUrls: [],
    indicatorDots: false,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    currentSwiper: 0,
    top:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          pixelRatio: res.pixelRatio,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth,
          tabWidth: res.windowWidth -  39
        })
      }
    }), this.policyType(), this.getBanner();
  },

  getUserInfo() {
    let d = this, url = '/api/user/index';
    app.post(url, {}).then((r) => {
      console.log(r.noticeNum)
      d.setData({ userInfo: r.userInfo, noticeNum: r.noticeNum })
      if (r.noticeNum != 0) {
        let num = r.noticeNum
        num = num.toString()
        wx.setTabBarBadge({
          index: 4,
          text: num
        })
      } else {
        wx.hideTabBarRedDot({
          index: 4,
        })
      }
    })
  },

  //获取轮播图
  getopbanner(){
    let that=this;
    let url = '/api/Article/getAttractInvestmentList';
    app.post(url, {}).then(r => {
      console.log(r)
      this.setData({
        imgUrls: r
      })


      if (r.length == 0) {
        console.log(73478)
        that.setData({
          top: '100rpx'
        })
      } else {
        console.log(345)

        that.setData({
          top: '390rpx'
        })
      }
    })

    console.log(this.data.imgUrls)
  },

  //轮播图
  swiperChange: function (e) {
    this.setData({
      currentSwiper: e.detail.current
    })
  },
  getBanner(){
    let url = '/api/Article/getAttractInvestmentBanner';
    app.post(url,{}).then(r=>{
      console.log(r)
      this.setData({
        banner: r.banner,
        
      })
    })
  },
  policyType(){
    let url = '/api/Article/category',a = this;
    app.post(url,{header: true}).then((res)=>{
      console.log(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
    }, list = page == 1 ? [] : t.data.list;app.post(url,params).then((res)=>{
      e.setData({
        list: list.concat(res.data),
        page: res.current_page,
        lastPage: res.last_page,
        ifData: !0,
        noMore: res.last_page==res.current_page?!0:!1
      })
      !e.data.list.concat(res.data).length?e.setData({
        ifData:!1
      }):''
      wx.stopPullDownRefresh();
    })
  },
  bannerDetail(e){
    let url = e.currentTarget.dataset.src
    console.log(url)
    wx.navigateTo({
      url: '/pages/attract/detail/detail?id='+e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成  v   
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getopbanner();
    this.getUserInfo()
  },

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

  },

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

  },

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

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

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

  }
})