Compare commits

13 Commits

Author SHA1 Message Date
zj
6d3eeeca34 update 2026-09-15 14:58:50 +08:00
zj
e82f3275cf update 2026-08-20 15:03:52 +08:00
zj
e744b1a898 完善日志打印 2026-08-20 15:01:24 +08:00
zj
d6a9b93f29 update 2026-08-18 17:17:08 +08:00
zj
38a6f81b39 新增分页打包 2026-08-18 09:15:31 +08:00
zj
9e3c82086c 修复分页回调each问题 2026-08-13 10:14:14 +08:00
zj
b1f99153b2 新增获取页码和每页数量 2026-08-12 19:35:32 +08:00
堂吉诃德拉曼查的英豪
c0586c93cd Merge remote-tracking branch 'origin/main' 2026-08-12 15:18:49 +08:00
堂吉诃德拉曼查的英豪
1647ae803d 核心helpers内部日志改用LogHelper 2026-08-12 15:11:44 +08:00
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
7 changed files with 210 additions and 5 deletions

View File

@@ -96,12 +96,15 @@ class Logger
if ($this->throwable !== null) {
$throwable = $this->throwable;
$previous = $throwable->getPrevious();
$content['throwable'] = [
'message' => $throwable->getMessage(),
'code' => $throwable->getCode(),
'file' => $throwable->getFile(),
'line' => $throwable->getLine(),
'trace' => $this->isError ? $throwable->getTrace() : null,
'previous_trace' => $previous ? $previous->getTrace() : null,
'previous_message' => $previous ? $previous->getMessage() : null,
'data' => method_exists($throwable, 'getData') ? $throwable->getData() : null,
];
}

View File

@@ -12,6 +12,8 @@ use Hyperf\Context\ApplicationContext;
use Hyperf\Collection\Arr;
use Hyperf\Carbon\Carbon;
use Dhd\Micro\Core\Library\Logger;
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Paginator\AbstractPaginator;
final class Util
{
@@ -308,12 +310,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 +342,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 +354,111 @@ 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';
}
/**
* 获取分页参数中的页码
* @param array $params
* @return int
*/
public static function getPageFromParams(array $params): int
{
$page = (int)Arr::get($params, 'page', 1) ?: 1;
return max($page, 1);
}
/**
* 获取分页参数中的每页数量
* @param array $params
* @return int
*/
public static function getPageSizeFromParams(array $params, int $maxSize = 100): int
{
$pageSize = (int)Arr::get(
$params,
'size',
Arr::get(
$params,
'page_size',
Arr::get($params, 'pageSize', 15)
)
) ?: 15;
return min(max($pageSize, 1), $maxSize);
}
/**
* 分页打包
* @param LengthAwarePaginatorInterface $paginator 分页对象
* @return array
*/
public static function handlePage(LengthAwarePaginatorInterface $paginator): array
{
if ($paginator instanceof AbstractPaginator) {
$items = $paginator->getCollection();
} else {
$items = Collection::make($paginator->items());
}
return [
'list' => $items->toArray(),
'total' => $paginator->total(),
];
}
}

View File

@@ -142,7 +142,7 @@ abstract class Model extends HyperfModel implements CacheableInterface
$size = max(1, min(100, $size));
$result = $query->paginate($size, ['*'], 'page', $page);
if (is_callable($eachCallback) && method_exists($result, 'each')) {
if (is_callable($eachCallback)) {
$result->each($eachCallback);
}
return [

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] ?? '未知错误';
}
}

View File

@@ -384,4 +384,12 @@ abstract class Service
{
Util::setObjectValueExcept($obj, $params, $except);
}
/**
* @return null|T
*/
public function findByFilter(array $params): mixed
{
return $this->repository->findByFilter($params);
}
}

View File

@@ -14,6 +14,7 @@ use Hyperf\Di\Annotation\Inject;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Hyperf\Validation\Validator as HyperfValidator;
use RuntimeException;
use Hyperf\Collection\Arr;
abstract class Validator
{
@@ -128,6 +129,17 @@ abstract class Validator
}
}
/**
* 获取待验证数据中指定键的值.
* 如果不存在该键,则返回null.
* @param string $key 待获取的键名
* @return mixed 键对应的值
*/
protected function getDataByKey(string $key): mixed
{
return Arr::get($this->data, $key);
}
/**
* 定义场景验证规则(子类实现).
*/

View File

@@ -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) {
// 忽略日志错误
}