// 双击点赞工具函数 import { likeAssetApi } from './api.js'; // 存储已点赞的作品,key: assetId, value: 点赞日期 (YYYY-MM-DD) const LIKE_STORAGE_KEY = 'liked_assets_daily'; /** * 获取今天日期字符串 */ function getTodayStr() { const now = new Date(); return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`; } /** * 检查作品今天是否已点赞 */ function hasLikedToday(assetId) { try { const storage = uni.getStorageSync(LIKE_STORAGE_KEY) || {}; const today = getTodayStr(); return storage[assetId] === today; } catch (e) { return false; } } /** * 记录作品今天已点赞 */ function markLikedToday(assetId) { try { const storage = uni.getStorageSync(LIKE_STORAGE_KEY) || {}; storage[assetId] = getTodayStr(); uni.setStorageSync(LIKE_STORAGE_KEY, storage); } catch (e) { console.error('存储点赞记录失败:', e); } } /** * 清除过期的点赞记录(昨天及之前) */ function cleanExpiredLikes() { try { const storage = uni.getStorageSync(LIKE_STORAGE_KEY) || {}; const today = getTodayStr(); const keys = Object.keys(storage); keys.forEach(key => { if (storage[key] !== today) { delete storage[key]; } }); uni.setStorageSync(LIKE_STORAGE_KEY, storage); } catch (e) { console.error('清除过期点赞记录失败:', e); } } /** * 双击点赞处理(每天每个作品只能点赞一次) * @param {string|number} assetId - 藏品ID * @param {Function} callback - 回调函数,参数为是否成功 */ export function doubleTapLike(assetId, callback) { // 清理过期记录 cleanExpiredLikes(); // 检查今天是否已点赞 if (hasLikedToday(assetId)) { uni.showToast({ title: '今日已点赞', icon: 'none' }); return; } likeAssetApi(assetId).then(res => { console.log('点赞成功', res); markLikedToday(assetId); if (callback) callback(true); }).catch(err => { console.error('点赞失败:', err); uni.showToast({ title: '今日已点赞', icon: 'none' }); if (callback) callback(false); }); }