当前位置 : 主页 > 网络编程 > JavaScript >

加入收藏 简单的存在缓存中

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt /** * 加入收藏 //idlis存了refActivityId 组成的字符串,用来判断是否加入收藏; * //collectionNum存了总收藏的数量,用来收藏列表里用; * //collection 存了某一条帖子内容; */ toCo
gistfile1.txt
/**
   * 加入收藏 //idlis存了refActivityId 组成的字符串,用来判断是否加入收藏;
   *         //collectionNum存了总收藏的数量,用来收藏列表里用;
   *         //collection  存了某一条帖子内容;
   */
  toCollection:function(){
    var that = this;
    var idlist = wx.getStorageSync('idlist');
    if (idlist.indexOf(that.data.content.refActivityId) >= 0) {
      
      that.setData({
        iscoll: true
      })
      return
    }
    try {
      var value = wx.getStorageSync('collectionNum');
      idlist = idlist + that.data.content.refActivityId;
      if (value) {
        var num = value+1;
        
        
        try {
          var collection = 'collection' + num;
          wx.setStorageSync(collection, that.data.content);
          wx.setStorageSync('collectionNum', num);
          wx.setStorageSync('idlist', idlist);

          wx.showToast({
            title: '收藏成功',
            icon: 'success',
            duration: 2000
          })
        } catch (e) {
          wx.showToast({
            title: '收藏失败',
            icon: 'success',
            duration: 2000
          })
        }
      }
      else {
        try {
          wx.setStorageSync('collection1', that.data.content);
          wx.setStorageSync('collectionNum', 1);
          wx.setStorageSync('idlist', that.data.content.refActivityId);
          wx.showToast({
            title: '收藏成功',
            icon: 'success',
            duration: 2000
          })
        } catch (e) {
          wx.showToast({
            title: '收藏失败',
            icon: 'success',
            duration: 2000
          })
        }
      }
    } catch (e) {
      // Do something when catch error
    }
  },
网友评论