优化:请求接口全局处理loading和错误提示

This commit is contained in:
2025-11-26 16:37:47 +08:00
24 changed files with 568 additions and 771 deletions

View File

@@ -230,70 +230,43 @@ function initScrollHeight() {
// 加载书籍详情
async function loadBookInfo() {
try {
const res = await bookApi.getBookInfo(bookId.value)
if (res.bookInfo) {
bookInfo.value = res.bookInfo
}
} catch (error) {
console.error('Failed to load book info:', error)
}
const res = await bookApi.getBookInfo(bookId.value)
bookInfo.value = res.bookInfo
}
// 加载购买商品信息
async function loadGoodsInfo() {
try {
const res = await bookApi.getBookGoods(bookId.value)
if (res.code === 0) {
goodsList.value = res.productList || []
}
} catch (error) {
console.error('Failed to load goods info:', error)
}
const res = await bookApi.getBookGoods(bookId.value)
goodsList.value = res.productList || []
}
// 加载统计数据
async function loadBookCount() {
try {
const res = await bookApi.getBookReadCount(bookId.value)
if (res.code === 0) {
readCount.value = res.readCount || 0
listenCount.value = res.listenCount || 0
buyCount.value = res.buyCount || 0
}
} catch (error) {
console.error('Failed to load book count:', error)
const res = await bookApi.getBookReadCount(bookId.value)
if (res.code === 0) {
readCount.value = res.readCount || 0
listenCount.value = res.listenCount || 0
buyCount.value = res.buyCount || 0
}
}
// 加载评论
async function loadComments() {
try {
const res = await bookApi.getBookComments(bookId.value, 1, 10)
if (res.commentsTree && res.commentsTree.length > 0) {
commentList.value = res.commentsTree
} else {
nullText.value = t('common.data_null')
}
} catch (error) {
const res = await bookApi.getBookComments(bookId.value, 1, 10)
if (res.commentsTree && res.commentsTree.length > 0) {
commentList.value = res.commentsTree
} else {
nullText.value = t('common.data_null')
console.error('Failed to load comments:', error)
}
}
// 加载推荐书籍
async function loadRecommendBooks() {
try {
const res = await bookApi.getRecommendBook(bookId.value)
if (res.bookList && res.bookList.length > 0) {
relatedBooks.value = res.bookList
} else {
nullBookText.value = t('common.data_null')
}
} catch (error) {
const res = await bookApi.getRecommendBook(bookId.value)
if (res.bookList && res.bookList.length > 0) {
relatedBooks.value = res.bookList
} else {
nullBookText.value = t('common.data_null')
console.error('Failed to load recommend books:', error)
}
}

View File

@@ -219,13 +219,9 @@ const vipInfo = ref<IVipInfo | null>(null)
* 获取VIP信息
*/
const getVipInfo = async () => {
try {
const res = await homeApi.getVipInfo()
if (res.vipInfo) {
vipInfo.value = res.vipInfo
}
} catch (error) {
console.error('获取VIP信息失败:', error)
const res = await homeApi.getVipInfo()
if (res.vipInfo) {
vipInfo.value = res.vipInfo
}
}
@@ -233,21 +229,17 @@ const getVipInfo = async () => {
* 获取我的书单
*/
const getMyBooks = async () => {
try {
const res = await homeApi.getMyBooks(1, 10)
if (res && res.code === 0) {
showMyBooks.value = true
if (res.page.records && res.page.records.length > 0) {
myBooksList.value = res.page.records
}
} else {
// 未登录,跳转到登录页
uni.navigateTo({
url: '/pages/login/login'
})
const res = await homeApi.getMyBooks(1, 10)
if (res && res.code === 0) {
showMyBooks.value = true
if (res.page.records && res.page.records.length > 0) {
myBooksList.value = res.page.records
}
} catch (error) {
console.error('获取我的书单失败:', error)
} else {
// 未登录,跳转到登录页
uni.navigateTo({
url: '/pages/login/login'
})
}
}
@@ -255,13 +247,9 @@ const getMyBooks = async () => {
* 获取推荐图书
*/
const getRecommendBooks = async () => {
try {
const res = await homeApi.getRecommendBooks()
if (res.books && res.books.length > 0) {
recommendBooksList.value = res.books
}
} catch (error) {
console.error('获取推荐图书失败:', error)
const res = await homeApi.getRecommendBooks()
if (res.books && res.books.length > 0) {
recommendBooksList.value = res.books
}
}
@@ -269,16 +257,12 @@ const getRecommendBooks = async () => {
* 获取活动标签列表
*/
const getActivityLabels = async () => {
try {
const res = await homeApi.getBookLabelList(1)
showActivity.value = true
if (res.lableList && res.lableList.length > 0) {
activityLabelList.value = res.lableList
// 默认加载第一个标签的图书列表
await getBooksByLabel(res.lableList[0].id, 'activity')
}
} catch (error) {
console.error('获取活动标签失败:', error)
const res = await homeApi.getBookLabelList(1)
showActivity.value = true
if (res.lableList && res.lableList.length > 0) {
activityLabelList.value = res.lableList
// 默认加载第一个标签的图书列表
await getBooksByLabel(res.lableList[0].id, 'activity')
}
}
@@ -286,16 +270,12 @@ const getActivityLabels = async () => {
* 获取分类标签列表
*/
const getCategoryLabels = async () => {
try {
const res = await homeApi.getBookLabelList(0)
showCategory.value = true
if (res.lableList && res.lableList.length > 0) {
categoryLevel1List.value = res.lableList
// 默认加载第一个标签的二级标签
await getSubLabels(res.lableList[0].id, 0)
}
} catch (error) {
console.error('获取分类标签失败:', error)
const res = await homeApi.getBookLabelList(0)
showCategory.value = true
if (res.lableList && res.lableList.length > 0) {
categoryLevel1List.value = res.lableList
// 默认加载第一个标签的二级标签
await getSubLabels(res.lableList[0].id, 0)
}
}
@@ -303,21 +283,17 @@ const getCategoryLabels = async () => {
* 获取二级标签列表
*/
const getSubLabels = async (pid: number, index: number) => {
try {
const res = await homeApi.getSubLabelList(pid)
currentLevel1Index.value = index
if (res.lableList && res.lableList.length > 0) {
categoryLevel2List.value = res.lableList
currentLevel2Index.value = 0
// 加载第一个二级标签的图书列表
await getBooksByLabel(res.lableList[0].id, 'category')
} else {
// 没有二级标签,直接加载一级标签的图书列表
categoryLevel2List.value = []
await getBooksByLabel(pid, 'category')
}
} catch (error) {
console.error('获取二级标签失败:', error)
const res = await homeApi.getSubLabelList(pid)
currentLevel1Index.value = index
if (res.lableList && res.lableList.length > 0) {
categoryLevel2List.value = res.lableList
currentLevel2Index.value = 0
// 加载第一个二级标签的图书列表
await getBooksByLabel(res.lableList[0].id, 'category')
} else {
// 没有二级标签,直接加载一级标签的图书列表
categoryLevel2List.value = []
await getBooksByLabel(pid, 'category')
}
}
@@ -328,23 +304,19 @@ const getBooksByLabel = async (
labelId: number,
type: 'activity' | 'category'
) => {
try {
const res = await homeApi.getBooksByLabel(labelId)
if (type === 'activity') {
if (res.bookList && res.bookList.length > 0) {
activityList.value = res.bookList
} else {
activityList.value = []
}
const res = await homeApi.getBooksByLabel(labelId)
if (type === 'activity') {
if (res.bookList && res.bookList.length > 0) {
activityList.value = res.bookList
} else {
if (res.bookList && res.bookList.length > 0) {
categoryBookList.value = res.bookList
} else {
categoryBookList.value = []
}
activityList.value = []
}
} else {
if (res.bookList && res.bookList.length > 0) {
categoryBookList.value = res.bookList
} else {
categoryBookList.value = []
}
} catch (error) {
console.error('获取图书列表失败:', error)
}
}

View File

@@ -117,31 +117,20 @@ function initScrollHeight() {
// 加载书籍信息
async function loadBookInfo() {
try {
const res = await bookApi.getBookInfo(bookId.value)
if (res.bookInfo) {
bookInfo.value = res.bookInfo
}
} catch (error) {
console.error('Failed to load book info:', error)
}
const res = await bookApi.getBookInfo(bookId.value)
bookInfo.value = res.bookInfo
}
// 加载章节列表
async function loadChapterList() {
try {
const res = await bookApi.getBookChapter({
bookId: bookId.value
})
if (res.chapterList && res.chapterList.length > 0) {
chapterList.value = res.chapterList
} else {
nullText.value = t('common.data_null')
}
} catch (error) {
const res = await bookApi.getBookChapter({
bookId: bookId.value
})
if (res.chapterList && res.chapterList.length > 0) {
chapterList.value = res.chapterList
} else {
nullText.value = t('common.data_null')
console.error('Failed to load chapter list:', error)
}
}

View File

@@ -245,14 +245,8 @@ function initAudioContext() {
// 加载书籍信息
async function loadBookInfo() {
try {
const res = await bookApi.getBookInfo(bookId.value)
if (res.bookInfo) {
bookInfo.value = res.bookInfo
}
} catch (error) {
console.error('Failed to load book info:', error)
}
const res = await bookApi.getBookInfo(bookId.value)
bookInfo.value = res.bookInfo
}
// 加载章节列表

View File

@@ -164,13 +164,9 @@ function initScrollHeight() {
// 加载书籍信息
async function loadBookInfo() {
try {
const res = await bookApi.getBookInfo(bookId.value)
if (res.bookInfo) {
bookInfo.value = res.bookInfo
}
} catch (error) {
console.error('Failed to load book info:', error)
const res = await bookApi.getBookInfo(bookId.value)
if (res.bookInfo) {
bookInfo.value = res.bookInfo
}
}
@@ -180,20 +176,15 @@ async function loadComments() {
return
}
try {
const res = await bookApi.getBookComments(bookId.value, page.value.current, page.value.limit)
const res = await bookApi.getBookComments(bookId.value, page.value.current, page.value.limit)
commentsCount.value = res.commentsCount || 0
if (res.commentsTree && res.commentsTree.length > 0) {
commentList.value = [...commentList.value, ...res.commentsTree]
page.value.current += 1
} else if (commentList.value.length === 0) {
nullText.value = t('common.data_null')
}
} catch (error) {
commentsCount.value = res.commentsCount || 0
if (res.commentsTree && res.commentsTree.length > 0) {
commentList.value = [...commentList.value, ...res.commentsTree]
page.value.current += 1
} else if (commentList.value.length === 0) {
nullText.value = t('common.data_null')
console.error('Failed to load comments:', error)
}
}
@@ -275,33 +266,29 @@ function handleEmj(i: any) {
// 提交评论
async function submitComment() {
try {
const content = await getEditorContent()
if (!content || content === '<p><br></p>') {
uni.showToast({
title: t('bookDetails.enterText'),
icon: 'none'
})
return
}
const pid = replyTarget.value?.id || 0
await bookApi.insertComment(bookId.value, content, pid)
const content = await getEditorContent()
if (!content || content === '<p><br></p>') {
uni.showToast({
title: t('workOrder.submit_success'),
icon: 'success',
duration: 500
title: t('bookDetails.enterText'),
icon: 'none'
})
setTimeout(() => {
editorCtx.value?.clear()
resetComments()
}, 500)
} catch (error) {
console.error('Failed to submit comment:', error)
return
}
const pid = replyTarget.value?.id || 0
await bookApi.insertComment(bookId.value, content, pid)
uni.showToast({
title: t('workOrder.submit_success'),
icon: 'success',
duration: 500
})
setTimeout(() => {
editorCtx.value?.clear()
resetComments()
}, 500)
}
// 点赞/取消点赞
@@ -314,29 +301,25 @@ async function handleLike(comment: IComment) {
return
}
try {
if (comment.isLike === 0) {
await bookApi.likeComment(comment.id)
uni.showToast({
title: t('bookDetails.supportSuccess'),
icon: 'success',
duration: 1000
})
} else {
await bookApi.unlikeComment(comment.id)
uni.showToast({
title: t('bookDetails.supportCancel'),
icon: 'success',
duration: 1000
})
}
setTimeout(() => {
resetComments()
}, 200)
} catch (error) {
console.error('Failed to like comment:', error)
if (comment.isLike === 0) {
await bookApi.likeComment(comment.id)
uni.showToast({
title: t('bookDetails.supportSuccess'),
icon: 'success',
duration: 1000
})
} else {
await bookApi.unlikeComment(comment.id)
uni.showToast({
title: t('bookDetails.supportCancel'),
icon: 'success',
duration: 1000
})
}
setTimeout(() => {
resetComments()
}, 200)
}
// 删除评论
@@ -348,20 +331,16 @@ function handleDelete(comment: IComment) {
confirmText: t('common.confirm_text'),
success: async (res) => {
if (res.confirm) {
try {
await bookApi.deleteComment(comment.id)
uni.showToast({
title: t('bookDetails.deleteSuccess'),
icon: 'success',
duration: 500
})
setTimeout(() => {
resetComments()
}, 500)
} catch (error) {
console.error('Failed to delete comment:', error)
}
await bookApi.deleteComment(comment.id)
uni.showToast({
title: t('bookDetails.deleteSuccess'),
icon: 'success',
duration: 500
})
setTimeout(() => {
resetComments()
}, 500)
}
}
})

View File

@@ -70,14 +70,8 @@ onLoad((options: any) => {
* 获取VIP信息
*/
const getVipInfo = async () => {
try {
const res = await homeApi.getVipInfo()
if (res.vipInfo) {
vipInfo.value = res.vipInfo
}
} catch (error) {
console.error('获取VIP信息失败:', error)
}
const res = await homeApi.getVipInfo()
vipInfo.value = res.vipInfo
}
/**
@@ -91,26 +85,19 @@ const handleSearch = async () => {
loading.value = true
isEmpty.value = false
try {
const res = await homeApi.searchBooks({
title: keyword.value.trim(),
page: 1,
limit: 10,
})
if (res.bookList && res.bookList.length > 0) {
searchResults.value = res.bookList
isEmpty.value = false
} else {
searchResults.value = []
isEmpty.value = true
}
} catch (error) {
console.error('搜索失败:', error)
const res = await homeApi.searchBooks({
title: keyword.value.trim(),
page: 1,
limit: 10,
})
if (res.bookList && res.bookList.length > 0) {
searchResults.value = res.bookList
isEmpty.value = false
} else {
searchResults.value = []
isEmpty.value = true
} finally {
loading.value = false
}
loading.value = false
}
/**