Compare commits
6 Commits
zd-feature
...
c0586c93cd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c0586c93cd | ||
|
|
1647ae803d | ||
| 30e2cc5943 | |||
| ddc49e5b6a | |||
| 76dc5308a1 | |||
| ff7adab00d |
@@ -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
69
src/Response.php
Normal 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] ?? '未知错误';
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
use Hyperf\Context\ApplicationContext;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Logger\LoggerFactory;
|
||||
use Dhd\Micro\Core\Library\LogHelper;
|
||||
use function Hyperf\Config\config;
|
||||
|
||||
if (! function_exists('getNacosConfig')) {
|
||||
@@ -122,7 +123,7 @@ if (! function_exists('copy_file_safely')) {
|
||||
{
|
||||
$safeLog = function ($msg, $data) {
|
||||
try {
|
||||
f_log($msg, $data);
|
||||
LogHelper::error($msg, ['data' => $data]);
|
||||
} catch (Throwable $e) {
|
||||
// 忽略日志错误
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user