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

  /**
   * 页面的初始数据
   */
  data: {
    goodkind: [],
    sel: 0,
    page: 1,
    categories_id: '',
    kindgoodlist: [],
    keyword: ''
  },

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

  },
  enterword(e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  sousuo(){
    if(this.data.keyword==''){
      wx.showToast({
        title: '请输入商品/关键词',
        icon:'none'
      })
    }else{
      this.setData({
        page:1,
        kindgoodlist:[]
      })

      this.getfenleikind()
    }
  },

  //获取分类
  getkind() {
    let that = this;
    var url = 'sundry/get_category';

    app.post(url, '').then((res) => {
      console.log(res);
      that.setData({
        goodkind: res,
        categories_id: res[0].id
      })

      that.getfenleikind()



    }).catch((err) => {

    })
  },

  //获取分类列表
  getfenleikind() {
    let that = this;
    var url = 'goods/get_all';
    var params = {
      keyword: that.data.keyword,
      categories_id: that.data.categories_id,
      page: that.data.page,
      pageNum: 10
    }
    app.post(url, params).then((res) => {
      console.log(res);
      that.setData({
        kindgoodlist: that.data.kindgoodlist.concat(res)
      })



    }).catch((err) => {

    })
  },

  goodtail(e) {
    let token = wx.getStorageSync("token");
    if (token == '') {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })

      setTimeout(function () {
        wx.navigateTo({
          url: '/pages/login/login',
        })
      }, 1500)
    }else{
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/goodtail/goodtail?goodid=' + id,
      })
    }
    
  },
  goodsel(e) {
    this.setData({
      sel: e.currentTarget.dataset.index,
      categories_id: e.currentTarget.dataset.id,
      page: 1,
      kindgoodlist: []
    })

    console.log(this.data.categories_id)

    this.getfenleikind()
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      kindgoodlist: []
    })
    this.getkind()
  },

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

  },

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

  },

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

  },

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

    })

    this.getfenleikind()
  },

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

  }
})