From df1f9be029d3d9d593cbcd6b4bf395686099f247 Mon Sep 17 00:00:00 2001 From: jeremygan2021 Date: Mon, 2 Mar 2026 20:33:40 +0800 Subject: [PATCH] hart --- backend/community/admin.py | 14 ++++++++++---- backend/community/views.py | 4 ++-- frontend/src/pages/ForumDetail.jsx | 4 ++-- miniprogram/src/subpackages/forum/detail/index.tsx | 12 ++++++------ 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/backend/community/admin.py b/backend/community/admin.py index d0b571c..2d8df09 100644 --- a/backend/community/admin.py +++ b/backend/community/admin.py @@ -211,9 +211,10 @@ class TopicAdmin(OrderableAdminMixin, ModelAdmin): list_filter = ('status', 'category', 'is_pinned', 'created_at', 'related_product', 'related_service', 'related_course') search_fields = ('title', 'content', 'author__nickname') autocomplete_fields = ['author', 'related_product', 'related_service', 'related_course'] + filter_horizontal = ('likes',) inlines = [TopicMediaInline, ReplyInline] actions = ['reset_ordering', 'approve_topics', 'reject_topics'] - list_editable = ('status', 'is_pinned') + list_editable = ('status', 'is_pinned', 'view_count') @admin.action(description="批量通过审核") def approve_topics(self, request, queryset): @@ -245,7 +246,7 @@ class TopicAdmin(OrderableAdminMixin, ModelAdmin): fieldsets = ( ('帖子内容', { - 'fields': ('title', 'status', 'category', 'content', 'is_pinned') + 'fields': ('title', 'status', 'category', 'content', 'is_pinned', 'likes') }), ('关联信息', { 'fields': ('author', 'related_product', 'related_service', 'related_course'), @@ -274,16 +275,17 @@ class TopicAdmin(OrderableAdminMixin, ModelAdmin): @admin.register(Reply) class ReplyAdmin(ModelAdmin): - list_display = ('short_content', 'topic', 'author', 'is_pinned', 'created_at') + list_display = ('short_content', 'topic', 'author', 'is_pinned', 'like_count', 'created_at') list_filter = ('is_pinned', 'created_at') search_fields = ('content', 'author__nickname', 'topic__title') autocomplete_fields = ['author', 'topic', 'reply_to'] + filter_horizontal = ('likes',) list_editable = ('is_pinned',) inlines = [TopicMediaInline] fieldsets = ( ('回复内容', { - 'fields': ('topic', 'reply_to', 'content') + 'fields': ('topic', 'reply_to', 'content', 'likes') }), ('发布信息', { 'fields': ('author', 'is_pinned', 'created_at') @@ -291,6 +293,10 @@ class ReplyAdmin(ModelAdmin): ) readonly_fields = ('created_at',) + @display(description="点赞数") + def like_count(self, obj): + return obj.likes.count() + @display(description="内容摘要") def short_content(self, obj): return obj.content[:30] + '...' if len(obj.content) > 30 else obj.content diff --git a/backend/community/views.py b/backend/community/views.py index 2969df5..445f7b0 100644 --- a/backend/community/views.py +++ b/backend/community/views.py @@ -284,7 +284,7 @@ class TopicViewSet(viewsets.ModelViewSet): return Response({'error': '请先登录'}, status=401) return super().create(request, *args, **kwargs) - @action(detail=True, methods=['post']) + @action(detail=True, methods=['post'], permission_classes=[permissions.IsAuthenticated]) def like(self, request, pk=None): obj = self.get_object() user = get_current_wechat_user(request) @@ -326,7 +326,7 @@ class ReplyViewSet(viewsets.ModelViewSet): return Response({'error': '请先登录'}, status=401) return super().create(request, *args, **kwargs) - @action(detail=True, methods=['post']) + @action(detail=True, methods=['post'], permission_classes=[permissions.IsAuthenticated]) def like(self, request, pk=None): obj = self.get_object() user = get_current_wechat_user(request) diff --git a/frontend/src/pages/ForumDetail.jsx b/frontend/src/pages/ForumDetail.jsx index 3f5fea0..48ce8a0 100644 --- a/frontend/src/pages/ForumDetail.jsx +++ b/frontend/src/pages/ForumDetail.jsx @@ -293,7 +293,7 @@ const ForumDetail = () => { {topic.view_count} 阅读 - + {topic.is_liked ? : } {topic.like_count || 0} 点赞 @@ -362,7 +362,7 @@ const ForumDetail = () => { {new Date(reply.created_at).toLocaleString()} - handleLikeReply(reply.id)} style={{ cursor: 'pointer' }}> + handleLikeReply(reply.id)} style={{ cursor: 'pointer', background: reply.is_liked ? 'rgba(0, 185, 107, 0.15)' : 'transparent', padding: '2px 8px', borderRadius: 4 }}> {reply.is_liked ? : } {reply.like_count || 0} diff --git a/miniprogram/src/subpackages/forum/detail/index.tsx b/miniprogram/src/subpackages/forum/detail/index.tsx index 9708198..61514ae 100644 --- a/miniprogram/src/subpackages/forum/detail/index.tsx +++ b/miniprogram/src/subpackages/forum/detail/index.tsx @@ -239,9 +239,9 @@ const ForumDetail = () => { {topic.view_count} - - - {topic.like_count || 0} + + + {topic.like_count || 0} {userInfo && topic.author === userInfo.id && ( @@ -284,9 +284,9 @@ const ForumDetail = () => { #{idx + 1} • {new Date(reply.created_at).toLocaleDateString()} - handleLikeReply(reply.id)} style={{marginRight: 10, display: 'flex', alignItems: 'center'}}> - - {reply.like_count || 0} + handleLikeReply(reply.id)} style={{marginRight: 15, display: 'flex', alignItems: 'center', padding: '4px 8px', borderRadius: 4, backgroundColor: reply.is_liked ? '#fff0f0' : 'transparent'}}> + + {reply.like_count || 0} handleReplyToUser(reply.author_info?.nickname)} style={{marginRight: 10, padding: '2px 6px', background: '#f0f0f0', borderRadius: 4}}> 回复