fix(community): persist post liked status in MySQL and local storage to prevent state loss on page switch
这个提交包含在:
@@ -397,30 +397,44 @@ export async function universalVerifyCheckin(code, operatorId = 1001) {
|
||||
/**
|
||||
* 获取友圈广场动态列表 (真实读取 MySQL posts 表)
|
||||
*/
|
||||
export async function getPosts(category) {
|
||||
export async function getPosts(category, userId) {
|
||||
try {
|
||||
const query = category && category !== 'all' ? `?category=${encodeURIComponent(category)}` : '';
|
||||
const uid = userId || uni.getStorageSync('userId') || 1001;
|
||||
let query = `?userId=${uid}`;
|
||||
if (category && category !== 'all') {
|
||||
query += `&category=${encodeURIComponent(category)}`;
|
||||
}
|
||||
const res = await http.get(`/posts${query}`);
|
||||
let localLikedIds = uni.getStorageSync('COMMUNITY_LIKED_POST_IDS') || [];
|
||||
if (!Array.isArray(localLikedIds)) localLikedIds = [];
|
||||
|
||||
if (Array.isArray(res) && res.length > 0) {
|
||||
return res.map(p => ({
|
||||
id: p.id,
|
||||
author: p.author || '神秘同好',
|
||||
avatar: p.avatar || 'https://images.unsplash.com/photo-1534528741775-53994a69daeb?w=150',
|
||||
tag: p.tag || '同城认证',
|
||||
tagType: p.tagType || 'warm-tag',
|
||||
location: p.location || '同城热门商圈',
|
||||
distance: p.distance || '1km内',
|
||||
time: p.createdAt ? '刚刚' : '近期',
|
||||
content: p.content || '',
|
||||
topics: Array.isArray(p.topics) ? p.topics : [],
|
||||
images: Array.isArray(p.images) ? p.images : [],
|
||||
actionText: p.actionText || '聊一聊',
|
||||
likes: Number(p.likes) || 0,
|
||||
liked: false,
|
||||
comments: Number(p.comments) || 0,
|
||||
views: Number(p.views) || 1,
|
||||
category: p.category || 'partner'
|
||||
}));
|
||||
return res.map(p => {
|
||||
const strId = String(p.id);
|
||||
const isLocallyLiked = localLikedIds.includes(strId);
|
||||
const isServerLiked = p.liked === true || p.liked === 1;
|
||||
const finalLiked = p.liked !== undefined ? isServerLiked : isLocallyLiked;
|
||||
|
||||
return {
|
||||
id: p.id,
|
||||
author: p.author || '神秘同好',
|
||||
avatar: p.avatar || 'https://images.unsplash.com/photo-1534528741775-53994a69daeb?w=150',
|
||||
tag: p.tag || '同城认证',
|
||||
tagType: p.tagType || 'warm-tag',
|
||||
location: p.location || '同城热门商圈',
|
||||
distance: p.distance || '1km内',
|
||||
time: p.createdAt ? '刚刚' : '近期',
|
||||
content: p.content || '',
|
||||
topics: Array.isArray(p.topics) ? p.topics : [],
|
||||
images: Array.isArray(p.images) ? p.images : [],
|
||||
actionText: p.actionText || '聊一聊',
|
||||
likes: Number(p.likes) || 0,
|
||||
liked: finalLiked || isLocallyLiked,
|
||||
comments: Number(p.comments) || 0,
|
||||
views: Number(p.views) || 1,
|
||||
category: p.category || 'partner'
|
||||
};
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('[API] 获取友圈动态列表异常:', err.message);
|
||||
@@ -444,9 +458,14 @@ export async function createPost(payload) {
|
||||
/**
|
||||
* 动态点赞 (落盘 MySQL)
|
||||
*/
|
||||
export async function likePost(id) {
|
||||
export async function likePost(id, action, userId) {
|
||||
try {
|
||||
const res = await http.post(`/posts/${id}/like`);
|
||||
const uid = userId || uni.getStorageSync('userId') || 1001;
|
||||
const payload = { userId: uid };
|
||||
if (action) {
|
||||
payload.action = action;
|
||||
}
|
||||
const res = await http.post(`/posts/${id}/like`, payload);
|
||||
return res;
|
||||
} catch (err) {
|
||||
console.warn('[API] 点赞动态异常:', err.message);
|
||||
|
||||
@@ -376,9 +376,14 @@ onUnmounted(() => {
|
||||
|
||||
const loadRemotePosts = async () => {
|
||||
try {
|
||||
const list = await getPosts();
|
||||
const list = await getPosts(currentTab.value);
|
||||
if (list && list.length > 0) {
|
||||
posts.value = list;
|
||||
let localLikedIds = uni.getStorageSync('COMMUNITY_LIKED_POST_IDS') || [];
|
||||
if (!Array.isArray(localLikedIds)) localLikedIds = [];
|
||||
posts.value = list.map(item => ({
|
||||
...item,
|
||||
liked: item.liked !== undefined ? item.liked : localLikedIds.includes(String(item.id))
|
||||
}));
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('[Community] 获取动态列表失败,保持本地列表:', err.message);
|
||||
@@ -411,7 +416,20 @@ const feedTabs = ref([
|
||||
|
||||
const currentTab = ref('all');
|
||||
|
||||
const posts = ref([
|
||||
const syncInitialLikedState = (rawPosts) => {
|
||||
try {
|
||||
let localLikedIds = uni.getStorageSync('COMMUNITY_LIKED_POST_IDS') || [];
|
||||
if (!Array.isArray(localLikedIds) || localLikedIds.length === 0) return rawPosts;
|
||||
return rawPosts.map(p => ({
|
||||
...p,
|
||||
liked: localLikedIds.includes(String(p.id)) ? true : p.liked
|
||||
}));
|
||||
} catch (_) {
|
||||
return rawPosts;
|
||||
}
|
||||
};
|
||||
|
||||
const posts = ref(syncInitialLikedState([
|
||||
{
|
||||
id: 1,
|
||||
author: '小甜橙',
|
||||
@@ -476,7 +494,7 @@ const posts = ref([
|
||||
views: 520,
|
||||
category: 'partner'
|
||||
}
|
||||
]);
|
||||
]));
|
||||
|
||||
const filteredPosts = computed(() => {
|
||||
if (currentTab.value === 'all') return posts.value;
|
||||
@@ -488,16 +506,43 @@ const switchFeedTab = (key) => {
|
||||
};
|
||||
|
||||
const toggleLike = async (post) => {
|
||||
post.liked = !post.liked;
|
||||
post.likes += post.liked ? 1 : -1;
|
||||
const willLike = !post.liked;
|
||||
post.liked = willLike;
|
||||
post.likes += willLike ? 1 : -1;
|
||||
|
||||
// 1. 同步更新本地存储的点赞记录 (保障切页、断网或刷新时不丢失状态)
|
||||
let localLikedIds = uni.getStorageSync('COMMUNITY_LIKED_POST_IDS') || [];
|
||||
if (!Array.isArray(localLikedIds)) localLikedIds = [];
|
||||
const strId = String(post.id);
|
||||
if (willLike) {
|
||||
if (!localLikedIds.includes(strId)) localLikedIds.push(strId);
|
||||
} else {
|
||||
localLikedIds = localLikedIds.filter(id => id !== strId);
|
||||
}
|
||||
uni.setStorageSync('COMMUNITY_LIKED_POST_IDS', localLikedIds);
|
||||
|
||||
uni.showToast({
|
||||
title: post.liked ? '已点赞' : '已取消点赞',
|
||||
title: willLike ? '已点赞' : '已取消点赞',
|
||||
icon: 'none',
|
||||
duration: 1000
|
||||
});
|
||||
if (post.liked && post.id) {
|
||||
|
||||
if (post.id) {
|
||||
try {
|
||||
await likePost(post.id);
|
||||
const res = await likePost(post.id, willLike ? 'like' : 'unlike');
|
||||
if (res && res.likes !== undefined) {
|
||||
post.likes = res.likes;
|
||||
}
|
||||
if (res && res.liked !== undefined) {
|
||||
post.liked = res.liked;
|
||||
if (res.liked && !localLikedIds.includes(strId)) {
|
||||
localLikedIds.push(strId);
|
||||
uni.setStorageSync('COMMUNITY_LIKED_POST_IDS', localLikedIds);
|
||||
} else if (!res.liked && localLikedIds.includes(strId)) {
|
||||
localLikedIds = localLikedIds.filter(id => id !== strId);
|
||||
uni.setStorageSync('COMMUNITY_LIKED_POST_IDS', localLikedIds);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('[Community] 点赞落盘异常:', err.message);
|
||||
}
|
||||
|
||||
在新工单中引用
屏蔽一个用户