posts-data.js数据
var local_database = [
{
date: 'Sep 18 2018',
title: '正是虾肥蟹壮时',
imgSrc: '/images/post/crab.png',
avatar: '/images/1.png',
content: '正文正文正文正文正文正文正文正文正文正文正文正文正文正文正文',
reading: '112',
collection: '96',
headImgSrc:'/images/post/crab.png',
author:'小滨',
dateTime:'24小时前',
detail:'这是正文正是虾肥蟹壮时正是虾肥蟹壮时正是虾肥蟹壮时正是虾肥蟹壮时',
postId:0,
music: {
url: "http://mp3.qqmusic.cc/yq/102636799.mp3",
title: "演员-薛之谦",
coverImg: "http://y.gtimg.cn/music/photo_new/T002R300x300M000003y8dsH2wBHlo.jpg?max_age=2592000"
}
}
]
module.exports={
postList: local_database
}
---------------------------------------
app.json 配置
{
"pages": [
"pages/welcome/welcome",
"pages/posts/post",
"pages/posts/post-detail/post-detail",
"pages/movies/movies",
"pages/movies/more-movie/more-movie",
"pages/movies/movie-detail/movie-detail"
],
"window": {
"navigationBarBackgroundColor": "#405f80"
},
"tabBar": {
"borderStyle":"white",
"list": [
{
"pagePath": "pages/posts/post",
"text": "阅读",
"iconPath":"images/tab/yuedu.png",
"selectedIconPath":"images/tab/yuedu_hl.png"
},
{
"pagePath": "pages/movies/movies",
"text": "电影",
"iconPath": "images/tab/dianying.png",
"selectedIconPath": "images/tab/dianying_hl.png"
}
]
}
}
--------------------------------
app.js
App({
globalData:{
g_isPlayingMusic:false,
g_currentMusicPostId:null,
doubanBase:"http://t.yushu.im",
}
})
-------------------------------------
util.js
function convertToStarsArray(stars){
var num = stars.toString().substring(0,1);
var array = [];
for(var i=1; i<=5; i++){
if(i<=num){
array.push(1);
}
else{
array.push(0);
}
}
return array;
}
function http(url,callBack) {
wx.request({
url: url,
method: 'GET',
header: {
"content-type": "application/json"
},
success: function (res) {
callBack(res.data);
},
fail: function (error) {
console.log(error);
}
})
}
function convertToCastString(casts) {
var castsjoin = "";
for (var idx in casts) {
castsjoin = castsjoin + casts[idx].name + " / ";
}
return castsjoin.substring(0, castsjoin.length - 2);
}
function convertToCastInfos(casts) {
var castsArray = []
for (var idx in casts) {
var cast = {
img: casts[idx].avatars ? casts[idx].avatars.large : "",
name: casts[idx].name
}
castsArray.push(cast);
}
return castsArray;
}
module.exports = {
convertToStarsArray: convertToStarsArray,
http: http,
convertToCastString: convertToCastString,
convertToCastInfos: convertToCastInfos
}
-------------------
page{
height: 100%;
background-color: #b3d4db;
}
-------------------------------------------
post.js
var postsData=require('../../data/posts-data.js')
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function () {
this.setData({
posts_key: postsData.postList
});
},
onPostTap:function(event){
var postId = event.currentTarget.dataset.postid;
// console.log(postId);
wx.navigateTo({
url:'post-detail/post-detail?id='+postId
})
},
onSwiperTap: function (event) {
var postId = event.target.dataset.postid;
//target 和 currentTarget
//target指的是当前点击的组建 和 currentTarget指的是事件捕获的组建
//target这里指的是image,而currentTarget 指的是swiper
wx.navigateTo({
url: 'post-detail/post-detail?id=' + postId
})
}
})
-------------------------------------
post-detail.js
var postsData = require('../../../data/posts-data.js')
var app = getApp();
Page({
data: {
isPlayingMusic: false
},
onLoad: function (option) {
var globalData = app.globalData;
var postId = option.id;
this.data.currentPostId = postId;
var postData = postsData.postList[postId];
this.setData({
postData: postData
})
var postsCollected = wx.getStorageSync('posts_collected')
if (postsCollected) {
var postCollected = postsCollected[postId]
if (postCollected) {
this.setData({
collected: postCollected
})
}
}
else {
var postsCollected = {}
postsCollected[postId] = false;
wx.setStorageSync('posts_collected', postsCollected)
}
if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId === postId) {
this.setData({
isPlayingMusic: true
})
}
this.setMusicMonitor();
},
setMusicMonitor: function () {
var that = this;
wx.onBackgroundAudioPlay(function () {
that.setData({
isPlayingMusic: true
})
app.globalData.g_isPlayingMusic = true;
app.gloabalData.g_currentMusicPostId = that.data.currentPostId;
})
wx.onBackgroundAudioPause(function () {
that.setData({
isPlayingMusic: false
})
app.globalData.g_isPlayingMusic = false;
app.gloabalData.g_currentMusicPostId = null;
})
wx.onBackgroundAudioStop(function () {
that.setData({
isPlayingMusic: false
})
app.globalData.g_isPlayingMusic = false;
app.gloabalData.g_currentMusicPostId = null;
})
},
onCollectionTap: function (event) {
this.getPostsCollectedSyc();
// this.getPostsCollectedAsy();
},
getPostsCollectedAsy: function () {
var that = this;
wx.getStorage({
key: 'posts_collected',
success: function (res) {
var postsCollected = res.data;
var postCollected = postsCollected[that.data.currentPostId];
//收藏变成未收藏,未收藏变成收藏
postCollected = !postCollected;
postsCollected[that.data.currentPostId] = postCollected;
that.showToast(postsCollected, postCollected);
}
})
},
getPostsCollectedSyc: function () {
var postsCollected = wx.getStorageSync('posts_collected');
var postCollected = postsCollected[this.data.currentPostId];
//收藏变成未收藏,未收藏变成收藏
postCollected = !postCollected;
postsCollected[this.data.currentPostId] = postCollected;
this.showToast(postsCollected, postCollected);
},
showModal: function (postsCollected, postCollected) {
var that = this;
wx.showModal({
title: '收藏',
content: postCollected ? '收藏该文章?' : '取消收藏该文章?',
showCancel: 'true',
cancelText: '取消',
cancelColor: '#333',
confirmText: '确认',
confirmColor: '#405f80',
success: function (res) {
if (res.confirm) {
//更新文章是否为缓存值
wx.setStorageSync('posts_collected', postsCollected);
//更新数据绑定变量,从而实现切换图片
that.setData({
collected: postCollected
})
}
}
})
},
showToast: function (postsCollected, postCollected) {
//更新文章是否为缓存值
wx.setStorageSync('posts_collected', postsCollected);
//更新数据绑定变量,从而实现切换图片
this.setData({
collected: postCollected
})
wx.showToast({
title: postCollected ? '收藏成功' : '取消成功',
duration: 1000,
icon: 'success'
})
},
onShareTap: function (event) {
var itemList = [
'分享给微信好友',
'分享到朋友圈',
'分享到QQ',
'分享到微博'
]
wx.showActionSheet({
itemList: itemList,
itemColor: '#405f80',
success: function (res) {
// res.cancel
// res.tapIndex
wx.showModal({
title: '用户' + itemList[res.tapIndex],
content: '用户是否取消?' + res.cancel + '现在无法实现分享功能,什么时候能支持呢'
})
}
})
},
onMusicTap: function (event) {
var currentPostId = this.data.currentPostId;
var postData = postsData.postList[currentPostId];
var isPlayingMusic = this.data.isPlayingMusic;
if (isPlayingMusic) {
wx.pauseBackgroundAudio();
this.setData({
isPlayingMusic: false
})
}
else {
wx.playBackgroundAudio({
dataUrl: postData.music.url,
title: postData.music.title,
coverImgUrl: postData.music.coverImg
})
this.setData({
isPlayingMusic: true
})
}
}
})
共同学习,写下你的评论
评论加载中...
作者其他优质文章