微信小程序基于本地緩存實(shí)現(xiàn)點(diǎn)贊功能的方法

本文實(shí)例講述了微信小程序基于本地緩存實(shí)現(xiàn)點(diǎn)贊功能的方法。分享給大家供大家參考,具體如下:

成都創(chuàng)新互聯(lián)公司是少有的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、營(yíng)銷型企業(yè)網(wǎng)站、微信小程序開(kāi)發(fā)、手機(jī)APP,開(kāi)發(fā)、制作、設(shè)計(jì)、買(mǎi)鏈接、推廣優(yōu)化一站式服務(wù)網(wǎng)絡(luò)公司,公司2013年成立,堅(jiān)持透明化,價(jià)格低,無(wú)套路經(jīng)營(yíng)理念。讓網(wǎng)頁(yè)驚喜每一位訪客多年來(lái)深受用戶好評(píng)

wxml中的寫(xiě)法

注意:

1. 使用wx:if="{{condition}}" wx:else實(shí)現(xiàn)圖標(biāo)的切換效果;
2. 為圖片綁定點(diǎn)擊事件bindtap="toCollect",兩個(gè)image標(biāo)簽都要綁定!

<image wx:if="{{collection}}" src="/images/icon/pic1.png" bindtap="toCollect"></image>
<image wx:else src="/images/icon/pic2.png" bindtap="toCollect"></image>

js中的寫(xiě)法:

Page({
 data: {
 },
 onLoad: function(option) {
  // 獲取接收到的id值
  var getId = option.id;
  // 讓接收到的id值傳遞到data:{}里面
  this.setData({
   currentId: getId
  });
  // 讀取所有的文章列表點(diǎn)贊緩存狀態(tài)
  var cache = wx.getStorageSync('cache_key');
  // 如果緩存狀態(tài)存在
  if (cache) {
   // 拿到所有緩存狀態(tài)中的1個(gè)
   var currentCache = cache[getId];
   // 把拿到的緩存狀態(tài)中的1個(gè)賦值給data中的collection,如果當(dāng)前文章沒(méi)有緩存狀態(tài),currentCache 的值就是 false,如果當(dāng)前文章的緩存存在,那么 currentCache 就是有值的,有值的說(shuō)明 currentCache 的值是 true
   this.setData({
    collection: currentCache
   })
  } else {
   // 如果所有的緩存狀態(tài)都不存在 就讓不存在的緩存存在
   var cache = {};
   // 既然所有的緩存都不存在,那么當(dāng)前這個(gè)文章點(diǎn)贊的緩存也不存在,我們可以把當(dāng)前這個(gè)文章點(diǎn)贊的緩存值設(shè)置為 false
   cache[getId] = false;
   // 把設(shè)置的當(dāng)前文章點(diǎn)贊放在整體的緩存中
   wx.setStorageSync('cache_key',cache);
  }
 },
 // 點(diǎn)擊圖片的點(diǎn)贊事件 這里使用的是同步的方式
 toCollect: function(event) {
  // 獲取所有的緩存
  var cache = wx.getStorageSync('cache_key');
  // 獲取當(dāng)前文章是否被點(diǎn)贊的緩存
  var currentCache = cache[this.data.currentId];
  // 取反,點(diǎn)贊的變成未點(diǎn)贊 未點(diǎn)贊的變成點(diǎn)贊
  currentCache = !currentCache;
  // 更新cache中的對(duì)應(yīng)的1個(gè)的緩存值,使其等于當(dāng)前取反的緩存值
  cache[this.data.currentId] = currentCache;
  // 重新設(shè)置緩存
  wx.setStorageSync('cache_key',cache);
  // 更新數(shù)據(jù)綁定,從而切換圖片
  this.setData({
   // collection 默認(rèn)的是 false
   collection: currentCache
  });
  // 交互反饋
  wx.showToast({
   title: currentCache?'點(diǎn)贊':'取消',
   icon: 'success',
   duration: 2000
  });
 }
})

js中操作反饋—wx.showModal的寫(xiě)法:

// 點(diǎn)擊圖片的點(diǎn)贊事件 這里使用的是同步的方式
toCollect: function(event) {
  // 獲取緩存,得到當(dāng)前文章是否被點(diǎn)贊
  var cache = wx.getStorageSync('cache_key');
  // 獲取當(dāng)前文章是否被點(diǎn)贊的緩存
  var currentCache = cache[this.data.currentId];
  // 取反,點(diǎn)贊的變成未點(diǎn)贊 未點(diǎn)贊的變成點(diǎn)贊
  currentCache = !currentCache;
  // 更新cache中的對(duì)應(yīng)的1個(gè)的緩存值,使其等于當(dāng)前取反的緩存值
  cache[this.data.currentId] = currentCache;
  // 調(diào)用 showModal方法
  this.showModal(cache,currentCache);
},
showModal: function(cache,currentCache) {
 var that = this;
 wx.showModal({
  title: "點(diǎn)贊"
  content: currentCache?"要點(diǎn)贊嗎?":"要取消贊嗎?",
  showCancel: "true",
  cancelText: "取消",
  cancelColor: "#666",
  confirmText: "確定",
  confirmColor: "#222",
  success: function(res) {
   if (res.confirm) {
    // 重新設(shè)置緩存
    wx.setStorageSync('cache_key',cache);
    // 更新數(shù)據(jù)綁定,從而切換圖片
    that.setData({
     collection: currentCache
    })
   }
  }
 })
}

希望本文所述對(duì)大家微信小程序開(kāi)發(fā)有所幫助。

本文標(biāo)題:微信小程序基于本地緩存實(shí)現(xiàn)點(diǎn)贊功能的方法
地址分享:http://bm7419.com/article44/jdsohe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、域名注冊(cè)、網(wǎng)站策劃、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)