"admin", self::TYPE_WORKER => "worker", ]; const LOGIN_APP_OK = 1; const LOGIN_APP_NO = 0; public $loginAppMaps = [ self::LOGIN_APP_NO => '不能登录运维端', self::LOGIN_APP_OK => '能登录运维端', ]; const TYPE_STATUS_OK = 1; const TYPE_STATUS_NO = 0; public $statusMaps = [ self::TYPE_STATUS_NO => '禁用', self::TYPE_STATUS_OK => '正常', ]; public function getJWTIdentifier() { return $this->getKey(); } /** * 额外在 JWT 载荷中增加的自定义内容 * * @return array */ public function getJWTCustomClaims() { return []; } public function area(){ // 此处为地勤人员关联区域 return $this->belongsTo(Area::class,'area_id','id'); } public function areas(){ // 此处为管理员关联区域 return $this->belongsToMany(Area::class,'admin_area','admin_id','area_id'); } public function roles() { return $this->belongsToMany( AdminRole::class, 'admin_user_role', 'user_id', 'role_id' ); } public function permissions() { return $this->belongsToMany( AdminPermission::class, 'admin_user_permission', 'user_id', 'permission_id' ); } /** * 从请求数据中添加用户 * * @param array $inputs * @param bool $hashedPassword 传入的密码, 是否是没有哈希处理的明文密码 * * @return AdminUser|\Illuminate\Database\Eloquent\Model */ public static function createUser($inputs, $hashedPassword = false) { if (!$hashedPassword) { $inputs['password'] = bcrypt($inputs['password']); } return static::create($inputs); } /** * 从请求数据中, 更新一条记录 * * @param array $inputs * @param bool $hashedPassword 传入的密码, 是否是没有哈希处理的明文密码 * * @return bool */ public function updateUser($inputs, $hashedPassword = false) { // 更新时, 填了密码, 且没有经过哈希处理 if ( isset($inputs['password']) && !$hashedPassword ) { $inputs['password'] = bcrypt($inputs['password']); } return $this->update($inputs); } public function getPersIds(){ return $this->allPermissions()->pluck('id'); } /** * getAreaIdsByAdminId 根据管理员id 查询拥有哪些区域 * * @param $admin_id * @return array * @author Fx * */ public static function getAreaIdsByAdminId($admin_id){ if (Admin::isAdministrator() || Admin::user()->isRole(AdminRole::MERCHANTSADMIN) || Admin::isNormalAdministrator()) { return Area::query()->where(AdminMerchant::getMerchantWhere())->pluck('id')->toArray(); } $area_ids1 = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray(); // 管理员拥有那些区域权限ids if (in_array('99999', $area_ids1)) { $area_ids = Area::query()->where(AdminMerchant::getMerchantWhere())->pluck('id')->toArray(); return $area_ids; } $area_ids2 = Area::query()->where(AdminMerchant::getMerchantWhere())->where('admin_id', $admin_id)->pluck('id')->toArray(); // 区域创建者ids $area_ids = array_merge($area_ids1, $area_ids2); $area_ids_no = Area::query()->where(AdminMerchant::getMerchantWhere())->where('status', Area::STATUS_PAUSE)->pluck('id')->toArray(); // 禁用的区域ids $area_ids = array_diff($area_ids, $area_ids_no); return $area_ids; } // public static function getAreaIdsByAdminId($admin_id){ // if (Admin::isAdministrator() || Admin::isNormalAdministrator()) { // $area_ids = Area::query()->pluck('id')->toArray(); // return $area_ids; // } // $area_ids1 = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray(); // 管理员拥有那些区域权限ids // if (in_array('99999', $area_ids1)) { // $area_ids = Area::query()->pluck('id')->toArray(); // return $area_ids; // } // $area_ids2 = Area::query()->where('admin_id', $admin_id)->pluck('id')->toArray(); // 区域创建者ids // $area_ids = array_merge($area_ids1, $area_ids2); // $area_ids_no = Area::query()->where('status', Area::STATUS_PAUSE)->pluck('id')->toArray(); // 禁用的区域ids // $area_ids = array_diff($area_ids, $area_ids_no); // return $area_ids; // // } /** * 获取区域ids saas * @param $admin_id * @return array */ public static function getAreaIdsByAdminIdMch($admin_id){ if (Admin::isAdministrator() || Admin::user()->isRole(AdminRole::MERCHANTSADMIN) || Admin::isNormalAdministrator()) { return Area::query()->where(AdminMerchant::getMerchantWhere())->pluck('id')->toArray(); } $area_ids1 = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray(); // 管理员拥有那些区域权限ids if (in_array('99999', $area_ids1)) { $area_ids = Area::query()->where(AdminMerchant::getMerchantWhere())->pluck('id')->toArray(); return $area_ids; } $area_ids2 = Area::query()->where(AdminMerchant::getMerchantWhere())->where('admin_id', $admin_id)->pluck('id')->toArray(); // 区域创建者ids $area_ids = array_merge($area_ids1, $area_ids2); $area_ids_no = Area::query()->where(AdminMerchant::getMerchantWhere())->where('status', Area::STATUS_PAUSE)->pluck('id')->toArray(); // 禁用的区域ids $area_ids = array_diff($area_ids, $area_ids_no); return $area_ids; } /** * 获取所有区域管理员 * */ public static function getAreaAdmin(){ return self::query()->where('type',self::TYPE_ADMIN)->whereHas('roles',function($q){ $q->where('slug',AdminRole::AREA_ADMIN); })->get(); } /** * 获取所有超级管理员 * */ public static function getAAdmin(){ return self::query()->where('type',self::TYPE_ADMIN)->whereHas('roles',function($q){ $q->where('slug',AdminRole::ADMINISTROTOR); })->get(); } }