這篇文章主要介紹了微信小程序本地存儲怎么實現每日簽到、連續簽到功能的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇微信小程序本地存儲怎么實現每日簽到、連續簽到功能文章都會有所收獲,下面我們一起來看看吧。
先說說相關注意吧:
其一就是 storage中只能存放字符串!
我去,昨晚大部分時間都是在搞這個。以前一直認為存放的是對象,興致勃勃寫完發現點擊以后出現了“NAN”…
覺得 事情并沒有這么簡單。
仔細回去看了一下曾經Vue寫過的localStorage,發現一直弄錯了,應該存放字符串!
搞清楚這個以后,又有一個問題:你要“ 點擊加1 ”,這里總是把數字和字符串弄反,甚至想了用數組形式存放。。。最終想到了解決辦法:把存放的字符串轉為數字,加1后再轉為字符串存放到storage中 。
想到這我不禁喜形于色,終于可以了!
但是當我無意中狂點16下的時候,我又哭了…
new Date()函數控制日期——一分鐘/一天/…只能點一次:
var D=(new Date()).getDate().toString();
if(D != wx.getStorageSync('D')){ //判斷是否過了當天
//如果是新的一天,則...
}else{
//否則,例如:
wx.showToast({
title: '今日打卡已完成!',
icon:'loading',
duration:1200,
mask:true
})
}這里又出現一個問題,我在當前頁面開始時onLoad里面加了一段代碼:把當前時間存放到storage中,但是我發現,這樣以后就點不了了(當天),為什么?
因為沖突了啊,加載頁面時存放此時時間,那么你如果在這個事件內(本例:一天)去點擊,如上面代碼第一、二行,它不成立——都是“今天”,所以會執行else語句。
解決辦法: 去掉onLoad函數,這樣開始執行if時候會發現storage中沒有存儲,也就“!=”了。
下面放上示例代碼:
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"
}擴展時刻:
剛剛實現了簡單的簽到功能,那么,怎么實現連續簽到呢?
我想了一晚上,因為剛開始時思路跑到了“誤區”——判斷點擊后加1的事件是否匹配。但是你點擊后加1是個被動事件,唯一條件就是點擊,拿這個判斷豈不是很難受?
于是,我們同樣可以用parseInt()函數來把當前日期(時間)和緩存日期(時間)作比較 ,判斷他們是否滿足:
var D=(new Date()).getDate().toString();
在點擊事件onBindTap里:
var DT=wx.getStorageSync('D');
if(parseInt(D)!=parseInt(DT)+1){
//非連續簽到 對應的操作
}else{
//連續簽到
}易錯點提示:
上面 hello.js 代碼中有這么一行:this.data.firstTime
那有沒有人想過 只寫firstTime?
小程序中用data中的數據(變量)必須加上“this.data.”前綴!
關于“微信小程序本地存儲怎么實現每日簽到、連續簽到功能”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“微信小程序本地存儲怎么實現每日簽到、連續簽到功能”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。