fix(api): 修复评论获取接口中URL处理逻辑

改进评论获取接口,正确处理带斜杠和不带斜杠的URL路径,确保能匹配到正确的评论
This commit is contained in:
anghunk
2026-01-22 15:09:02 +08:00
parent 77a329030b
commit b8f01d0911
4 changed files with 39 additions and 8 deletions

View File

@@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="https://cwd.js.org/icon.png" <link rel="shortcut icon" href="https://cwd.js.org/icon.png"
type="image/x-icon"> type="image/x-icon">
<title>CWD 评论后台</title> <title>CWD 评论系统</title>
</head> </head>
<body> <body>

View File

@@ -14,7 +14,7 @@
/> />
</svg> </svg>
</button> </button>
<div class="layout-title">CWD 评论后台</div> <div class="layout-title">CWD 评论系统</div>
<div class="layout-actions-wrapper"> <div class="layout-actions-wrapper">
<div class="layout-domain-filter"> <div class="layout-domain-filter">
<select v-model="domainFilter" class="layout-domain-select"> <select v-model="domainFilter" class="layout-domain-select">

View File

@@ -1,7 +1,7 @@
<template> <template>
<div class="login-page"> <div class="login-page">
<div class="login-card"> <div class="login-card">
<h1 class="login-title">CWD 评论后台</h1> <h1 class="login-title">CWD 评论系统</h1>
<p class="login-subtitle">简洁的自托管评论系统管理面板</p> <p class="login-subtitle">简洁的自托管评论系统管理面板</p>
<form class="login-form" @submit.prevent="handleSubmit"> <form class="login-form" @submit.prevent="handleSubmit">
<div class="form-item"> <div class="form-item">

View File

@@ -3,25 +3,56 @@ import { Bindings } from '../../bindings'
import { getCravatar } from '../../utils/getAvatar' import { getCravatar } from '../../utils/getAvatar'
export const getComments = async (c: Context<{ Bindings: Bindings }>) => { export const getComments = async (c: Context<{ Bindings: Bindings }>) => {
const post_slug = c.req.query('post_slug') const rawPostSlug = c.req.query('post_slug') || ''
const postSlug = rawPostSlug.trim()
const page = parseInt(c.req.query('page') || '1') const page = parseInt(c.req.query('page') || '1')
const limit = Math.min(parseInt(c.req.query('limit') || '20'), 50) const limit = Math.min(parseInt(c.req.query('limit') || '20'), 50)
const nested = c.req.query('nested') !== 'false' const nested = c.req.query('nested') !== 'false'
const avatar_prefix = c.req.query('avatar_prefix') const avatar_prefix = c.req.query('avatar_prefix')
const offset = (page - 1) * limit const offset = (page - 1) * limit
if (!post_slug) return c.json({ message: "post_slug is required" }, 400) if (!postSlug) return c.json({ message: "post_slug is required" }, 400)
let slugList: string[] = [postSlug]
try {
const url = new URL(postSlug)
const origin = url.origin
const path = url.pathname || '/'
if (path === '/') {
const withSlash = origin + '/'
const withoutSlash = origin
slugList = Array.from(new Set([withSlash, withoutSlash]))
} else {
const hasTrailingSlash = path.endsWith('/')
const withSlash = origin + (hasTrailingSlash ? path : path + '/')
const withoutSlash = origin + (hasTrailingSlash ? path.slice(0, -1) : path)
slugList = Array.from(new Set([withSlash, withoutSlash]))
}
} catch {
slugList = [postSlug]
}
try { try {
const query = ` let query = `
SELECT id, name, email, url, content_text as contentText, SELECT id, name, email, url, content_text as contentText,
content_html as contentHtml, created, parent_id as parentId, content_html as contentHtml, created, parent_id as parentId,
post_slug as postSlug, priority post_slug as postSlug, priority
FROM Comment FROM Comment
WHERE post_slug = ? AND status = "approved" WHERE status = "approved" AND post_slug = ?
ORDER BY priority DESC, created DESC ORDER BY priority DESC, created DESC
` `
const { results } = await c.env.CWD_DB.prepare(query).bind(post_slug).all() if (slugList.length > 1) {
const placeholders = slugList.map(() => '?').join(', ')
query = `
SELECT id, name, email, url, content_text as contentText,
content_html as contentHtml, created, parent_id as parentId,
post_slug as postSlug, priority
FROM Comment
WHERE status = "approved" AND post_slug IN (${placeholders})
ORDER BY priority DESC, created DESC
`
}
const { results } = await c.env.CWD_DB.prepare(query).bind(...slugList).all()
// 2. 批量处理头像并格式化 // 2. 批量处理头像并格式化
const allComments = await Promise.all(results.map(async (row: any) => ({ const allComments = await Promise.all(results.map(async (row: any) => ({