diff --git a/build.gradle b/build.gradle index 16f124f..d6c7d09 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,7 @@ repositories { } dependencies { - implementation platform('run.halo.tools.platform:plugin:2.9.0-SNAPSHOT') + implementation platform('run.halo.tools.platform:plugin:2.13.0-SNAPSHOT') compileOnly 'run.halo.app:api' testImplementation 'run.halo.app:api' @@ -42,4 +42,5 @@ build { halo { version = "2.15.0-rc.1" + debug = true } diff --git a/packages/comment-widget/src/base-form.ts b/packages/comment-widget/src/base-form.ts index 216ff34..c8991aa 100644 --- a/packages/comment-widget/src/base-form.ts +++ b/packages/comment-widget/src/base-form.ts @@ -1,20 +1,23 @@ -import './emoji-button'; +import type { User } from '@halo-dev/api-client'; +import { consume } from '@lit/context'; import { css, html, LitElement } from 'lit'; +import { property, state } from 'lit/decorators.js'; import { createRef, Ref, ref } from 'lit/directives/ref.js'; import { allowAnonymousCommentsContext, baseUrlContext, + captchaEnabledContext, currentUserContext, groupContext, kindContext, nameContext, + toastContext, } from './context'; -import { property, state } from 'lit/decorators.js'; -import type { User } from '@halo-dev/api-client'; +import './emoji-button'; +import './icons/icon-loading'; +import { ToastManager } from './lit-toast'; import baseStyles from './styles/base'; -import { consume } from '@lit/context'; import varStyles from './styles/var'; -import './icons/icon-loading'; export class BaseForm extends LitElement { @consume({ context: baseUrlContext }) @@ -29,6 +32,10 @@ export class BaseForm extends LitElement { @state() allowAnonymousComments = false; + @consume({ context: captchaEnabledContext, subscribe: true }) + @state() + captchaEnabled = false; + @consume({ context: groupContext }) @state() group = ''; @@ -41,9 +48,17 @@ export class BaseForm extends LitElement { @state() name = ''; + @property({ type: String }) + @state() + captcha = ''; + @property({ type: Boolean }) submitting = false; + @consume({ context: toastContext, subscribe: true }) + @state() + toastManager: ToastManager | undefined; + textareaRef: Ref = createRef(); get customAccount() { @@ -58,6 +73,25 @@ export class BaseForm extends LitElement { return `/console/login?redirect_uri=${encodeURIComponent(window.location.href + parentDomId)}`; } + get showCaptcha() { + return this.captchaEnabled && !this.currentUser; + } + + async handleFetchCaptcha() { + if (!this.showCaptcha) { + return; + } + + const response = await fetch(`/apis/api.commentwidget.halo.run/v1alpha1/captcha/-/generate`); + + if (!response.ok) { + this.toastManager?.error('获取验证码失败'); + return; + } + + this.captcha = await response.text(); + } + handleOpenLoginPage() { window.location.href = this.loginUrl; } @@ -124,6 +158,7 @@ export class BaseForm extends LitElement { override connectedCallback(): void { super.connectedCallback(); this.addEventListener('keydown', this.onKeydown); + this.handleFetchCaptcha(); } override disconnectedCallback(): void { @@ -182,6 +217,20 @@ export class BaseForm extends LitElement { ` : ''}
+ ${this.showCaptcha + ? html` +
+ + captcha +
+ ` + : ''} +