AdminUser.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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','is_login_app'];
  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 LOGIN_APP_OK = 1;
  24. const LOGIN_APP_NO = 0;
  25. public $loginAppMaps = [
  26. self::LOGIN_APP_NO => '不能登录运维端',
  27. self::LOGIN_APP_OK => '能登录运维端',
  28. ];
  29. const TYPE_STATUS_OK = 1;
  30. const TYPE_STATUS_NO = 0;
  31. public $statusMaps = [
  32. self::TYPE_STATUS_NO => '禁用',
  33. self::TYPE_STATUS_OK => '正常',
  34. ];
  35. public function getJWTIdentifier()
  36. {
  37. return $this->getKey();
  38. }
  39. /**
  40. * 额外在 JWT 载荷中增加的自定义内容
  41. *
  42. * @return array
  43. */
  44. public function getJWTCustomClaims()
  45. {
  46. return [];
  47. }
  48. public function area(){
  49. // 此处为地勤人员关联区域
  50. return $this->belongsTo(Area::class,'area_id','id');
  51. }
  52. public function areas(){
  53. // 此处为管理员关联区域
  54. return $this->belongsToMany(Area::class,'admin_area','admin_id','area_id');
  55. }
  56. public function roles()
  57. {
  58. return $this->belongsToMany(
  59. AdminRole::class,
  60. 'admin_user_role',
  61. 'user_id',
  62. 'role_id'
  63. );
  64. }
  65. public function permissions()
  66. {
  67. return $this->belongsToMany(
  68. AdminPermission::class,
  69. 'admin_user_permission',
  70. 'user_id',
  71. 'permission_id'
  72. );
  73. }
  74. /**
  75. * 从请求数据中添加用户
  76. *
  77. * @param array $inputs
  78. * @param bool $hashedPassword 传入的密码, 是否是没有哈希处理的明文密码
  79. *
  80. * @return AdminUser|\Illuminate\Database\Eloquent\Model
  81. */
  82. public static function createUser($inputs, $hashedPassword = false)
  83. {
  84. if (!$hashedPassword) {
  85. $inputs['password'] = bcrypt($inputs['password']);
  86. }
  87. return static::create($inputs);
  88. }
  89. /**
  90. * 从请求数据中, 更新一条记录
  91. *
  92. * @param array $inputs
  93. * @param bool $hashedPassword 传入的密码, 是否是没有哈希处理的明文密码
  94. *
  95. * @return bool
  96. */
  97. public function updateUser($inputs, $hashedPassword = false)
  98. {
  99. // 更新时, 填了密码, 且没有经过哈希处理
  100. if (
  101. isset($inputs['password']) &&
  102. !$hashedPassword
  103. ) {
  104. $inputs['password'] = bcrypt($inputs['password']);
  105. }
  106. return $this->update($inputs);
  107. }
  108. public function getPersIds(){
  109. return $this->allPermissions()->pluck('id');
  110. }
  111. /**
  112. * getAreaIdsByAdminId 根据管理员id 查询拥有哪些区域
  113. *
  114. * @param $admin_id
  115. * @return array
  116. * @author Fx
  117. *
  118. */
  119. public static function getAreaIdsByAdminId($admin_id){
  120. if (Admin::isAdministrator() || Admin::user()->isRole(AdminRole::MERCHANTSADMIN) || Admin::isNormalAdministrator()) {
  121. return Area::query()->where(AdminMerchant::getMerchantWhere())->pluck('id')->toArray();
  122. }
  123. $area_ids1 = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray(); // 管理员拥有那些区域权限ids
  124. if (in_array('99999', $area_ids1)) {
  125. $area_ids = Area::query()->where(AdminMerchant::getMerchantWhere())->pluck('id')->toArray();
  126. return $area_ids;
  127. }
  128. $area_ids2 = Area::query()->where(AdminMerchant::getMerchantWhere())->where('admin_id', $admin_id)->pluck('id')->toArray(); // 区域创建者ids
  129. $area_ids = array_merge($area_ids1, $area_ids2);
  130. $area_ids_no = Area::query()->where(AdminMerchant::getMerchantWhere())->where('status', Area::STATUS_PAUSE)->pluck('id')->toArray(); // 禁用的区域ids
  131. $area_ids = array_diff($area_ids, $area_ids_no);
  132. return $area_ids;
  133. }
  134. // public static function getAreaIdsByAdminId($admin_id){
  135. // if (Admin::isAdministrator() || Admin::isNormalAdministrator()) {
  136. // $area_ids = Area::query()->pluck('id')->toArray();
  137. // return $area_ids;
  138. // }
  139. // $area_ids1 = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray(); // 管理员拥有那些区域权限ids
  140. // if (in_array('99999', $area_ids1)) {
  141. // $area_ids = Area::query()->pluck('id')->toArray();
  142. // return $area_ids;
  143. // }
  144. // $area_ids2 = Area::query()->where('admin_id', $admin_id)->pluck('id')->toArray(); // 区域创建者ids
  145. // $area_ids = array_merge($area_ids1, $area_ids2);
  146. // $area_ids_no = Area::query()->where('status', Area::STATUS_PAUSE)->pluck('id')->toArray(); // 禁用的区域ids
  147. // $area_ids = array_diff($area_ids, $area_ids_no);
  148. // return $area_ids;
  149. //
  150. // }
  151. /**
  152. * 获取区域ids saas
  153. * @param $admin_id
  154. * @return array
  155. */
  156. public static function getAreaIdsByAdminIdMch($admin_id){
  157. if (Admin::isAdministrator() || Admin::user()->isRole(AdminRole::MERCHANTSADMIN) || Admin::isNormalAdministrator()) {
  158. return Area::query()->where(AdminMerchant::getMerchantWhere())->pluck('id')->toArray();
  159. }
  160. $area_ids1 = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray(); // 管理员拥有那些区域权限ids
  161. if (in_array('99999', $area_ids1)) {
  162. $area_ids = Area::query()->where(AdminMerchant::getMerchantWhere())->pluck('id')->toArray();
  163. return $area_ids;
  164. }
  165. $area_ids2 = Area::query()->where(AdminMerchant::getMerchantWhere())->where('admin_id', $admin_id)->pluck('id')->toArray(); // 区域创建者ids
  166. $area_ids = array_merge($area_ids1, $area_ids2);
  167. $area_ids_no = Area::query()->where(AdminMerchant::getMerchantWhere())->where('status', Area::STATUS_PAUSE)->pluck('id')->toArray(); // 禁用的区域ids
  168. $area_ids = array_diff($area_ids, $area_ids_no);
  169. return $area_ids;
  170. }
  171. /**
  172. * 获取所有区域管理员
  173. * */
  174. public static function getAreaAdmin(){
  175. return self::query()->where('type',self::TYPE_ADMIN)->whereHas('roles',function($q){
  176. $q->where('slug',AdminRole::AREA_ADMIN);
  177. })->get();
  178. }
  179. /**
  180. * 获取所有超级管理员
  181. * */
  182. public static function getAAdmin(){
  183. return self::query()->where('type',self::TYPE_ADMIN)->whereHas('roles',function($q){
  184. $q->where('slug',AdminRole::ADMINISTROTOR);
  185. })->get();
  186. }
  187. }