*/ use HasFactory, Notifiable, HasApiTokens; protected $table = "system_users"; /** * 可批量赋值的属性 */ protected $fillable = [ 'username', 'password', 'nickname', 'remark', 'dept_id', 'post_ids', 'email', 'mobile', 'sex', 'avatar', 'status', 'login_ip', 'login_date', 'creator', 'create_time', 'updater', 'update_time', 'deleted', 'tenant_id', ]; /** * 隐藏属性 */ protected $hidden = [ 'password', ]; /** * 自定义时间戳字段名 */ const CREATED_AT = 'create_time'; const UPDATED_AT = 'update_time'; /** * 类型转换 */ protected function casts(): array { return [ 'login_date' => 'datetime', 'create_time' => 'datetime', 'update_time' => 'datetime', 'password' => 'hashed', ]; } /** * 用户角色关联 */ public function userRoles() { return $this->hasMany(SystemUserRole::class, 'user_id'); } /** * 用户的角色(多对多) */ public function roles() { return $this->belongsToMany(SystemRole::class, 'system_user_role', 'user_id', 'role_id'); } /** * 获取用户的角色ID列表 */ public function getRoleIds(): array { return $this->userRoles()->pluck('role_id')->toArray(); } /** * 获取用户的角色编码列表 */ public function getRoleCodes(): array { return $this->roles()->pluck('code')->toArray(); } /** * 检查用户是否拥有指定角色 */ public function hasRole(int $roleId): bool { return $this->userRoles()->where('role_id', $roleId)->exists(); } /** * 检查用户是否拥有指定角色编码 */ public function hasRoleCode(string $roleCode): bool { return $this->roles()->where('code', $roleCode)->exists(); } /** * 检查用户是否拥有任一角色 */ public function hasAnyRole(array $roleIds): bool { return $this->userRoles()->whereIn('role_id', $roleIds)->exists(); } /** * 检查用户是否拥有任一角色编码 */ public function hasAnyRoleCode(array $roleCodes): bool { return $this->roles()->whereIn('code', $roleCodes)->exists(); } /** * 检查用户是否拥有所有角色 */ public function hasAllRoles(array $roleIds): bool { $userRoleIds = $this->getRoleIds(); return empty(array_diff($roleIds, $userRoleIds)); } /** * 获取用户权限范围内的租户ID */ public function getCurrentTenantId(): ?int { return $this->tenant_id; } /** * 用户的学校校区关联 */ public function schoolCampuses() { return $this->hasMany(SystemUserSchoolCampus::class, 'userid'); } /** * 用户的班级关联(作为老师) */ public function teacherClasses() { return $this->hasMany(TeacherClass::class, 'teacher_id'); } /** * 获取用户管理的学校ID列表 */ public function getSchoolIds(): array { return $this->schoolCampuses()->pluck('schoolid')->unique()->toArray(); } /** * 获取用户管理的校区ID列表 */ public function getCampusIds(): array { return $this->schoolCampuses()->pluck('campusid')->toArray(); } /** * 获取用户管理的班级ID列表 */ public function getClassIds(): array { return $this->teacherClasses()->pluck('class_id')->toArray(); } /** * 检查用户是否管理指定学校 */ public function managesSchool(int $schoolId): bool { return $this->schoolCampuses()->where('schoolid', $schoolId)->exists(); } /** * 检查用户是否管理指定校区 */ public function managesCampus(int $campusId): bool { return $this->schoolCampuses()->where('campusid', $campusId)->exists(); } /** * 检查用户是否管理指定班级 */ public function managesClass(int $classId): bool { return $this->teacherClasses()->where('class_id', $classId)->exists(); } }