Files
loongyan/web/src/routes/album/[id]/+page.server.js
lzw-723 2bb8a83bbc
Some checks failed
Dart CI / build (push) Failing after 9s
实现排序功能
2026-04-05 19:39:37 +08:00

55 lines
1.3 KiB
JavaScript

import { fetchApi } from '$lib/api/client';
/** @type {import('./$types').PageServerLoad} */
export async function load({ params, fetch, url }) {
const albumId = params.id;
// 支持分页参数,默认每页 100 张照片
const page = parseInt(url.searchParams.get('page') || '1', 10);
const pageSize = parseInt(url.searchParams.get('pageSize') || '100', 10);
// 支持排序参数
const sort = url.searchParams.get('sort') || 'fileName';
const order = url.searchParams.get('order') || 'asc';
try {
const [album, photos] = await Promise.all([
fetchApi(`/album/${albumId}`, fetch),
// 后端使用 size 作为分页参数名
fetchApi(
`/album/${albumId}/photo?page=${page}&size=${pageSize}&sort=${sort}&order=${order}`,
fetch
)
]);
if (!album) {
return {
album: null,
photos: [],
hasMore: false,
totalPhotos: 0
};
}
// 检测后端是否返回了分页信息
const hasMore = photos?.items
? (photos.page * photos.size) < photos.total
: photos?.length === pageSize;
const totalPhotos = photos?.total ?? photos?.length ?? 0;
return {
album,
photos: photos?.items ?? photos ?? [],
hasMore,
totalPhotos
};
} catch {
return {
album: null,
photos: [],
hasMore: false,
totalPhotos: 0
};
}
}