丰富基础代码

This commit is contained in:
技术老胡
2026-03-10 18:39:22 +08:00
parent 9de902231f
commit add2c49272
17 changed files with 931 additions and 14 deletions

View File

@@ -41,6 +41,30 @@ class BaseController
]);
}
/**
* 统一分页返回结构
*
* @param mixed $paginator Laravel/Eloquent paginator
*/
protected function page(mixed $paginator): Response
{
if (!is_object($paginator) || !method_exists($paginator, 'items')) {
return $this->success([
'list' => [],
'total' => 0,
'page' => 1,
'size' => 10,
]);
}
return $this->success([
'list' => $paginator->items(),
'total' => $paginator->total(),
'page' => $paginator->currentPage(),
'size' => $paginator->perPage(),
]);
}
/**
* 获取当前登录用户的 token 载荷
*

View File

@@ -62,11 +62,8 @@ abstract class BaseRepository
{
$query = $this->model->newQuery();
foreach ($where as $field => $value) {
if ($value === null || $value === '') {
continue;
}
$query->where($field, $value);
if (!empty($where)) {
$query->where($where);
}
return $query->paginate($pageSize, $columns, 'page', $page);