From 9b2731607bd324c6d836fe4e672bf6638f90e01a Mon Sep 17 00:00:00 2001 From: kuaifan Date: Sat, 11 Oct 2025 10:42:49 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/IndexController.php | 2 +- app/Module/Base.php | 57 ++++++++++++++++++++++++ cmd | 2 +- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/IndexController.php b/app/Http/Controllers/IndexController.php index 9e15cde73..dea8947d2 100755 --- a/app/Http/Controllers/IndexController.php +++ b/app/Http/Controllers/IndexController.php @@ -61,7 +61,7 @@ class IndexController extends InvokeController $array = Base::json2array(file_get_contents($hotFile)); $style = null; $script = preg_replace("/^(\/\/(.*?))(:\d+)?\//i", "$1:" . $array['APP_DEV_PORT'] . "/", asset_main("resources/assets/js/app.js")); - $proxyUri = env('VSCODE_PROXY_URI'); + $proxyUri = Base::liveEnv('VSCODE_PROXY_URI'); if (is_string($proxyUri) && preg_match('/^https?:\/\//i', $proxyUri)) { $script = preg_replace('/^(https?:\/\/|\/\/)[^\/]+/', rtrim($proxyUri, '/'), $script, 1); } diff --git a/app/Module/Base.php b/app/Module/Base.php index bbed5b8ed..ab0baeccb 100755 --- a/app/Module/Base.php +++ b/app/Module/Base.php @@ -3073,4 +3073,61 @@ class Base return $html; } } + + /** + * 实时读取 .env 配置(不受配置缓存影响) + * @param string $key 配置键名 + * @param mixed $default 默认值 + * @return mixed + */ + public static function liveEnv($key, $default = null) + { + $envFile = base_path('.env'); + if (!file_exists($envFile)) { + return $default; + } + + $envContent = file_get_contents($envFile); + $lines = explode("\n", $envContent); + + foreach ($lines as $line) { + $line = trim($line); + + // 跳过注释和空行 + if (empty($line) || str_starts_with($line, '#')) { + continue; + } + + // 解析 KEY=VALUE + if (str_contains($line, '=')) { + [$envKey, $envValue] = explode('=', $line, 2); + $envKey = trim($envKey); + + if ($envKey === $key) { + $envValue = trim($envValue); + + // 移除引号 + if (preg_match('/^(["\'])(.*)\1$/', $envValue, $matches)) { + $envValue = $matches[2]; + } + + // 处理布尔值 + $lowerValue = strtolower($envValue); + if ($lowerValue === 'true') { + return true; + } + if ($lowerValue === 'false') { + return false; + } + if ($lowerValue === 'null' || $lowerValue === '(null)') { + return null; + } + + return $envValue; + } + } + } + + return $default; + } } diff --git a/cmd b/cmd index c9d27fe45..7a72642d4 100755 --- a/cmd +++ b/cmd @@ -175,7 +175,7 @@ web_build() { fi if [ "$type" = "dev" ]; then echo "" > ./index.html - if [[ -z "$(env_get APP_DEV_PORT)" ]] || [[ -z "${VSCODE_PROXY_URI:-}" ]]; then + if [ -z "$(env_get APP_DEV_PORT)" ]; then env_set APP_DEV_PORT $(rand 20001 30000) fi if [ -n "${VSCODE_PROXY_URI:-}" ]; then