Admin.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace App\Repositories\Models\Base;
  3. use App\Http\Middleware\SingleLoginLimit;
  4. use App\Repositories\Enums\ModelStatusEnum;
  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. parent::booted(); // TODO: Change the autogenerated stub
  87. self::updated(function (Admin $model) {
  88. if ($model->isDirty('status')) {
  89. //退出登录
  90. if ($model->status == ModelStatusEnum::PAUSE) {
  91. SingleLoginLimit::delToken('admins', $model->id);
  92. }
  93. }
  94. });
  95. self::deleted(function (Admin $model) {
  96. SingleLoginLimit::delToken('admins', $model->id);
  97. });
  98. }
  99. public function department()
  100. {
  101. return $this->belongsTo(Department::class)->select(['id', 'name'])->withDefault([
  102. 'id' => 0,
  103. 'name' => '未知',
  104. ]);
  105. }
  106. public function company()
  107. {
  108. return $this->belongsTo(Department::class)->select(['id', 'name'])->withDefault([
  109. 'id' => 0,
  110. 'name' => '未知',
  111. ]);
  112. }
  113. public function job()
  114. {
  115. return $this->belongsTo(Job::class)->select(['id', 'name'])->withDefault([
  116. 'id' => 0,
  117. 'name' => '未知',
  118. ]);
  119. }
  120. /**
  121. * 检查用户名是否存在
  122. * @param $username
  123. * @param $ignore_id
  124. * @return bool
  125. */
  126. public static function checkUsernameIsUnique($username, $ignore_id = 0)
  127. {
  128. return self::query()->where('username', $username)->where('id', '<>', $ignore_id)->exists();
  129. }
  130. /**
  131. * 用户ID转shopId[门店]
  132. * @param $admin_id
  133. * @return
  134. */
  135. public static function byIdGetRoleDataPermission($id)
  136. {
  137. $admin = self::query()->where('id', $id)->first();
  138. $role = $admin->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 function hasMenuBtn($mid, $admin = false)
  150. {
  151. if (!$admin) $admin = login_admin();
  152. if ($admin->isSuperAdmin()) return true;
  153. $roles = $admin->roles()->pluck('id');
  154. return DB::table('base_roles_menus')->whereIn('role_id', $roles)->where('menu_id', $mid)->exists();
  155. }
  156. }