源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

[JavaScript] 小程序点赞收藏功能的实现代码示例

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2018-9-7 14:14:34 | 显示全部楼层 |阅读模式
这篇文章主要介绍了小程序点赞收藏功能的实现代码示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

本文介绍了小程序点赞收藏功能的实现代码示例,分享给大家,具体如下:

/收藏/

focusFavoriteTab: function (e) {
  var that = this;
  var isFocus = that.data.isFocus;
  console.log("isFocus", isFocus)
  var itemId = that.data.itemId;
  if (isFocus) {
   favoriteService.cancel(that, itemId).then((res) => {
    wx.showToast({
     title: "取消收藏",
     icon: 'success',
     duration: 1000
    });
    this.setData({
     isFocus: false,
    });
   })
  } else {
   favoriteService.add(that, itemId).then((res) => {
    wx.showToast({
     title: res.data == 1 ? "收藏成功" : "收藏失败",
     icon: 'success',
     duration: 1000
    });
    this.setData({
     isFocus: true,
    });
   });
  }
 },

/点赞/

focuslikeTab: function (e) {
  var that = this;
  var isLike = that.data.isLike;
  let itemDetail = this.data.itemDetail 
  var itemId = that.data.itemId;
  if (isLike) {
   itemService.cancellike(that, itemId).then((res) => {
    wx.showToast({
     title: "点赞取消",
     icon: 'success',
     duration: 1000
    });
    itemDetail.liked--;
    this.setData({
     itemDetail:itemDetail
    });
    this.setData({
     isLike: false,
    });
   })
  } else {
   itemService.addlike(that, itemId).then((res) => {
    wx.showToast({
     title: res.data == 1 ? "点赞成功" : "点赞失败",
     icon: 'success',
     duration: 1000
    });
    itemDetail.liked++;
    this.setData({
     itemDetail: itemDetail
    });
    this.setData({
     isLike: true,
    });
   });
  }
 },

取消点赞功能

 /*更新点赞*/ 
 update_zan:function(e){ 
  var that = this; 
  var data = e.currentTarget.dataset; 
  var mid = data.mid; 
  var cookie_mid = wx.getStorageSync('zan') || [];//获取全部点赞的mid 
  var isadd = 1; 
  var newmessage = []; 
  if (cookie_mid.includes(mid)){//说明已经点过赞,取消赞 
   isadd = 0; 
   var m = 0; 
   for (var j in cookie_mid){ 
    if(cookie_mid[j] != mid){ 
     newmessage[m] = cookie_mid[j]; 
     m++ 
    } 
   } 
   wx.setStorageSync('zan', newmessage);//删除取消赞的mid 
  }else{ 
   cookie_mid.unshift(mid); 
   wx.setStorageSync('zan', cookie_mid);//新增赞的mid 
  } 
  wx.request({ 
   url: app.globalData.api.api_system, 
   data: { 
    action: 'zannum', 
    mid: mid, 
    isadd: isadd, 
    wxid: app.globalData.wxid 
   }, 
   method: 'GET', 
   // header: {},  
   success: function (res) { 
    var message = that.data.message; 
    for (var i in message) { 
     if(message[i].mid == mid){ 
      if(isadd){ 
       message[i].zan = parseInt(message[i].zan) + 1 
      }else{ 
       message[i].zan = parseInt(message[i].zan) - 1 
      } 
       
     } 
    } 
    that.setData({ 
     message: message 
    }) 
   } 
  }) 

重点注意代码:

(1)要把所有已经点过赞的留言id给存储到本地缓存之中,这样如果重复点赞的,就可以判断出来,并取消点赞,取消点赞的同时要去掉缓存中的id;

(2)新增点赞和取消点赞用到同一个api,通过isadd=1或者isadd=0来判断是新增还是取消点赞

(3)为了不频繁去读取留言列表,可以直接调用this.data.message,并通过循环遍历改变某个留言的点赞数,然后直接重新赋值,就能动态改变点赞数!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

回复

使用道具 举报

0

主题

1万

回帖

1万

积分

论坛元老

Rank: 8Rank: 8

积分
18256
发表于 2022-9-9 00:51:47 | 显示全部楼层
还不错啊
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2022-9-18 03:42:44 | 显示全部楼层
可以,看卡巴
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
24607
发表于 2022-12-12 22:52:52 | 显示全部楼层
人都不在了啊 啊
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25002
发表于 2023-8-22 10:35:37 | 显示全部楼层
飞飞飞飞飞飞飞飞飞飞飞飞飞
回复 支持 反对

使用道具 举报

3

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25245
发表于 2023-8-24 01:34:05 | 显示全部楼层
论坛有你更精彩!
TS人妖演出表演服务q3268336102电话13168842816
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
20242
发表于 2024-3-30 11:40:03 | 显示全部楼层
很不错的玩意
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25143
发表于 2024-8-1 09:24:30 | 显示全部楼层
来看看!!!
回复 支持 反对

使用道具 举报

3

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25104
发表于 2024-9-23 21:11:11 | 显示全部楼层
哟哟哟哟哟以偶
回复 支持 反对

使用道具 举报

13

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25254
发表于 2024-10-1 03:37:18 | 显示全部楼层
谢谢小Y分享
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2025-2-12 03:59 , Processed in 0.188851 second(s), 41 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表