From fc48deb33889ce6babdba8d55b16f5a6948101cb Mon Sep 17 00:00:00 2001 From: zpj80231 Date: Wed, 25 Mar 2026 14:57:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(cwd-api,=20widget):=20=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E8=AF=84=E8=AE=BA=E5=8F=96=E6=B6=88=E7=82=B9=E8=B5=9E=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加了评论取消点赞的逻辑处理 - 新增 DELETE /api/comments/like 接口用于取消点赞 - 修改点赞接口支持根据请求方法判断增加或减少点赞数 - 优化点赞数计算逻辑,支持正确的增减操作 - 更新错误提示信息为更通用的操作失败提示 --- cwd-api/src/api/public/likeComment.ts | 27 +++++++++-------------- cwd-api/src/index.ts | 1 + docs/widget/src/components/CommentItem.js | 6 ++++- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/cwd-api/src/api/public/likeComment.ts b/cwd-api/src/api/public/likeComment.ts index d4dd849..df47dc2 100644 --- a/cwd-api/src/api/public/likeComment.ts +++ b/cwd-api/src/api/public/likeComment.ts @@ -27,6 +27,7 @@ export const likeComment = async (c: Context<{ Bindings: Bindings }>) => { } const id = parsed; + const method = c.req.method; try { const existing = await c.env.CWD_DB.prepare( @@ -39,26 +40,20 @@ export const likeComment = async (c: Context<{ Bindings: Bindings }>) => { return c.json({ message: 'Comment not found' }, 404); } + const delta = method === 'DELETE' ? -1 : 1; + const currentLikes = typeof existing.likes === 'number' && Number.isFinite(existing.likes) && existing.likes >= 0 + ? existing.likes + : 0; + const newLikes = Math.max(0, currentLikes + delta); + await c.env.CWD_DB.prepare( - 'UPDATE Comment SET likes = COALESCE(likes, 0) + 1 WHERE id = ?' + 'UPDATE Comment SET likes = ? WHERE id = ?' ) - .bind(id) + .bind(newLikes, id) .run(); - const updated = await c.env.CWD_DB.prepare( - 'SELECT COALESCE(likes, 0) as likes FROM Comment WHERE id = ?' - ) - .bind(id) - .first<{ likes?: number }>(); - - const likes = - updated && typeof updated.likes === 'number' && Number.isFinite(updated.likes) && updated.likes >= 0 - ? updated.likes - : ((existing.likes || 0) + 1); - - return c.json({ id, likes }); + return c.json({ id, likes: newLikes }); } catch (e: any) { - return c.json({ message: e?.message || '点赞失败' }, 500); + return c.json({ message: e?.message || '操作失败' }, 500); } }; - diff --git a/cwd-api/src/index.ts b/cwd-api/src/index.ts index cc87437..8f765a4 100644 --- a/cwd-api/src/index.ts +++ b/cwd-api/src/index.ts @@ -273,6 +273,7 @@ app.get('/api/analytics/pv', getPagePv); app.get('/api/like', getLikeStatus); app.post('/api/like', likePage); app.post('/api/comments/like', likeComment); +app.delete('/api/comments/like', likeComment); app.post('/api/telegram/webhook', telegramWebhook); app.get('/api/config/comments', async (c) => { try { diff --git a/docs/widget/src/components/CommentItem.js b/docs/widget/src/components/CommentItem.js index 9fe0b57..998bbb3 100644 --- a/docs/widget/src/components/CommentItem.js +++ b/docs/widget/src/components/CommentItem.js @@ -371,8 +371,12 @@ export class CommentItem extends Component { likedComments.add(commentId); this.saveLikedComments(likedComments); this.props.onLikeComment(commentId, true); + } else { + // 已点赞,执行取消点赞 + likedComments.delete(commentId); + this.saveLikedComments(likedComments); + this.props.onLikeComment(commentId, false); } - // 已点赞则不做任何操作 } /**