User.php
5.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 模型层:管理端用户 模型类
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Models\Admin
* @package App\Models\Admin
* @author Richer <yangzi1028@163.com>
* @date 2021年6月16日10:41:08
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Models\Admin;
use App\Models\User\User as ClientUser;
use App\Models\Area\Group;
use App\Models\Area\Village;
use App\Models\Traits\UserTrait;
use App\Models\User\Area as UserArea;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Arr;
use Tymon\JWTAuth\Contracts\JWTSubject;
/**
* Class User
*
* @category App\Models\Admin
* @package App\Models\Admin
* @author Richer <yangzi1028@163.com>
* @date 2021年6月16日10:41:08
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class User extends Authenticatable implements JWTSubject
{
// use UserTrait;
use SoftDeletes;
// 指定数据库表
const TABLE = 'admin_users';
protected $table = self::TABLE;
// 指定对象显示名称:方便系统统一查询和做其他处理
const OBJ_NAME = 'admin';
const OBJ_NAME_ZH = '管理用户';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'id','username', 'password','name','avatar','mobile','remember_token', 'status','role', 'teacher_id', 'user_id'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* 访问器被附加到模型数组的形式。
*
* @var array
*/
protected $appends = ['status_show', 'role'];
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return ['role' => 'admin'];
}
/**
* A user has and belongs to many roles.
*
* @return BelongsToMany
*/
public function roles(): BelongsToMany
{
$pivotTable = config('admin.database.role_users_table');
$relatedModel = config('admin.database.roles_model');
return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'role_id');
}
// /**
// * @return BelongsTo
// */
// public function role(): BelongsTo
// {
// $pivotTable = config('admin.database.role_users_table');
//
// return $this->belongsTo(Role::class, 'user_id', 'role_id', $pivotTable);
// }
/**
* @return BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(ClientUser::class);
}
/**
*
* @return HasMany
*/
public function areas(): hasMany
{
return $this->hasMany(UserArea::class);
}
/**
* A user has and belongs to many roles.
*
* @return BelongsToMany
*/
public function villages(): BelongsToMany
{
return $this->belongsToMany(Village::class, UserArea::class, 'user_id', 'area_id')
->where('area_type', Village::OBJ_NAME);
}
/**
* A user has and belongs to many roles.
*
* @return BelongsToMany
*/
public function groups(): BelongsToMany
{
return $this->belongsToMany(Group::class, UserArea::class, 'user_id', 'area_id')
->where('area_type', Group::OBJ_NAME);
}
/**
* 获取角色
*
* @return mixed
*/
public function getRoleAttribute()
{
// 只获取最新的角色
$role = $this->roles()->first();
return optional($role)->slug;
return $this->roles()->get();
}
/**
* 获取角色
*
* @return mixed
*/
public function getRoleNameAttribute()
{
// 只获取最新的角色
$role = $this->roles()->first();
return optional($role)->name;
}
/**
* 获取状态的中文显示
*
* @return mixed
*/
public function getStatusShowAttribute()
{
return Arr::get(\App\Models\User\User::STATUS_OPTIONS, $this->status ? : \App\Models\User\User::ENABLE);
}
}