*/ protected $fillable = [ 'room_name', 'room_auto', 'room_owner', 'room_des', 'room_top', 'room_title', 'room_keep', 'room_time', 'room_tt', 'room_html', 'room_exp', 'build_time', 'permit_level', 'door_open', 'announcement', ]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'room_time' => 'datetime', 'build_time' => 'datetime', 'room_keep' => 'boolean', 'room_tt' => 'boolean', 'room_html' => 'boolean', 'door_open' => 'boolean', ]; } /** * 兼容新版视图访问器:返回房间名称。 */ public function getNameAttribute(): string { return $this->room_name ?? ''; } /** * 兼容新版视图访问器:返回房间介绍。 */ public function getDescriptionAttribute(): string { return $this->room_des ?? ''; } /** * 兼容新版视图访问器:返回房主用户名。 */ public function getMasterAttribute(): string { return $this->room_owner ?? ''; } /** * 兼容新版视图访问器:判断是否系统房间。 */ public function getIsSystemAttribute(): bool { return (bool) $this->room_keep; } /** * 关联房间房主用户。 */ public function masterUser(): BelongsTo { return $this->belongsTo(User::class, 'room_owner', 'username'); } /** * 判断指定用户是否允许进入当前房间。 */ public function canUserEnter(User $user): bool { if ($this->userCanBypassEntryRestrictions($user)) { return true; } if (! $this->door_open) { return false; } return $user->user_level >= (int) ($this->permit_level ?? 0); } /** * 返回用户被拒绝进入房间时的中文提示。 */ public function entryDeniedMessage(User $user): string { if (! $this->door_open && ! $this->userCanBypassEntryRestrictions($user)) { return '该房间当前已关闭,暂不允许进入。'; } return '您的等级不足,暂时无法进入该房间。'; } /** * 判断用户是否可绕过房间开放状态与等级限制。 */ private function userCanBypassEntryRestrictions(User $user): bool { $superLevel = (int) Sysparam::getValue('superlevel', '100'); return $user->id === 1 || $user->username === $this->master || $user->user_level >= $superLevel; } }