update code level
This commit is contained in:
@@ -1228,6 +1228,51 @@ body {
|
||||
color: var(--medium-gray);
|
||||
}
|
||||
|
||||
/* 搜索提示样式 */
|
||||
.search-hint {
|
||||
text-align: center;
|
||||
padding: 60px 20px;
|
||||
color: var(--text-light);
|
||||
font-size: 1.2rem;
|
||||
max-width: 600px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.search-hint .fa-search {
|
||||
font-size: 3rem;
|
||||
margin-bottom: 20px;
|
||||
color: var(--medium-gray);
|
||||
}
|
||||
|
||||
.search-hint h3 {
|
||||
color: var(--text-dark);
|
||||
margin-bottom: 15px;
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
|
||||
.search-hint p {
|
||||
margin-bottom: 15px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.search-hint ul {
|
||||
list-style: none;
|
||||
text-align: left;
|
||||
max-width: 400px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.search-hint li {
|
||||
margin-bottom: 10px;
|
||||
padding-left: 0;
|
||||
}
|
||||
|
||||
.search-hint .btn-primary {
|
||||
margin-top: 10px;
|
||||
padding: 8px 16px;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
/* 响应式媒体查询 - 移动端优化 */
|
||||
@media (max-width: 1024px) {
|
||||
.container {
|
||||
|
||||
142
assets/js/app.js
142
assets/js/app.js
@@ -263,52 +263,89 @@ async function loadAlbums(reset = false) {
|
||||
}
|
||||
|
||||
// 搜索相册
|
||||
async function searchAlbums() {
|
||||
// 防抖搜索函数
|
||||
function debouncedSearch() {
|
||||
const searchTerm = document.getElementById('search-input').value.toLowerCase().trim();
|
||||
|
||||
|
||||
if (searchTerm === '') {
|
||||
// 显示所有相册,重置分页
|
||||
loadAlbums(true);
|
||||
// 显示所有已加载的相册
|
||||
displayAlbums(allAlbums);
|
||||
updateStats();
|
||||
return;
|
||||
}
|
||||
|
||||
// 在当前已加载的数据集中搜索
|
||||
const filteredAlbums = searchInLoadedAlbums(searchTerm);
|
||||
|
||||
if (filteredAlbums.length > 0) {
|
||||
// 如果当前已加载的数据中有匹配项,直接显示
|
||||
displayAlbums(filteredAlbums);
|
||||
document.getElementById('stats').innerHTML = `在当前页面中找到 ${filteredAlbums.length} 个包含 "${searchTerm}" 的相册`;
|
||||
} else {
|
||||
// 如果没有找到匹配项,显示提示信息
|
||||
showSearchHint(searchTerm);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// 重置搜索状态
|
||||
currentPage = 1;
|
||||
allAlbums = [];
|
||||
hasMoreAlbums = true;
|
||||
// 在当前已加载的数据集中搜索
|
||||
function searchInLoadedAlbums(searchTerm) {
|
||||
if (!allAlbums.length) return [];
|
||||
|
||||
return allAlbums.filter(album => {
|
||||
// 搜索标题和描述
|
||||
const titleMatch = album.title.toLowerCase().includes(searchTerm);
|
||||
const descriptionMatch = album.description && album.description.toLowerCase().includes(searchTerm);
|
||||
|
||||
return titleMatch || descriptionMatch;
|
||||
});
|
||||
}
|
||||
|
||||
// 显示搜索提示
|
||||
function showSearchHint(searchTerm) {
|
||||
const container = document.getElementById('albums-container');
|
||||
container.innerHTML = `
|
||||
<div class="search-hint">
|
||||
<i class="fas fa-search"></i>
|
||||
<h3>未在当前页面找到 "${searchTerm}"</h3>
|
||||
<p>尝试:</p>
|
||||
<ul>
|
||||
<li>继续滚动加载更多相册</li>
|
||||
<li>使用更精确的搜索词</li>
|
||||
<li><button class="btn-primary" onclick="performServerSearch('${searchTerm}')">在全站搜索 "${searchTerm}"</button></li>
|
||||
</ul>
|
||||
</div>
|
||||
`;
|
||||
document.getElementById('stats').innerHTML = `在当前页面中未找到包含 "${searchTerm}" 的相册`;
|
||||
}
|
||||
|
||||
// 执行服务器端搜索(全站搜索)
|
||||
async function performServerSearch(searchTerm) {
|
||||
try {
|
||||
// 显示搜索中的骨架屏
|
||||
showSkeleton(4);
|
||||
|
||||
const response = await fetch(`${API_BASE}/search?q=${encodeURIComponent(searchTerm)}&page=${currentPage}&limit=${albumsPerPage}`);
|
||||
const response = await fetch(`${API_BASE}/search?q=${encodeURIComponent(searchTerm)}&page=1&limit=${albumsPerPage}`);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const filteredAlbums = await response.json();
|
||||
allAlbums = filteredAlbums;
|
||||
const serverResults = await response.json();
|
||||
|
||||
// 检查是否还有更多数据
|
||||
if (filteredAlbums.length < albumsPerPage) {
|
||||
hasMoreAlbums = false;
|
||||
if (serverResults.length === 0) {
|
||||
showError(`没有找到包含 "${searchTerm}" 的相册`);
|
||||
return;
|
||||
}
|
||||
|
||||
displayAlbums(filteredAlbums);
|
||||
|
||||
if (filteredAlbums.length === 0) {
|
||||
document.getElementById('stats').innerHTML = `没有找到包含 "${searchTerm}" 的相册`;
|
||||
} else {
|
||||
document.getElementById('stats').innerHTML = `找到 ${filteredAlbums.length} 个包含 "${searchTerm}" 的相册`;
|
||||
}
|
||||
|
||||
// 显示服务器搜索结果
|
||||
displayAlbums(serverResults);
|
||||
document.getElementById('stats').innerHTML = `在全站找到 ${serverResults.length} 个包含 "${searchTerm}" 的相册`;
|
||||
|
||||
// 初始化图片懒加载
|
||||
initLazyLoading();
|
||||
|
||||
currentPage++;
|
||||
} catch (error) {
|
||||
console.error('搜索失败:', error);
|
||||
showError('搜索失败,请重试');
|
||||
console.error('全站搜索失败:', error);
|
||||
showError('全站搜索失败,请重试');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -410,6 +447,9 @@ function preloadNextBatchImages() {
|
||||
|
||||
// 更新统计信息
|
||||
function updateStats(albums) {
|
||||
if (!albums) {
|
||||
albums = allAlbums;
|
||||
}
|
||||
const totalImages = albums.reduce((sum, album) => sum + album.image_count, 0);
|
||||
document.getElementById('stats').innerHTML = `共发现 ${albums.length} 个相册,包含 ${totalImages} 张图片`;
|
||||
}
|
||||
@@ -545,23 +585,47 @@ optimizeScrollPerformance();
|
||||
|
||||
// 随机打开相册
|
||||
function openRandomAlbum() {
|
||||
if (allAlbums.length === 0) {
|
||||
alert('暂无相册数据,请稍后再试');
|
||||
return;
|
||||
}
|
||||
|
||||
const randomIndex = Math.floor(Math.random() * allAlbums.length);
|
||||
const randomAlbum = allAlbums[randomIndex];
|
||||
|
||||
// 添加随机动画效果
|
||||
const randomButton = document.querySelector('.random-button');
|
||||
randomButton.style.transform = 'scale(0.95)';
|
||||
setTimeout(() => {
|
||||
randomButton.style.transform = '';
|
||||
}, 200);
|
||||
randomButton.disabled = true;
|
||||
|
||||
// 显示加载状态
|
||||
const originalText = randomButton.textContent;
|
||||
randomButton.textContent = '随机加载中...';
|
||||
|
||||
// 跳转到随机相册
|
||||
window.location.href = `/album?id=${randomAlbum.id}`;
|
||||
// 调用新的随机相册API
|
||||
fetch(`${API_BASE}/album/random`)
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
if (response.status === 404) {
|
||||
throw new Error('暂无相册数据,请稍后再试');
|
||||
}
|
||||
throw new Error('获取随机相册失败');
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(randomAlbum => {
|
||||
// 恢复按钮状态
|
||||
setTimeout(() => {
|
||||
randomButton.style.transform = '';
|
||||
randomButton.disabled = false;
|
||||
randomButton.textContent = originalText;
|
||||
}, 200);
|
||||
|
||||
// 跳转到随机相册
|
||||
window.location.href = `/album?id=${randomAlbum.id}`;
|
||||
})
|
||||
.catch(error => {
|
||||
// 恢复按钮状态
|
||||
setTimeout(() => {
|
||||
randomButton.style.transform = '';
|
||||
randomButton.disabled = false;
|
||||
randomButton.textContent = originalText;
|
||||
}, 200);
|
||||
|
||||
alert(error.message);
|
||||
});
|
||||
}
|
||||
|
||||
// 显示收藏夹
|
||||
|
||||
Reference in New Issue
Block a user