您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)怎么在小程序中實(shí)現(xiàn)一個(gè)點(diǎn)贊收藏功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
具體如下:
/收藏/
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, }); }); } },
/點(diǎn)贊/
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: "點(diǎn)贊取消", 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 ? "點(diǎn)贊成功" : "點(diǎn)贊失敗", icon: 'success', duration: 1000 }); itemDetail.liked++; this.setData({ itemDetail: itemDetail }); this.setData({ isLike: true, }); }); } },
取消點(diǎn)贊功能
/*更新點(diǎn)贊*/ update_zan:function(e){ var that = this; var data = e.currentTarget.dataset; var mid = data.mid; var cookie_mid = wx.getStorageSync('zan') || [];//獲取全部點(diǎn)贊的mid var isadd = 1; var newmessage = []; if (cookie_mid.includes(mid)){//說明已經(jīng)點(diǎn)過贊,取消贊 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 }) } })
重點(diǎn)注意代碼:
(1)要把所有已經(jīng)點(diǎn)過贊的留言id給存儲(chǔ)到本地緩存之中,這樣如果重復(fù)點(diǎn)贊的,就可以判斷出來,并取消點(diǎn)贊,取消點(diǎn)贊的同時(shí)要去掉緩存中的id;
(2)新增點(diǎn)贊和取消點(diǎn)贊用到同一個(gè)api,通過isadd=1或者isadd=0來判斷是新增還是取消點(diǎn)贊
(3)為了不頻繁去讀取留言列表,可以直接調(diào)用this.data.message,并通過循環(huán)遍歷改變某個(gè)留言的點(diǎn)贊數(shù),然后直接重新賦值,就能動(dòng)態(tài)改變點(diǎn)贊數(shù)!
看完上述內(nèi)容,你們對(duì)怎么在小程序中實(shí)現(xiàn)一個(gè)點(diǎn)贊收藏功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。