1
- import './base-form' ;
2
1
import { CommentVo , Reply , ReplyRequest , ReplyVo , User } from '@halo-dev/api-client' ;
3
- import { html , LitElement } from 'lit' ;
4
- import { createRef , Ref , ref } from 'lit/directives/ref.js' ;
2
+ import { consume } from '@lit/context' ;
3
+ import { LitElement , html } from 'lit' ;
4
+ import { property , state } from 'lit/decorators.js' ;
5
+ import { Ref , createRef , ref } from 'lit/directives/ref.js' ;
6
+ import './base-form' ;
7
+ import { BaseForm } from './base-form' ;
5
8
import {
6
9
allowAnonymousCommentsContext ,
7
10
baseUrlContext ,
8
11
currentUserContext ,
9
12
toastContext ,
10
13
} from './context' ;
11
- import { property , state } from 'lit/decorators.js' ;
12
- import { BaseForm } from './base-form' ;
13
- import { consume } from '@lit/context' ;
14
14
import { ToastManager } from './lit-toast' ;
15
+ import { getCaptchaCodeHeader , isRequireCaptcha } from './utils/captcha' ;
15
16
16
17
export class ReplyForm extends LitElement {
17
18
@consume ( { context : baseUrlContext } )
@@ -39,6 +40,9 @@ export class ReplyForm extends LitElement {
39
40
@state ( )
40
41
submitting = false ;
41
42
43
+ @state ( )
44
+ captcha = '' ;
45
+
42
46
baseFormRef : Ref < BaseForm > = createRef < BaseForm > ( ) ;
43
47
44
48
override connectedCallback ( ) : void {
@@ -53,6 +57,7 @@ export class ReplyForm extends LitElement {
53
57
override render ( ) {
54
58
return html ` < base-form
55
59
.submitting =${ this . submitting }
60
+ .captcha =${ this . captcha }
56
61
${ ref ( this . baseFormRef ) }
57
62
@submit="${ this . onSubmit } "
58
63
> </ base-form > ` ;
@@ -105,11 +110,21 @@ export class ReplyForm extends LitElement {
105
110
method : 'POST' ,
106
111
headers : {
107
112
'Content-Type' : 'application/json' ,
113
+ ...getCaptchaCodeHeader ( data . captchaCode ) ,
108
114
} ,
109
115
body : JSON . stringify ( replyRequest ) ,
110
116
}
111
117
) ;
112
118
119
+ if ( isRequireCaptcha ( response ) ) {
120
+ const { captcha, detail } = await response . json ( ) ;
121
+ this . captcha = captcha ;
122
+ this . toastManager ?. warn ( detail ) ;
123
+ return ;
124
+ }
125
+
126
+ this . captcha = '' ;
127
+
113
128
if ( ! response . ok ) {
114
129
throw new Error ( '评论失败,请稍后重试' ) ;
115
130
}
0 commit comments