Admin.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\Factories\HasFactory;
  17. use Illuminate\Support\Facades\DB;
  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 $guarded = [];
  31. /**
  32. * The attributes excluded from the model's JSON form.
  33. *
  34. * @var array
  35. */
  36. protected $hidden = [
  37. 'password',
  38. ];
  39. protected static function booted()
  40. {
  41. // self::language();
  42. }
  43. /**
  44. * 兼容 Laravel 8 的 Factory.
  45. *
  46. * @return UserFactory
  47. */
  48. protected static function newFactory()
  49. {
  50. return UserFactory::new();
  51. }
  52. /**
  53. * Get the identifier that will be stored in the subject claim of the JWT.
  54. *
  55. * @return mixed
  56. */
  57. public function getJWTIdentifier()
  58. {
  59. return $this->getKey();
  60. }
  61. /**
  62. * Return a key value array, containing any custom claims to be added to the JWT.
  63. *
  64. * @return array
  65. */
  66. public function getJWTCustomClaims()
  67. {
  68. return ['role' => 'admin'];
  69. }
  70. // public function role()
  71. // {
  72. // return $this->belongsTo(Role::class);
  73. // }
  74. public function getRolesNameAttribute()
  75. {
  76. // return $this->morphToMany(
  77. // config('permission.models.role'),
  78. // 'model',
  79. // config('permission.table_names.model_has_roles'),
  80. // config('permission.column_names.model_morph_key'),
  81. // 'role_id'
  82. // )->select(['id', 'name', 'nickname']);
  83. return DB::table(config('permission.table_names.roles'))->select(['id', 'name', 'nickname'])->whereIn('id',
  84. DB::table(config('permission.table_names.model_has_roles'))->where('model_id', $this->attributes['id'])->pluck('role_id')
  85. )->get();
  86. }
  87. public function department()
  88. {
  89. return $this->belongsTo(Department::class)->select(['id', 'name'])->withDefault(['id' => 0, 'name' => '--']);
  90. }
  91. public function getHeadimgAttribute($val)
  92. {
  93. if (empty($val)) {
  94. return path_to_url('default/headImg.jpeg');
  95. }
  96. return path_to_url($val);
  97. }
  98. // public function shop()
  99. // {
  100. // return $this->belongsTo(Shop::class)->select(['id', 'name']);
  101. // }
  102. // public function setProjectIdsAttribute($val)
  103. // {
  104. // $this->attributes['project_ids'] = '-' . $val . '-';
  105. // }
  106. //
  107. // public function getProjectIdsAttribute($val)
  108. // {
  109. // return str2arr0(trim($val, '-'), '-');
  110. // }
  111. //
  112. // public function getProjectsAttribute()
  113. // {
  114. // $ids = str2arr0(trim($this->attributes['project_ids'], '-'), '-');
  115. //
  116. // if (in_array(0, $ids)) {
  117. // return ['全部'];
  118. // }
  119. // return Projects::query()->whereIn('id', $ids)->select(['id', 'name'])->get();
  120. // }
  121. /**
  122. * 根据学生id或者老师id获取id
  123. * @param $type_id
  124. * @param $type
  125. * @return mixed
  126. * Author: Mead
  127. */
  128. public static function byTypeIdGetId($type_id, $type)
  129. {
  130. return self::query()->where('type_id', $type_id)->where('type', $type)->value('id') ?? 0;
  131. }
  132. public static function byIdGetTypeId($id, $type)
  133. {
  134. return self::query()->where('id', $id)->where('type', $type)->value('type_id') ?? 0;
  135. }
  136. /**
  137. * 是否为超级管理员
  138. * @return bool
  139. */
  140. public function isSuperAdmin()
  141. {
  142. return in_array($this->attributes['id'], config('site.superAdmin_ids', []));
  143. }
  144. }