From 74315314e98cd655d3b08c2af1eca55ff5591cf7 Mon Sep 17 00:00:00 2001 From: zj Date: Wed, 5 Aug 2026 15:07:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=96=87=E4=BB=B6=E5=9F=9F?= =?UTF-8?q?=E5=90=8D=E7=9B=B8=E5=85=B3=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Library/Util.php | 64 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/Library/Util.php b/src/Library/Util.php index 3d74312..29275a0 100644 --- a/src/Library/Util.php +++ b/src/Library/Util.php @@ -25,6 +25,17 @@ final class Util return ApplicationContext::getContainer()->get($name); } + /** + * 获取nacos配置 + * @param string $key 键 + * @param mixed $default 默认值 + * @return mixed + */ + public static function getNacosConfig(string $key, $default = null) + { + return config('nacos_config.' . $key, $default); + } + /** * 获取日志记录器 */ @@ -269,4 +280,57 @@ final class Util } return false; } + + /** + * 获取文件中心域名 + * @return string + */ + public static function getFileDomainFromNacos(): string + { + return self::getNacosConfig('file_domain', ''); + } + + /** + * 为内容中的图片路径添加文件域名 + * @param string|array $content 内容 + * @param bool $simple 是否简单处理 + * @param string|null $fileDomain 文件域名 + * @return array|string|null + */ + public static function addFileDomain(array|string $content, bool $simple = false, ?string $fileDomain = null): array|string|null + { + if (empty($content)) { + return $content; + } + + $fileDomain = $fileDomain ?? self::getFileDomainFromNacos() . '/'; + // 简单处理 + if ($simple) { + return is_string($content) ? $fileDomain . ltrim($content, '/') : $content; + } + + $content = preg_replace_callback('/]*src\s*=\s*["\'])([^"\']*)(["\'][^>]*)>/i', function ($matches) use ($fileDomain) { + $fullMatch = $matches[0]; + $prefix = $matches[1]; + $src = $matches[2]; + $suffix = $matches[3]; + + $newSrc = $fileDomain . $src; + return ''; + }, $content); + + return $content; + } + + /** + * 移除内容中图片的文件域名 + */ + public static function removeFileDomain(array|string $content, ?string $fileDomain = null): array|string + { + if (empty($content)) { + return $content; + } + $fileDomain = $fileDomain ?? self::getFileDomainFromNacos() . '/'; + return str_replace($fileDomain, '', $content); + } }