优化:请求接口全局处理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

@@ -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)
}
}
})