Compare commits

...

4 Commits

Author SHA1 Message Date
zj
30e2cc5943 update 2026-08-11 11:27:23 +08:00
zj
ddc49e5b6a 新增Response类 2026-08-06 16:47:04 +08:00
zj
76dc5308a1 新增字节自动格式化易读单位 2026-08-06 15:53:59 +08:00
zj
ff7adab00d 新增:使用正则替换内容中的图片路径公共方法 2026-08-06 15:47:46 +08:00
2 changed files with 133 additions and 3 deletions

View File

@@ -308,12 +308,12 @@ final class Util
/**
* 为内容中的图片路径添加文件域名
* @param string|array $content 内容
* @param string|array|null $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
public static function addFileDomain(array|string|null $content, bool $simple = false, ?string $fileDomain = null): array|string|null
{
if (empty($content)) {
return $content;
@@ -340,8 +340,11 @@ final class Util
/**
* 移除内容中图片的文件域名
* @param string|array|null $content 内容
* @param string|null $fileDomain 文件域名
* @return array|string
*/
public static function removeFileDomain(array|string $content, ?string $fileDomain = null): array|string
public static function removeFileDomain(array|string|null $content, ?string $fileDomain = null): array|string
{
if (empty($content)) {
return $content;
@@ -349,4 +352,62 @@ final class Util
$fileDomain = $fileDomain ?? self::getFileDomainFromNacos() . '/';
return str_replace($fileDomain, '', $content);
}
/**
* 使用正则替换内容中的图片路径
* @param array|string $content 内容
* @param string|null $oldDomain 旧域名
* @param string $search 搜索字符串
* @param string $replace 替换字符串
* @return array|string
*/
public static function replaceImgDomainPreg(array|string $content, ?string $oldDomain, string $search = '', string $replace = ''): array
{
if (empty($content)) {
return $content;
}
$content = self::removeFileDomain($content, $oldDomain);
$filePath = [];
$content = preg_replace_callback('/<img([^>]*src\s*=\s*["\'])([^"\']*)(["\'][^>]*)>/i', function ($matches) use ($search, $replace, &$filePath) {
$prefix = $matches[1];
$src = $matches[2];
$suffix = $matches[3];
if (!empty($search)) {
$newSrc = str_replace($search, $replace, $src);
} else {
$newSrc = $src;
}
$filePath[] = [
'old' => $src,
'new' => $newSrc
];
return '<img' . $prefix . $newSrc . $suffix . '>';
}, $content);
return [$content, $filePath];
}
/**
* 字节自动格式化易读单位
* @param float $bytes
* @return string
*/
public static function prettyBytes(float $bytes): string
{
$step = pow(1024, 4);
if ($bytes >= $step) {
return round($bytes / $step, 2) . 'TB';
}
$step = pow(1024, 3);
if ($bytes >= $step) {
return round($bytes / $step, 2) . 'GB';
}
$step = pow(1024, 2);
if ($bytes >= $step) {
return round($bytes / $step, 2) . 'MB';
}
$step = 1024;
return round($bytes / $step, 2) . 'KB';
}
}

69
src/Response.php Normal file
View File

@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace Dhd\Micro\Core;
class Response
{
/**
* 成功响应.
* @param null|mixed $data 数据
* @param string $message 消息
*/
public static function success(mixed $data = null, string $message = 'ok'): array
{
return [
'data' => $data,
'code' => 200,
'message' => $message ?: 'success',
];
}
/**
* 错误响应.
* @param int $code 错误码
* @param string $message 错误消息
* @param null|mixed $data 附加数据
*/
public static function error(string $message = 'fail', int $code = -1, mixed $data = null): array
{
return [
'code' => $code,
'data' => $data,
'message' => $message ?: self::getDefaultMessage($code),
];
}
public static function page($list, int $total, int $page, int $size): array
{
return [
'code' => 200,
'message' => 'ok',
'list' => [
'list' => $list,
'page' => $page,
'size' => $size,
'total' => $total,
'pages' => (int) ceil($total / $size),
],
];
}
/**
* 获取默认错误消息.
*/
private static function getDefaultMessage(int $code): string
{
$messages = [
400 => '请求参数错误',
401 => '未授权',
403 => '禁止访问',
404 => '资源不存在',
500 => '服务器内部错误',
10001 => '校区不存在',
];
return $messages[$code] ?? '未知错误';
}
}