Admin.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Repositories\Models\Base;
  11. use App\Repositories\Models\Model;
  12. use Database\Factories\UserFactory;
  13. use Illuminate\Auth\Authenticatable;
  14. use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
  15. use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  16. use Illuminate\Database\Eloquent\Builder;
  17. use Illuminate\Database\Eloquent\Factories\HasFactory;
  18. use Illuminate\Support\Facades\App;
  19. use Illuminate\Support\Facades\Hash;
  20. use Laravel\Lumen\Auth\Authorizable;
  21. use Spatie\Permission\Traits\HasRoles;
  22. use Tymon\JWTAuth\Contracts\JWTSubject;
  23. class Admin extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
  24. {
  25. protected $table = 'base_admins';
  26. use Authenticatable, Authorizable, HasFactory, HasRoles;
  27. /**
  28. * The attributes that are mass assignable.
  29. *
  30. * @var array
  31. */
  32. // protected $fillable = [
  33. // 'name', 'avatar', 'username'
  34. // ];
  35. protected $guarded = [];
  36. /**
  37. * The attributes excluded from the model's JSON form.
  38. *
  39. * @var array
  40. */
  41. protected $hidden = [
  42. 'password',
  43. ];
  44. protected static function booted()
  45. {
  46. self::language();
  47. }
  48. /**
  49. * 兼容 Laravel 8 的 Factory.
  50. *
  51. * @return UserFactory
  52. */
  53. protected static function newFactory()
  54. {
  55. return UserFactory::new();
  56. }
  57. /**
  58. * Get the identifier that will be stored in the subject claim of the JWT.
  59. *
  60. * @return mixed
  61. */
  62. public function getJWTIdentifier()
  63. {
  64. return $this->getKey();
  65. }
  66. /**
  67. * Return a key value array, containing any custom claims to be added to the JWT.
  68. *
  69. * @return array
  70. */
  71. public function getJWTCustomClaims()
  72. {
  73. // return ['admin'];
  74. return ['role' => 'admin'];
  75. }
  76. public function role()
  77. {
  78. return $this->belongsTo(Role::class);
  79. }
  80. public function department()
  81. {
  82. return $this->belongsTo(Department::class);
  83. }
  84. public function getHeadimgAttribute($val)
  85. {
  86. if (is_null($val)) {
  87. return path_to_url('default/headImg.jpeg');
  88. }
  89. return path_to_url($val);
  90. }
  91. }