feat(comments): 实现评论点赞功能

- 在数据库和API中添加likes字段支持
- 新增评论点赞接口和前端交互逻辑
- 在前端展示点赞数并处理点赞操作
- 更新相关组件和存储逻辑以支持点赞功能
This commit is contained in:
anghunk
2026-01-22 19:12:33 +08:00
parent 048da0e2ab
commit 9c51339559
13 changed files with 242 additions and 11 deletions

View File

@@ -105,6 +105,28 @@ export class CommentItem extends Component {
},
text: '回复'
}),
this.createElement('span', {
className: 'cwd-comment-like',
children: [
this.createElement('button', {
className: 'cwd-comment-like-button',
attributes: {
type: 'button',
onClick: () => this.handleLikeComment()
},
text: '赞'
}),
this.createTextElement(
'span',
String(
typeof comment.likes === 'number' && Number.isFinite(comment.likes) && comment.likes >= 0
? comment.likes
: 0
),
'cwd-comment-like-count'
)
]
}),
this.createTextElement('span', formatRelativeTime(comment.created), 'cwd-comment-time')
]
})
@@ -259,6 +281,12 @@ export class CommentItem extends Component {
}
}
handleLikeComment() {
if (this.props.onLikeComment) {
this.props.onLikeComment(this.props.comment.id);
}
}
handleSubmitReply() {
if (this.props.onSubmitReply) {
this.props.onSubmitReply(this.props.comment.id);

View File

@@ -100,7 +100,8 @@ export class CommentList extends Component {
onSubmitReply: (commentId) => this.handleSubmitReply(commentId),
onCancelReply: () => this.handleCancelReply(),
onUpdateReplyContent: (content) => this.handleUpdateReplyContent(content),
onClearReplyError: () => this.handleClearReplyError()
onClearReplyError: () => this.handleClearReplyError(),
onLikeComment: (commentId) => this.handleLikeComment(commentId)
});
commentItem.render();
// 缓存 CommentItem 实例
@@ -219,6 +220,12 @@ export class CommentList extends Component {
}
}
handleLikeComment(commentId) {
if (this.props.onLikeComment) {
this.props.onLikeComment(commentId);
}
}
handlePrevPage() {
if (this.props.onPrevPage) {
this.props.onPrevPage();