From 9eca0a82cafee1471c4793e1acec5863c34cc65e Mon Sep 17 00:00:00 2001 From: panliang <1163816051@qq.com> Date: Sat, 27 May 2023 09:57:32 +0800 Subject: [PATCH] auth --- app/Admin/Controllers/AuthController.php | 67 ++++++++++++++++++++++++ app/Admin/routes.php | 4 ++ lang/zh_CN/admin.php | 2 +- 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/app/Admin/Controllers/AuthController.php b/app/Admin/Controllers/AuthController.php index 594631f..c8f61a0 100644 --- a/app/Admin/Controllers/AuthController.php +++ b/app/Admin/Controllers/AuthController.php @@ -2,9 +2,76 @@ namespace App\Admin\Controllers; +use Illuminate\Support\Arr; +use Illuminate\Validation\Rule; +use Illuminate\Support\Facades\Hash; use Slowlyo\OwlAdmin\Controllers\AuthController as AdminAuthController; +use Slowlyo\OwlAdmin\Renderers\{Page, Form, TextControl, ImageControl}; class AuthController extends AdminAuthController { + public function userSetting(): \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource + { + $user = $this->user(); + $form = Form::make() + ->title() + ->panelClassName('px-48 m:px-0') + ->mode('horizontal') + ->data(Arr::only($user->toArray(), ['avatar', 'name'])) + ->api('put:' . admin_url('/user_setting')) + // ->persistDataKeys(['avatar', 'name']) + // ->resetAfterSubmit() + ->body([ + ImageControl::make() + ->label(__('admin.admin_user.avatar')) + ->name('avatar') + ->receiver($this->uploadImagePath()), + TextControl::make() + ->label(__('admin.admin_user.name')) + ->name('name') + ->required(), + TextControl::make() + ->type('input-password') + ->label(__('admin.old_password')) + ->name('old_password'), + TextControl::make() + ->type('input-password') + ->label('新密码') + ->name('password') + ->validations(['minLength' => 6]) + ->requiredOn('!!this.old_password'), + TextControl::make() + ->type('input-password') + ->label(__('admin.confirm_password')) + ->name('confirm_password') + ->validations(['minLength' => 6]) + ->requiredOn('!!this.old_password'), + ]); + + return $this->response()->success(Page::make()->body($form)); + } + + public function saveUserSetting(): \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource + { + $request = request(); + $request->validate([ + 'old_password' => ['nullable', 'current_password:sanctum'], + 'password' => Rule::requiredIf($request->filled('old_password')), + 'confirm_password' => [Rule::requiredIf($request->filled('old_password')), 'same:password'], + ], [ + 'password.required' => '新密码必填', + 'confirm_password.required' => '确认密码必填', + 'old_password.current_password' => __('admin.admin_user.old_password_error'), + 'confirm_password.same' => __('admin.admin_user.password_confirmation'), + ]); + $user = $this->user(); + $attributes = $request->only(['avatar', 'name']); + if ($request->filled('old_password')) { + $attributes['password'] + = Hash::make($request->input('password')); + } + $user->update($attributes); + return $this->response()->successMessage(__('admin.action_success')); + } } diff --git a/app/Admin/routes.php b/app/Admin/routes.php index b69e2be..ceb4338 100644 --- a/app/Admin/routes.php +++ b/app/Admin/routes.php @@ -12,6 +12,10 @@ Route::group([ $router->get('/_settings', '\App\Admin\Controllers\IndexController@settings'); $router->resource('system/admin_users', App\Admin\Controllers\AdminUserController::class); + // 用户设置 + $router->get('user_setting', [App\Admin\Controllers\AuthController::class, 'userSetting']); + $router->put('user_setting', [App\Admin\Controllers\AuthController::class, 'saveUserSetting']); + $router->group([ 'prefix' => 'api', ], function (Router $router) { diff --git a/lang/zh_CN/admin.php b/lang/zh_CN/admin.php index 6f7bf78..7b6786b 100644 --- a/lang/zh_CN/admin.php +++ b/lang/zh_CN/admin.php @@ -6,7 +6,7 @@ return [ 'logout' => '退出登录', 'username' => '用户名', 'password' => '密码', - 'old_password' => '旧密码', + 'old_password' => '原密码', 'confirm_password' => '确认密码', 'captcha' => '验证码', 'captcha_error' => '验证码有误',