Admin.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\Area\Projects;
  12. use App\Repositories\Models\Model;
  13. use Database\Factories\UserFactory;
  14. use Illuminate\Auth\Authenticatable;
  15. use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
  16. use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  17. use Illuminate\Database\Eloquent\Factories\HasFactory;
  18. use Laravel\Lumen\Auth\Authorizable;
  19. use Spatie\Permission\Traits\HasRoles;
  20. use Tymon\JWTAuth\Contracts\JWTSubject;
  21. class Admin extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
  22. {
  23. protected $table = 'base_admins';
  24. use Authenticatable, Authorizable, HasFactory, HasRoles;
  25. /**
  26. * The attributes that are mass assignable.
  27. *
  28. * @var array
  29. */
  30. // protected $fillable = [
  31. // 'name', 'avatar', 'username'
  32. // ];
  33. protected $guarded = [];
  34. /**
  35. * The attributes excluded from the model's JSON form.
  36. *
  37. * @var array
  38. */
  39. protected $hidden = [
  40. 'password',
  41. ];
  42. protected static function booted()
  43. {
  44. self::language();
  45. }
  46. /**
  47. * 兼容 Laravel 8 的 Factory.
  48. *
  49. * @return UserFactory
  50. */
  51. protected static function newFactory()
  52. {
  53. return UserFactory::new();
  54. }
  55. /**
  56. * Get the identifier that will be stored in the subject claim of the JWT.
  57. *
  58. * @return mixed
  59. */
  60. public function getJWTIdentifier()
  61. {
  62. return $this->getKey();
  63. }
  64. /**
  65. * Return a key value array, containing any custom claims to be added to the JWT.
  66. *
  67. * @return array
  68. */
  69. public function getJWTCustomClaims()
  70. {
  71. return ['admin'];
  72. }
  73. public function role()
  74. {
  75. return $this->belongsTo(Role::class);
  76. }
  77. public function department()
  78. {
  79. return $this->belongsTo(Department::class);
  80. }
  81. public function getHeadimgAttribute($val)
  82. {
  83. if (is_null($val)) {
  84. return path_to_url('default/headImg.jpeg');
  85. }
  86. return path_to_url($val);
  87. }
  88. public function shop()
  89. {
  90. return $this->belongsTo(Shop::class)->select(['id', 'name']);
  91. }
  92. public function setProjectIdsAttribute($val)
  93. {
  94. // if (count($val) == 0) return '';
  95. // $this->attributes['project_ids'] = '-' . arr2str0($val, '-') . '-';
  96. $this->attributes['project_ids'] = '-' . $val . '-';
  97. }
  98. public function getProjectIdsAttribute($val)
  99. {
  100. return str2arr0(trim($val, '-'), '-');
  101. }
  102. public function getProjectsAttribute()
  103. {
  104. $ids = str2arr0(trim($this->attributes['project_ids'], '-'), '-');
  105. if (in_array(0, $ids)) {
  106. return ['全部'];
  107. }
  108. return Projects::query()->whereIn('id', $ids)->select(['id', 'name'])->get();
  109. }
  110. }