From a512bf6c611ad14e78e0fb2d3b3c7371e7e28203 Mon Sep 17 00:00:00 2001 From: huanghaosheng <386998068@qq.com> Date: Fri, 30 Oct 2026 13:30:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(frontend):=20=E8=AF=84=E8=AE=BA=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E6=90=BA=E5=B8=A6=E7=A8=B3=E5=AE=9A=E7=9A=84=20reques?= =?UTF-8?q?t=5Fid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 服务端已经能按 request_id 判重,客户端要做的是把「同一次提交」表达清楚: - api/comment: 新增 newRequestId()(crypto.getRandomValues,不用 randomUUID——生产入口是非安全上下文),publish 带 request_id。 - CommentDrawer / VideoDetailView: 同一条视频 + 同样的内容复用同一个 request_id;发送失败时保留(再按一次是在重试),成功后清空。内容一改 必然换新键,避免把「用户真的想再发一条」误判成重复。 验证:npm run build(vue-tsc + vite)通过。 --- frontend/src/api/comment.ts | 20 ++++++++++++++++++-- frontend/src/api/types.ts | 2 +- frontend/src/components/CommentDrawer.vue | 15 +++++++++++++-- frontend/src/views/VideoDetailView.vue | 15 ++++++++++++++- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/frontend/src/api/comment.ts b/frontend/src/api/comment.ts index 798c3d5..69ac339 100644 --- a/frontend/src/api/comment.ts +++ b/frontend/src/api/comment.ts @@ -7,8 +7,24 @@ export async function listAll(videoId: number) { return normalizeCommentList(comments) } -export function publish(videoId: number, content: string) { - return postJson('/comment/publish', { video_id: videoId, content }, { authRequired: true }) +// request_id 是"这一次用户想发这条评论"的键。服务端把它写进 comments 的 +// 唯一索引,所以:同一次提交的重试必须复用它(否则重试会变成两条评论), +// 内容变了必须换新的(否则用户真的想再发一条相同内容时会被判成重复)。 +// +// 用 crypto.getRandomValues 而不是 crypto.randomUUID():生产入口是 +// http://goanchor.top:9000,非安全上下文里 randomUUID 根本不存在。 +export function newRequestId(): string { + const bytes = new Uint8Array(16) + crypto.getRandomValues(bytes) + return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('') +} + +export function publish(videoId: number, content: string, requestId: string) { + return postJson( + '/comment/publish', + { video_id: videoId, content, request_id: requestId }, + { authRequired: true }, + ) } export function remove(commentId: number) { diff --git a/frontend/src/api/types.ts b/frontend/src/api/types.ts index 7c1f6b1..6b970c9 100644 --- a/frontend/src/api/types.ts +++ b/frontend/src/api/types.ts @@ -1,4 +1,4 @@ -export type MessageResponse = { message: string } +export type MessageResponse = { message: string; request_id?: string } export type DirectMessage = { id: number diff --git a/frontend/src/components/CommentDrawer.vue b/frontend/src/components/CommentDrawer.vue index 4a0954e..0731774 100644 --- a/frontend/src/components/CommentDrawer.vue +++ b/frontend/src/components/CommentDrawer.vue @@ -1,5 +1,5 @@