// 表白墙数据管理:对接后端接口 // 点赞去重用 localStorage 记录已点过的表白 id(演示用,防止同设备重复点赞) import { api } from '../api/index.js' const LIKED_KEY = 'xpx_liked_confessions' // 拉取表白列表(仅后端已审核通过的) export async function fetchConfessions() { const res = await api.getConfessions({ pageSize: 50 }) return (res && res.list) || [] } // 表白详情(含评论) export async function fetchConfession(id) { return await api.getConfession(id) } // 发布表白(后端置为待审核) export async function publishConfession(payload) { return await api.publishConfession(payload) } // 点赞(成功后本地记录,避免重复点) export async function likeConfession(id) { const res = await api.likeConfession(id) addLiked(id) return res && res.likes } // 发表评论 export async function commentConfession(id, payload) { return await api.commentConfession(id, payload) } // —— 本地点赞记录 —— export function getLikedSet() { try { return new Set(JSON.parse(localStorage.getItem(LIKED_KEY) || '[]')) } catch (e) { return new Set() } } export function hasLiked(id) { return getLikedSet().has(id) } function addLiked(id) { try { const s = getLikedSet() s.add(id) localStorage.setItem(LIKED_KEY, JSON.stringify([...s])) } catch (e) { /* ignore */ } } // 相对时间格式化:刚刚 / x分钟前 / x小时前 / 昨天 / MM-DD export function formatTime(iso) { const t = new Date(iso).getTime() if (!t) return '' const diff = Date.now() - t const min = 60 * 1000 const hr = 60 * min const day = 24 * hr if (diff < min) return '刚刚' if (diff < hr) return `${Math.floor(diff / min)}分钟前` if (diff < day) return `${Math.floor(diff / hr)}小时前` if (diff < 2 * day) return '昨天' const d = new Date(t) const p = (n) => String(n).padStart(2, '0') return `${p(d.getMonth() + 1)}-${p(d.getDate())}` } // 卡片配色主题(供墙页与详情页共用) export const THEMES = [ { bg: 'linear-gradient(135deg, #fff1f4, #ffe1ea)', accent: '#ff5c8a' }, { bg: 'linear-gradient(135deg, #fff7e8, #ffe9c9)', accent: '#ff9f2e' }, { bg: 'linear-gradient(135deg, #eefaf1, #d8f3e0)', accent: '#22c55e' }, { bg: 'linear-gradient(135deg, #eef4ff, #dbe7ff)', accent: '#1677ff' }, { bg: 'linear-gradient(135deg, #eafaff, #cdeffb)', accent: '#0fc6c2' } ] export function themeAt(i) { return THEMES[Number(i) || 0] || THEMES[0] }