feat(评论): 添加评论置顶功能

- 在评论表中添加 priority 字段用于控制置顶权重
- 修改所有评论查询接口按 priority 和 created 排序
- 在管理后台添加置顶权重编辑功能
- 在评论列表显示置顶标识
This commit is contained in:
anghunk
2026-01-21 21:25:08 +08:00
parent 47c2eed6b5
commit 3871620fed
7 changed files with 51 additions and 16 deletions

View File

@@ -4,7 +4,7 @@ import { Bindings } from '../../bindings';
export const exportComments = async (c: Context<{ Bindings: Bindings }>) => {
try {
const { results } = await c.env.CWD_DB.prepare(
'SELECT * FROM Comment ORDER BY created DESC'
'SELECT * FROM Comment ORDER BY priority DESC, created DESC'
).all();
return c.json(results);

View File

@@ -27,7 +27,7 @@ export const listComments = async (c: Context<{ Bindings: Bindings }>) => {
.first<{ count: number }>();
const { results } = await c.env.CWD_DB.prepare(
`SELECT * FROM Comment ${whereSql} ORDER BY created DESC LIMIT ? OFFSET ?`
`SELECT * FROM Comment ${whereSql} ORDER BY priority DESC, created DESC LIMIT ? OFFSET ?`
)
.bind(...params, limit, offset)
.all();
@@ -52,6 +52,7 @@ export const listComments = async (c: Context<{ Bindings: Bindings }>) => {
contentText: row.content_text,
contentHtml: row.content_html,
status: row.status,
priority: row.priority,
ua: row.ua,
avatar: await getCravatar(row.email, avatarPrefix || undefined)
}))

View File

@@ -25,10 +25,10 @@ export const updateComment = async (c: Context<{ Bindings: Bindings }>) => {
}
const existing = await c.env.CWD_DB.prepare(
'SELECT id, status, post_slug FROM Comment WHERE id = ?'
'SELECT id, status, post_slug, priority FROM Comment WHERE id = ?'
)
.bind(id)
.first<{ id: number; status: string; post_slug: string }>();
.first<{ id: number; status: string; post_slug: string; priority: number | null }>();
if (!existing) {
return c.json({ message: 'Comment not found' }, 404);
@@ -38,6 +38,7 @@ export const updateComment = async (c: Context<{ Bindings: Bindings }>) => {
const rawEmail = typeof body.email === 'string' ? body.email : '';
const rawUrl = typeof body.url === 'string' ? body.url : '';
const rawStatus = typeof body.status === 'string' ? body.status : existing.status;
const rawPriority = body.priority;
const hasPostSlugField =
Object.prototype.hasOwnProperty.call(body, 'postSlug') ||
Object.prototype.hasOwnProperty.call(body, 'post_slug');
@@ -63,6 +64,22 @@ export const updateComment = async (c: Context<{ Bindings: Bindings }>) => {
? (rawPostSlug.trim() || existing.post_slug)
: existing.post_slug;
let priority: number = typeof existing.priority === 'number' && Number.isFinite(existing.priority)
? existing.priority
: 1;
if (rawPriority !== undefined && rawPriority !== null) {
const parsed =
typeof rawPriority === 'number'
? rawPriority
: typeof rawPriority === 'string' && rawPriority.trim()
? Number.parseInt(rawPriority.trim(), 10)
: NaN;
if (Number.isFinite(parsed) && parsed >= 1) {
priority = parsed;
}
}
if (!name) {
return c.json({ message: '昵称不能为空' }, 400);
}
@@ -90,9 +107,9 @@ export const updateComment = async (c: Context<{ Bindings: Bindings }>) => {
});
const { success } = await c.env.CWD_DB.prepare(
'UPDATE Comment SET name = ?, email = ?, url = ?, content_text = ?, content_html = ?, status = ?, post_slug = ? WHERE id = ?'
'UPDATE Comment SET name = ?, email = ?, url = ?, content_text = ?, content_html = ?, status = ?, post_slug = ?, priority = ? WHERE id = ?'
)
.bind(name, email, url, contentText, contentHtml, status, postSlug, id)
.bind(name, email, url, contentText, contentHtml, status, postSlug, priority, id)
.run();
if (!success) {

View File

@@ -13,14 +13,13 @@ export const getComments = async (c: Context<{ Bindings: Bindings }>) => {
if (!post_slug) return c.json({ message: "post_slug is required" }, 400)
try {
// 1. 查询审核通过的评论
const query = `
SELECT id, name, email, url, content_text as contentText,
content_html as contentHtml, created, parent_id as parentId,
post_slug as postSlug
post_slug as postSlug, priority
FROM Comment
WHERE post_slug = ? AND status = "approved"
ORDER BY created DESC
ORDER BY priority DESC, created DESC
`
const { results } = await c.env.CWD_DB.prepare(query).bind(post_slug).all()
@@ -105,4 +104,4 @@ export const getComments = async (c: Context<{ Bindings: Bindings }>) => {
} catch (e: any) {
return c.json({ message: e.message }, 500)
}
}
}