Compare commits

..

9 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
6 changed files with 77 additions and 2 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
{
@@ -410,4 +412,53 @@ final class Util
$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 [

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