一、01init
1-1 view设100%没用,必须给page先设置100%
1-2 wx.switchTab跳转到tabbar页面
二、02swiper
<swiper>
<swiper-item>
'''
</swiper-item>
</swiper>
三、03collect-storage
//3-1 设一一个对象装载缓存
{
"0":"false",
"1":"true",
"2":"false",
"3":"true"
}
//3-2 有缓存设获取缓存,没有缓存则设置缓存
var collection =wx.getStorageSync('collection');
/* 有缓存,获取缓存 */
if(collection){
collected = collection[id];
this.setData({
isCollected:collected
})
}else{
/*没有缓存,就设置缓存 */
var collection = {};
collection[id] = false;
wx.setStorageSync('collection', collection)
/* {"0":"false","1":"false"} */
}
//3-3 设计点击事件
<image bind:tap="handleCollect" ...></image>
handleCollect(){
/* 获取缓存 */
var collection= wx.getStorageSync('collection')
var collected = !collection[this.data.id];
collection[this.data.id] = collected;
/* 更新缓存 */
wx.setStorageSync('collection', collection)
this.setData({
isCollected:collected
})
}
四、showModal实现收藏
handleCollect(){
/* 获取缓存 */
var collection= wx.getStorageSync('collection')
var collected = !collection[this.data.id];
collection[this.data.id] = collected;
/* 更新缓存 */
this.showModal(collected,collection)
},
showMoal(collected,collection){
wx.showModal({
title: '收藏',
content: '收藏文章',
success: (res) => {
if (res.confirm) {
//点击确定触发
if (collected) {
wx.setStorageSync('collection', collection)
this.setData({
isCollected: collected
})
}
} else if (res.cancel) {
//点击取消触发
if (collected == false) {
wx.setStorageSync('collection', collection)
this.setData({
isCollected: collected
})
}
}
}
})
}
4-1 分享
wx.showActionSheet({
itemList: [
'分享到微信',
'分享到朋友圈'
],
itemColor: '#000000',
success: (res) => {
console.log(res.tapIndex)
},
fail: () => { },
complete: () => { }
});
五、音乐播放
5-1 点击事件
<image class="music" bind:tap="handleMusic"></image>
5-2 实现音乐播放,暂停
var audio = wx.getBackgroundAudioManager()
handleMusic(){
if(this.data.isPlay){
audio.pause();
this.setData({
isPlay:false
})
}else{
audio.title = this.data.item.music.title
audio.src = this.data.item.music.url
this.setData({
isPlay:true
})
}
}
5-3 监听音乐播放 bottom-icon
audio.onPlay(()=>{
this.setData({
isPlay:true
})
})
audio.onPause(()=>{
this.setData({
isPlay:false
})
})
5-4 进入和退出一直
//app.js
//5-4-1 app.js 定义g_isPlay记录音乐播放的状态
App({
onLaunch: function(options) {
},
globalData: {
g_isPlay:false
}
});
//5-4-2 在handleMusic事件中设置g_isPlay
handleMusic(){
if(this.data.isPlay){
....
app.globalData. g_isPlay = false
console.log(app.globalData.g_isPlay)
}else{
...
app.globalData. g_isPlay = true;
console.log(app.globalData.g_isPlay)
}
}
//5-4-3 在onLoad生命周期中监听
/* 让退入和进入音乐播放按钮一致 */
if(app.globalData.g_isPlay){
this.setData({
isPlay:true
})
}else{
this.setData({
isPlay:false
})
}