From f9f4e73747a25dba0aca1a679c3151db9ac9cf34 Mon Sep 17 00:00:00 2001 From: lafay <2021211506@stu.hit.edu.cn> Date: Tue, 24 Mar 2026 05:21:46 +0800 Subject: [PATCH] feat(ProcessPostUseCase, HomeScreen, PostDetailScreen, PostService): implement share count synchronization after post sharing - Added applyShareCountUpdate method in ProcessPostUseCase to synchronize share counts across post lists and details. - Updated sharePost method in PostService to return the latest shares_count from the server. - Modified share handling in HomeScreen and PostDetailScreen to utilize the new share count synchronization logic after a successful share operation. - Enhanced error handling for share operations to improve user feedback. --- src/core/usecases/ProcessPostUseCase.ts | 29 +++++++++++++++++++++++++ src/screens/home/HomeScreen.tsx | 5 ++++- src/screens/home/PostDetailScreen.tsx | 10 ++++++++- src/services/postService.ts | 20 ++++++++++++----- 4 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/core/usecases/ProcessPostUseCase.ts b/src/core/usecases/ProcessPostUseCase.ts index fb709f7..bb42fa4 100644 --- a/src/core/usecases/ProcessPostUseCase.ts +++ b/src/core/usecases/ProcessPostUseCase.ts @@ -333,6 +333,35 @@ class ProcessPostUseCase { }); } + /** + * 分享计数上报成功后,同步各列表与详情中的分享数 + */ + applyShareCountUpdate(postId: string, sharesCount: number): void { + this.postsState.forEach((state, key) => { + const index = state.posts.findIndex((p) => p.id === postId); + if (index !== -1) { + const newPosts = [...state.posts]; + const cur = newPosts[index]; + newPosts[index] = { + ...cur, + sharesCount, + shares_count: sharesCount, + }; + this.updatePostsState(key, { posts: newPosts }); + } + }); + const detail = this.getPostDetailState(postId); + if (detail.post) { + this.updatePostDetailState(postId, { + post: { + ...detail.post, + sharesCount, + shares_count: sharesCount, + }, + }); + } + } + // ==================== 帖子列表操作 ==================== /** diff --git a/src/screens/home/HomeScreen.tsx b/src/screens/home/HomeScreen.tsx index 7410251..1221925 100644 --- a/src/screens/home/HomeScreen.tsx +++ b/src/screens/home/HomeScreen.tsx @@ -336,7 +336,10 @@ export const HomeScreen: React.FC = () => { const handleShare = async (post: Post) => { if (!post?.id) return; try { - await postService.sharePost(post.id); + const res = await postService.sharePost(post.id, { channel: 'copy_link' }); + if (res.ok && res.shares_count != null) { + processPostUseCase.applyShareCountUpdate(post.id, res.shares_count); + } } catch (shareError) { console.error('上报分享次数失败:', shareError); } diff --git a/src/screens/home/PostDetailScreen.tsx b/src/screens/home/PostDetailScreen.tsx index 520f4a0..56e24ff 100644 --- a/src/screens/home/PostDetailScreen.tsx +++ b/src/screens/home/PostDetailScreen.tsx @@ -514,7 +514,15 @@ export const PostDetailScreen: React.FC = () => { const handleShare = useCallback(async () => { if (!post?.id) return; try { - await postService.sharePost(post.id); + const res = await postService.sharePost(post.id, { channel: 'copy_link' }); + if (res.ok && res.shares_count != null) { + setPost((prev) => + prev + ? { ...prev, shares_count: res.shares_count!, sharesCount: res.shares_count! } + : null + ); + processPostUseCase.applyShareCountUpdate(post.id, res.shares_count); + } } catch (error) { console.error('上报分享次数失败:', error); } diff --git a/src/services/postService.ts b/src/services/postService.ts index 71a3430..9989122 100644 --- a/src/services/postService.ts +++ b/src/services/postService.ts @@ -250,14 +250,24 @@ class PostService { } } - // 分享帖子 - async sharePost(postId: string): Promise { + /** 上报分享成功;返回服务端最新 shares_count 供界面同步 */ + async sharePost( + postId: string, + body?: { channel?: string } + ): Promise<{ ok: boolean; shares_count?: number }> { try { - const response = await api.post(`/posts/${postId}/share`); - return response.code === 0; + const response = await api.post<{ success: boolean; shares_count: number }>( + `/posts/${postId}/share`, + body && body.channel ? { channel: body.channel } : undefined + ); + if (response.code !== 0) { + return { ok: false }; + } + const n = response.data?.shares_count; + return { ok: true, shares_count: typeof n === 'number' ? n : undefined }; } catch (error) { console.error('分享帖子失败:', error); - return false; + return { ok: false }; } }