Files
coruna-lab/server/app/Models/Channel.php
T
2026-08-07 05:21:44 +08:00

91 lines
2.0 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Channel extends Model
{
public const OFFICIAL_USER_ID = 0;
protected $fillable = [
'channel_id', 'user_id', 'domains', 'status', 'remark',
];
protected function casts(): array
{
return [
'domains' => 'array',
'status' => 'integer',
'user_id' => 'integer',
];
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
public function isEnabled(): bool
{
return (int) $this->status === 1;
}
public function isOfficial(): bool
{
return (int) $this->user_id === self::OFFICIAL_USER_ID;
}
public function agentLabel(): string
{
if ($this->isOfficial()) {
return '官方';
}
return $this->user?->username ?: ('#'.$this->user_id);
}
/**
* @return list<string>
*/
public function resolvedDomains(): array
{
// Domains come from env only (CORUNA_CHANNEL_DOMAINS).
$domains = config('coruna.channel_domains', []);
return array_values(array_filter(array_map(static function ($d) {
return is_string($d) ? trim($d) : '';
}, is_array($domains) ? $domains : [])));
}
/**
* @return list<string>
*/
public function supportLinks(): array
{
$links = [];
foreach ($this->resolvedDomains() as $domain) {
$host = preg_replace('#^https?://#i', '', rtrim($domain, '/'));
if ($host === '') {
continue;
}
$links[] = 'https://'.$host.'/web/'.$this->channel_id.'/support.html';
}
return $links;
}
public static function randomChannelId(): string
{
return bin2hex(random_bytes(16));
}
public static function maxPerAgent(): int
{
$n = (int) config('coruna.channels.max_per_agent', 5);
return max(1, $n);
}
}