合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
~~~ <?php namespace App\Models\Auth; use App\Traits\SerializeListTime; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use App\Services\AdminUsersService; use Tymon\JWTAuth\Contracts\JWTSubject; /** * AdminUsersModel 管理员表 */ class AdminUsers extends Authenticatable implements JWTSubject { use Notifiable, SerializeListTime; /** * 开启软删除 */ use SoftDeletes; /** * 数据表名称 * @var string */ protected $table = "admin_users"; /** * 验证的字段 * * @var string[] */ protected $fillable = [ 'name', 'sex', 'person_sign', 'username', 'email', 'type', 'status', 'password', 'password_modify' ]; /** * 查询到的数据中需要被隐藏的字段 * * @var string[] */ protected $hidden = ['password']; /** * 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 []; } /** * @return AdminUsers */ public static function getInstance(): AdminUsers { static $models = []; $class = get_called_class(); if (!isset($models[$class])) { $models[$class] = new $class(); } return $models[$class]; } /** * 触发器(添加) 使用触发器在插入用户的时候为密码加密 */ protected static function boot() { parent::boot(); // 在创建用户前,为用户的密码进行加密 static::creating(function ($model) { $model->password = AdminUsersService::getPassword($model->password); }); // 在更新用户前,为用户的密码进行加密 static::updating(function ($model) { // 判断密码是否改变 if ($model->isDirty('password')) { $model->password = AdminUsersService::getPassword($model->password); } }); } /** * 用户所属的角色组 * * @return HasManyThrough */ public function roleGroups(): HasManyThrough { return $this->hasManyThrough( AdminRoleGroup::class, AdminUserHasRoleGroup::class, 'admin_id', 'id', 'id', 'group_id' ); } /** * 用户拥有的角色 * * @return HasManyThrough */ public function roles(): HasManyThrough { return $this->hasManyThrough( AdminRole::class, AdminUserHasRole::class, 'admin_id', 'id', 'id', 'role_id' ); } /** * 关联角色表(多对多) * * @return BelongsToMany */ public function relationRoles(): BelongsToMany { return $this->belongsToMany(AdminRole::class, (new AdminRoleMenu())->getTable()); } /** * 搜索查询 * * @param Builder $builder * @param array $selectFields 查询字段数组 * @param array $orderBy 排序字段数组 * @param int $pageSize 每页数量 * @return array */ public function scopeSearch( Builder $builder, array $selectFields, array $orderBy, int $pageSize ): array { // 1:字段查询 $builder->select($selectFields)->with(['relationRoles' => function ($query) { return $query->select(['id', 'name']); }]); // 2:排序查询 if (!empty($orderBy)) { $orderBy['order'] = $orderBy['order'] == 'ascending' ? 'asc' : 'desc'; } else { $orderBy = ["prop" => "id", "order" => "desc"]; } $builder->orderBy($orderBy['prop'], $orderBy['order']); // 3:每页大小查询 $pageSize = $pageSize ?: config('admin.defaultPageSize'); // 4:分页查询 $data = $builder->paginate($pageSize); // 获取列表 return $data ? $data->toArray() : []; } } ~~~