-
¥${p.price}
-
-¥${p.coupon}券
+
+
${isFavorited('product_'+p.id) ? '❤️' : '🤍'}
+
+
+
+
${p.title}
+
${PLATFORM_NAME[p.platform] || p.platform} ${p.shop}
+
+ ¥${p.price}
+ -¥${p.coupon}券
+
+
🎁 返 ¥${p.commission || 0}
-
🎁 返 ¥${p.commission || 0}
`).join('');
@@ -348,19 +405,91 @@
// Stats updated from data
}
+ // Local Storage Keys
+ const FAVORITES_KEY = 'zhibisheng_favorites';
+ const SEARCH_HISTORY_KEY = 'zhibisheng_search_history';
+
+ // Load favorites from localStorage
+ function getFavorites() {
+ try {
+ return JSON.parse(localStorage.getItem(FAVORITES_KEY) || '[]');
+ } catch { return []; }
+ }
+
+ // Save to favorites
+ function toggleFavorite(item, type) {
+ let favorites = getFavorites();
+ const id = type + '_' + item.id;
+ const index = favorites.findIndex(f => f.id === id);
+ if (index > -1) {
+ favorites.splice(index, 1);
+ showToast('已取消收藏');
+ } else {
+ favorites.unshift({ ...item, id, type, addedAt: Date.now() });
+ showToast('已收藏 ~');
+ }
+ localStorage.setItem(FAVORITES_KEY, JSON.stringify(favorites));
+ renderMyTab();
+ }
+
+ // Check if favorited
+ function isFavorited(id) {
+ return getFavorites().some(f => f.id === id);
+ }
+
+ // Toast notification
+ function showToast(msg) {
+ const existing = document.querySelector('.toast');
+ if (existing) existing.remove();
+ const toast = document.createElement('div');
+ toast.className = 'toast';
+ toast.textContent = msg;
+ toast.style.cssText = 'position:fixed;top:50%;left:50%;transform:translate(-50%,-50%);background:rgba(0,0,0,0.75);color:white;padding:12px 24px;border-radius:8px;font-size:14px;z-index:9999;animation:fadeIn 0.3s';
+ document.body.appendChild(toast);
+ setTimeout(() => { toast.style.opacity = '0'; setTimeout(() => toast.remove(), 300); }, 1500);
+ }
+
+ // Copy text to clipboard
+ function copyToClipboard(text, successMsg) {
+ if (navigator.clipboard) {
+ navigator.clipboard.writeText(text).then(() => showToast(successMsg || '已复制')).catch(() => fallbackCopy(text));
+ } else {
+ fallbackCopy(text);
+ }
+ }
+
+ function fallbackCopy(text) {
+ const ta = document.createElement('textarea');
+ ta.value = text;
+ ta.style.position = 'fixed'; ta.style.opacity = '0';
+ document.body.appendChild(ta);
+ ta.select();
+ document.execCommand('copy');
+ document.body.removeChild(ta);
+ showToast('已复制');
+ }
+
// Actions
function openDeal(deal) {
if (deal.link && deal.link !== '#') {
window.open(deal.link, '_blank');
} else {
- alert('复制商品链接,请在淘宝/京东App打开购买~');
+ // Generate mock coupon link
+ const couponLink = `https://taobao.com/item?id=${deal.id}`;
+ copyToClipboard(couponLink, '链接已复制,打开淘宝购买');
}
}
+
function goToCoupon(product) {
if (product.link && product.link !== '#') {
window.open(product.link, '_blank');
} else {
- alert('复制商品链接,请在淘宝/京东App打开购买~');
+ const couponText = `${product.title}
+原价: ¥${product.originalPrice}
+券后: ¥${product.price}
+${product.shop}
+【下载淘宝APP打开链接购买】`;
+ copyToClipboard(couponText, '已复制商品信息');
}
}
@@ -369,13 +498,93 @@
searchInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') doSearch(searchInput.value); });
// Tab switching
+ const mainContent = document.getElementById('mainContent');
+ const myContent = document.getElementById('myContent');
+
document.querySelectorAll('.tab').forEach(tab => {
tab.addEventListener('click', () => {
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
tab.classList.add('active');
currentTab = tab.dataset.tab;
+
+ if (currentTab === 'my') {
+ mainContent.style.display = 'none';
+ myContent.style.display = 'block';
+ renderMyTab();
+ } else {
+ mainContent.style.display = 'block';
+ myContent.style.display = 'none';
+ }
});
});
+
+ // Render My Tab
+ function renderMyTab() {
+ const favorites = getFavorites();
+ document.getElementById('myFavCount').textContent = favorites.length;
+
+ // Render favorites
+ if (favorites.length === 0) {
+ document.getElementById('favoritesList').innerHTML = '
暂无收藏
';
+ } else {
+ document.getElementById('favoritesList').innerHTML = favorites.map(f => `
+
+
+
+
${f.title}
+
¥${f.price}
+
+
✕
+
+ `).join('');
+ }
+
+ // Render search history
+ try {
+ const history = JSON.parse(localStorage.getItem(SEARCH_HISTORY_KEY) || '[]');
+ document.getElementById('mySearchCount').textContent = history.length;
+ if (history.length === 0) {
+ document.getElementById('searchHistoryList').innerHTML = '
暂无搜索记录
';
+ } else {
+ document.getElementById('searchHistoryList').innerHTML = history.slice(0, 10).map(h => `
+
+ 🔍 ${h.keyword}
+ ${formatTime(h.time)}
+
+ `).join('');
+ }
+ } catch {}
+ }
+
+ function formatTime(ts) {
+ const d = new Date(ts);
+ return `${d.getMonth()+1}/${d.getDate()} ${d.getHours()}:${String(d.getMinutes()).padStart(2, '0')}`;
+ }
+
+ function searchFromHistory(keyword) {
+ searchInput.value = keyword;
+ document.querySelector('[data-tab="home"]').click();
+ doSearch(keyword);
+ }
+
+ function clearSearchHistory() {
+ localStorage.removeItem(SEARCH_HISTORY_KEY);
+ renderMyTab();
+ showToast('已清空');
+ }
+
+ // Save search to history
+ const originalDoSearch = doSearch;
+ async function doSearch(keyword) {
+ if (!keyword.trim()) return;
+ try {
+ const history = JSON.parse(localStorage.getItem(SEARCH_HISTORY_KEY) || '[]');
+ const filtered = history.filter(h => h.keyword !== keyword);
+ filtered.unshift({ keyword, time: Date.now() });
+ localStorage.setItem(SEARCH_HISTORY_KEY, JSON.stringify(filtered.slice(0, 20)));
+ } catch {}
+ return originalDoSearch(keyword);
+ }
// Start
init();