第一次提交

This commit is contained in:
@fawn-nine
2024-05-22 13:42:15 +08:00
commit bb53af8bde
2133 changed files with 129959 additions and 0 deletions

471
utils/music.js Normal file
View File

@@ -0,0 +1,471 @@
import store from '@/store/index.js'
import $http from '@/config/requestConfig.js';
// #ifdef APP-PLUS
const bgm = uni.getBackgroundAudioManager();
// #endif
// #ifdef H5
const bgm = {};
// #endif
// const bgm = uni.createInnerAudioContext();
bgm.musicList = [] // 播放目录
bgm.src = ''
bgm.htimes = 0 // 历史播放秒数
bgm.cTime = 0
bgm.title = ''
bgm.interval=null // 存储定时器
bgm.loop = true;
bgm.coverImgUrl = ''
bgm.oldIndex = 0 // 前面一首的播放索引
bgm.playIndex = 0
bgm.playing = false // 播放进行时
store.state.userInfo.playIndex !== undefined ? bgm.playIndex = store.state.userInfo.playIndex : bgm.playIndex = 0
var music = {
//
//mute 表示是否是播放,,默认不播放
playBgm({mute=false}){
// console.log(bgm.musicList,'src')
if (!bgm) return;
if(mute){
bgm.pause()
}else{
// bgm.src = bgm.musicList[bgm.playIndex].url
// 判断播放列表是否空
if(bgm.musicList.length == 0){
uni.showModal({
title: '提示',
showCancel:false,
confirmText:'好的',
content: '请先选择要播放的音频',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定');
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
return
}
// 没有就添加添加url到播放器播放新的
if(bgm.src == ''){
console.log(bgm.playIndex,'播放的索引',store.state.userInfo.playIndex,'播放的时长',store.state.userInfo.playTimes)
store.commit('setUserInfo',{'playTitle': bgm.musicList[bgm.playIndex].chapter})
store.commit('setUserInfo',{'fengImg': bgm.musicList[bgm.playIndex].bookImage})
store.commit('setUserInfo',{'playingInfo': bgm.musicList[bgm.playIndex]})
console.log(store.state.userInfo,'chapterName',bgm.playIndex)
store.state.userInfo.playTimes ? bgm.htimes = store.state.userInfo.playTimes : ''
// 设置默认原生播放组件的显示标题和图片
// bgm.title = bgm.musicList[bgm.playIndex].chapter
bgm.title = '正在播放'
console.log('应该显示的title',bgm.musicList[bgm.playIndex].chapter)
// bgm.artist = '暂无'
//bgm.singer = '暂无'
bgm.coverImgUrl = 'https://www.nuttyreading.com/images/logo.png'
bgm.image = 'https://www.nuttyreading.com/images/logo.png'
this.getChartUrl()
// 获取历史秒数
}else{
}
}
bgm.onPause(()=>{
console.log('暂停背景音乐');
bgm.title = '未在播放'
this.saveTimes()
clearInterval(bgm.interval)
bgm.interval = null
bgm.playing = false
store.commit('setUserInfo',{'playFlag': false})
})
bgm.onPlay(() => {
store.commit('setUserInfo',{'playFlag': true})
bgm.playing = true
console.log('开始播放音乐#######');
store.commit('setUserInfo',{'playingInfo': bgm.musicList[bgm.playIndex]})
//console.log(store.state.userInfo.playingInfo,'chapterName2222')
uni.hideLoading()
// 第一次存储
//this.saveIndex()
//this.saveRate(bgm.musicList[bgm.playIndex])
// end
bgm.interval = setInterval(()=>{
console.log('存一次')
this.saveIndex()
store.commit('setUserInfo',{'playTimes': bgm.currentTime}) // 本地存储
this.saveRate(bgm.musicList[bgm.playIndex]) // 线上存储
},180000)
})
bgm.onError((res) => {
console.log(res)
bgm.playing = false
uni.showModal({
title: '提示',
showCancel:false,
confirmText:'好的',
content: '音频路径异常,请联系管理员',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定');
}
}
});
return
})
bgm.onTimeUpdate((res) =>{
// this.currentTime = Math.max(0, this.videoContext.currentTime)
setTimeout(()=>{
store.commit('setUserInfo',{'currentTime': Math.max(0, bgm.currentTime)})
},1000)
})
bgm.onWaiting(() => {
// 加载时
uni.showLoading()
console.log('加载时')
})
bgm.onCanplay(() => {
// 可以播放时
console.log('可以播放时')
uni.hideLoading()
this.saveIndex()
this.saveRate(bgm.musicList[bgm.playIndex])
// console.log('历史播放进度,秒数', bgm.htimes)
bgm.seek(bgm.htimes)
bgm.title = '正在播放'
console.log('应该显示的title', bgm.musicList[bgm.playIndex].chapter)
bgm.artist = '暂无'
//bgm.singer = '暂无'
bgm.coverImgUrl = 'https://www.nuttyreading.com/images/logo.png'
bgm.image = 'https://www.nuttyreading.com/images/logo.png'
// console.log(bgm,'bgm')
})
bgm.onEnded(() => {
bgm.htimes = 0 // 重置播放秒数
bgm.playing = false
console.log('播放完毕了',bgm.playing)
store.commit('setUserInfo',{'playFlag': false})
this.setPlayIndex('next') // 下一首
})
bgm.onPrev(() => {
console.log('点了上一曲')
if(bgm.playIndex - 1 >= 0){
this.setPlayIndex('prve') // 上一首
}else{
console.log('没有上一首了')
}
})
bgm.onNext(() => {
console.log('点了下一曲')
if(bgm.playIndex + 1 <= bgm.musicList.length){
this.setPlayIndex('next') // 下一首
}else{
console.log('没有下一首了,到头了')
}
})
},
// 添加播放列表
setList(list,op,playindex,time){
if(bgm.interval){
console.log('存在定时器,清空')
clearInterval(bgm.interval)
bgm.interval = null
}
console.log(time,'time',playindex,'playindex')
bgm.musicList = list
//console.log(bgm.musicList,'bgm.musicList')
bgm.oldIndex = bgm.playIndex // 暂存上一个播放得index
// console.log('地址异常报错前index:',bgm.oldIndex)
// playindex ? bgm.playIndex = playindex : bgm.playIndex = 0
store.commit('setUserInfo',{'playingInfo': {}})
if(playindex){
bgm.playIndex = playindex
console.log(playindex,'传值了')
if(time){ // 如果传了历史播放秒数
bgm.htimes = time
}else{
// console.log('重置播放秒数')
bgm.htimes = 0
}
}else{
console.log('---------')
bgm.htimes = 0
bgm.playIndex = 0
}
if(op == 'autoPlay'){
store.commit('setUserInfo',{'playTimes': 0})
if(bgm._options.src == ''){
// this.getChartUrl()
// console.log(bgm,'+++++++++++')
this.playBgm({mute:false})
}else{
console.log(bgm, '----')
this.getChartUrl() // 获取对应的播放路径
}
}
},
// 获取线上听书进度
getListenRate(val,op){
let data = {
bookId: val.bookid,
userId: store.state.userInfo.id,
}
$http.request({
url: "book/listening/getReadRate",
method: "POST", // POST、GET、PUT、DELETE具体说明查看官方文档
data,
header: { //默认 无 说明:请求头
'Content-Type': 'application/json'
},
}).then(res => {
console.log(res,'历史记录')
if(res.readRate.chapterId){
var item = res.readRate
// 有听书进度时
bgm.playIndex = bgm.musicList.findIndex(function(info){
// console.log(info,'info')
if(item.chapterId == info.chapterId && item.bookId == info.bookid ){
return info;
}
})
store.commit('setUserInfo',{'playingInfo':bgm.musicList[bgm.playIndex] })
// console.log('线下的播放index是', store.state.userInfo.playingInfo)
}else{
}
if(op == 'autoPlay'){
store.commit('setUserInfo',{'playTimes': 0})
if(bgm._options.src == ''){
// this.getChartUrl()
this.playBgm({mute:false})
console.log('playBgm')
}else{
this.getChartUrl() // 获取对应的播放路径
// console.log('getChartUrl')
}
}
})
},
// 获取章节url
getChartUrl(){
// bgm.stop()
let data = {
'userId': store.state.userInfo.id,
'bookId': bgm.musicList[bgm.playIndex].bookId,
'chapterId': bgm.musicList[bgm.playIndex].id
}
console.log(data,'data')
$http.post('book/bookchaptercontent/getBooksCatalogue', data)
// $http.post('book/bookchaptercontent/getCatal', data)
.then(res => {
console.log(res,'鉴权结果')
if(res.code == 0){
if(res.jq==false ){
store.commit('setUserInfo',{'playFlag': false})
if(bgm.interval){
console.log('存在定时器,清空')
clearInterval(bgm.interval)
bgm.interval = null
}
// 鉴权失败
if(res.product == null){
uni.showModal({
title: '提示',
confirmText:'好的',
content: '您还未拥有此书哦~',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定');
}
}
});
}else{
uni.showModal({
title: '提示',
cancelText: '暂不购买',
confirmText:'立即购买',
content: '抱歉!该章节需要购买后才可以听哦~',
success: function (res1) {
if (res1.confirm) {
console.log('用户点击确定',res.product);
uni.navigateTo({
url: '../bookShop/commodityDetail?id=' + res.product
});
}
}
});
}
}
// bgm.stop()
// 有听权限时
if(res.chapter.voices != null && res.chapter.voices != ''){ // 空值url:null
// 先清除定时器
clearInterval(bgm.interval)
bgm.interval = null
bgm.src = res.chapter.voices
store.commit('setUserInfo',{'playIndex': bgm.playIndex})
store.commit('setUserInfo',{'playingInfo':bgm.musicList[bgm.playIndex] })
}else{
bgm.playIndex = bgm.oldIndex
console.log('地址异常报错后index:',bgm.playIndex)
store.commit('setUserInfo',{'playIndex': bgm.playIndex})
store.commit('setUserInfo',{'playingInfo':bgm.musicList[bgm.playIndex] })
uni.showToast({
title:'音频地址异常',
icon:'error',
duration:2000
})
// store.commit('setUserInfo',{'playFlag': false})
}
}
}).catch((e)=>{
console.log(e,'e')
console.log('异常')
// bgm.pause()
bgm.playIndex = bgm.oldIndex
store.commit('setUserInfo',{'playFlag': false})
if(bgm.interval){
console.log('存在定时器,清空')
clearInterval(bgm.interval)
bgm.interval = null
}
})
},
// 本地存储播放信息
saveIndex(){
console.log(bgm.playIndex,'存储得index')
store.commit('setUserInfo',{'playIndex': bgm.playIndex})
store.commit('setUserInfo',{'duration':bgm.duration})
store.commit('setUserInfo',{'playTitle': bgm.musicList[bgm.playIndex].chapter})
store.commit('setUserInfo',{'fengImg': bgm.musicList[bgm.playIndex].bookImage})
// store.commit('setUserInfo',{'playFlag': true})
store.commit('setUserInfo',{'playingInfo': bgm.musicList[bgm.playIndex]})
uni.setStorage({
key: 'playingInfo',
data: bgm.musicList[bgm.playIndex],
success: function () {
console.log('success');
}
});
},
// 存储播放(本地)
saveTimes(){
store.commit('setUserInfo',{'playTimes': bgm.currentTime})
},
// 存储播放进度(线上)
saveRate(val){
console.log(val,'要存储的线上播放信息',)
$http.post('book/listening/save', {
'userId': store.state.userInfo.id,
'bookId': val.bookId,
'chapterId': val.id,
'chapterName': val.chapter,
'precent': Math.ceil(bgm.currentTime),
'loadAnimate':'none', // 请求加载动画
})
.then(res => {
console.log(res,'线上存储进度')
if(res.code == 0){
console.log(res,'存储完成')
}
}).catch((e)=>{
console.log(e,'e')
})
},
// 设置播放index
setPlayIndex(opName){ // 切换音频
if(opName == 'next'){ // 下一首
if(bgm.playIndex == bgm.musicList.length - 1){
uni.showToast({
title:'列表播放完毕~~',
icon: 'none'
})
return
}
bgm.oldIndex = bgm.playIndex
bgm.playIndex += 1
console.log('下一首',bgm.playIndex,bgm.oldIndex, store.state.userInfo.playIndex)
// bgm.stop()
bgm.htimes = 0
this.getChartUrl() // 获取章节url
//this.playBgm({'mute':false})
// store.commit('setUserInfo',{'playFlag': false})
}else{ // 上一首
if(bgm.playIndex == 0){
uni.showToast({
title:'没有上一首了~',
icon: 'none'
})
return
}
bgm.oldIndex = bgm.playIndex
bgm.playIndex -= 1
console.log('上一首',bgm.playIndex)
bgm.htimes = 0
this.getChartUrl() // 获取章节url
}
},
// 设置封面图片
setCoverImg(url){
// console.log(bgm.coverImgUrl,'bgm.coverImgUrl')
bgm.coverImgUrl = url
},
// 获取封面图片
getCoverImg(){
// console.log(bgm.coverImgUrl,'bgm.coverImgUrl')
return bgm.coverImgUrl
},
// 播放单曲
setOneMusic(item){
console.log(item,'item')
// 显示播放组件
store.commit('setUserInfo',{'playVisible': true})
uni.setStorage({
key: 'playVisible',
data: true,
success: function () {
console.log('success');
}
});
bgm.htimes = 0
bgm.oldIndex = bgm.playIndex
bgm.playIndex = bgm.musicList.findIndex(function(info){
// console.log(info,'info')
if(item.id == info.id && item.bookId == info.bookId ){
return info;
}
})
console.log(bgm.playIndex,'bgm.playIndex')
this.getChartUrl()
},
// 关闭音频
setCloseBgm(){
if(bgm.interval){
console.log('存在定时器,清空')
clearInterval(bgm.interval)
bgm.interval = null
bgm.stop()
}
}
}
module.exports = {music,bgm}