AdminUser.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace App\Models;
  3. use App\Traits\ModelHelpers;
  4. use App\Utils\Admin;
  5. use App\Utils\HasPermissions;
  6. use Illuminate\Database\Eloquent\Builder;
  7. use Illuminate\Foundation\Auth\User as Authenticatable;
  8. use Illuminate\Notifications\Notifiable;
  9. use Tymon\JWTAuth\Contracts\JWTSubject;
  10. class AdminUser extends Authenticatable implements JWTSubject
  11. {
  12. use HasPermissions;
  13. use Notifiable;
  14. use ModelHelpers;
  15. protected $fillable = ['account', 'password','pid','job_number','status', 'name', 'avatar','phone','admin_id','area_id','type'];
  16. protected $hidden = ['password'];
  17. const TYPE_WORKER = 2;
  18. const TYPE_ADMIN = 1;
  19. public $typeMaps = [
  20. self::TYPE_ADMIN => "admin",
  21. self::TYPE_WORKER => "worker",
  22. ];
  23. const TYPE_STATUS_OK = 1;
  24. const TYPE_STATUS_NO = 0;
  25. public $statusMaps = [
  26. self::TYPE_STATUS_NO => '禁用',
  27. self::TYPE_STATUS_OK => '正常',
  28. ];
  29. public function getJWTIdentifier()
  30. {
  31. return $this->getKey();
  32. }
  33. /**
  34. * 额外在 JWT 载荷中增加的自定义内容
  35. *
  36. * @return array
  37. */
  38. public function getJWTCustomClaims()
  39. {
  40. return [];
  41. }
  42. public function area(){
  43. // 此处为地勤人员关联区域
  44. return $this->belongsTo(Area::class,'area_id','id');
  45. }
  46. public function areas(){
  47. // 此处为管理员关联区域
  48. return $this->belongsToMany(Area::class,'admin_area','admin_id','area_id');
  49. }
  50. public function roles()
  51. {
  52. return $this->belongsToMany(
  53. AdminRole::class,
  54. 'admin_user_role',
  55. 'user_id',
  56. 'role_id'
  57. );
  58. }
  59. public function permissions()
  60. {
  61. return $this->belongsToMany(
  62. AdminPermission::class,
  63. 'admin_user_permission',
  64. 'user_id',
  65. 'permission_id'
  66. );
  67. }
  68. /**
  69. * 从请求数据中添加用户
  70. *
  71. * @param array $inputs
  72. * @param bool $hashedPassword 传入的密码, 是否是没有哈希处理的明文密码
  73. *
  74. * @return AdminUser|\Illuminate\Database\Eloquent\Model
  75. */
  76. public static function createUser($inputs, $hashedPassword = false)
  77. {
  78. if (!$hashedPassword) {
  79. $inputs['password'] = bcrypt($inputs['password']);
  80. }
  81. return static::create($inputs);
  82. }
  83. /**
  84. * 从请求数据中, 更新一条记录
  85. *
  86. * @param array $inputs
  87. * @param bool $hashedPassword 传入的密码, 是否是没有哈希处理的明文密码
  88. *
  89. * @return bool
  90. */
  91. public function updateUser($inputs, $hashedPassword = false)
  92. {
  93. // 更新时, 填了密码, 且没有经过哈希处理
  94. if (
  95. isset($inputs['password']) &&
  96. !$hashedPassword
  97. ) {
  98. $inputs['password'] = bcrypt($inputs['password']);
  99. }
  100. return $this->update($inputs);
  101. }
  102. /**
  103. * getAreaIdsByAdminId 根据管理员id 查询拥有哪些区域
  104. *
  105. * @param $admin_id
  106. * @return array
  107. * @author Fx
  108. *
  109. */
  110. public static function getAreaIdsByAdminId($admin_id){
  111. if (Admin::isAdministrator() || Admin::isNormalAdministrator()) {
  112. $area_ids = Area::query()->pluck('id')->toArray();
  113. return $area_ids;
  114. }
  115. $area_ids1 = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray(); // 管理员拥有那些区域权限ids
  116. if (in_array('99999', $area_ids1)) {
  117. $area_ids = Area::query()->pluck('id')->toArray();
  118. return $area_ids;
  119. }
  120. $area_ids2 = Area::query()->where('admin_id', $admin_id)->pluck('id')->toArray(); // 区域创建者ids
  121. $area_ids = array_merge($area_ids1, $area_ids2);
  122. $area_ids_no = Area::query()->where('status', Area::STATUS_PAUSE)->pluck('id')->toArray(); // 禁用的区域ids
  123. $area_ids = array_diff($area_ids, $area_ids_no);
  124. return $area_ids;
  125. }
  126. /**
  127. * 获取所有区域管理员
  128. * */
  129. public static function getAreaAdmin(){
  130. return self::query()->where('type',self::TYPE_ADMIN)->whereHas('roles',function($q){
  131. $q->where('slug',AdminRole::AREA_ADMIN);
  132. })->get();
  133. }
  134. /**
  135. * 获取所有超级管理员
  136. * */
  137. public static function getAAdmin(){
  138. return self::query()->where('type',self::TYPE_ADMIN)->whereHas('roles',function($q){
  139. $q->where('slug',AdminRole::ADMINISTROTOR);
  140. })->get();
  141. }
  142. }