134 lines
2.9 KiB
Vue
134 lines
2.9 KiB
Vue
<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>
|