Skip to content

Commit

Permalink
feat(views): Add captcha.blade.php view file
Browse files Browse the repository at this point in the history
- Added a new view file 'resources/views/captcha.blade.php'.
- Contains JavaScript code for captcha functionality.
  • Loading branch information
guanguans committed Oct 17, 2023
1 parent 1a3f1ea commit 3f83e47
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 56 deletions.
27 changes: 0 additions & 27 deletions config/login-captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,4 @@
'font' => null,
'fingerprint' => null,
'captcha_phrase_session_key' => 'login_captcha_phrase',
'script' => <<<'JS'
(function () {
const captchaDOM = `
<fieldset class="form-label-group form-group position-relative has-icon-left dcat-login-captcha">
<input id="captcha" class="form-control" type="text" style="width: 61%;" name="captcha" placeholder="{{captchaLang}}" required>
<span class="captcha-img" style="width: 37%;height: 33.5px;position: absolute;top: 0;right: 0;border-radius: .25rem;border: 1px solid #dbe3e6;">
<img id="verify" class="captcha" src="{{captchaUrl}}" data-src="{{captchaUrl}}" alt="{{captchaLang}}" title="{{refreshCaptchaLang}}" style="cursor: pointer;width: 100%;height: 100%;border-radius: .25rem;">
</span>
<div class="form-control-position"><i class="feather icon-image"></i></div>
<label for="captcha">{{captchaLang}}</label>
<div class="help-block with-errors"></div>
</fieldset>
`;
$(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,
];
25 changes: 25 additions & 0 deletions resources/views/captcha.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(function () {
const captchaDOM = `
<fieldset class="form-label-group form-group position-relative has-icon-left dcat-login-captcha">
<input id="captcha" class="form-control" type="text" style="width: 61%;" name="captcha" placeholder="{{ \Guanguans\DcatLoginCaptcha\LoginCaptchaServiceProvider::trans('login-captcha.captcha') }}" required>
<span class="captcha-img" style="width: 37%;height: 33.5px;position: absolute;top: 0;right: 0;border-radius: .25rem;border: 1px solid #dbe3e6;">
<img id="verify" class="captcha" src="{{ login_captcha_url() }}" data-src="{{ login_captcha_url() }}" alt="{{ \Guanguans\DcatLoginCaptcha\LoginCaptchaServiceProvider::trans('login-captcha.captcha') }}" title="{{ \Guanguans\DcatLoginCaptcha\LoginCaptchaServiceProvider::trans('login-captcha.refresh_captcha') }}" style="cursor: pointer;width: 100%;height: 100%;border-radius: .25rem;">
</span>
<div class="form-control-position"><i class="feather icon-image"></i></div>
<label for="captcha">{{ \Guanguans\DcatLoginCaptcha\LoginCaptchaServiceProvider::trans('login-captcha.captcha') }}</label>
<div class="help-block with-errors"></div>
</fieldset>
`;

$(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");
}
});
})();
41 changes: 12 additions & 29 deletions src/LoginCaptchaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -138,50 +140,31 @@ 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")) {
$validator = Validator::make(Request::post(), [
'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
*/
Expand Down

0 comments on commit 3f83e47

Please sign in to comment.