xz_jiangzi/pages/newsDetail/newsDetail.js

245 lines
7.5 KiB
JavaScript

// pages/newsDetail/newsDetail.js
var app = getApp();
var audioContentx = null;
Page({
/**
* 页面的初始数据
*/
data: {
newsId: '',
newsBean: null,
imgUrl: app.imgUrl,
pageHeight: 500,
currentVideoUrl: '',
currentDesc: '',
currentVideoIndex: 0,
sliderValue: 0,
currentTime: 0,
duration: 0,
currentAudioIndex: 0,
currentAudioDesc: '',
canPlay: false,
isPlaying: false,
totalValue: 0,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.setData({
newsId: options.id,
pageHeight: wx.getSystemInfoSync().windowHeight
})
this.getNewsDetail();
this.saveBigdata(options.id);
},
saveBigdata(id) {
var params = "小程序" + id + "新闻";
var info = {
requestUrl: params
}
app.restAjax.get(app.restAjax.path(app.bigDataUrl + '/app/contentcensusrelease/log', []), info, null,
function (code, data) {
}, function (code, err) {
})
},
getNewsDetail() {
var _self = this;
// app.dialog.loading('正在加载');
app.restAjax.get(app.restAjax.path('{newsUrl}/app/newscontent/getnewscontentbyidrelease/{id}', [app.newsUrl, _self.data.newsId]), null, null,
function (code, data) {
_self.setData({
newsBean: data
})
//视频
if (_self.data.newsBean.newsContentType == '4') {
_self.setData({
currentVideoUrl: _self.data.imgUrl + _self.data.newsBean.fileList[0].contentFileFileId,
currentDesc: _self.data.newsBean.fileList[0].contentFileText
})
}
//音频
if (_self.data.newsBean.newsContentType == '3') {
_self.setData({
currentAudioUrl: _self.data.imgUrl + _self.data.newsBean.fileList[0].contentFileFileId,
currentAudioDesc: _self.data.newsBean.fileList[0].contentFileText,
})
_self.initAudio()
}
},
function (code, data) {
app.dialog.msg(data.msg);
},
function () {
wx.stopPullDownRefresh();
// wx.hideLoading();
});
},
setVideoUrl(e) {
var _self = this;
var item = e.currentTarget.dataset.item
var index = e.currentTarget.dataset.index
this.setData({
currentVideoUrl: _self.data.imgUrl + item.contentFileFileId,
currentDesc: item.contentFileText,
currentVideoIndex: index
})
},
//初始化音频播放器
initAudio() {
var _self = this;
audioContentx = wx.createInnerAudioContext();
audioContentx.src = _self.data.currentAudioUrl;
// 准备好歌曲流,可以播放
audioContentx.onCanplay(() => {
_self.setData({
canPlay: true,
duration: audioContentx.duration * 1000
})
console.log(audioContentx.duration)
})
audioContentx.onTimeUpdate(function () {
const currentTime = audioContentx.currentTime * 1000
_self.setData({
duration: audioContentx.duration * 1000,
currentTime: currentTime,
sliderValue: audioContentx.currentTime,
totalValue: audioContentx.duration
})
});
audioContentx.onStop(() => {
_self.setData({
isPlaying: false,
sliderValue: 0,
})
});
audioContentx.onEnded(() => {
_self.setData({
isPlaying: false,
sliderValue: 0,
})
});
audioContentx.onPlay(() => {
_self.setData({
isPlaying: true,
})
});
audioContentx.onError((res) => {
console.log(res.errMsg)
console.log(res.errCode)
})
},
playAudio() {
if (audioContentx && audioContentx.paused) {
audioContentx.stop();
audioContentx.play();
this.setData({
isPlaying: true
})
} else {
audioContentx.pause();
this.setData({
isPlaying: false
})
}
},
//下一个音频
nextAudio() {
var _self = this;
var index = _self.data.currentAudioIndex + 1
if (index >= _self.data.newsBean.fileList.length) {
index = _self.data.newsBean.fileList.length - 1
var item = _self.data.newsBean.fileList[index]
_self.setData({
currentAudioIndex: index,
sliderValue: 0,
currentAudioDesc: item.contentFileText
})
if (audioContentx) {
audioContentx.stop()
audioContentx.src = _self.data.imgUrl + item.contentFileFileId
audioContentx.play()
}
} else {
var item = _self.data.newsBean.fileList[index]
_self.setData({
currentAudioIndex: index,
sliderValue: 0,
currentAudioDesc: item.contentFileText
})
if (audioContentx) {
audioContentx.stop()
audioContentx.src = _self.data.imgUrl + item.contentFileFileId
audioContentx.play()
}
}
},
setAudioUrl(e) {
var _self = this;
var item = e.currentTarget.dataset.item
var index = e.currentTarget.dataset.index
if (audioContentx) {
audioContentx.stop()
audioContentx.src = _self.data.imgUrl + item.contentFileFileId;
audioContentx.play()
this.setData({
currentAudioIndex: index,
sliderValue: 0
})
}
},
//上一个音频
preAudio() {
var _self = this;
var index = _self.data.currentAudioIndex - 1
if (index <= 0) {
index = 0
var item = _self.data.newsBean.fileList[index]
_self.setData({
currentAudioIndex: index,
sliderValue: 0,
currentAudioDesc: item.contentFileText
})
if (audioContentx) {
audioContentx.stop()
audioContentx.src = _self.data.imgUrl + item.contentFileFileId
audioContentx.play()
}
} else {
var item = _self.data.newsBean.fileList[index]
_self.setData({
currentAudioIndex: index,
sliderValue: 0,
currentAudioDesc: item.contentFileText
})
if (audioContentx) {
audioContentx.stop()
audioContentx.src = _self.data.imgUrl + item.contentFileFileId
audioContentx.play()
}
}
},
//slider进度调整
handleSliderChange(event) {
this.setData({
currentTime: event.detail.value * 1000
})
audioContentx.pause()
audioContentx.seek(event.detail.value)
audioContentx.play()
},
onUnload() {
if (audioContentx) {
audioContentx.stop();
audioContentx.destroy();
}
}
})