From b019d400090b4cfaf4b3e370f07ccf06e2180b2a Mon Sep 17 00:00:00 2001 From: kuaifan Date: Fri, 14 Jan 2022 23:15:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20websocket=E8=8E=B7=E5=8F=96=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Module/Base.php | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/app/Module/Base.php b/app/Module/Base.php index 9673b351..f48f12dd 100755 --- a/app/Module/Base.php +++ b/app/Module/Base.php @@ -725,7 +725,7 @@ class Base public static function getHost($var = '') { if (empty($var)) { - $var = url("/"); + $var = self::url(); } $arr = parse_url($var); return $arr['host']; @@ -756,11 +756,7 @@ class Base ) { return $str; } else { - $url = url($str); - if (str_starts_with($url, "http://localhost/")) { - $url = self::localhostAndPort() . substr($url, 17); - } - return $url; + return self::url($str); } } @@ -777,16 +773,35 @@ class Base } return $str; } - $str = Base::leftDelete($str, url('') . '/'); - return Base::leftDelete($str, self::localhostAndPort()); + return Base::leftDelete($str, self::url() . '/'); } /** - * 获取localhost和端口,如:http://localhost:8888/ - * @return string + * 获取url + * @param $path + * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\UrlGenerator|string */ - public static function localhostAndPort() { - return "http://localhost:" . env("APP_PORT", "80") . "/"; + public static function url($path = '') + { + try { + $url = url($path); + if (str_starts_with($url, "http://localhost/")) { + $url = "http://localhost:" . env("APP_PORT", "80") . "/" . substr($url, 17); + } + } catch (\Throwable) { + $url = self::getSchemeAndHost() . "/" . $path; + } + return $url; + } + + /** + * 获取主地址 + * @return string 如:http://127.0.0.1:8080 + */ + public static function getSchemeAndHost() + { + $scheme = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://'; + return $scheme.($_SERVER['HTTP_HOST'] ?? ''); } /**