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 @@