This commit is contained in:
@fawn-nine
2023-03-03 12:11:23 +08:00
commit f8e1a3015b
502 changed files with 57308 additions and 0 deletions

809
pages/eBook/2/book.vue Normal file
View File

@@ -0,0 +1,809 @@
<template>
<view class="page">
<!-- 仅计算用 -->
<cus-page id="preChapter" class="hidden" :content="preChapter.content" :fontSize="fontSize"
:lineHeight="lineHeight"></cus-page>
<cus-page id="curChapter" class="hidden" :content="curChapter.content" :fontSize="fontSize"
:lineHeight="lineHeight"></cus-page>
<cus-page id="nextChapter" class="hidden" :content="nextChapter.content" :fontSize="fontSize"
:lineHeight="lineHeight"></cus-page>
<!-- 上一页 -->
<cus-page v-if="prePage.ready" :zIndex="102" :bgColor="bgColor" :transform="prePage.pageTransform[turnType]"
:showAnimation="showAnimation" :showShadow="showShadow" :turnType="turnType"
:chapterName="prePage.chapterName" :innerHeight="innerHeight" :content="prePage.content"
:fontSize="fontSize" :lineHeight="lineHeight" :textColor="textColor" :pageNum="prePage.pageNum"
:systemTimeStr="systemTimeStr" :totalPage="prePage.totalPage" :clipPath="prePage.clipPath"
:bgStyle="prePage.bgStyle" :subBgStyle="prePage.subBgStyle">
</cus-page>
<!-- 本页 -->
<cus-page :isTouch="true" :zIndex="101" :bgColor="bgColor" :transform="curPage.pageTransform[turnType]"
:showAnimation="showAnimation" :showShadow="showShadow" :turnType="turnType"
:chapterName="curPage.chapterName" :innerHeight="innerHeight" :content="curPage.content"
:fontSize="fontSize" :lineHeight="lineHeight" :textColor="textColor" :pageNum="curPage.pageNum"
:systemTimeStr="systemTimeStr" :totalPage="curPage.totalPage" :clipPath="curPage.clipPath"
:bgStyle="curPage.bgStyle" :subBgStyle="curPage.subBgStyle" @touch-start="touchStart"
@touch-move="touchMove" @touch-end="touchEnd" @touch-cancel="touchCancel">
</cus-page>
<!-- 下一页 -->
<cus-page ref="curChapter" v-if="nextPage.ready" :zIndex="100" :bgColor="bgColor"
:transform="nextPage.pageTransform[turnType]" :showAnimation="showAnimation" :showShadow="showShadow"
:turnType="turnType" :chapterName="nextPage.chapterName" :innerHeight="innerHeight"
:content="nextPage.content" :fontSize="fontSize" :lineHeight="lineHeight" :textColor="textColor"
:pageNum="nextPage.pageNum" :systemTimeStr="systemTimeStr" :totalPage="nextPage.totalPage">
</cus-page>
<!-- 点击屏幕弹出 -->
<content-setting ref="contentSetting" :bgColor="bgColor" :textColor="textColor" :isDark="isDark"
:fontSize="fontSize" :bgColorList="bgColorList" :textColorList="textColorList" :turnTypeList="turnTypeList"
:colorType="colorType" :turnType="turnType" :chapterData="chapterData"
:chapterIndex="curChapter.chapterIndex" @updateProps="updateProps" @updatePage="updatePage"
@updateCatalog="updateCatalog" @whenHide="hideMenu" @moreHandle="moreHandle" @listenHandle="listenHandle">
</content-setting>
<!-- 悬浮窗 -->
<cus-audio-play ref="audioPlay" :bookId="id" :bookName="title" :chapterName="curChapter.chapterName"
:content="curChapter.content" :styleBottom="120" @show="getAudioPlayShow"></cus-audio-play>
</view>
</template>
<script>
import contentSetting from './contentSetting.vue'
import {
mapGetters,
mapMutations,
mapActions
}
from 'vuex'
export default {
components: {
contentSetting,
},
data() {
return {
id:"5f9c97aa52f7032983c485e4",
title: '开局就杀皇帝',
chapterData: [],
preChapter: { //上一章数据
ready: false, //是否准备完毕
chapterIndex: 0,
chapterName: '',
content: '',
totalPage: 1,
},
curChapter: { //本一章数据
chapterIndex: 0,
chapterName: '',
content: '',
totalPage: 1,
},
nextChapter: { //下一章数据
ready: false, //是否准备完毕
chapterIndex: 0,
chapterName: '',
content: '',
totalPage: 1,
},
prePage: { //上一页数据
ready: false, //是否准备完毕
chapterName: '',
content: '',
pageNum: 0,
totalPage: 1,
pageTransform: ['', '', '', ''], //页面位移,三个数对应三种翻页方式
clipPath: '', // 仿真翻页
bgStyle: {},
subBgStyle: {},
},
curPage: { //本页数据
ready: false, //是否准备完毕
chapterName: '',
content: '',
pageNum: 0,
totalPage: 1,
pageTransform: ['', '', '', ''], //页面位移,三个数对应三种翻页方式
clipPath: '', // 仿真翻页
subBgStyle: {},
},
nextPage: { //下一页数据
ready: false, //是否准备完毕
chapterName: '',
content: '',
pageNum: 0,
totalPage: 1,
pageTransform: ['', '', '', ''], //页面位移,三个数对应三种翻页方式
},
showAnimation: false, //是否开启动画
showShadow: false, //是否显示页面阴影
aimdistance: 50, // 滑动翻页的最小距离
windowWidth: 0, //可用屏幕宽度
windowHeight: 0, //可用屏幕高度
statusBarHeight: 0,
contentHeight: 0, //阅读区域高度
systemTimeStr: '', //系统时间字符串
touchStartX: 0, // 触屏起始点x
touchStartY: 0, // 触屏起始点y
deltaX: 0, // 手指瞬时位移
deltaY: 0, // 手指瞬时位移
bgColorList: ['#CCE8CF', '#CCBF95', '#E1C7CA', '#3C3734'], //背景色列表
textColorList: ['#352C1D', '#352C1D', '#FFFFFF', '#352C1D'], //字体色列表
turnTypeList: ['覆盖', '平移', '仿真', '上下'],
maxFontSize: 30, //最大字体大小px
minFontSize: 14, //最小字体大小px
turnType: 0, //翻页方式 覆盖0平移1仿真2上下3
fontSize: 16, //字体大小,
simplified: 1, //是否简体
lineHeight: 1.5, //行高注意是fontSize的倍数
isDark: false, // 是否夜间模式
colorType: 0,
history: { //本书历史记录
chapterIndex: 0,
progress: 0
},
bgColor: '', // 背景色
textColor: '', // 字体色
over: 40,
turnDirect: 0, //向左向下-1 向右向上1
}
},
computed: {
...mapGetters({
curSource: 'get_curSource',
readHistorys: 'get_readHistorys',
setting: 'get_setting'
}),
//截取至整行的最大高度
innerHeight() {
let lineHeight = this.fontSize * this.lineHeight
let lineNum = Math.floor(this.contentHeight / lineHeight)
return lineNum * lineHeight
},
},
onLoad() {
// this.id = option.id
// this.title = option.title
this.getSystemInfo()
this.setPageInfo()
this.initPage()
},
onUnload() {
uni.hideLoading()
// 浏览记录
let t_progress = Math.floor((this.curPage.pageNum + 1) / this.curPage.totalPage * 100) / 100
let idx = this.readHistorys.findIndex(item => item._id === this.id)
if (idx > -1) {
let params = {
...this.readHistorys[idx],
index: this.curPage.chapterIndex,
progress: t_progress || 0,
readChapter: this.chapterData[this.curPage.chapterIndex].title,
readHistory: true
}
this.updateReadHistory(params)
}
},
methods: {
...mapMutations(['set_setting']),
...mapActions(['updateReadHistory']),
async initPage() {
uni.hideLoading()
uni.showLoading({
mask: true,
title: '章节准备中'
})
this.getChapterInfo()
await this.getChapterList()
await this.getThreeChapter(this.history.chapterIndex)
let page = Math.floor((this.curChapter.totalPage - 1) * this.history.progress)
this.goToPage(page)
uni.hideLoading()
},
getChapterInfo() {
let res = this.readHistorys.filter(item => item._id === this.id)
if (res && res.length) {
this.history = {
chapterIndex: res[0].index,
progress: res[0].progress
}
}
},
// 获取设备信息
getSystemInfo() {
let sysInfo = uni.getSystemInfoSync()
this.windowWidth = sysInfo.windowWidth
this.windowHeight = sysInfo.windowHeight
this.statusBarHeight = sysInfo.statusBarHeight
// 设置时间
// this.systemTimeStr = this.$formatTimestamp('', 'hh:mm')
// setInterval(() => {
// this.systemTimeStr = this.$formatTimestamp('', 'hh:mm')
// }, 60000)
},
// 计算页面布局信息
setPageInfo() {
this.contentHeight = this.windowHeight - 30 - 30 - 10 - this.statusBarHeight
let t_setting = this.setting
this.turnType = t_setting.hasOwnProperty('turnType') ? t_setting.turnType : this.turnType
this.fontSize = t_setting.hasOwnProperty('fontSize') ? t_setting.fontSize : this.fontSize
this.simplified = t_setting.hasOwnProperty('simplified') ? t_setting.simplified : this.simplified
this.lineHeight = t_setting.hasOwnProperty('lineHeight') ? t_setting.lineHeight : this.lineHeight
this.isDark = t_setting.hasOwnProperty('isDark') ? t_setting.isDark : this.isDark
this.colorType = t_setting.hasOwnProperty('colorType') ? t_setting.colorType : this.colorType
this.$nextTick(function() {
this.$refs.contentSetting.changeBackground(this.colorType, this.isDark)
})
},
// 获取章节列表
async getChapterList() {
let res = await this.$store.dispatch('getChapterList', this.id)
if (res) {
this.chapterData = res
}
},
// 获取章节内容
async getBookContent(idx, attrName) {
let params = this.chapterData[idx]
let res = await this.$store.dispatch('getBookContent', params)
if (res) {
// console.log(this.chapterData[idx]);
this[attrName] = {
chapterIndex: idx,
chapterName: this.chapterData[idx].title,
content: res,
}
await this.calcChapter(attrName)
}
},
// 计算章节页数
calcChapter(attrName) {
const self = this
return new Promise((resolve, reject) => {
const query = uni.createSelectorQuery().in(this)
this.$nextTick(function() {
query.select(`#${attrName} .book-inner`).boundingClientRect(data => {
if (data.height) {
let totalPage = Math.ceil(data.height / self.innerHeight)
self[attrName].totalPage = totalPage || 1
self[attrName].ready = true //章节准备完毕
resolve(true)
} else {
resolve(false)
}
}).exec()
})
})
},
// 获取三章内容
async getThreeChapter(idx) {
await this.getBookContent(idx, 'curChapter') // 获取当前章节的内容
if (idx > 0) {
await this.getBookContent(idx - 1, 'preChapter') // 获取上一个章节的内容
} else {
this.preChapter = {
ready: true,
isOver: true
}
}
if (idx < this.chapterData.length - 1) {
await this.getBookContent(idx + 1, 'nextChapter') // 获取下一个章节的内容
} else {
this.nextChapter = {
ready: true,
isOver: true
}
}
},
// 根据页码跳转
goToPage(page) {
let winW = this.windowWidth,
winH = this.windowHeight
this.showAnimation = false
this.curPage = {
...this.curChapter,
pageNum: page < this.curChapter.totalPage - 1 ? page : this.curChapter.totalPage - 1,
pageTransform: [
`translate(0,0)`,
`translate(0,0)`,
`translate(0,0)`,
`translate(0,0)`
],
clipPath: '', // 仿真翻页
bgStyle: {},
subBgStyle: {},
}
let perCommon = {
pageTransform: [
`translate(${-winW}px,0)`,
`translate(${-winW}px,0)`,
`translate(${-winW}px,0)`,
`translate(0,${-winH}px)`
],
clipPath: '', // 仿真翻页
bgStyle: {},
subBgStyle: {},
}
// 翻到章节的第一页并且没有上一章
if (page === 0 && this.preChapter.isOver) {
this.prePage = {
ready: true,
isOver: true,
...perCommon
}
}
// 翻到章节的第一页并且有上一章
else if (page === 0 && !this.preChapter.isOver) {
this.prePage = {
...this.preChapter,
pageNum: this.preChapter.totalPage - 1,
...perCommon
}
}
//还有上一页
else {
this.prePage = {
...this.curChapter,
pageNum: page - 1,
...perCommon
}
}
let nextCommon = {
pageTransform: [
`translate(0,0)`,
`translate(${winW}px,0)`,
`translate(0,0)`,
`translate(0,0)`
],
}
// 翻到章节的最后一页并且没有下一章
if (page >= this.curChapter.totalPage - 1 && this.nextChapter.isOver) {
this.nextPage = {
ready: true,
isOver: true,
...nextCommon
}
}
// 翻到章节的最后一页并且有下一章
else if (page >= this.curChapter.totalPage - 1 && !this.nextChapter.isOver) {
this.nextPage = {
...this.nextChapter,
pageNum: 0,
...nextCommon
}
}
//还有下一页
else {
this.nextPage = {
...this.curPage,
pageNum: page + 1,
...nextCommon
}
}
},
// 章节轮换
async chapterRotate(type) {
uni.showLoading({
mask: true,
title: '章节准备中'
})
let curIndex = 0
switch (type) {
// 上一章
case 'pre':
this.nextChapter = JSON.parse(JSON.stringify(this.curChapter))
this.curChapter = JSON.parse(JSON.stringify(this.preChapter))
curIndex = this.curChapter.chapterIndex
if (curIndex > 0) {
await this.getBookContent(curIndex - 1, 'preChapter') // 获取上一个章节的内容
} else {
this.preChapter = {
ready: true,
isOver: true
}
}
break
// 下一章
case 'next':
this.preChapter = JSON.parse(JSON.stringify(this.curChapter))
this.curChapter = JSON.parse(JSON.stringify(this.nextChapter))
curIndex = this.curChapter.chapterIndex
if (curIndex < this.chapterData.length - 1) {
await this.getBookContent(curIndex + 1, 'nextChapter') // 获取下一个章节的内容
} else {
this.nextChapter = {
ready: true,
isOver: true
}
}
break
}
uni.hideLoading()
},
// 去上一页
// num: 页码
async goPrePage(num) {
let winW = this.windowWidth
let winH = this.windowHeight
if (num === -1) {
if (this.preChapter.ready && this.preChapter.isOver) {
return
}
await this.chapterRotate('pre')
num = this.curChapter.totalPage - 1
}
let pre = [].concat(this.prePage.pageTransform)
let cur = [].concat(this.curPage.pageTransform)
let tClipPath = this.prePage.clipPath
let tBgStyle = JSON.parse(JSON.stringify(this.prePage.bgStyle))
let tSubBgStyle = JSON.parse(JSON.stringify(this.prePage.subBgStyle))
this.goToPage(num)
this.prePage.pageTransform = [
`translate(${-winW}px,0)`,
`translate(${-winW}px,0)`,
`translate(${-winW}px,0)`,
`translate(0,${-winH}px)`
]
this.curPage.pageTransform = pre
this.nextPage.pageTransform = cur
if (this.turnType === 2) {
this.curPage.clipPath = tClipPath
this.curPage.bgStyle = tBgStyle
this.curPage.subBgStyle = tSubBgStyle
}
setTimeout(() => {
this.pageReset('pre')
}, 50)
},
// 去下一页
// num: 页码
async goNextPage(num) {
let winW = this.windowWidth
let winH = this.windowHeight
if (num === this.curPage.totalPage) {
if (this.nextChapter.ready && this.nextChapter.isOver) {
return
}
await this.chapterRotate('next')
num = 0
}
let cur = [].concat(this.curPage.pageTransform)
let next = [].concat(this.nextPage.pageTransform)
let tClipPath = this.curPage.clipPath
let tBgStyle = JSON.parse(JSON.stringify(this.curPage.bgStyle))
let tSubBgStyle = JSON.parse(JSON.stringify(this.curPage.subBgStyle))
this.goToPage(num)
this.prePage.pageTransform = cur
this.curPage.pageTransform = next
this.nextPage.pageTransform = [
`translate(0,0)`,
`translate(${winW}px,0)`,
`translate(0,0)`,
'translate(0,0)'
]
if (this.turnType === 2) {
this.prePage.clipPath = tClipPath
this.prePage.bgStyle = tBgStyle
this.prePage.subBgStyle = tSubBgStyle
}
setTimeout(() => {
this.pageReset('next')
}, 50)
},
touchStart(touche) {
this.showAnimation = false
this.touchStartX = touche.clientX;
this.touchStartY = touche.clientY;
},
touchMove(touche) {
this.showShadow = true;
let deltaX = touche.clientX - this.touchStartX
let deltaY = touche.clientY - this.touchStartY
let winW = this.windowWidth
let winH = this.windowHeight
// 限制边界
if (this.turnType !== 2) {
deltaX = Math.abs(deltaX) < winW ? deltaX : deltaX / Math.abs(deltaX) * winW
deltaY = Math.abs(deltaY) < winH ? deltaY : deltaY / Math.abs(deltaY) * winH
}
let delta = this.getTouche(deltaX, deltaY)
if (Math.abs(delta) < 5) {
return
}
if (this.turnDirect === 0) {
this.turnDirect = delta / Math.abs(delta)
}
let attrName = this.turnDirect > 0 ? 'prePage' : 'nextPage'
if (this[attrName].ready && this[attrName].isOver) { //翻至封面了
this.deltaX = 0
this.deltaY = 0
return
}
this.deltaX = deltaX;
this.deltaY = deltaY;
let over = this.over
if (this.turnDirect < 0) {
// console.log('向下翻页');
// 向下翻页
if (this.turnType === 2) {
let absDeltaY = Math.abs(deltaY)
let symbolY = deltaY / absDeltaY
let posX1 = winW + deltaX - (symbolY === 1 ? absDeltaY : 0)
let posX2 = winW + deltaX - (symbolY === 1 ? 0 : absDeltaY)
if (posX1 <= 0 || posX2 <= 0) {
return
}
let rotateZ = ((Math.atan2(winH * 0.5, absDeltaY) * 180 / Math.PI))
rotateZ = (90 - rotateZ).toFixed(0)
let blurDis = 1 - ((posX1 + posX2) / (winW * 2))
blurDis = blurDis < 0.4 ? 0.4 : blurDis
this.curPage.clipPath =
`polygon(0px 0px, ${posX1}px 0px, ${posX2}px ${winH}px, 0px ${winH}px)`
this.curPage.bgStyle = {
filter: `drop-shadow(0px 0px ${40*blurDis}px rgba(0, 0, 0, 0.8))`,
transform: `rotateY(180deg) rotateZ(${rotateZ*symbolY}deg)`,
transformOrigin: `${winW + deltaX - absDeltaY*0.5}px 50%`,
display: 'block'
}
this.curPage.subBgStyle = {
clipPath: `polygon(${posX1}px ${-over}px, ${winW+over}px ${-over}px, ${winW+over}px ${winH+over}px, ${posX2}px ${winH+over}px)`,
boxShadow: `inset -10px 0 20px 0px rgba(0,0,0,0.1)`,
}
}
this.curPage.pageTransform = [
`translate(${deltaX}px,0)`,
`translate(${deltaX}px,0)`,
`translate(0,0)`,
`translate(0,${deltaY}px)`
]
this.nextPage.pageTransform = [
`translate(0,0)`,
`translate(${winW+deltaX}px,0)`,
`translate(0,0)`,
`translate(0,0)`
]
} else {
// console.log('向上翻页');
// 向上翻页
if (this.turnType === 2) {
let posX = touche.clientX
if (posX <= 0) {
return
}
let blurDis = 1 - (posX / winW)
blurDis = blurDis < 0.4 ? 0.4 : blurDis
this.prePage.clipPath =
`polygon(0px 0px, ${posX}px 0px, ${posX}px ${winH}px, 0px ${winH}px)`
this.prePage.bgStyle = {
filter: `drop-shadow(0px 0px ${40*blurDis}px rgba(0, 0, 0, 0.8))`,
transform: `rotateY(180deg) rotateZ(0deg)`,
transformOrigin: `${posX}px 50%`,
display: 'block'
}
this.prePage.subBgStyle = {
clipPath: `polygon(${posX}px ${-over}px, ${winW+over}px ${-over}px, ${winW+over}px ${winH+over}px, ${posX}px ${winH+over}px)`,
boxShadow: `inset -10px 0 20px 0px rgba(0,0,0,0.1)`,
}
}
this.prePage.pageTransform = [
`translate(${-winW+deltaX}px,0)`,
`translate(${-winW+deltaX}px,0)`,
`translate(0,0)`,
`translate(0,${-winH+deltaY}px)`
]
this.curPage.pageTransform = [
`translate(0,0)`,
`translate(${deltaX}px,0)`,
`translate(0,0)`,
`translate(0,0)`
]
}
},
async touchEnd(touche) {
let winW = this.windowWidth
let winH = this.windowHeight
let touchStart = this.getTouche(this.touchStartX, this.touchStartY)
let client = this.getTouche(touche.clientX, touche.clientY)
let delta = client - touchStart
if (Math.abs(delta) < 0.8) { //部分手机点击屏幕时无法做到delta===0
if (touche.clientX < winW / 3) { //点击屏幕左1/3为上一页
this.showAnimation = true
this.showShadow = false
this.goPrePage(this.curPage.pageNum - 1)
} else if (touche.clientX > winW / 3 * 2) { //点击屏幕右1/3为下一页
this.showAnimation = true
this.showShadow = false
this.goNextPage(this.curPage.pageNum + 1)
} else { //点击屏幕中间1/3为呼出菜单
if (this.turnDirect === 0) {
this.showMenu()
}
}
} else {
let symbol = delta / Math.abs(delta)
// 如果小于最小允许翻页的距离
if (Math.abs(delta) < this.aimdistance) {
this.pageReset(symbol < 0 ? 'pre' : 'next')
return
}
this.showShadow = false
if (symbol < 0) {
this.goNextPage(this.curPage.pageNum + 1)
} else {
this.goPrePage(this.curPage.pageNum - 1)
}
}
},
touchCancel(e) {
this.pageReset()
},
pageReset(type = 'next') {
let over = this.over
// let type1 = this.turnDirect > 0 ? 'next' : 'pre'
// console.log(type, type1);
this.turnDirect = 0
this.showAnimation = true
this.showShadow = false
//取消翻页,重置页面
let winW = this.windowWidth,
winH = this.windowHeight
this.prePage.pageTransform = [
`translate(${-winW}px,0)`,
`translate(${-winW}px,0)`,
`translate(${type==='next'?0:-winW}px,0)`,
`translate(0,${-winH}px)`
]
this.curPage.pageTransform = [
`translate(0,0)`,
`translate(0,0)`,
`translate(0,0)`,
`translate(0,0)`
]
this.nextPage.pageTransform = [
`translate(0,0)`,
`translate(${winW}px,0)`,
`translate(0,0)`,
`translate(0,0)`
]
if (this.turnType === 2) {
let posX = type === 'next' ? 0 : winW
let prop = type === 'next' ? 'prePage' : 'curPage'
this[prop].clipPath =
`polygon(0px 0px, ${posX}px 0px, ${posX}px ${winH}px, 0px ${winH}px)`
this[prop].bgStyle = {
transform: `rotateY(180deg) rotateZ(0deg)`,
transformOrigin: `${posX}px 50%`,
display: 'block'
}
this[prop].subBgStyle = {
clipPath: `polygon(${posX}px ${-over}px, ${winW+over}px ${-over}px, ${winW+over}px ${winH+over}px, ${posX}px ${winH+over}px)`,
boxShadow: `inset -10px 0 20px 0px rgba(0,0,0,0.1)`,
}
}
setTimeout(() => {
this.prePage.pageTransform = [
`translate(${-winW}px,0)`,
`translate(${-winW}px,0)`,
`translate(${-winW}px,0)`,
`translate(0,${-winH}px)`
]
}, 100)
},
getTouche(x, y) {
let result = 0
switch (this.turnType) {
// 覆盖
case 0:
result = x
break
// 平移
case 1:
result = x
break
// 仿真
case 2:
result = x
break
// 上下
case 3:
result = y
break
}
return result
},
// 显示操作菜单
showMenu() {
this.$refs.curChapter.setFullscreen(false)
this.$refs.contentSetting.show()
},
// 隐藏操作菜单
hideMenu() {
this.$refs.curChapter.setFullscreen(true)
},
updateProps(res) {
res.forEach(item => {
this[item[0]] = item[1]
if (item.length === 2 || (item.length > 2 && !item[2])) {
this.updateSetting(item[0])
}
})
},
async updatePage() {
await this.calcChapter('preChapter')
await this.calcChapter('curChapter')
await this.calcChapter('nextChapter')
this.goToPage(this.curPage.pageNum)
},
updateSetting(prop) {
let t_setting = Object.assign({}, this.setting || {})
t_setting[prop] = this[prop]
this.set_setting(t_setting)
},
updateCatalog(index) {
let idx = this.readHistorys.findIndex(item => item._id = this.id)
if (idx > -1) {
let params = {
...this.readHistorys[idx],
index: index,
progress: 0,
readChapter: this.chapterData[index].title
}
this.updateReadHistory(params)
}
this.initPage()
},
moreHandle() {
uni.redirectTo({
url: '/pages/public/bookDetail?id=' + this.id
})
},
listenHandle() {
this.$refs.audioPlay.show()
},
getAudioPlayShow(data) {
this.show = data
},
}
}
</script>
<style lang="scss">
.page {
position: absolute;
width: 100%;
height: 100%;
}
.hidden {
opacity: 0;
}
</style>

View File

@@ -0,0 +1,461 @@
<template>
<view>
<view class="fixed menu" v-show="menuFlag" @touchend="hide">
<cus-navbar :style="{boxShadow: `0 2rpx 8rpx ${textColor}29`}" :bgColor="bgColor" :titleColor="textColor">
<block slot="right">
<text class="iconfont icon-gengduo" @tap.stop.prevent="moreHandle"></text>
</block>
</cus-navbar>
<view class="bottom"
:style="{backgroundColor:bgColor,color:textColor,boxShadow: `0 -2rpx 8rpx ${textColor}29`}">
<view class="item" @touchend.prevent.stop="catalogHandle">
<text class="iconfont icon-caidan1"></text>
<text>目录</text>
</view>
<view class="item" @touchend.prevent.stop="darkHandle">
<text class="iconfont" :class="isDark?'icon-rijian':'icon-yejian'"></text>
<text>{{isDark?'日间':'夜间'}}</text>
</view>
<view class="item" @touchend.prevent.stop="cacheHandle">
<text v-if="!isCacheing" class="iconfont icon-xiazai"></text>
<text v-else>{{curCaches.length}}/{{chapterData.length}}</text>
<text>{{curCaches.length!==chapterData.length?'缓存':'已缓存'}}</text>
</view>
<view class="item" @touchend.prevent.stop="settingHandle">
<text class="iconfont icon-shezhi"></text>
<text>设置</text>
</view>
</view>
<!-- 听书 -->
<view v-if="!settingFlag" class="listen" :style="{color:bgColor}" @touchend.prevent.stop="listenHandle">
</view>
<!-- 设置 -->
<view class="setting" v-show="settingFlag"
:style="{backgroundColor:bgColor,color:textColor,boxShadow: `0 -2rpx 8rpx ${textColor}29`}"
@touchend.stop>
<view class="line">
<view class="title">字号</view>
<cus-button data-str="-" bgColor="rgba(0,0,0,.1)" :textColor="textColor" width="60px"
fontSize="14px" :disabled="fontSize<=minFontSize" :border="false" @click="changeFontSize(-2)">A
</cus-button>
<view style="margin: 0 8px;">{{fontSize}}</view>
<cus-button data-str="+" bgColor="rgba(0,0,0,.1)" :textColor="textColor" width="60px"
fontSize="14px" :disabled="fontSize>=maxFontSize" :border="false" @click="changeFontSize(2)">A
</cus-button>
</view>
<view class="line">
<view class="title">背景</view>
<cus-button class="background" v-for="(item,index) in bgColorList" :key="index" :bgColor="item"
:border="index===colorType" @click="changeBackground(index)">
</cus-button>
</view>
<view class="line">
<view class="title">翻页</view>
<cus-button v-for="(item,index) in turnTypeList" :key="index" :bgColor="bgColor" :border="true"
:textColor="textColor" width="60px" :hairLine="index!==turnType" @click="changeTurnType(index)">
{{item}}
</cus-button>
</view>
</view>
</view>
<!-- 侧边目录 -->
<view class="fixed catalog" v-show="catalogFlag">
<view class="mask" @tap="catalogFlag=false"></view>
<view class="catalog-content" :style="{backgroundColor:bgColor,color:textColor}">
<view class="order iconfont" :class="descFlag?'icon-paixu1':'icon-paixu'"
@touchend.prevent.stop="sortHandle">
</view>
<scroll-view class="scroll-view" scroll-y="true" :scroll-into-view="'siv'+sivId">
<view :class="{'reverse':descFlag}">
<view class="item" :id="'siv'+index"
:class="{'text-gray':curCaches.includes(item.index),'text-red':chapterIndex===index}"
v-for="(item,index) in chapterData" :key="item.index" @tap="clickCatalog(index)">
<view class="chapter-name">{{item.title}}</view>
<view v-if="curCaches.includes(item.index)">已缓存</view>
</view>
</view>
</scroll-view>
</view>
</view>
</view>
</template>
<script>
import {
mapGetters,
mapMutations
}
from 'vuex'
export default {
return: {
// 背景色
bgColor: {
type: String,
default: '#cdeeda'
},
// 字体颜色
textColor: {
type: String,
default: '#000'
},
// 夜间模式背景色
darkBgColor: {
type: String,
default: '#000000'
},
// 夜间模式字体颜色
darkTextColor: {
type: String,
default: '#666666'
},
// 是否是夜间模式
isDark: {
type: Boolean,
default: false
},
// 字体大小
fontSize: {
type: Number,
default: 16
},
// 背景颜色列表
bgColorList: {
type: Array,
default: []
},
// 字体颜色列表
textColorList: {
type: Array,
default: []
},
// 翻页方式列表
turnTypeList: {
type: Array,
default: []
},
// 颜色类别,对应颜色列表的下标
colorType: {
type: Number,
default: 0
},
// 翻页方式类别,对应翻页方式列表的下标
turnType: {
type: Number,
default: 0
},
// 章节列表
chapterData: {
type: Array,
default: []
},
// 当前章节的下标
chapterIndex: {
type: Number,
default: 0
},
},
data() {
return {
menuFlag: false, // 控制菜单的显隐
settingFlag: false, // 控制设置的显隐
minFontSize: 15,
maxFontSize: 40,
catalogFlag: false, // 控制目录的显隐
descFlag: false, // 是否倒序排序
sivId: null, // 打开目录时跳转至位置
curCaches: [], // 缓存的章节id
isCacheing: false, // 是否缓存中
}
},
computed: {
...mapGetters({
setting: 'get_setting',
curSource: 'get_curSource',
}),
},
watch: {
curCaches: {
handler(nVal, oVal) {
if (nVal.length === this.chapterData.length) {
this.isCacheing = false
}
},
deep: true,
immediate: true
}
},
methods: {
...mapMutations(['set_setting']),
// 显示操作菜单
async show() {
this.menuFlag = true
if (this.chapterData && this.chapterData.length > 0) {
// 从缓存获取信息
// bookChapterCache
let bcc = await this.$fileReader(`_bcc_${this.curSource.value}_${this.chapterData[0].id}`)
this.curCaches = JSON.parse(bcc || '[]')
}
},
hide() {
this.menuFlag = false
this.settingFlag = false
this.$emit('whenHide')
},
// 章节目录的显示
catalogHandle() {
this.catalogFlag = true
this.settingFlag = false
this.hide()
this.$nextTick(function() {
this.sivId = this.chapterIndex === 0 ? 0 : this.chapterIndex - 5
})
},
// 夜间/日间
darkHandle() {
this.$emit('updateProps', [
['isDark', !this.isDark],
])
this.changeBackground(this.colorType, !this.isDark)
},
// 缓存
async cacheHandle() {
if (this.isCacheing) {
return
}
if (this.curCaches.length === this.chapterData.length) {
return
}
this.isCacheing = true
for (let chapter of this.chapterData) {
if (this.curCaches.includes(chapter.index)) {
continue
}
await this.doBookContent(chapter)
}
this.isCacheing = false
},
async doBookContent(params, num = 0) {
if (num === 20) {
return false
}
let res = await this.$store.dispatch('getBookContent', params)
if (res) {
this.curCaches.push(params.index)
return true
} else {
num += 1
await this.doBookContent(params, num)
}
},
// 设置
settingHandle() {
this.settingFlag = !this.settingFlag
},
// 听书
listenHandle() {
this.$emit('listenHandle')
},
changeFontSize(num) {
this.$emit('updateProps', [
['fontSize', this.fontSize + num],
])
this.$emit('updatePage')
},
changeBackground(index, isDark) {
if (isDark) {
this.$emit('updateProps', [
['bgColor', this.darkBgColor],
['textColor', this.darkTextColor]
])
} else {
this.$emit('updateProps', [
['bgColor', this.bgColorList[index], true],
['textColor', this.textColorList[index], true],
['colorType', index],
])
}
},
changeTurnType(index) {
this.$emit('updateProps', [
['turnType', index],
])
},
// 点击某一章节目录
async clickCatalog(index) {
this.catalogFlag = false
this.sivId = index
this.$emit('updateCatalog', index)
},
// 排序
sortHandle() {
this.descFlag = !this.descFlag
},
moreHandle() {
this.$emit('moreHandle')
},
}
}
</script>
<style lang="scss">
.fixed {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: transparent;
z-index: 1024;
}
.menu {
.bottom {
position: absolute;
width: 100%;
height: 80px;
bottom: 0;
display: flex;
justify-content: space-around;
align-items: center;
font-size: 11px;
.item {
width: 100%;
height: 80px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
.iconfont {
font-size: 21px;
}
text {
line-height: 21px;
}
}
}
.setting {
position: absolute;
bottom: 80px;
width: 100%;
padding: 20px 0;
background-color: rgba($color: #000000, $alpha: 0.8);
font-size: 11px;
.line {
padding: 0 40rpx;
display: flex;
justify-content: flex-start;
align-items: center;
.title {
margin-right: 40rpx;
}
.background+.background {
margin-left: 30px;
}
}
.line+.line {
padding-top: 24px;
}
}
}
.catalog {
width: 600rpx;
.mask {
position: fixed;
width: 100%;
height: 100%;
background-color: #000000;
opacity: 0.5;
}
.catalog-content {
position: absolute;
width: 100%;
height: 100%;
padding-top: var(--status-bar-height);
.order {
position: sticky;
top: 0;
padding: 32rpx 40rpx;
text-align: right;
font-size: 50rpx;
line-height: 56rpx;
}
.scroll-view {
height: calc(100% - 120rpx);
box-sizing: border-box;
opacity: 1;
.item {
padding: 0 10px;
height: 36px;
line-height: 36px;
font-size: 12px;
white-space: nowrap;
overflow: hidden;
display: flex;
justify-content: space-between;
align-items: center;
.chapter-name {
width: 480rpx;
overflow: hidden;
text-overflow: ellipsis;
}
}
}
}
}
.listen {
position: fixed;
bottom: 120px;
right: 20px;
width: 60px;
height: 60px;
line-height: 60px;
border-radius: 50%;
background-color: #6b6b6b;
text-align: center;
font-size: 30px;
z-index: 999;
}
.reverse {
display: flex;
flex-direction: column-reverse;
}
.text-gray {
color: #999999;
}
.text-red {
color: #ff0000;
}
</style>