Skip to content

Commit

Permalink
用户管理后台
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Mar 7, 2022
1 parent ba3044c commit 0d21c13
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
25 changes: 25 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
}
109 changes: 109 additions & 0 deletions config/administrator/users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

use App\Models\User;
use Illuminate\Support\Facades\Auth;

return [
// 页面标题
'title' => '用户',

// 模型单数,用作页面『新建 $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' : '<img src="'.$avatar.'" width="40">';
},

// 是否允许排序
'sortable' => false,
],

'name' => [
'title' => '用户名',
'sortable' => false,
'output' => function ($name, $model) {
return '<a href="/users/'.$model->id.'" target=_blank>'.$name.'</a>';
},
],

'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' => '邮箱',
],
],
];

0 comments on commit 0d21c13

Please sign in to comment.