From ec52abe746f63356257cb583088f2ccd660eb9ad Mon Sep 17 00:00:00 2001 From: weiwait Date: Wed, 8 Feb 2023 11:04:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6=E9=AA=8C=E8=AF=81=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 17 ++ resources/lang/en/auth.php | 12 + resources/lang/zh_CN/auth.php | 12 + resources/views/login.blade.php | 239 ++++++++++++++++++++ src/DcatVueServiceProvider.php | 3 +- src/Forms/AuthSetting.php | 51 +++++ src/Helper.php | 8 + src/Http/Controllers/DcatAuthController.php | 58 +++++ src/Http/routes.php | 4 + src/Setting.php | 25 +- version.php | 3 + 11 files changed, 427 insertions(+), 5 deletions(-) create mode 100644 resources/lang/en/auth.php create mode 100644 resources/lang/zh_CN/auth.php create mode 100644 resources/views/login.blade.php create mode 100644 src/Forms/AuthSetting.php create mode 100644 src/Http/Controllers/DcatAuthController.php diff --git a/README.md b/README.md index 07e6884..dde310b 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,23 @@ ->optionsFromKeyValue('kvs'); // 用于结合vKeyValue进行选项选择 ``` +##### 登录验证码 +![示例图片](https://github.com/weiwait/images/blob/main/dcat-auth.png?raw=true) + +```php +class SettingController extends Controller +{ + public function index(Content $content): Content + { + $tab = Tab::make(); + $tab->add('站点配置', new AuthSetting()); + + return $content->title('配置') + ->body($tab->withCard()); + } +} +``` + [comment]: <> (### Donate) [comment]: <> (![示例图片](https://github.com/weiwait/images/blob/main/donate.png?raw=true)) diff --git a/resources/lang/en/auth.php b/resources/lang/en/auth.php new file mode 100644 index 0000000..a65ab5c --- /dev/null +++ b/resources/lang/en/auth.php @@ -0,0 +1,12 @@ + 'Captcha', + 'enable_captcha' => 'Enable Captcha', + 'background' => 'Auth Background', + 'footer' => 'Footer', + 'footers' => [ + 'name' => 'ICP', + 'path' => 'Link' + ], +]; diff --git a/resources/lang/zh_CN/auth.php b/resources/lang/zh_CN/auth.php new file mode 100644 index 0000000..41129d9 --- /dev/null +++ b/resources/lang/zh_CN/auth.php @@ -0,0 +1,12 @@ + '验证码', + 'enable_captcha' => '开启验证码', + 'background' => '登录背景', + 'footer' => '页脚', + 'footers' => [ + 'name' => '备案号', + 'path' => '链接' + ], +]; diff --git a/resources/views/login.blade.php b/resources/views/login.blade.php new file mode 100644 index 0000000..f945ba6 --- /dev/null +++ b/resources/views/login.blade.php @@ -0,0 +1,239 @@ + + +
+ + + @if($config->get('footer')) + + @endif +
+ + diff --git a/src/DcatVueServiceProvider.php b/src/DcatVueServiceProvider.php index b26185c..b1d1e0d 100644 --- a/src/DcatVueServiceProvider.php +++ b/src/DcatVueServiceProvider.php @@ -74,10 +74,9 @@ class DcatVueServiceProvider extends ServiceProvider $configs = collect(admin_setting_array('weiwait_filesystem')); -// dd($configs); - app()->booted(function () use ($configs) { Helper::injectFilesystemConfig($configs); + Helper::injectAuthConfigs(); }); Event::listen('admin:booted', function () use ($configs) { diff --git a/src/Forms/AuthSetting.php b/src/Forms/AuthSetting.php new file mode 100644 index 0000000..1612183 --- /dev/null +++ b/src/Forms/AuthSetting.php @@ -0,0 +1,51 @@ + $input]); + + is_file(app()->getCachedConfigPath()) && Artisan::call('config:cache'); + + return $this + ->response() + ->success('保存成功') + ->refresh(); + } + + public function form() + { + $this->switch('enable_captcha', DcatVueServiceProvider::trans('auth.enable_captcha')) + ->default(true); + $this->vImage('background', DcatVueServiceProvider::trans('auth.background')); + $this->array('footer', DcatVueServiceProvider::trans('auth.footer'), function (NestedForm $form) { + $form->text('name', DcatVueServiceProvider::trans('auth.footers.name')); + $form->text('path', DcatVueServiceProvider::trans('auth.footers.path')); + }); + } + + /** + * The data of the form. + * + * @return array + */ + public function default() + { + return admin_setting_array('weiwait_auth'); + } +} diff --git a/src/Helper.php b/src/Helper.php index 1446e9a..dcec645 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -103,4 +103,12 @@ class Helper // 获取临时密钥,计算签名 return (new Sts())->getTempKeys($config); } + + public static function injectAuthConfigs() + { + config()->set('captcha.default.length', 4); + config()->set('captcha.default.height', 34); + config()->set('captcha.default.width', 100); + config()->set('captcha.default.quality', 100); + } } diff --git a/src/Http/Controllers/DcatAuthController.php b/src/Http/Controllers/DcatAuthController.php new file mode 100644 index 0000000..7da5ae0 --- /dev/null +++ b/src/Http/Controllers/DcatAuthController.php @@ -0,0 +1,58 @@ +config = collect(admin_setting()->getArray('weiwait_auth')); + } + + public function getLogin(Content $content) + { + if ($this->guard()->check()) { + return redirect($this->getRedirectPath()); + } + + return $content->full()->body(view('weiwait.dcat-vue::login', ['config' => $this->config])); + } + + public function postLogin(Request $request) + { + $credentials = $request->only([$this->username(), 'password']); + $remember = (bool) $request->input('remember', false); + + $rules = [ + $this->username() => 'required', + 'password' => 'required', + ]; + + if ($this->config->get('enable_captcha')) { + $rules['captcha'] = 'required|captcha'; + } + + /** @var \Illuminate\Validation\Validator $validator */ + $validator = Validator::make($request->all(), $rules, ['captcha.captcha' => '验证码错误']); + + if ($validator->fails()) { + return $this->validationErrorsResponse($validator); + } + + if ($this->guard()->attempt($credentials, $remember)) { + return $this->sendLoginResponse($request); + } + + return $this->validationErrorsResponse([ + $this->username() => $this->getFailedLoginMessage(), + ]); + } +} diff --git a/src/Http/routes.php b/src/Http/routes.php index f88e942..c0ba000 100644 --- a/src/Http/routes.php +++ b/src/Http/routes.php @@ -23,3 +23,7 @@ Route::get('weiwait/distpicker/address2ll', [Controllers\DcatVueController::clas // 坐标位置信息 Route::get('weiwait/distpicker/ll2address', [Controllers\DcatVueController::class, 'll2address']) ->name('weiwait.distpicker.ll2address'); + +// 替换原生登录 +Route::get('auth/login', [Controllers\DcatAuthController::class, 'getLogin']); +Route::post('auth/login', [Controllers\DcatAuthController::class, 'postLogin']); diff --git a/src/Setting.php b/src/Setting.php index a06473d..0308600 100644 --- a/src/Setting.php +++ b/src/Setting.php @@ -3,16 +3,35 @@ namespace Weiwait\DcatVue; use Dcat\Admin\Extend\Setting as Form; +use Dcat\Admin\Form\NestedForm; +use Dcat\Admin\Http\JsonResponse; class Setting extends Form { public function form() { - // Todo + $this->tab('站点', function (\Dcat\Admin\Widgets\Form $form) { + $form->switch('weiwait_auth.enable_captcha', DcatVueServiceProvider::trans('auth.enable_captcha')) + ->default(true); + $form->vImage('weiwait_auth.background', DcatVueServiceProvider::trans('auth.background')); + $form->array('weiwait_auth.footer', DcatVueServiceProvider::trans('auth.footer'), function (NestedForm $form) { + $form->text('name', DcatVueServiceProvider::trans('auth.footers.name')); + $form->text('path', DcatVueServiceProvider::trans('auth.footers.path')); + }); + }); } - public function handle(array $input) + public function handle(array $input): JsonResponse { - // Todo + admin_setting(['weiwait_auth' => $input['weiwait_auth']]); + + return $this->response()->success('保存成功')->refresh(); + } + + public function default(): array + { + return [ + 'weiwait_auth' => admin_setting_array('weiwait_auth'), + ]; } } diff --git a/version.php b/version.php index 0154baf..7cd4e89 100644 --- a/version.php +++ b/version.php @@ -80,4 +80,7 @@ return [ '修复vList为空时dcat依赖的默认值', '一些优化', ], + '2.6.0' => [ + '合并验证码到此', + ], ];