|
|
@@ -8,51 +8,77 @@ use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
/**
|
|
|
* 代理推广域名:总后台绑定什么就保存什么(含路径和参数,原样返回作为推广链接),不再自动拼注册路径和邀请码。
|
|
|
+ * 每个代理的 PC、H5 都可以绑定多个域名;一个主机名只能属于一个代理。
|
|
|
* 主机名(host)用于判断域名是否已被其他代理绑定,以及注册时按访问域名识别代理。
|
|
|
*/
|
|
|
class AgentDomainService
|
|
|
{
|
|
|
+ public const MAX_PER_TYPE = 50;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存代理的 PC / H5 域名。每种类型传字符串(一个域名,兼容旧写法)或数组(多个);
|
|
|
+ * 传了的类型整体替换为本次的列表,空字符串 / 空数组表示清空;没传的类型不变。
|
|
|
+ */
|
|
|
public function save(Agent $agent, array $domains): array
|
|
|
{
|
|
|
return DB::transaction(function () use ($agent, $domains) {
|
|
|
+ $poolEnabled = \Illuminate\Support\Facades\Schema::hasTable('agent_domain_pool');
|
|
|
+ $adminId = isset(request()->user->id) ? (int) request()->user->id : null;
|
|
|
foreach (['pc', 'h5'] as $type) {
|
|
|
if (!array_key_exists($type, $domains)) {
|
|
|
continue;
|
|
|
}
|
|
|
- $url = $this->normalize((string) $domains[$type]);
|
|
|
- if ($url === '') {
|
|
|
- AgentDomain::query()->where('agent_id', $agent->id)->where('type', $type)->delete();
|
|
|
- continue;
|
|
|
+ $values = is_array($domains[$type]) ? $domains[$type] : [$domains[$type]];
|
|
|
+ $wanted = [];
|
|
|
+ foreach ($values as $value) {
|
|
|
+ $url = $this->normalize((string) $value);
|
|
|
+ if ($url === '') continue;
|
|
|
+ $host = $this->host($url);
|
|
|
+ $wanted[$host] ??= $url; // 同一主机名只保留第一个
|
|
|
+ }
|
|
|
+ if (count($wanted) > self::MAX_PER_TYPE) {
|
|
|
+ throw new \InvalidArgumentException('每种类型最多绑定 ' . self::MAX_PER_TYPE . ' 个域名');
|
|
|
+ }
|
|
|
+ foreach ($wanted as $host => $url) {
|
|
|
+ $owner = AgentDomain::query()->where('host', $host)->where('agent_id', '<>', $agent->id)->lockForUpdate()->first();
|
|
|
+ if ($owner) {
|
|
|
+ throw new \RuntimeException('该域名已绑定其他代理:' . $host);
|
|
|
+ }
|
|
|
+ // 登记到域名池(已停用的不能绑定);迁移未执行前跳过
|
|
|
+ if ($poolEnabled) (new AgentDomainPoolService($this))->register($url, $host, $type, $adminId);
|
|
|
}
|
|
|
- $host = $this->host($url);
|
|
|
- $owner = AgentDomain::query()->where('host', $host)->lockForUpdate()->first();
|
|
|
- if ($owner && (int) $owner->agent_id !== (int) $agent->id) {
|
|
|
- throw new \RuntimeException('该域名已绑定其他代理');
|
|
|
+ $existing = AgentDomain::query()->where('agent_id', $agent->id)->where('type', $type)->lockForUpdate()->get()->keyBy('host');
|
|
|
+ foreach ($existing as $host => $row) {
|
|
|
+ if (!isset($wanted[$host])) $row->delete();
|
|
|
}
|
|
|
- // 登记到域名池(已停用的不能绑定);迁移未执行前跳过
|
|
|
- if (\Illuminate\Support\Facades\Schema::hasTable('agent_domain_pool')) {
|
|
|
- (new AgentDomainPoolService($this))->register($url, $host, $type, isset(request()->user->id) ? (int) request()->user->id : null);
|
|
|
+ foreach ($wanted as $host => $url) {
|
|
|
+ if (isset($existing[$host])) {
|
|
|
+ $existing[$host]->fill(['domain' => $url, 'status' => 1])->save();
|
|
|
+ } else {
|
|
|
+ AgentDomain::query()->create(['agent_id' => $agent->id, 'type' => $type, 'domain' => $url, 'host' => $host, 'status' => 1]);
|
|
|
+ }
|
|
|
}
|
|
|
- AgentDomain::query()->updateOrCreate(
|
|
|
- ['agent_id' => $agent->id, 'type' => $type],
|
|
|
- ['domain' => $url, 'host' => $host, 'status' => 1]
|
|
|
- );
|
|
|
}
|
|
|
return $this->format($agent->fresh());
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 域名信息。pc_domains / h5_domains 为全部绑定(按绑定先后);
|
|
|
+ * pc_domain / h5_domain 和 *_register_url 为第一个,兼容只显示一个的旧页面。推广链接 = 绑定的地址本身。
|
|
|
+ */
|
|
|
public function format(Agent $agent): array
|
|
|
{
|
|
|
- $domains = $agent->domains()->where('status', 1)->pluck('domain', 'type');
|
|
|
- $pc = (string) ($domains['pc'] ?? '');
|
|
|
- $h5 = (string) ($domains['h5'] ?? '');
|
|
|
+ $rows = $agent->domains()->where('status', 1)->orderBy('id')->get(['type', 'domain']);
|
|
|
+ $pc = $rows->where('type', 'pc')->pluck('domain')->values()->all();
|
|
|
+ $h5 = $rows->where('type', 'h5')->pluck('domain')->values()->all();
|
|
|
return [
|
|
|
- 'pc_domain' => $pc,
|
|
|
- 'h5_domain' => $h5,
|
|
|
- // 推广链接 = 绑定的地址本身
|
|
|
- 'pc_register_url' => $pc,
|
|
|
- 'h5_register_url' => $h5,
|
|
|
+ 'pc_domain' => (string) ($pc[0] ?? ''),
|
|
|
+ 'h5_domain' => (string) ($h5[0] ?? ''),
|
|
|
+ 'pc_register_url' => (string) ($pc[0] ?? ''),
|
|
|
+ 'h5_register_url' => (string) ($h5[0] ?? ''),
|
|
|
+ 'pc_domains' => $pc,
|
|
|
+ 'h5_domains' => $h5,
|
|
|
];
|
|
|
}
|
|
|
|