feat(frontend): 评论提交携带稳定的 request_id

服务端已经能按 request_id 判重,客户端要做的是把「同一次提交」表达清楚:

- api/comment: 新增 newRequestId()(crypto.getRandomValues,不用
  randomUUID——生产入口是非安全上下文),publish 带 request_id。
- CommentDrawer / VideoDetailView: 同一条视频 + 同样的内容复用同一个
  request_id;发送失败时保留(再按一次是在重试),成功后清空。内容一改
  必然换新键,避免把「用户真的想再发一条」误判成重复。

验证:npm run build(vue-tsc + vite)通过。
This commit is contained in:
2026-10-30 13:30:26 +08:00
parent 953b609970
commit a512bf6c61
4 changed files with 46 additions and 6 deletions

View File

@@ -7,8 +7,24 @@ export async function listAll(videoId: number) {
return normalizeCommentList(comments)
}
export function publish(videoId: number, content: string) {
return postJson<MessageResponse>('/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<MessageResponse>(
'/comment/publish',
{ video_id: videoId, content, request_id: requestId },
{ authRequired: true },
)
}
export function remove(commentId: number) {

View File

@@ -1,4 +1,4 @@
export type MessageResponse = { message: string }
export type MessageResponse = { message: string; request_id?: string }
export type DirectMessage = {
id: number