diff --git a/config/login-captcha.php b/config/login-captcha.php index fea80e2..24e947a 100644 --- a/config/login-captcha.php +++ b/config/login-captcha.php @@ -19,31 +19,4 @@ 'font' => null, 'fingerprint' => null, 'captcha_phrase_session_key' => 'login_captcha_phrase', - 'script' => <<<'JS' - (function () { - const captchaDOM = ` -
- `; - - $(captchaDOM).insertAfter($('#login-form fieldset.form-label-group').get(1)); - - $('#verify').click(function () { - $(this).attr('src', $(this).data('src').replace(/\?.*$/, '') + '?' + Math.random()); - }); - - $('.dcat-login-captcha .with-errors').bind('DOMNodeInserted', function () { - if ($('#captcha').val() !== '' && $(this).html().length > 0) { - $("#verify").trigger("click"); - } - }); - })(); - JS, ]; diff --git a/resources/views/captcha.blade.php b/resources/views/captcha.blade.php new file mode 100644 index 0000000..cf28d64 --- /dev/null +++ b/resources/views/captcha.blade.php @@ -0,0 +1,25 @@ +(function () { + const captchaDOM = ` + + `; + + $(captchaDOM).insertAfter($('#login-form fieldset.form-label-group').get(1)); + + $('#verify').click(function () { + $(this).attr('src', $(this).data('src').replace(/\?.*$/, '') + '?' + Math.random()); + }); + + $('.dcat-login-captcha .with-errors').bind('DOMNodeInserted', function () { + if ($('#captcha').val() !== '' && $(this).html().length > 0) { + $("#verify").trigger("click"); + } + }); +})(); diff --git a/src/LoginCaptchaServiceProvider.php b/src/LoginCaptchaServiceProvider.php index b5d4518..179004f 100644 --- a/src/LoginCaptchaServiceProvider.php +++ b/src/LoginCaptchaServiceProvider.php @@ -16,12 +16,10 @@ use Dcat\Admin\Extend\ServiceProvider; use Dcat\Admin\Support\Helper; use Dcat\Admin\Traits\HasFormResponse; -use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Foundation\Application; use Illuminate\Http\Exceptions\HttpResponseException; use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Validator; -use Illuminate\Support\MessageBag; use Illuminate\Support\Str; class LoginCaptchaServiceProvider extends ServiceProvider @@ -51,7 +49,11 @@ public function init(): void $this->setupConfig() ->loadMigrations() ->extendValidator() - ->bootingAdmin(); + ->bootingCaptcha(); + + $this->publishes([ + $this->getViewPath() => resource_path(sprintf('views/vendor/%s', $this->getName())), + ]); } public function provides(): array @@ -138,14 +140,15 @@ protected function extendValidator(): self } /** - * @throws BindingResolutionException + * @psalm-suppress InvalidCast + * @psalm-suppress InvalidArgument */ - protected function bootingAdmin(): self + protected function bootingCaptcha(): self { Admin::booting(function (): void { $loginPath = ltrim(admin_base_path('auth/login'), '/'); if (Helper::matchRequestPath("GET:$loginPath")) { - Admin::script($this->buildScript()); + Admin::script((string) view(sprintf('%s::captcha', $this->getName()))); } if (Helper::matchRequestPath("POST:$loginPath")) { @@ -153,35 +156,15 @@ protected function bootingAdmin(): self 'captcha' => 'required|dcat_login_captcha', ]); - $validator->fails() and $this->throwHttpResponseException($validator); + if ($validator->fails()) { + throw new HttpResponseException($validator); + } } }); return $this; } - /** - * @psalm-suppress InvalidArgument - */ - protected function buildScript(): string - { - $replacedRules = [ - '{{captchaUrl}}' => login_captcha_url(), - '{{captchaLang}}' => self::trans('login-captcha.captcha'), - '{{refreshCaptchaLang}}' => self::trans('login-captcha.refresh_captcha'), - ]; - - return str_replace(array_keys($replacedRules), $replacedRules, static::setting('script')); - } - - /** - * @param array|\Illuminate\Validation\Validator|MessageBag $validationMessages - */ - protected function throwHttpResponseException($validationMessages): void - { - throw new HttpResponseException($this->validationErrorsResponse($validationMessages)); - } - /** * @param class-string $class */