This commit is contained in:
lychang
2026-03-20 02:31:07 +08:00
commit 5893cd5e31
19 changed files with 5653 additions and 0 deletions

182
assets/js/album.js Normal file
View File

@@ -0,0 +1,182 @@
// 全局变量
let currentAlbum = null;
let currentImageIndex = 0;
let images = [];
// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
const urlParams = new URLSearchParams(window.location.search);
const albumId = urlParams.get('id');
if (albumId) {
loadAlbum(albumId);
} else {
showError('缺少相册ID参数');
}
});
// 加载相册详情
async function loadAlbum(albumId) {
try {
const response = await fetch(`/api/album/${albumId}`);
if (!response.ok) {
if (response.status === 404) {
throw new Error('相册不存在');
}
throw new Error(`HTTP error! status: ${response.status}`);
}
const albumData = await response.json();
currentAlbum = albumData.album;
images = albumData.images;
updateAlbumInfo();
displayImages();
} catch (error) {
console.error('加载相册失败:', error);
showError('加载相册失败: ' + error.message);
}
}
// 更新相册信息
function updateAlbumInfo() {
document.getElementById('album-title').textContent = currentAlbum.title;
document.getElementById('album-info').innerHTML =
`<i class="fas fa-images"></i> 共 ${images.length} 张图片`;
document.title = `${currentAlbum.title} - 图片展示服务`;
}
// 显示图片列表
function displayImages() {
const container = document.getElementById('images-container');
if (images.length === 0) {
container.innerHTML = '<div class="no-results"><i class="fas fa-images"></i><br>此相册暂无图片</div>';
return;
}
container.innerHTML = images.map((image, index) => {
// 优先使用缩略图API,如果没有则使用原始URL
let imageUrl = '/assets/placeholder.svg';
if (image.id) {
imageUrl = `/api/thumbnail/${image.id}`;
} else if (image.original_url) {
imageUrl = image.original_url;
}
const title = image.title || `图片 ${index + 1}`;
return `
<div class="image-item" onclick="openLightbox(${index})">
<img src="${imageUrl}" alt="${title}" onerror="this.src='/assets/placeholder.svg'">
<div class="image-caption">
${image.width ? `<small>${image.width}×${image.height}</small>` : ''}
${image.file_size ? `<small>${(image.file_size / 1024 ).toFixed(2)} KB</small>` : ''}
</div>
</div>
`;
}).join('');
}
// 打开图片查看器
function openLightbox(index) {
currentImageIndex = index;
const lightbox = document.getElementById('lightbox');
const lightboxImg = document.getElementById('lightbox-img');
const lightboxInfo = document.getElementById('lightbox-info');
const image = images[index];
// 在lightbox中优先使用原始图片以获得更好的显示效果
let imageUrl = '/assets/placeholder.svg';
if (image.id) {
imageUrl = `/api/original/${image.id}`;
} else if (image.original_url) {
imageUrl = image.original_url;
}
lightboxImg.src = imageUrl;
const title = image.title || `图片 ${index + 1}`;
let infoText = `${title} (${index + 1}/${images.length})`;
if (image.width && image.height) {
infoText += ` | ${image.width}×${image.height}`;
}
if (image.file_size) {
infoText += ` | ${(image.file_size / 1024 ).toFixed(2)} KB`;
}
lightboxInfo.textContent = infoText;
lightbox.style.display = 'flex';
document.body.style.overflow = 'hidden';
}
// 关闭图片查看器
function closeLightbox() {
const lightbox = document.getElementById('lightbox');
lightbox.style.display = 'none';
document.body.style.overflow = 'auto';
}
// 下一张图片
function nextImage() {
currentImageIndex = (currentImageIndex + 1) % images.length;
updateLightboxImage();
}
// 上一张图片
function prevImage() {
currentImageIndex = (currentImageIndex - 1 + images.length) % images.length;
updateLightboxImage();
}
// 更新图片查看器显示
function updateLightboxImage() {
const lightboxImg = document.getElementById('lightbox-img');
const lightboxInfo = document.getElementById('lightbox-info');
const image = images[currentImageIndex];
// 在lightbox中优先使用原始图片以获得更好的显示效果
let imageUrl = '/assets/placeholder.svg';
if (image.id) {
imageUrl = `/api/original/${image.id}`;
} else if (image.original_url) {
imageUrl = image.original_url;
}
lightboxImg.src = imageUrl;
const title = image.title || `图片 ${currentImageIndex + 1}`;
let infoText = `${title} (${currentImageIndex + 1}/${images.length})`;
if (image.width && image.height) {
infoText += ` | ${image.width}×${image.height}`;
}
if (image.file_size) {
infoText += ` | ${(image.file_size / 1024 ).toFixed(2)} KB`;
}
lightboxInfo.textContent = infoText;
}
// 键盘导航
document.addEventListener('keydown', function(event) {
const lightbox = document.getElementById('lightbox');
if (lightbox.style.display === 'flex') {
if (event.key === 'Escape') closeLightbox();
if (event.key === 'ArrowRight') nextImage();
if (event.key === 'ArrowLeft') prevImage();
}
});
// 点击背景关闭
document.getElementById('lightbox').addEventListener('click', function(event) {
if (event.target === this) closeLightbox();
});
// 显示错误信息
function showError(message) {
const container = document.getElementById('images-container');
container.innerHTML = `<div class="no-results"><i class="fas fa-exclamation-triangle"></i><br>${message}</div>`;
document.getElementById('album-title').textContent = '加载失败';
document.getElementById('album-info').innerHTML = '';
}

115
assets/js/app.js Normal file
View File

@@ -0,0 +1,115 @@
// 全局变量
let allAlbums = [];
// API基础URL
const API_BASE = '/api';
// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
loadAlbums();
});
// 加载所有相册
async function loadAlbums() {
try {
const response = await fetch(`${API_BASE}/albums`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
allAlbums = await response.json();
displayAlbums(allAlbums);
updateStats(allAlbums);
} catch (error) {
console.error('加载相册失败:', error);
showError('加载相册数据失败,请刷新页面重试');
}
}
// 搜索相册
async function searchAlbums() {
const searchTerm = document.getElementById('search-input').value.toLowerCase().trim();
if (searchTerm === '') {
// 显示所有相册
displayAlbums(allAlbums);
updateStats(allAlbums);
return;
}
try {
const response = await fetch(`${API_BASE}/search?q=${encodeURIComponent(searchTerm)}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const filteredAlbums = await response.json();
displayAlbums(filteredAlbums);
if (filteredAlbums.length === 0) {
document.getElementById('stats').innerHTML = `没有找到包含 "${searchTerm}" 的相册`;
} else {
document.getElementById('stats').innerHTML = `找到 ${filteredAlbums.length} 个包含 "${searchTerm}" 的相册`;
}
} catch (error) {
console.error('搜索失败:', error);
showError('搜索失败,请重试');
}
}
// 显示相册列表
function displayAlbums(albums) {
const container = document.getElementById('albums-container');
if (albums.length === 0) {
container.innerHTML = '<div class="no-results"><i class="fas fa-search"></i><br>没有找到匹配的相册</div>';
return;
}
container.innerHTML = albums.map(album => {
// 处理封面图片URL
let coverImageUrl = '/assets/placeholder.svg';
if (album.cover_image_id) {
coverImageUrl = `/api/thumbnail/${album.cover_image_id}`;
}
// 格式化日期
const createdDate = new Date(album.created_at).toLocaleDateString('zh-CN');
return `
<div class="album-card">
<div class="album-cover">
<img src="${coverImageUrl}" alt="${album.title}" onerror="this.src='/assets/placeholder.svg'">
</div>
<div class="album-info">
<h3>${album.title}</h3>
<div class="album-meta">
<span><i class="fas fa-images"></i> ${album.image_count} 张图片</span>
<span><i class="fas fa-calendar"></i> ${createdDate}</span>
</div>
<a href="/album?id=${album.id}" class="view-button">
<i class="fas fa-eye"></i> 查看相册
</a>
</div>
</div>
`;
}).join('');
}
// 更新统计信息
function updateStats(albums) {
const totalImages = albums.reduce((sum, album) => sum + album.image_count, 0);
document.getElementById('stats').innerHTML = `共发现 ${albums.length} 个相册,包含 ${totalImages} 张图片`;
}
// 显示错误信息
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 = '';
}
// 键盘事件处理
function handleKeyPress(event) {
if (event.key === 'Enter') {
searchAlbums();
}
}