refactor: 支持通过配置的别名动态渲染前台页面提示
continuous-integration/drone/push Build is passing

This commit is contained in:
seahi committed 2026-09-16 12:51:16 +08:00
1 parent 19061047b1
commit 5c993bb6cc
6 files changed
+18 -14

No files matched your search

+4 -3
View File
@@ -3,9 +3,10 @@
import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params, platform }) => {
export const load: PageServerLoad = async ({ params, platform, parent }) => {
const db = platform?.env?.DB;
const slug = params.slug;
const { aliases } = await parent();
if (!db) {
throw error(500, '数据库未连接');
@@ -28,7 +29,7 @@ export const load: PageServerLoad = async ({ params, platform }) => {
}>();
if (!page) {
throw error(404, '该页面不存在');
throw error(404, `该${aliases?.page || '页面'}不存在`);
}
return {
@@ -37,6 +38,6 @@ export const load: PageServerLoad = async ({ params, platform }) => {
} catch (err: unknown) {
if ((err as any)?.status === 404) throw err;
console.error('Error loading page:', err);
throw error(500, '加载页面失败');
throw error(500, '读取详情失败');
}
};
+1 -1
View File
@@ -26,6 +26,6 @@ export const load: PageServerLoad = async ({ platform }) => {
};
} catch (err: any) {
console.error('Error fetching public categories list:', err);
throw error(500, '读取分类列表失败');
throw error(500, '读取列表失败');
}
};
+4 -3
View File
@@ -25,8 +25,9 @@ function createExcerpt(source: string): string {
return plainText ? `${plainText}...` : '';
}
export const load: PageServerLoad = async ({ platform, params, url }) => {
export const load: PageServerLoad = async ({ platform, params, url, parent }) => {
const db = platform?.env.DB;
const { aliases } = await parent();
if (!db) throw error(500, '数据库未连接');
try {
@@ -34,7 +35,7 @@ export const load: PageServerLoad = async ({ platform, params, url }) => {
.prepare('SELECT id, name, slug FROM categories WHERE slug = ? COLLATE NOCASE')
.bind(params.slug)
.first<{ id: number; name: string; slug: string }>();
if (!category) throw error(404, '该分类不存在');
if (!category) throw error(404, `该${aliases?.category || '分类'}不存在`);
const countResult = await db
.prepare('SELECT COUNT(*) AS total FROM posts WHERE category_id = ?')
@@ -95,6 +96,6 @@ export const load: PageServerLoad = async ({ platform, params, url }) => {
} catch (cause) {
if (isHttpError(cause, 404)) throw cause;
console.error('Error loading category posts:', cause);
throw error(500, '读取分类内容列表失败');
throw error(500, '读取列表失败');
}
};
+1 -1
View File
@@ -26,6 +26,6 @@ export const load: PageServerLoad = async ({ platform }) => {
};
} catch (err: any) {
console.error('Error fetching public series list:', err);
throw error(500, '读取系列列表失败');
throw error(500, '读取列表失败');
}
};
+4 -3
View File
@@ -1,15 +1,16 @@
import { error, isHttpError } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ platform, params }) => {
export const load: PageServerLoad = async ({ platform, params, parent }) => {
const db = platform?.env?.DB;
const { aliases } = await parent();
if (!db) throw error(500, '数据库未连接');
try {
const series = await db
.prepare('SELECT id, name, slug FROM series WHERE slug = ?')
.bind(params.slug)
.first<{ id: number; name: string; slug: string }>();
if (!series) throw error(404, '系列不存在');
if (!series) throw error(404, `${aliases?.series || '系列'}不存在`);
const { results } = await db
.prepare(
`SELECT id, slug, title, series_weight, published_at
@@ -33,6 +34,6 @@ export const load: PageServerLoad = async ({ platform, params }) => {
} catch (err: unknown) {
if (isHttpError(err, 404)) throw err;
console.error('Error fetching series detail:', err);
throw error(500, '读取系列失败');
throw error(500, '读取详情失败');
}
};
+4 -3
View File
@@ -25,8 +25,9 @@ function createExcerpt(source: string): string {
return plainText ? `${plainText}...` : '';
}
export const load: PageServerLoad = async ({ platform, params, url }) => {
export const load: PageServerLoad = async ({ platform, params, url, parent }) => {
const db = platform?.env.DB;
const { aliases } = await parent();
if (!db) throw error(500, '数据库还未成功相连');
try {
@@ -34,7 +35,7 @@ export const load: PageServerLoad = async ({ platform, params, url }) => {
.prepare('SELECT id, name FROM tags WHERE name = ? COLLATE NOCASE')
.bind(params.tag)
.first<{ id: number; name: string }>();
if (!tag) throw error(404, '尚未录入此标签或原归类已被卸除');
if (!tag) throw error(404, `未找到该${aliases?.tag || '标签'}`);
const countResult = await db
.prepare('SELECT COUNT(*) AS total FROM post_tags WHERE tag_id = ?')
@@ -96,6 +97,6 @@ export const load: PageServerLoad = async ({ platform, params, url }) => {
} catch (cause) {
if (isHttpError(cause, 404)) throw cause;
console.error('Error loading tag posts:', cause);
throw error(500, '读取分类标签对应内容列表受阻中断');
throw error(500, '读取列表失败');
}
};