* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace App\Repositories\Models\Base; use App\Repositories\Models\Model; use Database\Factories\UserFactory; use Illuminate\Auth\Authenticatable; use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Support\Facades\DB; use Laravel\Lumen\Auth\Authorizable; use Spatie\Permission\Traits\HasRoles; use Tymon\JWTAuth\Contracts\JWTSubject; class Admin extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject { protected $table = 'base_admins'; use Authenticatable, Authorizable, HasFactory, HasRoles; /** * The attributes that are mass assignable. * * @var array */ protected $guarded = []; /** * The attributes excluded from the model's JSON form. * * @var array */ protected $hidden = [ 'password', ]; protected static function booted() { // self::language(); } /** * 兼容 Laravel 8 的 Factory. * * @return UserFactory */ protected static function newFactory() { return UserFactory::new(); } /** * 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']; } // public function role() // { // return $this->belongsTo(Role::class); // } public function getRolesNameAttribute() { // return $this->morphToMany( // config('permission.models.role'), // 'model', // config('permission.table_names.model_has_roles'), // config('permission.column_names.model_morph_key'), // 'role_id' // )->select(['id', 'name', 'nickname']); return DB::table(config('permission.table_names.roles'))->select(['id', 'name', 'nickname'])->whereIn('id', DB::table(config('permission.table_names.model_has_roles'))->where('model_id', $this->attributes['id'])->pluck('role_id') )->get(); } public function department() { return $this->belongsTo(Department::class)->select(['id', 'name'])->withDefault(['id' => 0, 'name' => '--']); } public function getHeadimgAttribute($val) { if (empty($val)) { return path_to_url('default/headImg.jpeg'); } return path_to_url($val); } // public function shop() // { // return $this->belongsTo(Shop::class)->select(['id', 'name']); // } // public function setProjectIdsAttribute($val) // { // $this->attributes['project_ids'] = '-' . $val . '-'; // } // // public function getProjectIdsAttribute($val) // { // return str2arr0(trim($val, '-'), '-'); // } // // public function getProjectsAttribute() // { // $ids = str2arr0(trim($this->attributes['project_ids'], '-'), '-'); // // if (in_array(0, $ids)) { // return ['全部']; // } // return Projects::query()->whereIn('id', $ids)->select(['id', 'name'])->get(); // } /** * 根据学生id或者老师id获取id * @param $type_id * @param $type * @return mixed * Author: Mead */ public static function byTypeIdGetId($type_id, $type) { return self::query()->where('type_id', $type_id)->where('type', $type)->value('id') ?? 0; } public static function byIdGetTypeId($id, $type) { return self::query()->where('id', $id)->where('type', $type)->value('type_id') ?? 0; } /** * 是否为超级管理员 * @return bool */ public function isSuperAdmin() { return in_array($this->attributes['id'], config('site.superAdmin_ids', [])); } }