@ -81,6 +81,8 @@ function flattenTree(nodes: CommentNode[], depth = 0): Array<{ node: CommentNode
const replyToId = ref < number | null > ( null )
const draftBody = ref ( '' )
const guestName = ref ( '' )
const guestEmail = ref ( '' )
const guestIsAnonymous = ref ( false )
const submitting = ref ( false )
function authorLine ( node : CommentNode ) : string {
@ -129,6 +131,10 @@ async function submitComment() {
toast . add ( { title : '请填写昵称' , color : 'error' } )
return
}
if ( ! guestIsAnonymous . value && ! guestEmail . value . trim ( ) ) {
toast . add ( { title : '请填写邮箱' , color : 'error' } )
return
}
}
submitting . value = true
@ -136,7 +142,13 @@ async function submitComment() {
const payload = {
parentId : replyToId . value ,
body : draftBody . value ,
... ( loggedIn . value ? { } : { guestDisplayName : guestName . value . trim ( ) } ) ,
... ( loggedIn . value
? { }
: {
guestDisplayName : guestName . value . trim ( ) ,
guestEmail : guestEmail . value . trim ( ) ,
guestIsAnonymous : guestIsAnonymous . value ,
} ) ,
}
await fetchData < { id : number } > ( ` ${ base } /comments ` , {
@ -146,6 +158,8 @@ async function submitComment() {
draftBody . value = ''
guestName . value = ''
guestEmail . value = ''
guestIsAnonymous . value = false
replyToId . value = null
await refreshComments ( )
toast . add ( { title : '评论已发送' , color : 'success' } )
@ -223,6 +237,14 @@ async function submitComment() {
< / template >
< template v-else >
< UInput v -model = " guestName " placeholder = "你的昵称" class = "w-full max-w-md" / >
< UInput
v - model = "guestEmail"
type = "email"
: required = "!guestIsAnonymous"
: placeholder = "guestIsAnonymous ? '你的邮箱(匿名时可选)' : '你的邮箱(必填)'"
class = "w-full max-w-md"
/ >
< UCheckbox v -model = " guestIsAnonymous " label = "匿名评论" / >
< UTextarea v -model = " draftBody " placeholder = "写下你的评论…" :rows ="4" class = "w-full" / >
< UButton :loading ="submitting" label = "发表" @click ="submitComment" / >
< / template >