fix(community): persist post liked status in MySQL and local storage to prevent state loss on page switch

这个提交包含在:
xcbyy
2026-10-13 15:46:04 +08:00
父节点 3d895a1a0a
当前提交 497fb65359
共有 6 个文件被更改,包括 218 次插入 和 40 次删除

查看文件

@@ -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);