diff --git a/mini-program/src/pages/life-event/detail.vue b/mini-program/src/pages/life-event/detail.vue index 6c8287c..b8f1529 100644 --- a/mini-program/src/pages/life-event/detail.vue +++ b/mini-program/src/pages/life-event/detail.vue @@ -134,14 +134,41 @@ const isDescriptionCollapsed = ref(false) const scrollTop = ref(0) const currentScrollTop = ref(0) +// 从 localStorage 读取缓存的收藏状态 +const getCachedFavoriteStatus = (id) => { + const cached = uni.getStorageSync(`favorite_event_${id}`) + return cached === 'true' +} + +// 缓存收藏状态到 localStorage +const setCachedFavoriteStatus = (id, status) => { + uni.setStorageSync(`favorite_event_${id}`, status ? 'true' : 'false') +} + onMounted(async () => { const info = uni.getWindowInfo() safeAreaBottom.value = info.safeAreaInsets?.bottom || 0 const pages = getCurrentPages() eventId.value = pages[pages.length - 1]?.options?.id || '' + + // 先从缓存读取收藏状态,避免闪烁 + if (eventId.value) { + isFavorite.value = getCachedFavoriteStatus(eventId.value) + } + cachedEvent.value = uni.getStorageSync('current_life_event') || null if (!store.events?.length) await store.fetchEvents() - isFavorite.value = Boolean(uni.getStorageSync(`event_favorite_${eventId.value}`)) + + // 异步校验真实收藏状态并更新缓存 + if (eventId.value) { + const result = await store.checkEventFavorite(eventId.value) + if (result.success) { + const realStatus = result.data + isFavorite.value = realStatus + setCachedFavoriteStatus(eventId.value, realStatus) + } + } + analytics.trackPageView(pagePath, { life_event_id: eventId.value, tag_count: Array.isArray(displayEvent.value.tags) ? displayEvent.value.tags.length : 0 @@ -305,20 +332,19 @@ const editEvent = () => { const toggleFavorite = async () => { if (!eventId.value) return - const next = !isFavorite.value - const result = await store.favoriteEvent({ id: eventId.value, favorite: next }) + const result = await store.toggleEventFavorite(eventId.value) if (!result.success) { uni.showToast({ title: result.error || '收藏失败', icon: 'none' }) return } - isFavorite.value = next - if (next) uni.setStorageSync(`event_favorite_${eventId.value}`, '1') - else uni.removeStorageSync(`event_favorite_${eventId.value}`) + const newStatus = result.data?.isFavorited ?? false + isFavorite.value = newStatus + setCachedFavoriteStatus(eventId.value, newStatus) analytics.track('life_event_favorite', { life_event_id: eventId.value, - favorite: next + favorite: newStatus }, { eventType: 'life_event', pagePath }) - uni.showToast({ title: next ? '已收藏' : '已取消收藏', icon: 'success' }) + uni.showToast({ title: newStatus ? '已收藏' : '已取消收藏', icon: 'success' }) } const chatEvent = async () => { diff --git a/mini-program/src/pages/main/ScriptLibraryView.vue b/mini-program/src/pages/main/ScriptLibraryView.vue index 2479c18..bff4316 100644 --- a/mini-program/src/pages/main/ScriptLibraryView.vue +++ b/mini-program/src/pages/main/ScriptLibraryView.vue @@ -147,9 +147,9 @@