Files
ClassContent/历届学生/苏琪/项目练习/newApp/appFont/pages/bookinfo/read.vue
2024-09-27 02:06:13 +08:00

134 lines
2.9 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view class="read">
<scroll-view style="height: 100%;" scroll-y="true" @scroll="scroll">
<rich-text :nodes="nodes"></rich-text>
</scroll-view>
</view>
</template>
<script>
export default {
data() {
return {
bookid: null,
chapterId: null,
nodes: [],
history: localStorage.getItem("history") == null ? [] : JSON.parse(localStorage.getItem("history")),
currentIndex: null
}
},
onLoad(data) {
this.chapterId = data.chapterId;
this.bookid = data.bookid;
var status = true;
if(this.history.length != 0){
this.history.forEach((val,index) => {
// 如果该书已经被阅读过,那么status true,没有阅读 false
if (val.bookid == data.bookid) {
this.currentIndex = index
status = true
} else {
status = false
}
})
if (status) {
this.chapterId = this.history[this.currentIndex].chapterId;
this.bookid = data.bookid;
} else {
this.history.push({
bookid: data.bookid,
chapterId: data.chapterId
})
localStorage.setItem("history", JSON.stringify(this.history))
}
}
this.getReadList();
},
onReachBottom() {
this.getReadList();
},
methods: {
scroll(e) {
console.log(e)
},
async getReadList () {
if(this.history.length == 0){
this.history.push({
bookid: this.bookid,
chapterId: this.chapterId
})
localStorage.setItem("history", JSON.stringify(this.history))
} else {
this.history.forEach((val,index) => {
// 如果该书已经被阅读过,那么status true,没有阅读 false
if (val.bookid == this.bookid) {
this.currentIndex = index
status = true
} else {
status = false
}
});
if (status) {
this.history[this.currentIndex].chapterId = this.chapterId;
localStorage.setItem("history", JSON.stringify(this.history))
} else {
this.history.push({
bookid: this.bookid,
chapterId: this.chapterId
})
localStorage.setItem("history", JSON.stringify(this.history))
}
};
let data = (await this.$http.startRead({
data: {
bookid: this.bookid,
chapterId: this.chapterId
}
})).result;
uni.setNavigationBarTitle({
title: `${data.bookName} | ${data.chapterName}`
});
data.content = JSON.parse((JSON.stringify(data.content.split("</p>"))).replace(/<p>/g, ""));
data.content.unshift(data.chapterName+data.chapterNum)
data.content.forEach((val) => {
this.nodes.push({
name: 'p',
attrs: {
style: 'margin-bottom:10px;'
},
children: [{
type: 'text',
text: val
}]
})
})
this.chapterId = data.nextChapterId
}
}
}
</script>
<style scoped lang="less">
.read {
font-size: 17px;
background: rgb(230, 224, 208);
padding: 8px;
uni-rich-text {
text-indent: 10px;
}
}
</style>