### 设置和获取缓存演示
~~~javascript
wx.setStorageSync('bizzbee', '就是我');
wx.setStorageSync('小程序班级', {
'同学1':'袁嘉晨',
'同学2':'季炎'
})
~~~
~~~javascript
onCollectionTap:function(event){
var students = wx.getStorageSync('小程序班级');
console.log(students);
},
~~~
### 收藏功能
`post-detail.wxml`
~~~html
<image wx:if="{{collected}}" catchtap="onCollectionTap" src='/images/icon/collection.png'></image>
<image wx:else catchtap="onCollectionTap" src="/images/icon/collection-anti.png"></image>
~~~
onload中把当前id放入data,初始化收藏状态
~~~javascript
onLoad: function (options) {
var postId = options.id;
this.setData({
currentPostId : postId
});
var postData = postsData.postList[postId];
console.log(postData);
this.setData(postData);
var postsCollected = wx.getStorageSync('posts_collected');
if(postsCollected){
var postCollected = postsCollected[postId];
this.setData({
collected:postCollected
});
}else{
var postsCollected = {};
postsCollected[postId] = false;
wx.setStorageSync('posts_collected', postsCollected);
}
},
onCollectionTap:function(event){
var postsCollected = wx.getStorageSync('posts_collected');
var postCollected = postsCollected[this.data.currentPostId];
postCollected = !postCollected;
postsCollected[this.data.currentPostId] = postCollected;
wx.setStorageSync('posts_collected', postsCollected);
this.setData({
collected:postCollected
});
// var students = wx.getStorageSync('小程序班级');
// console.log(students);
},
~~~
收藏弹窗
~~~javascript
wx.showToast({
title:postCollected?'收藏成功':'取消成功',
duration:1000,
icon:"success"
})
~~~
### 使用模态弹出框
~~~javascript
onCollectionTap:function(event){
var postsCollected = wx.getStorageSync('posts_collected');
var postCollected = postsCollected[this.data.currentPostId];
postCollected = !postCollected;
postsCollected[this.data.currentPostId] = postCollected;
this.showModal(postsCollected,postCollected);
},
showModal:function(postsCollected,postCollected){
var that = this;
wx.showModal({
title: '收藏',
content:postCollected?"收藏该文章?":"取消收藏该文章",
showCancel:"true",
cancelText:"取消",
confirmText:"确认",
confirmColor:"#405f80",
success:function(res){
if(res.confirm){
wx.setStorageSync('posts_collected', postsCollected);
that.setData({
collected: postCollected
});
}
}
})
},
~~~
### 使用ActionSheet模拟分享
~~~javascript
onShareTap:function(){
var itemList = [
"分享给微信好友",
"分享到朋友圈",
"分享到陌陌",
"分享到探探"
];
wx.showActionSheet({
itemList: itemList,
itemColor:"#405f80",
success:function(res){
wx.showModal({
title: '用户'+itemList[res.tapIndex],
content: '用户是否取消?'+res.cancel
})
}
})
},
~~~