pinpai.js 2.8 KB
// pages/myblock/jiupin/jiupin.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    noData: true,
    noMore: false,
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  collect(e) {
    let id = e.currentTarget.id, url = '/index/Common/favorite', index = e.currentTarget.dataset.index, params = {
      sort: 2,
      id: id
    };
    app.post(url, params).then(r => {
      this.data.list.splice(index,1);
        wx.showToast({
          title: '操作成功',
          icon:'none',
          duration: 1300
        })
        this.setData({
          list: this.data.list
        })
    })
  },
  getList() {
    let url = 'user/Center/favorite_brand', params = {
      page: this.data.page
    };
    app.post(url, params).then(r => {
      var list = this.data.page == 1 ? r.list : this.data.list.concat(r.list);
      this.setData({
        list: list,
        totalPage: r.total_page,
        noData: list.length > 0 ? true : false,
        noMore: r.totalPage == r.page ? !0 : !1
      })
    })
  },

  //品牌详情
  pinpaijump(e) {
    let id = e.currentTarget.dataset.id;
    console.log(id)
    wx.navigateTo({
      url: '/pages/homeblock/brandlist/brandlist?id=' + id,
    })
  },

  //品牌收藏和取消收藏
  brandcang(e) {
    let id = e.currentTarget.dataset.id;
    let that = this;
    let url = 'index/Common/favorite', params = {
      id: id,
      sort: 2

    }
    app.post(url, params).then((res) => {
      console.log(res);
      wx.showToast({
        title: res,
        icon: "none"
      })

      let newlist = that.data.list;
      for (var obj of newlist) {
        if (obj.id == id) {
          if (obj.is_favorite == 0) {
            obj.is_favorite = 1
          } else {
            obj.is_favorite = 0
          }
        }
      }

      that.setData({
        list: newlist
      })





    }).catch((err) => {

    })

  },

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

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

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

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

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    !this.data.noMore ? (this.setData({
      page: page + 1
    }), this.list()) : wx.showToast({
      title: '没有更多了',
      icon: 'none'
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})