Admin.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Repositories\Models\Base;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Models\Mentor\Grade;
  5. use App\Repositories\Models\Model;
  6. use Illuminate\Auth\Authenticatable;
  7. use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
  8. use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  9. use Illuminate\Database\Eloquent\Factories\HasFactory;
  10. use Illuminate\Support\Facades\DB;
  11. use Laravel\Lumen\Auth\Authorizable;
  12. use Spatie\Permission\Traits\HasRoles;
  13. use Tymon\JWTAuth\Contracts\JWTSubject;
  14. class Admin extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
  15. {
  16. use Authenticatable, Authorizable, HasFactory, HasRoles;
  17. /**
  18. * @var string
  19. */
  20. protected $table = 'base_admins';
  21. /**
  22. * The attributes that are mass assignable.
  23. *
  24. * @var array
  25. */
  26. protected $fillable = [
  27. 'name',
  28. 'username',
  29. 'mobile',
  30. 'email',
  31. 'headimg',
  32. 'sex',
  33. 'password',
  34. 'department_id',
  35. 'shop_id',
  36. 'company_id',
  37. 'is_view_user_info',
  38. 'job_id',
  39. 'roles_id',
  40. 'type',
  41. 'wechat_auth_id',
  42. 'extra_fields',
  43. 'status',
  44. 'last_login_ip',
  45. 'last_login_time',
  46. 'user_no',
  47. 'grade',
  48. 'class_name',
  49. ];
  50. // protected $guarded = [];
  51. /**
  52. * The attributes excluded from the model's JSON form.
  53. *
  54. * @var array
  55. */
  56. protected $hidden = [
  57. 'password'
  58. ];
  59. protected $casts = [
  60. 'extra_fields' => 'json',
  61. ];
  62. /**
  63. * Get the identifier that will be stored in the subject claim of the JWT.
  64. *
  65. * @return mixed
  66. */
  67. public function getJWTIdentifier()
  68. {
  69. return $this->getKey();
  70. }
  71. /**
  72. * Return a key value array, containing any custom claims to be added to the JWT.
  73. *
  74. * @return array
  75. */
  76. public function getJWTCustomClaims()
  77. {
  78. return ['role' => 'admin'];
  79. }
  80. public function isSuperAdmin()
  81. {
  82. return in_array($this->attributes['id'], config('site.superAdmin_ids', []));
  83. }
  84. protected static function booted()
  85. {
  86. // self::saved(function (Admin $admin) {
  87. // $admin->syncRoles($admin->type);
  88. // });
  89. }
  90. public function department()
  91. {
  92. return $this->belongsTo(Department::class)->select(['id', 'name'])->withDefault([
  93. 'id' => 0,
  94. 'name' => '未知',
  95. ]);
  96. }
  97. public function company()
  98. {
  99. return $this->belongsTo(Department::class)->select(['id', 'name'])->withDefault([
  100. 'id' => 0,
  101. 'name' => '未知',
  102. ]);
  103. }
  104. public function job()
  105. {
  106. return $this->belongsTo(Job::class)->select(['id', 'name'])->withDefault([
  107. 'id' => 0,
  108. 'name' => '未知',
  109. ]);
  110. }
  111. public function shop()
  112. {
  113. return $this->belongsTo(Grade::class, 'shop_id');
  114. }
  115. /**
  116. * 检查用户名是否存在
  117. * @param $username
  118. * @param $ignore_id
  119. * @return bool
  120. */
  121. public static function checkUsernameIsUnique($username, $ignore_id = 0)
  122. {
  123. return self::query()->where('username', $username)->where('id', '<>', $ignore_id)->exists();
  124. }
  125. /**
  126. * 用户ID转shopId[门店]
  127. * @param $admin_id
  128. * @return
  129. */
  130. // public static function id2ShopId($admin_id)
  131. // {
  132. // return Cache::remember("model:Admin:id2ShopId:{$admin_id}", Carbon::now()->addDay(), function () use ($admin_id) {
  133. // return ModelAdmin::byAdminIdGetModelId($admin_id, Shop::class);
  134. // });
  135. // }
  136. // public function byIdGetRoleDataPermission()
  137. // {
  138. // $role = $this->roles()->orderBy('data_permission_type', 'asc')->first();
  139. // if ($role->data_permission_type == Role::DATA_TYPE_CUSTOM) {
  140. // return [
  141. // 'type' => $role->data_permission_type,
  142. // 'ids' => $role->departments->pluck('id')->toArray(),
  143. // ];
  144. // }
  145. // return [
  146. // 'type' => $role->data_permission_type,
  147. // ];
  148. // }
  149. public static function byIdGetRoleDataPermission($id)
  150. {
  151. $admin = self::query()->where('id', $id)->first();
  152. $role = $admin->roles()->orderBy('data_permission_type', 'asc')->first();
  153. if ($role->data_permission_type == Role::DATA_TYPE_CUSTOM) {
  154. return [
  155. 'type' => $role->data_permission_type,
  156. 'ids' => $role->departments->pluck('id')->toArray(),
  157. ];
  158. }
  159. return [
  160. 'type' => $role->data_permission_type,
  161. ];
  162. }
  163. public function hasMenuBtn($mid, $admin = false)
  164. {
  165. if (!$admin) $admin = login_admin();
  166. if ($admin->isSuperAdmin()) return true;
  167. $roles = $admin->roles()->pluck('id');
  168. return DB::table('base_roles_menus')->whereIn('role_id', $roles)->where('menu_id', $mid)->exists();
  169. }
  170. public function records()
  171. {
  172. return $this->hasMany(\App\Repositories\Models\Exam\Record::class, 'admin_id');
  173. }
  174. }