|
|
@@ -28,7 +28,6 @@ class AgentDomainPoolService
|
|
|
$limit = min(100, max(1, (int) ($params['limit'] ?? 20)));
|
|
|
$query = AgentDomainPool::query();
|
|
|
if (!empty($params['keyword'])) $query->where('domain', 'like', '%' . trim((string) $params['keyword']) . '%');
|
|
|
- if (isset($params['type']) && $params['type'] !== null) $query->where('type', (string) $params['type']);
|
|
|
if (isset($params['status']) && $params['status'] !== '') $query->where('status', (int) $params['status']);
|
|
|
$bound = fn ($q) => $q->select(DB::raw(1))->from('agent_domains')->whereColumn('agent_domains.host', 'agent_domain_pool.host');
|
|
|
if (($params['usage'] ?? '') === 'in_use') $query->whereExists($bound);
|
|
|
@@ -47,26 +46,24 @@ class AgentDomainPoolService
|
|
|
return compact('total', 'page', 'limit', 'list') + ['summary' => $this->summary()];
|
|
|
}
|
|
|
|
|
|
- /** 绑定代理时的下拉:启用、未被使用、类型匹配(通用的两种都可用)。 */
|
|
|
- public function options(?string $type): array
|
|
|
+ /** 绑定代理时的下拉:启用且未被使用的域名。 */
|
|
|
+ public function options(): array
|
|
|
{
|
|
|
$query = AgentDomainPool::query()->where('status', 1)
|
|
|
->whereNotExists(fn ($q) => $q->select(DB::raw(1))->from('agent_domains')->whereColumn('agent_domains.host', 'agent_domain_pool.host'));
|
|
|
- if ($type) $query->whereIn('type', ['', $type]);
|
|
|
- return $query->orderByDesc('id')->limit(500)->get(['id', 'domain', 'type', 'remark'])
|
|
|
- ->map(fn (AgentDomainPool $row) => ['id' => $row->id, 'domain' => $row->domain, 'type' => $row->type,
|
|
|
- 'type_text' => AgentDomainPool::TYPE_TEXT[$row->type] ?? '', 'remark' => $row->remark])->all();
|
|
|
+ return $query->orderByDesc('id')->limit(500)->get(['id', 'domain', 'remark'])
|
|
|
+ ->map(fn (AgentDomainPool $row) => ['id' => $row->id, 'domain' => $row->domain, 'remark' => $row->remark])->all();
|
|
|
}
|
|
|
|
|
|
/** 批量添加:每行一个域名;已存在或重复的跳过并说明原因。 */
|
|
|
- public function create(array $domains, string $type, string $remark, ?int $adminId): array
|
|
|
+ public function create(array $domains, string $remark, ?int $adminId): array
|
|
|
{
|
|
|
$domains = array_values(array_filter(array_map(fn ($d) => trim((string) $d), $domains), fn ($d) => $d !== ''));
|
|
|
if ($domains === []) throw new \InvalidArgumentException('请输入域名');
|
|
|
if (count($domains) > self::MAX_BATCH) throw new \InvalidArgumentException('一次最多添加 ' . self::MAX_BATCH . ' 个域名');
|
|
|
$created = [];
|
|
|
$skipped = [];
|
|
|
- DB::transaction(function () use ($domains, $type, $remark, $adminId, &$created, &$skipped) {
|
|
|
+ DB::transaction(function () use ($domains, $remark, $adminId, &$created, &$skipped) {
|
|
|
$seen = [];
|
|
|
foreach ($domains as $domain) {
|
|
|
try {
|
|
|
@@ -81,7 +78,7 @@ class AgentDomainPoolService
|
|
|
if (AgentDomainPool::query()->where('host', $host)->lockForUpdate()->exists()) {
|
|
|
$skipped[] = ['domain' => $domain, 'reason' => '域名已存在']; continue;
|
|
|
}
|
|
|
- $created[] = AgentDomainPool::query()->create(['domain' => $value, 'host' => $host, 'type' => $type,
|
|
|
+ $created[] = AgentDomainPool::query()->create(['domain' => $value, 'host' => $host, 'type' => '',
|
|
|
'status' => 1, 'remark' => $remark, 'created_by' => $adminId])->id;
|
|
|
}
|
|
|
});
|
|
|
@@ -102,7 +99,7 @@ class AgentDomainPoolService
|
|
|
$row->domain = $value;
|
|
|
$row->host = $host;
|
|
|
}
|
|
|
- foreach (['type', 'remark'] as $field) {
|
|
|
+ foreach (['remark'] as $field) {
|
|
|
if (array_key_exists($field, $data) && $data[$field] !== null) $row->{$field} = (string) $data[$field];
|
|
|
}
|
|
|
if (array_key_exists('status', $data) && $data['status'] !== null) $row->status = (int) $data['status'];
|
|
|
@@ -121,14 +118,14 @@ class AgentDomainPoolService
|
|
|
}
|
|
|
|
|
|
/** 代理列表绑定域名前调用:停用的不能绑定;没登记的自动登记。调用方已在事务内。 */
|
|
|
- public function register(string $domain, string $host, string $slotType, ?int $adminId): void
|
|
|
+ public function register(string $domain, string $host, ?int $adminId): void
|
|
|
{
|
|
|
$row = AgentDomainPool::query()->where('host', $host)->lockForUpdate()->first();
|
|
|
if ($row) {
|
|
|
if ((int) $row->status !== 1) throw new \RuntimeException('该域名已停用,不能绑定');
|
|
|
return;
|
|
|
}
|
|
|
- AgentDomainPool::query()->create(['domain' => $domain, 'host' => $host, 'type' => $slotType, 'status' => 1,
|
|
|
+ AgentDomainPool::query()->create(['domain' => $domain, 'host' => $host, 'type' => '', 'status' => 1,
|
|
|
'remark' => '代理列表绑定时自动登记', 'created_by' => $adminId]);
|
|
|
}
|
|
|
|
|
|
@@ -137,7 +134,7 @@ class AgentDomainPoolService
|
|
|
return AgentDomain::query()->where('host', $host)->exists();
|
|
|
}
|
|
|
|
|
|
- /** host => [绑定信息…](同一代理的 PC 和 H5 可能绑定同一主机名)。 */
|
|
|
+ /** host => [绑定信息](一个主机名只属于一个代理,数组里最多一项)。 */
|
|
|
private function bindings(array $hosts): array
|
|
|
{
|
|
|
if ($hosts === []) return [];
|
|
|
@@ -156,8 +153,6 @@ class AgentDomainPoolService
|
|
|
'agent_type' => $agent ? (int) $agent->type : null,
|
|
|
'agent_type_text' => $agent?->type_text ?? '',
|
|
|
'agent_status' => $agent ? (int) $agent->status : null,
|
|
|
- 'slot' => $row->type,
|
|
|
- 'slot_text' => AgentDomainPool::TYPE_TEXT[$row->type] ?? $row->type,
|
|
|
'bound_domain' => $row->domain,
|
|
|
'bound_at' => $row->created_at ? (string) $row->created_at : null,
|
|
|
];
|
|
|
@@ -170,7 +165,6 @@ class AgentDomainPoolService
|
|
|
$inUse = $bindings !== [];
|
|
|
return [
|
|
|
'id' => (int) $row->id, 'domain' => $row->domain, 'host' => $row->host,
|
|
|
- 'type' => $row->type, 'type_text' => AgentDomainPool::TYPE_TEXT[$row->type] ?? '',
|
|
|
'status' => (int) $row->status, 'status_text' => (int) $row->status === 1 ? '启用' : '停用',
|
|
|
'remark' => $row->remark, 'in_use' => $inUse, 'usage_text' => $inUse ? '使用中' : ((int) $row->status === 1 ? '可用' : '停用'),
|
|
|
'bindings' => $bindings, 'created_by' => $row->created_by,
|