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

View File

@@ -1,5 +1,5 @@
<script setup lang="ts">
import { reactive } from 'vue'
import { reactive, ref } from 'vue'
import { ApiError } from '../api/client'
import * as commentApi from '../api/comment'
@@ -20,6 +20,11 @@ const drawer = reactive({
content: '',
})
// 上一次提交的意图:同一条视频 + 同样的内容 => 复用同一个 request_id。
// 发送失败时**不清空**,这样用户再按一次「发送」是在重试同一次提交,
// 而不是制造第二条评论;只有成功后才清空,内容一改就必然换新键。
const pendingIntent = ref<{ videoId: number; content: string; requestId: string } | null>(null)
function needLogin() {
toast.error('请先登录')
}
@@ -49,10 +54,16 @@ async function publishComment() {
if (!auth.isLoggedIn) return needLogin()
const content = drawer.content.trim()
if (!content) return
const videoId = props.video.id
if (pendingIntent.value?.content !== content || pendingIntent.value?.videoId !== videoId) {
pendingIntent.value = { videoId, content, requestId: commentApi.newRequestId() }
}
const requestId = pendingIntent.value.requestId
drawer.loading = true
drawer.error = ''
try {
await commentApi.publish(props.video.id, content)
await commentApi.publish(videoId, content, requestId)
pendingIntent.value = null
drawer.content = ''
await loadComments()
toast.success('评论已发布')

View File

@@ -40,6 +40,10 @@ const drawer = reactive({
content: '',
})
// 上一次提交的意图:同一条视频 + 同样的内容 => 复用同一个 request_id。
// 发送失败时不清空(用户再按一次是在重试同一次提交),成功后清空。
const pendingIntent = ref<{ videoId: number; content: string; requestId: string } | null>(null)
async function needLogin() {
toast.error('请先登录')
await router.push('/account')
@@ -187,10 +191,19 @@ async function publishComment() {
const content = drawer.content.trim()
if (!content) return
const videoId = state.video.id
// 同一条视频 + 同样的内容 => 复用同一个 request_id(重试而不是重发);
// 失败时保留,成功后清空。
if (pendingIntent.value?.content !== content || pendingIntent.value?.videoId !== videoId) {
pendingIntent.value = { videoId, content, requestId: commentApi.newRequestId() }
}
const requestId = pendingIntent.value.requestId
drawer.loading = true
drawer.error = ''
try {
await commentApi.publish(state.video.id, content)
await commentApi.publish(videoId, content, requestId)
pendingIntent.value = null
drawer.content = ''
await loadComments()
toast.success('评论已发布')