微信小程序实现收藏功能

话不多说,直接上代码

js

data:{
    isFavorite: false,
},
  //收藏 
  collect: function(options) {
    console.log(options)
    var that = this;
    var openid = wx.getStorageSync('openid');
    var isFavorite = this.data.isFavorite;
    var id = options.currentTarget.dataset.id
    if (!isFavorite) {
      var roomId = that.data.roomId;
      that.setData({
        isFavorite: true
      })
      wx.showToast({
        title: '收藏成功',
        icon: 'none'
      })
      // 收藏接口
      wx.request({
        url: ********",
        method: 'post',
        data: {
          openid: openid,
          id: this.data.id,
        },
        success(res) {
          that.setData({
            code:10000
          })
        }
      })
    }
    //取消收藏
    else if (isFavorite) {
      var roomId = that.data.roomId;
      that.setData({
        isFavorite: false
      })
      wx.showToast({
        title: '取消成功',
        icon: 'none'
      })
      // 取消收藏
      wx.request({
        url:********",
        method: 'post',
        data: {
          openid: openid,
          id: this.data.id,
        },
        success(res) {
          if (res.data.code == 1) {
            that.setData({})
          }
        }
      })
    }
  },

wxml

  
    
      
    
    收藏
  

你可能感兴趣的:(微信小程序,javascript,前端)