最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信小程序本地存儲(chǔ)實(shí)現(xiàn)每日簽到、連續(xù)簽到功能

 更新時(shí)間:2019年10月09日 09:19:08   作者:云風(fēng)清  
這篇文章主要介紹了微信小程序本地存儲(chǔ)實(shí)現(xiàn)每日簽到、連續(xù)簽到功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

昨天在看自己寫的小程序項(xiàng)目,無(wú)意中打開(kāi)了CSDN APP,突然間覺(jué)得,我去,如果在小程序中加個(gè)“簽到”功能,豈不美哉?。ê冒?,其實(shí)是買的書昨天沒(méi)到貨,然后閑著無(wú)事,就想起了大明湖畔的“簽到”)

但是吧,又不想寫和服務(wù)器交互的,本著“簡(jiǎn)單點(diǎn)”的原則,我想起了曾經(jīng)的摯愛(ài)—— 本地存儲(chǔ) 。

先說(shuō)說(shuō)相關(guān)注意吧:

其一就是 storage中只能存放字符串!

我去,昨晚大部分時(shí)間都是在搞這個(gè)。以前一直認(rèn)為存放的是對(duì)象,興致勃勃寫完發(fā)現(xiàn)點(diǎn)擊以后出現(xiàn)了“NAN”…

覺(jué)得 事情并沒(méi)有這么簡(jiǎn)單。

仔細(xì)回去看了一下曾經(jīng)Vue寫過(guò)的localStorage,發(fā)現(xiàn)一直弄錯(cuò)了,應(yīng)該存放字符串!

搞清楚這個(gè)以后,又有一個(gè)問(wèn)題:你要“ 點(diǎn)擊加1 ”,這里總是把數(shù)字和字符串弄反,甚至想了用數(shù)組形式存放。。。最終想到了解決辦法:把存放的字符串轉(zhuǎn)為數(shù)字,加1后再轉(zhuǎn)為字符串存放到storage中 。

想到這我不禁喜形于色,終于可以了!

但是當(dāng)我無(wú)意中狂點(diǎn)16下的時(shí)候,我又哭了…

new Date()函數(shù)控制日期——一分鐘/一天/…只能點(diǎn)一次:

var D=(new Date()).getDate().toString();
 if(D != wx.getStorageSync('D')){ //判斷是否過(guò)了當(dāng)天
  //如果是新的一天,則...
 }else{
 //否則,例如:
  wx.showToast({
  title: '今日打卡已完成!',
  icon:'loading',
  duration:1200,
  mask:true
  })
 }

這里又出現(xiàn)一個(gè)問(wèn)題,我在當(dāng)前頁(yè)面開(kāi)始時(shí)onLoad里面加了一段代碼:把當(dāng)前時(shí)間存放到storage中,但是我發(fā)現(xiàn),這樣以后就點(diǎn)不了了(當(dāng)天),為什么?

因?yàn)闆_突了啊,加載頁(yè)面時(shí)存放此時(shí)時(shí)間,那么你如果在這個(gè)事件內(nèi)(本例:一天)去點(diǎn)擊,如上面代碼第一、二行,它不成立——都是“今天”,所以會(huì)執(zhí)行else語(yǔ)句。

解決辦法: 去掉onLoad函數(shù),這樣開(kāi)始執(zhí)行if時(shí)候會(huì)發(fā)現(xiàn)storage中沒(méi)有存儲(chǔ),也就“!=”了。

下面放上示例代碼:

hello.wxml

<view class="container">
 <view class="mxc1">
 <text>您已簽到 {{firstTime}} 次</text>
 </view>
 <view class="{{flag?'mxc2-1':'mxc2-2'}}" bindtap="onBindTap">
 <text>我要簽到</text>
 </view>
</view>

hello.wxss

.container{
 background-color: ghostwhite;
 width: 100%;
 height: 100%;
 flex-direction: column;
 display: flex;
 align-items: center;
 min-height: 100vh;
}
.mxc1{
 position: relative;
 width: 100%;
 height: 400rpx;
 border-top: 1px solid #000;
 border-bottom: 1px solid #000;
 margin-top: -70rpx;
 flex-direction: column;
 display: flex;
 align-items: center;
 background-color: #efeff4;
}
.mxc1 text{
 font-size: 30rpx;
 font-weight: bold;
 line-height: 400rpx;
}
.mxc2-1{
 position: absolute;
 width: 60%;
 height: 74rpx;
 border: 1px solid rgba(247, 2, 2, 0.959);
 background-color: rgba(247, 2, 2, 0.959);
 border-radius: 3px;
 flex-direction: column;
 display: flex;
 align-items: center;
 margin-top: 396rpx;
}
.mxc2-1 text{
 color: white;
 font-size: 32rpx;
 line-height: 74rpx;
}
.mxc2-2{
 position: absolute;
 width: 60%;
 height: 74rpx;
 border: 1px solid rgba(182, 177, 177, 0.959);
 background-color: rgba(182, 177, 177, 0.959);
 border-radius: 3px;
 flex-direction: column;
 display: flex;
 align-items: center;
 margin-top: 396rpx;
}
.mxc2-2 text{
 color: #000;
 font-size: 32rpx;
 line-height: 74rpx;
}

hello.js

Page({
 data:{
 firstTime:'0',
 flag:true
 },
 onBindTap:function(){
 var D=(new Date()).getDate().toString();
 if(D != wx.getStorageSync('D')){
  wx.setStorageSync('D', D);
  wx.setStorage({
  key: 'FirstTime',
  data: (parseInt(this.data.firstTime) + 1).toString(),
  })
  var that = this;
  var firstTime = wx.getStorage({
  key: 'FirstTime',
  success: function (res) {
   that.setData({
   firstTime: res.data,
   flag:false
   })
   wx.showToast({
   title: '簽到成功!',
   icon: 'success',
   duration: 1200,
   mask: true
   })
  },
  })
 }else{
  wx.showToast({
  title: '今日打卡已完成!',
  icon:'loading',
  duration:1200,
  mask:true
  })
 }
 },
 onShow:function(options){
 var that = this;
 var firstTime = wx.getStorage({
  key: 'FirstTime',
  success: function (res) {
  that.setData({
   firstTime: res.data
  })
  },
 })
 var D = (new Date()).getDate().toString();
 if (D != wx.getStorageSync('D')){
  this.setData({
  flag:true
  })
 }else{
  this.setData({
  flag:false
  })
 }
 },
})

hello.json

{
 "navigationBarTitleText": "簽到",
 "navigationBarTextStyle": "black",
 "navigationBarBackgroundColor": "ghostwhite"
}

擴(kuò)展時(shí)刻:

剛剛實(shí)現(xiàn)了簡(jiǎn)單的簽到功能,那么,怎么實(shí)現(xiàn)連續(xù)簽到呢?

我想了一晚上,因?yàn)閯傞_(kāi)始時(shí)思路跑到了“誤區(qū)”——判斷點(diǎn)擊后加1的事件是否匹配。但是你點(diǎn)擊后加1是個(gè)被動(dòng)事件,唯一條件就是點(diǎn)擊,拿這個(gè)判斷豈不是很難受?

于是,我們同樣可以用parseInt()函數(shù)來(lái)把當(dāng)前日期(時(shí)間)和緩存日期(時(shí)間)作比較 ,判斷他們是否滿足:

var D=(new Date()).getDate().toString();

在點(diǎn)擊事件onBindTap里:

var DT=wx.getStorageSync('D');
if(parseInt(D)!=parseInt(DT)+1){
 //非連續(xù)簽到 對(duì)應(yīng)的操作
}else{
 //連續(xù)簽到
}

易錯(cuò)點(diǎn)提示:

上面 hello.js 代碼中有這么一行:this.data.firstTime

那有沒(méi)有人想過(guò) 只寫firstTime?

小程序中用data中的數(shù)據(jù)(變量)必須加上“this.data.”前綴!

總結(jié)

以上所述是小編給大家介紹的微信小程序本地存儲(chǔ)實(shí)現(xiàn)每日簽到、連續(xù)簽到功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論

平谷区| 海原县| 阆中市| 类乌齐县| 舒兰市| 玉龙| 阜南县| 金堂县| 青冈县| 平阴县| 静安区| 青冈县| 天等县| 无棣县| 巴彦淖尔市| 新乡县| 乾安县| 交城县| 呈贡县| 新民市| 平凉市| 太仓市| 旌德县| 胶南市| 浮梁县| 大兴区| 潼南县| 防城港市| 丰都县| 汤阴县| 湟源县| 临颍县| 陕西省| 乌鲁木齐县| 东港市| 乌鲁木齐县| 黄陵县| 姚安县| 敦煌市| 德化县| 西盟|