Files

143 lines
3.2 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* 文件功能:聊天房间模型
*
* 对应原 ASP 文件:room 表
*
* @author ChatRoom Laravel
*
* @version 1.0.0
*/
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
2026-04-19 14:42:52 +08:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2026-04-19 14:42:52 +08:00
/**
* 类功能:承载聊天室房间资料与准入规则。
*/
class Room extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
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<string, string>
*/
protected function casts(): array
{
return [
'room_time' => 'datetime',
'build_time' => 'datetime',
'room_keep' => 'boolean',
'room_tt' => 'boolean',
'room_html' => 'boolean',
'door_open' => 'boolean',
];
}
2026-04-19 14:42:52 +08:00
/**
* 兼容新版视图访问器:返回房间名称。
*/
public function getNameAttribute(): string
{
return $this->room_name ?? '';
}
2026-04-19 14:42:52 +08:00
/**
* 兼容新版视图访问器:返回房间介绍。
*/
public function getDescriptionAttribute(): string
{
return $this->room_des ?? '';
}
2026-04-19 14:42:52 +08:00
/**
* 兼容新版视图访问器:返回房主用户名。
*/
public function getMasterAttribute(): string
{
return $this->room_owner ?? '';
}
2026-04-19 14:42:52 +08:00
/**
* 兼容新版视图访问器:判断是否系统房间。
*/
public function getIsSystemAttribute(): bool
{
return (bool) $this->room_keep;
}
2026-04-19 14:42:52 +08:00
/**
* 关联房间房主用户。
*/
public function masterUser(): BelongsTo
{
return $this->belongsTo(User::class, 'room_owner', 'username');
}
2026-04-19 14:42:52 +08:00
/**
* 判断指定用户是否允许进入当前房间。
*/
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;
}
}