dev #5
@@ -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,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 帖子列表操作 ====================
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -250,14 +250,24 @@ class PostService {
|
||||
}
|
||||
}
|
||||
|
||||
// 分享帖子
|
||||
async sharePost(postId: string): Promise<boolean> {
|
||||
/** 上报分享成功;返回服务端最新 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 };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user