update
This commit is contained in:
276
assets/js/app.js
276
assets/js/app.js
@@ -14,6 +14,9 @@ let isInFavoritesPage = false; // 标记当前是否在收藏页
|
||||
let currentFilter = null; // { type: 'tag'|'person', value: string }
|
||||
let collapsedSections = { people: false, tags: false };
|
||||
|
||||
// 侧边栏状态(持久化)
|
||||
let sidebarCollapsed = localStorage.getItem('sidebarCollapsed') === 'true';
|
||||
|
||||
// API基础URL
|
||||
const API_BASE = '/api';
|
||||
|
||||
@@ -622,14 +625,32 @@ function updateStats(albums) {
|
||||
albums = allAlbums;
|
||||
}
|
||||
const totalImages = albums.reduce((sum, album) => sum + album.image_count, 0);
|
||||
document.getElementById('stats').innerHTML = `共发现 ${albums.length} 个相册,包含 ${totalImages} 张图片`;
|
||||
|
||||
// 更新统计条
|
||||
const statAlbums = document.getElementById('stat-albums');
|
||||
const statImages = document.getElementById('stat-images');
|
||||
if (statAlbums) statAlbums.textContent = albums.length.toLocaleString();
|
||||
if (statImages) statImages.textContent = totalImages.toLocaleString();
|
||||
|
||||
// 兼容旧版 stats
|
||||
const oldStats = document.getElementById('stats');
|
||||
if (oldStats) oldStats.innerHTML = `共发现 ${albums.length} 个相册,包含 ${totalImages} 张图片`;
|
||||
|
||||
// 获取人物数量
|
||||
fetch(`${API_BASE}/recommend/popular-persons?limit=1`).then(r => r.ok && r.json()).then(data => {
|
||||
if (data && data.length > 0 && data[0].total) {
|
||||
const statPeople = document.getElementById('stat-people');
|
||||
if (statPeople) statPeople.textContent = data[0].total.toLocaleString();
|
||||
}
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
// 显示错误信息
|
||||
function showError(message) {
|
||||
const container = document.getElementById('albums-container');
|
||||
container.innerHTML = `<div class="no-results"><i class="fas fa-exclamation-triangle"></i><br>${message}</div>`;
|
||||
document.getElementById('stats').innerHTML = '';
|
||||
const oldStats = document.getElementById('stats');
|
||||
if (oldStats) oldStats.innerHTML = '';
|
||||
}
|
||||
|
||||
// 搜索相册函数
|
||||
@@ -1237,64 +1258,92 @@ async function loadDiscoverData() {
|
||||
}
|
||||
}
|
||||
|
||||
// 渲染标签云
|
||||
// 渲染标签(侧边栏列表)
|
||||
function renderTagCloud(tags) {
|
||||
// 新版:侧边栏标签列表
|
||||
const sidebarTags = document.getElementById('sidebar-tags');
|
||||
if (sidebarTags && tags && tags.length) {
|
||||
sidebarTags.innerHTML = tags.slice(0, 25).map(tag => {
|
||||
const count = tag.album_cnt || tag.count || 1;
|
||||
const name = tag.tag || tag.name || '';
|
||||
const isActive = currentFilter && currentFilter.type === 'tag' && currentFilter.value === name;
|
||||
return `<div class="sidebar-tag ${isActive ? 'active' : ''}" onclick="filterByTag('${name}')">
|
||||
<span class="sidebar-tag-name">${name}</span>
|
||||
<span class="sidebar-tag-count">${count}</span>
|
||||
</div>`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
// 兼容旧版:标签云区域
|
||||
const section = document.getElementById('tags-section');
|
||||
const cloud = document.getElementById('tag-cloud');
|
||||
if (!section || !cloud || !tags.length) return;
|
||||
|
||||
section.style.display = 'block';
|
||||
|
||||
// 计算标签大小范围
|
||||
const maxCount = Math.max(...tags.map(t => t.album_cnt || t.count || 1));
|
||||
const minCount = Math.min(...tags.map(t => t.album_cnt || t.count || 1));
|
||||
|
||||
cloud.innerHTML = tags.map(tag => {
|
||||
const count = tag.album_cnt || tag.count || 1;
|
||||
const name = tag.tag || tag.name || '';
|
||||
// 计算字体大小 (12px - 22px)
|
||||
const ratio = maxCount === minCount ? 0.5 : (count - minCount) / (maxCount - minCount);
|
||||
const fontSize = 12 + ratio * 10;
|
||||
// 计算热度颜色
|
||||
const hue = 210 - ratio * 50; // 蓝色到紫色
|
||||
const opacity = 0.6 + ratio * 0.4;
|
||||
|
||||
return `<span class="tag-chip"
|
||||
style="font-size:${fontSize}px; color:hsl(${hue}, 70%, 45%); opacity:${opacity};"
|
||||
onclick="filterByTag('${name}')"
|
||||
title="${count} 个相册">
|
||||
${name} <small>(${count})</small>
|
||||
</span>`;
|
||||
}).join('');
|
||||
if (section && cloud && tags && tags.length) {
|
||||
section.style.display = 'block';
|
||||
const maxCount = Math.max(...tags.map(t => t.album_cnt || t.count || 1));
|
||||
const minCount = Math.min(...tags.map(t => t.album_cnt || t.count || 1));
|
||||
cloud.innerHTML = tags.map(tag => {
|
||||
const count = tag.album_cnt || tag.count || 1;
|
||||
const name = tag.tag || tag.name || '';
|
||||
const ratio = maxCount === minCount ? 0.5 : (count - minCount) / (maxCount - minCount);
|
||||
const fontSize = 12 + ratio * 10;
|
||||
const hue = 210 - ratio * 50;
|
||||
const opacity = 0.6 + ratio * 0.4;
|
||||
return `<span class="tag-chip"
|
||||
style="font-size:${fontSize}px; color:hsl(${hue}, 70%, 45%); opacity:${opacity};"
|
||||
onclick="filterByTag('${name}')"
|
||||
title="${count} 个相册">
|
||||
${name} <small>(${count})</small>
|
||||
</span>`;
|
||||
}).join('');
|
||||
}
|
||||
}
|
||||
|
||||
// 渲染热门人物
|
||||
// 渲染热门人物(横向滚动条)
|
||||
function renderPeopleCloud(people) {
|
||||
// 新版:横向滚动条
|
||||
const strip = document.getElementById('people-strip');
|
||||
const scroll = document.getElementById('people-scroll');
|
||||
if (strip && scroll && people && people.length) {
|
||||
strip.style.display = 'flex';
|
||||
scroll.innerHTML = people.slice(0, 20).map(p => {
|
||||
const name = p.name || p.display_name || '';
|
||||
const albumCount = p.total_albums || 0;
|
||||
const isActive = currentFilter && currentFilter.type === 'person' && currentFilter.value === name;
|
||||
return `<div class="person-chip-scroll ${isActive ? 'active' : ''}" onclick="filterByPerson('${name}')" title="${albumCount} 个相册">
|
||||
<div class="person-chip-avatar">${name.charAt(0)}</div>
|
||||
<div class="person-chip-info">
|
||||
<div class="person-chip-name">${name}</div>
|
||||
<div class="person-chip-count">${albumCount} 相册</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
// 兼容旧版:人物卡片区域
|
||||
const section = document.getElementById('people-section');
|
||||
const cloud = document.getElementById('people-cloud');
|
||||
if (!section || !cloud || !people.length) return;
|
||||
|
||||
section.style.display = 'block';
|
||||
|
||||
cloud.innerHTML = people.map(p => {
|
||||
const name = p.name || p.display_name || '';
|
||||
const albumCount = p.total_albums || 0;
|
||||
const imageCount = p.total_images || 0;
|
||||
|
||||
return `<div class="person-chip" onclick="filterByPerson('${name}')" title="${albumCount} 个相册, ${imageCount} 张图片">
|
||||
<div class="person-avatar">${name.charAt(0)}</div>
|
||||
<div class="person-info">
|
||||
<div class="person-name">${name}</div>
|
||||
<div class="person-meta">${albumCount} 相册 · ${imageCount} 图</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}).join('');
|
||||
if (section && cloud && people && people.length) {
|
||||
section.style.display = 'block';
|
||||
cloud.innerHTML = people.map(p => {
|
||||
const name = p.name || p.display_name || '';
|
||||
const albumCount = p.total_albums || 0;
|
||||
const imageCount = p.total_images || 0;
|
||||
return `<div class="person-chip" onclick="filterByPerson('${name}')" title="${albumCount} 个相册, ${imageCount} 张图片">
|
||||
<div class="person-avatar">${name.charAt(0)}</div>
|
||||
<div class="person-info">
|
||||
<div class="person-name">${name}</div>
|
||||
<div class="person-meta">${albumCount} 相册 · ${imageCount} 图</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}).join('');
|
||||
}
|
||||
}
|
||||
|
||||
// 按标签筛选
|
||||
async function filterByTag(tag) {
|
||||
currentFilter = { type: 'tag', value: tag };
|
||||
showFilterStatus(`标签: ${tag}`);
|
||||
renderTagCloud(await fetchTags().catch(() => []));
|
||||
|
||||
try {
|
||||
showSkeleton(6);
|
||||
@@ -1311,6 +1360,13 @@ async function filterByTag(tag) {
|
||||
}
|
||||
}
|
||||
|
||||
// 获取标签(辅助函数)
|
||||
async function fetchTags() {
|
||||
const resp = await fetch(`${API_BASE}/tags/popular?limit=25`);
|
||||
if (!resp.ok) return [];
|
||||
return resp.json();
|
||||
}
|
||||
|
||||
// 按人物筛选
|
||||
async function filterByPerson(personName) {
|
||||
currentFilter = { type: 'person', value: personName };
|
||||
@@ -1323,8 +1379,6 @@ async function filterByPerson(personName) {
|
||||
const persons = await response.json();
|
||||
|
||||
if (persons && persons.length > 0) {
|
||||
// 先尝试通过人物详情获取相册
|
||||
const personId = persons[0].id;
|
||||
try {
|
||||
const detailResp = await fetch(`${API_BASE}/people/${encodeURIComponent(personName)}`);
|
||||
if (detailResp.ok) {
|
||||
@@ -1336,22 +1390,26 @@ async function filterByPerson(personName) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// 回退到标题搜索
|
||||
}
|
||||
} catch (e) { /* 回退 */ }
|
||||
}
|
||||
|
||||
// 回退:使用标题搜索
|
||||
await performServerSearch(personName);
|
||||
} catch (error) {
|
||||
console.error('人物筛选失败:', error);
|
||||
// 回退到标题搜索
|
||||
await performServerSearch(personName);
|
||||
}
|
||||
}
|
||||
|
||||
// 显示筛选状态
|
||||
function showFilterStatus(text) {
|
||||
// 新版筛选条
|
||||
const bar = document.getElementById('filter-bar');
|
||||
const pill = document.getElementById('current-filter-pill');
|
||||
if (bar && pill) {
|
||||
pill.textContent = text;
|
||||
bar.style.display = 'flex';
|
||||
}
|
||||
|
||||
// 兼容旧版
|
||||
const status = document.getElementById('filter-status');
|
||||
const label = document.getElementById('current-filter');
|
||||
if (status && label) {
|
||||
@@ -1363,24 +1421,104 @@ function showFilterStatus(text) {
|
||||
// 清除筛选
|
||||
function clearFilter() {
|
||||
currentFilter = null;
|
||||
|
||||
// 新版筛选条
|
||||
const bar = document.getElementById('filter-bar');
|
||||
if (bar) bar.style.display = 'none';
|
||||
|
||||
// 兼容旧版
|
||||
const status = document.getElementById('filter-status');
|
||||
if (status) status.style.display = 'none';
|
||||
|
||||
// 清空搜索框
|
||||
const searchInput = document.getElementById('search-input');
|
||||
if (searchInput) searchInput.value = '';
|
||||
if (searchInput) {
|
||||
searchInput.value = '';
|
||||
updateSearchClear();
|
||||
}
|
||||
|
||||
// 重新渲染标签和人物(清除高亮)
|
||||
loadDiscoverData();
|
||||
|
||||
// 重新加载所有相册
|
||||
loadAlbums(true);
|
||||
}
|
||||
|
||||
// 折叠/展开区域
|
||||
// ============ 侧边栏交互 ============
|
||||
|
||||
// 初始化侧边栏状态
|
||||
function initSidebar() {
|
||||
const sidebar = document.getElementById('sidebar');
|
||||
if (sidebar && sidebarCollapsed) {
|
||||
sidebar.classList.add('collapsed');
|
||||
}
|
||||
}
|
||||
|
||||
// 切换侧边栏折叠
|
||||
function toggleSidebar() {
|
||||
const sidebar = document.getElementById('sidebar');
|
||||
if (!sidebar) return;
|
||||
sidebarCollapsed = !sidebarCollapsed;
|
||||
sidebar.classList.toggle('collapsed', sidebarCollapsed);
|
||||
localStorage.setItem('sidebarCollapsed', sidebarCollapsed);
|
||||
}
|
||||
|
||||
// 移动端切换侧边栏
|
||||
function toggleMobileSidebar() {
|
||||
const sidebar = document.getElementById('sidebar');
|
||||
const overlay = document.getElementById('mobile-overlay');
|
||||
if (!sidebar || !overlay) return;
|
||||
sidebar.classList.toggle('mobile-open');
|
||||
overlay.classList.toggle('active');
|
||||
}
|
||||
|
||||
// 切换视图(首页/收藏夹)
|
||||
function switchView(view) {
|
||||
// 更新导航高亮
|
||||
document.querySelectorAll('.nav-item').forEach(item => {
|
||||
item.classList.toggle('active', item.dataset.view === view);
|
||||
});
|
||||
|
||||
// 移动端关闭侧边栏
|
||||
const sidebar = document.getElementById('sidebar');
|
||||
const overlay = document.getElementById('mobile-overlay');
|
||||
if (sidebar && sidebar.classList.contains('mobile-open')) {
|
||||
sidebar.classList.remove('mobile-open');
|
||||
overlay.classList.remove('active');
|
||||
}
|
||||
|
||||
if (view === 'home') {
|
||||
backToHome();
|
||||
}
|
||||
}
|
||||
|
||||
// 搜索框清除按钮
|
||||
function updateSearchClear() {
|
||||
const input = document.getElementById('search-input');
|
||||
const clear = document.getElementById('search-clear');
|
||||
if (input && clear) {
|
||||
clear.style.display = input.value.length > 0 ? 'flex' : 'none';
|
||||
}
|
||||
}
|
||||
|
||||
// 清除搜索
|
||||
function clearSearch() {
|
||||
const input = document.getElementById('search-input');
|
||||
if (input) {
|
||||
input.value = '';
|
||||
updateSearchClear();
|
||||
if (!currentFilter) {
|
||||
loadAlbums(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 折叠/展开区域(兼容旧版)
|
||||
function toggleSection(name) {
|
||||
collapsedSections[name] = !collapsedSections[name];
|
||||
const cloudId = name === 'people' ? 'people-cloud' : 'tag-cloud';
|
||||
const cloud = document.getElementById(cloudId);
|
||||
const btn = document.querySelector(`#${name}-section .section-toggle i`);
|
||||
|
||||
if (cloud) {
|
||||
if (collapsedSections[name]) {
|
||||
cloud.style.display = 'none';
|
||||
@@ -1392,6 +1530,24 @@ function toggleSection(name) {
|
||||
}
|
||||
}
|
||||
|
||||
// ============ 初始化 ============
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
initSidebar();
|
||||
|
||||
// 搜索框输入事件
|
||||
const searchInput = document.getElementById('search-input');
|
||||
if (searchInput) {
|
||||
searchInput.addEventListener('input', updateSearchClear);
|
||||
}
|
||||
|
||||
// 键盘快捷键:ESC 关闭筛选/搜索
|
||||
document.addEventListener('keydown', function(e) {
|
||||
if (e.key === 'Escape') {
|
||||
if (currentFilter) clearFilter();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 导出函数供全局使用
|
||||
window.searchAlbums = searchAlbums;
|
||||
window.openRandomAlbum = openRandomAlbum;
|
||||
@@ -1406,4 +1562,8 @@ window.removeFavoriteFromPreview = removeFavoriteFromPreview;
|
||||
window.filterByTag = filterByTag;
|
||||
window.filterByPerson = filterByPerson;
|
||||
window.clearFilter = clearFilter;
|
||||
window.toggleSection = toggleSection;
|
||||
window.toggleSection = toggleSection;
|
||||
window.toggleSidebar = toggleSidebar;
|
||||
window.toggleMobileSidebar = toggleMobileSidebar;
|
||||
window.switchView = switchView;
|
||||
window.clearSearch = clearSearch;
|
||||
Reference in New Issue
Block a user