homepage.js 3.1 KB
// pages/homepage/homepage.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    indicatorDots: false,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    currentSwiper: 0,
    categories_id: '',
    goodlist: [],
    page: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  swiperChange: function(e) {
    this.setData({
      currentSwiper: e.detail.current
    })
  },
  

  // 进入搜索页
  gosearch() {
    let token = wx.getStorageSync("token");
    if (token == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })

      setTimeout(function() {
        wx.navigateTo({
          url: '/pages/login/login',
        })
      }, 1500)
    } else {
      wx.navigateTo({
        url: '/pages/searchbox/searchbox',
      })
    }

  },


  // 获取首页数据
  getindexdata() {
    let that = this;
    let url = 'goods/get_all';
    let param = {
      keyword: that.data.keyword,
      categories_id: that.data.categories_id,
      page: that.data.page,
      pageNum: 10

    }
    app.post(url, param, 'post').then((res) => {
      console.log(res);
      that.setData({
        goodlist: that.data.goodlist.concat(res)
      })

    }).catch((err) => {

    })
  },

  //获取首页轮播图
  getbanner() {
    let that = this;
    let url = 'sundry/get_slide';
    let param = {


    }
    app.post(url, '', 'post').then((res) => {
      console.log(res);
     
      if(res.length>5){
        let newimgUrls = []
        for(var i=0;i<5;i++){
          newimgUrls.push(res[i])
        }

        that.setData({
          imgUrls:newimgUrls
        })

      }else{
        that.setData({
          imgUrls: res
        })
      }
      



    }).catch((err) => {

    })
  },

  //进入商品详情页
  gotogood(e) {
    let token = wx.getStorageSync("token");
    if (token == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })

      setTimeout(function() {
        wx.navigateTo({
          url: '/pages/login/login',
        })
      }, 1500)
    } else {
      let goodid = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/goodtail/goodtail?goodid=' + goodid,
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      page:1,
      goodlist:[]
    })
    this.getindexdata();
    this.getbanner()
  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let newpage = this.data.page;
    newpage++;
    this.setData({
      page: newpage
    })

    this.getindexdata();
  },

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

  }
})