mirror of
https://gitee.com/qingqing-coconut-tea/RcKtFezyRAZX
synced 2026-07-21 10:32:32 +08:00
251 lines
6.5 KiB
JavaScript
251 lines
6.5 KiB
JavaScript
// pages/article-detail/article-detail.js
|
||
// 文章详情逻辑处理
|
||
|
||
const app = getApp()
|
||
const api = require('../../services/api')
|
||
|
||
Page({
|
||
data: {
|
||
articleId: null,
|
||
article: null,
|
||
commentList: [],
|
||
commentPage: 1,
|
||
commentPageSize: 10,
|
||
commentHasMore: true,
|
||
commentLoading: false,
|
||
commentContent: '',
|
||
replyTo: null,
|
||
isFavorite: false,
|
||
isLike: false,
|
||
likeCount: 0,
|
||
favoriteCount: 0
|
||
},
|
||
|
||
onLoad(options) {
|
||
if (options.id) {
|
||
this.setData({ articleId: options.id })
|
||
this.loadArticleDetail()
|
||
this.loadCommentList()
|
||
}
|
||
},
|
||
|
||
onShow() {
|
||
if (this.data.articleId) {
|
||
api.getArticleDetail(this.data.articleId).then(res => {
|
||
if (res.code === 200 && res.data) {
|
||
this.setData({
|
||
isFavorite: res.data.isFavorite || false,
|
||
isLike: res.data.isLike || false,
|
||
likeCount: res.data.likeCount || 0,
|
||
favoriteCount: res.data.favoriteCount || 0
|
||
})
|
||
}
|
||
})
|
||
}
|
||
},
|
||
|
||
onShareAppMessage() {
|
||
const article = this.data.article
|
||
if (article) {
|
||
return {
|
||
title: article.title,
|
||
path: `/pages/article-detail/article-detail?id=${article.id}`,
|
||
imageUrl: article.coverImage
|
||
}
|
||
}
|
||
},
|
||
|
||
// 加载文章详情
|
||
loadArticleDetail() {
|
||
wx.showLoading({ title: '加载中...' })
|
||
api.getArticleDetail(this.data.articleId).then(res => {
|
||
wx.hideLoading()
|
||
if (res.code === 200) {
|
||
this.setData({
|
||
article: res.data,
|
||
isFavorite: res.data.isFavorite || false,
|
||
isLike: res.data.isLike || false,
|
||
likeCount: res.data.likeCount || 0,
|
||
favoriteCount: res.data.favoriteCount || 0
|
||
})
|
||
this.addViewCount()
|
||
} else {
|
||
wx.showToast({ title: res.message || '加载失败', icon: 'none' })
|
||
}
|
||
}).catch(() => {
|
||
wx.hideLoading()
|
||
wx.showToast({ title: '加载失败', icon: 'none' })
|
||
})
|
||
},
|
||
|
||
// 增加浏览次数
|
||
addViewCount() {
|
||
api.addViewCount({ articleId: this.data.articleId }).then(() => {
|
||
const article = this.data.article
|
||
if (article) {
|
||
article.viewCount = (article.viewCount || 0) + 1
|
||
this.setData({ article })
|
||
}
|
||
})
|
||
},
|
||
|
||
// 加载评论列表
|
||
loadCommentList() {
|
||
if (this.data.commentLoading || !this.data.commentHasMore) return
|
||
|
||
this.setData({ commentLoading: true })
|
||
|
||
api.getCommentList({
|
||
articleId: this.data.articleId,
|
||
page: this.data.commentPage,
|
||
pageSize: this.data.commentPageSize
|
||
}).then(res => {
|
||
this.setData({ commentLoading: false })
|
||
if (res.code === 200) {
|
||
const list = res.data.list || []
|
||
this.setData({
|
||
commentList: this.data.commentPage === 1 ? list : this.data.commentList.concat(list),
|
||
commentHasMore: list.length >= this.data.commentPageSize
|
||
})
|
||
}
|
||
}).catch(() => {
|
||
this.setData({ commentLoading: false })
|
||
})
|
||
},
|
||
|
||
// 触底加载更多评论
|
||
onReachBottom() {
|
||
if (this.data.commentHasMore && !this.data.commentLoading) {
|
||
this.setData({ commentPage: this.data.commentPage + 1 })
|
||
this.loadCommentList()
|
||
}
|
||
},
|
||
|
||
// 收藏文章
|
||
onFavorite() {
|
||
if (!app.checkLogin()) return
|
||
|
||
const isFavorite = !this.data.isFavorite
|
||
api.updateFavorite({ articleId: this.data.articleId, isFavorite }).then(res => {
|
||
if (res.code === 200) {
|
||
this.setData({
|
||
isFavorite,
|
||
favoriteCount: isFavorite ? this.data.favoriteCount + 1 : this.data.favoriteCount - 1
|
||
})
|
||
wx.showToast({
|
||
title: isFavorite ? '收藏成功' : '取消收藏',
|
||
icon: 'success'
|
||
})
|
||
}
|
||
})
|
||
},
|
||
|
||
// 点赞文章
|
||
onLike() {
|
||
if (!app.checkLogin()) return
|
||
|
||
const isLike = !this.data.isLike
|
||
api.updateLike({ articleId: this.data.articleId, isLike }).then(res => {
|
||
if (res.code === 200) {
|
||
this.setData({
|
||
isLike,
|
||
likeCount: isLike ? this.data.likeCount + 1 : this.data.likeCount - 1
|
||
})
|
||
}
|
||
})
|
||
},
|
||
|
||
// 输入评论
|
||
onCommentInput(e) {
|
||
this.setData({ commentContent: e.detail.value })
|
||
},
|
||
|
||
// 提交评论
|
||
submitComment() {
|
||
if (!app.checkLogin()) return
|
||
|
||
const content = this.data.commentContent.trim()
|
||
if (!content) {
|
||
wx.showToast({ title: '请输入评论内容', icon: 'none' })
|
||
return
|
||
}
|
||
|
||
api.addComment({
|
||
articleId: this.data.articleId,
|
||
content,
|
||
parentId: this.data.replyTo ? this.data.replyTo.id : null
|
||
}).then(res => {
|
||
if (res.code === 200) {
|
||
wx.showToast({ title: '评论成功', icon: 'success' })
|
||
this.setData({ commentContent: '', replyTo: null })
|
||
this.refreshCommentList()
|
||
} else {
|
||
wx.showToast({ title: res.message || '评论失败', icon: 'none' })
|
||
}
|
||
})
|
||
},
|
||
|
||
// 刷新评论列表
|
||
refreshCommentList() {
|
||
this.setData({ commentPage: 1, commentList: [], commentHasMore: true })
|
||
this.loadCommentList()
|
||
},
|
||
|
||
// 回复评论
|
||
onReply(e) {
|
||
const comment = e.currentTarget.dataset.comment
|
||
this.setData({ replyTo: comment })
|
||
wx.showModal({
|
||
title: '回复 ' + comment.nickname,
|
||
editable: true,
|
||
placeholderText: '请输入回复内容',
|
||
success: res => {
|
||
if (res.confirm && res.content) {
|
||
api.addComment({
|
||
articleId: this.data.articleId,
|
||
content: res.content,
|
||
parentId: comment.id
|
||
}).then(res => {
|
||
if (res.code === 200) {
|
||
wx.showToast({ title: '回复成功', icon: 'success' })
|
||
this.refreshCommentList()
|
||
}
|
||
})
|
||
}
|
||
}
|
||
})
|
||
},
|
||
|
||
// 复制链接
|
||
onCopyLink() {
|
||
wx.setClipboardData({
|
||
data: this.data.article.shareUrl || '',
|
||
success: () => {
|
||
wx.showToast({ title: '链接已复制', icon: 'success' })
|
||
}
|
||
})
|
||
},
|
||
|
||
// 关注作者
|
||
onFollow() {
|
||
if (!app.checkLogin()) return
|
||
|
||
const article = this.data.article
|
||
if (!article || !article.authorId) return
|
||
|
||
api.followUser({ userId: article.authorId }).then(res => {
|
||
if (res.code === 200) {
|
||
wx.showToast({ title: '关注成功', icon: 'success' })
|
||
article.isFollowing = true
|
||
this.setData({ article })
|
||
}
|
||
})
|
||
}
|
||
})
|
||
|
||
/**
|
||
* @联系作者:16768118056
|
||
* @描述:(此项目非免费分享)源码百分百可用,搞定毕设不发愁,支持远程调试安装、二次开发、定制、讲解、文档类。
|
||
* @访问:https://www.notmaker.com/detail/8326a897119d4542896c863a69d7bbe7/gtt20260415,查看完整运行演示。
|
||
*/
|