web端优化数据加载

This commit is contained in:
Xwite
2024-10-15 14:55:40 +08:00
parent 5807dfa0b6
commit 41e2c4f177
21 changed files with 197 additions and 139 deletions
+17 -54
View File
@@ -17,7 +17,7 @@
>
<PopCatalog @getContent="getContent" class="popup" />
<template #reference>
<div class="tool-icon" :class="{ 'no-point': noPoint }">
<div class="tool-icon" :class="{ 'no-point': false }">
<div class="iconfont">&#58905;</div>
<div class="icon-text">目录</div>
</div>
@@ -318,7 +318,6 @@ const getContent = (index: number, reloadChapter = true, chapterPos = 0) => {
}
},
err => {
ElMessage({ message: '获取章节内容失败', type: 'error' })
const content = ['获取章节内容失败!']
chapterData.value.push({ index, content, title })
store.setShowContent(true)
@@ -496,61 +495,25 @@ onMounted(() => {
chapterPos,
isSeachBook,
}
/* const bookStr = localStorage.getItem(bookUrl);
if (isNullOrBlank(bookStr)) {
return setTimeout(toShelf, 500);
}
book = JSON.parse(bookStr as string);
if (
book == null ||
chapterIndex != book.chapterIndex ||
chapterPos != book.chapterPos
) {
book = {
name: bookName!!,
author: bookAuthor!!,
bookUrl,
chapterIndex,
chapterPos,
isSeachBook
};
localStorage.setItem(bookUrl, JSON.stringify(book));
} */
onResize()
window.addEventListener('resize', onResize)
loadingWrapper(
API.getChapterList(bookUrl as string).then(
res => {
if (!res.data.isSuccess) {
ElMessage({ message: res.data.errorMsg, type: 'error' })
setTimeout(toShelf, 500)
return
}
const data = res.data.data
store.setCatalog(data)
store.setReadingBook(book)
getContent(chapterIndex, true, chapterPos)
window.addEventListener('keyup', handleKeyPress)
window.addEventListener('keydown', ignoreKeyPress)
// 兼容Safari < 14
document.addEventListener('visibilitychange', onVisibilityChange)
//监听底部加载
scrollObserver = new IntersectionObserver(onReachBottom, {
rootMargin: '-100% 0% 20% 0%',
})
if (infiniteLoading.value === true)
scrollObserver.observe(loading.value)
//第二次点击同一本书 页面标题不会变化
document.title = '...'
document.title =
(name as string) + ' | ' + catalog.value[chapterIndex].title
},
err => {
ElMessage({ message: '获取书籍目录失败', type: 'error' })
throw err
},
),
store.loadWebCatalog(book).then(chapters => {
store.setReadingBook(book)
getContent(chapterIndex, true, chapterPos)
window.addEventListener('keyup', handleKeyPress)
window.addEventListener('keydown', ignoreKeyPress)
// 兼容Safari < 14
document.addEventListener('visibilitychange', onVisibilityChange)
//监听底部加载
scrollObserver = new IntersectionObserver(onReachBottom, {
rootMargin: '-100% 0% 20% 0%',
})
if (infiniteLoading.value === true) scrollObserver.observe(loading.value)
//第二次点击同一本书 页面标题不会变化
document.title = '...'
document.title = (name as string) + ' | ' + chapters[chapterIndex].title
}),
)
})
+7 -35
View File
@@ -288,11 +288,7 @@ const toDetail = (
const loadShelf = async () => {
try {
if (store.configInited === false) {
const config = await API.getReadConfig()
applyReadConfig(config)
} else {
}
//await store.loadWebConfig() called in router.beforeEach
await store.saveBookProgress()
//确保各种网络情况下同步请求先完成
await fetchBookShelfData()
@@ -304,35 +300,11 @@ const loadShelf = async () => {
}
}
const fetchBookShelfData = () => {
return API.getBookShelf().then(response => {
store.setConnectType('success')
if (response.data.isSuccess) {
//store.increaseBookNum(response.data.data.length);
store.addBooks(
response.data.data.sort(function (a, b) {
const x = a['durChapterTime'] || 0
const y = b['durChapterTime'] || 0
return y - x
}),
)
} else {
if (
response.data.errorMsg.includes('还没有添加小说') &&
shelf.value.length > 0
) {
ElNotification.warning({
title: '提示',
message: '当前书架上的书籍已经被删除',
position: 'bottom-right',
})
return store.clearBooks()
}
ElMessage.error(response.data.errorMsg ?? '后端返回格式错误!')
}
store.setConnectStatus('已连接 ' + legado_http_entry_point)
store.setNewConnect(false)
})
const fetchBookShelfData = async () => {
await store.loadBookShelf()
store.setConnectType('primary')
store.setConnectStatus('已连接 ' + legado_http_entry_point)
store.setNewConnect(false)
}
onMounted(() => {
//获取最近阅读书籍
@@ -348,7 +320,7 @@ onMounted(() => {
})
</script>
<style scoped>
<style lang="scss" scoped>
.index-wrapper {
height: 100%;
width: 100%;