From 0d21c13d25f4218eba18d5e6f4f6327af11825d5 Mon Sep 17 00:00:00 2001 From: Summer Date: Mon, 7 Mar 2022 19:02:31 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86=E5=90=8E?= =?UTF-8?q?=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/User.php | 25 ++++++++ config/administrator/users.php | 109 +++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 config/administrator/users.php diff --git a/app/Models/User.php b/app/Models/User.php index b0a70c628..9de2a8b68 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -10,6 +10,7 @@ use Illuminate\Auth\MustVerifyEmail as MustVerifyEmailTrait; use Illuminate\Support\Facades\Auth; use Spatie\Permission\Traits\HasRoles; +use Illuminate\Support\Str; class User extends Authenticatable implements MustVerifyEmail { @@ -73,4 +74,28 @@ public function markAsRead() $this->save(); $this->unreadNotifications->markAsRead(); } + + public function setPasswordAttribute($value) + { + // 如果值的长度等于 60,即认为是已经做过加密的情况 + if (strlen($value) != 60) { + + // 不等于 60,做密码加密处理 + $value = bcrypt($value); + } + + $this->attributes['password'] = $value; + } + + public function setAvatarAttribute($path) + { + // 如果不是 `http` 子串开头,那就是从后台上传的,需要补全 URL + if ( ! Str::startsWith($path, 'http')) { + + // 拼接完整的 URL + $path = config('app.url') . "/uploads/images/avatars/$path"; + } + + $this->attributes['avatar'] = $path; + } } diff --git a/config/administrator/users.php b/config/administrator/users.php new file mode 100644 index 000000000..6f1aba5d9 --- /dev/null +++ b/config/administrator/users.php @@ -0,0 +1,109 @@ + '用户', + + // 模型单数,用作页面『新建 $single』 + 'single' => '用户', + + // 数据模型,用作数据的 CRUD + 'model' => User::class, + + // 设置当前页面的访问权限,通过返回布尔值来控制权限。 + // 返回 True 即通过权限验证,False 则无权访问并从 Menu 中隐藏 + 'permission'=> function() + { + return Auth::user()->can('manage_users'); + }, + + // 字段负责渲染『数据表格』,由无数的『列』组成, + 'columns' => [ + + // 列的标示,这是一个最小化『列』信息配置的例子,读取的是模型里对应 + // 的属性的值,如 $model->id + 'id', + + 'avatar' => [ + // 数据表格里列的名称,默认会使用『列标识』 + 'title' => '头像', + + // 默认情况下会直接输出数据,你也可以使用 output 选项来定制输出内容 + 'output' => function ($avatar, $model) { + return empty($avatar) ? 'N/A' : ''; + }, + + // 是否允许排序 + 'sortable' => false, + ], + + 'name' => [ + 'title' => '用户名', + 'sortable' => false, + 'output' => function ($name, $model) { + return ''.$name.''; + }, + ], + + 'email' => [ + 'title' => '邮箱', + ], + + 'operation' => [ + 'title' => '管理', + 'sortable' => false, + ], + ], + + // 『模型表单』设置项 + 'edit_fields' => [ + 'name' => [ + 'title' => '用户名', + ], + 'email' => [ + 'title' => '邮箱', + ], + 'password' => [ + 'title' => '密码', + + // 表单使用 input 类型 password + 'type' => 'password', + ], + 'avatar' => [ + 'title' => '用户头像', + + // 设置表单条目的类型,默认的 type 是 input + 'type' => 'image', + + // 图片上传必须设置图片存放路径 + 'location' => public_path() . '/uploads/images/avatars/', + ], + 'roles' => [ + 'title' => '用户角色', + + // 指定数据的类型为关联模型 + 'type' => 'relationship', + + // 关联模型的字段,用来做关联显示 + 'name_field' => 'name', + ], + ], + + // 『数据过滤』设置 + 'filters' => [ + 'id' => [ + + // 过滤表单条目显示名称 + 'title' => '用户 ID', + ], + 'name' => [ + 'title' => '用户名', + ], + 'email' => [ + 'title' => '邮箱', + ], + ], +];