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);
+ }
}