|
|
@@ -6,6 +6,10 @@ use App\Models\Agent\Agent;
|
|
|
use App\Models\Agent\AgentDomain;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
+/**
|
|
|
+ * 代理推广域名:总后台绑定什么就保存什么(含路径和参数,原样返回作为推广链接),不再自动拼注册路径和邀请码。
|
|
|
+ * 主机名(host)用于判断域名是否已被其他代理绑定,以及注册时按访问域名识别代理。
|
|
|
+ */
|
|
|
class AgentDomainService
|
|
|
{
|
|
|
public function save(Agent $agent, array $domains): array
|
|
|
@@ -42,8 +46,9 @@ class AgentDomainService
|
|
|
return [
|
|
|
'pc_domain' => $pc,
|
|
|
'h5_domain' => $h5,
|
|
|
- 'pc_register_url' => $pc === '' ? '' : $this->registerUrl($pc, (string) config('agent.pc_register_path'), $agent->invitation_code),
|
|
|
- 'h5_register_url' => $h5 === '' ? '' : $this->registerUrl($h5, (string) config('agent.h5_register_path'), $agent->invitation_code),
|
|
|
+ // 推广链接 = 绑定的地址本身
|
|
|
+ 'pc_register_url' => $pc,
|
|
|
+ 'h5_register_url' => $h5,
|
|
|
];
|
|
|
}
|
|
|
|
|
|
@@ -61,30 +66,25 @@ class AgentDomainService
|
|
|
return $domainRow ? Agent::query()->whereKey($domainRow->agent_id)->where('status', Agent::STATUS_ENABLED)->first() : null;
|
|
|
}
|
|
|
|
|
|
+ /** 只去掉首尾空白,其余原样保留;校验能解析出 http/https 主机名。 */
|
|
|
private function normalize(string $value): string
|
|
|
{
|
|
|
$value = trim($value);
|
|
|
if ($value === '') {
|
|
|
return '';
|
|
|
}
|
|
|
- $candidate = preg_match('#^https?://#i', $value) ? $value : 'https://' . $value;
|
|
|
- $scheme = strtolower((string) parse_url($candidate, PHP_URL_SCHEME));
|
|
|
- $host = strtolower((string) parse_url($candidate, PHP_URL_HOST));
|
|
|
- $port = parse_url($candidate, PHP_URL_PORT);
|
|
|
- if (!in_array($scheme, ['http', 'https'], true) || $host === '') {
|
|
|
+ if (preg_match('/\s/u', $value) || $this->host($value) === '') {
|
|
|
throw new \InvalidArgumentException('域名格式错误');
|
|
|
}
|
|
|
- return $scheme . '://' . $host . ($port ? ':' . $port : '');
|
|
|
- }
|
|
|
-
|
|
|
- private function registerUrl(string $domain, string $path, string $code): string
|
|
|
- {
|
|
|
- $separator = str_contains($path, '?') ? '&' : '?';
|
|
|
- return rtrim($domain, '/') . '/' . ltrim($path, '/') . $separator . 'agent_code=' . urlencode($code);
|
|
|
+ return $value;
|
|
|
}
|
|
|
|
|
|
+ /** 主机名(小写);没写协议时按 https 解析。只允许 http/https。 */
|
|
|
private function host(string $url): string
|
|
|
{
|
|
|
- return strtolower((string) parse_url($url, PHP_URL_HOST));
|
|
|
+ $candidate = preg_match('#^[a-z][a-z0-9+.-]*://#i', $url) ? $url : 'https://' . $url;
|
|
|
+ $scheme = strtolower((string) parse_url($candidate, PHP_URL_SCHEME));
|
|
|
+ if (!in_array($scheme, ['http', 'https'], true)) return '';
|
|
|
+ return strtolower((string) parse_url($candidate, PHP_URL_HOST));
|
|
|
}
|
|
|
}
|