优化代码

main
gzydong 2021-09-15 21:52:20 +08:00
parent 5e34ef7412
commit 7430f86fb2
2 changed files with 33 additions and 30 deletions

View File

@ -5,7 +5,7 @@ namespace App\Repository;
use App\Traits\RepositoryTrait; use App\Traits\RepositoryTrait;
use Hyperf\Database\Model\Model; use Hyperf\Database\Model\Model;
use Hyperf\Database\Model\Builder; use Hyperf\Database\Model\Builder as ModelBuilder;
use Hyperf\DbConnection\Db; use Hyperf\DbConnection\Db;
use Hyperf\Utils\Collection; use Hyperf\Utils\Collection;
@ -61,7 +61,7 @@ abstract class BaseRepository
* @param array $where 查询条件 * @param array $where 查询条件
* @param string[] $fields 查询字段 * @param string[] $fields 查询字段
* @param bool $is_array 是否返回数组格式 * @param bool $is_array 是否返回数组格式
* @return Builder|Model|object|array|null * @return ModelBuilder|Model|object|array|null
*/ */
final public function first(array $where = [], array $fields = ['*'], bool $is_array = false) final public function first(array $where = [], array $fields = ['*'], bool $is_array = false)
{ {
@ -112,7 +112,7 @@ abstract class BaseRepository
$model = $this->buildWhere($where); $model = $this->buildWhere($where);
return $this->toPaginate($model, $fields, $page, $size); return toPaginate($model, $fields, $page, $size);
} }
/** /**
@ -192,31 +192,4 @@ abstract class BaseRepository
{ {
return Db::select($query, $bindings, $useReadPdo); return Db::select($query, $bindings, $useReadPdo);
} }
/**
* 通过 Model 读取分页数据
*
* @param Builder $model 查询构造器
* @param array $fields 查询字段
* @param int $page 当前分页
* @param int $size 分页大小
* @return array
*/
public function toPaginate(Builder $model, array $fields = ['*'], int $page = 1, int $size = 15): array
{
$total = $model->count();
$data = [
'rows' => [],
'paginate' => [
'page' => $page,
'size' => $size,
'total' => $total,
]
];
if ($total > 0) $data['rows'] = $model->forPage($page, $size)->get($fields)->toArray();
return $data;
}
} }

View File

@ -6,6 +6,8 @@
*/ */
use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Database\Query\Builder as QueryBuilder;
use Hyperf\Database\Model\Builder as ModelBuilder;
use Hyperf\HttpServer\Contract\ResponseInterface; use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Logger\LoggerFactory; use Hyperf\Logger\LoggerFactory;
use Hyperf\Server\ServerFactory; use Hyperf\Server\ServerFactory;
@ -224,3 +226,31 @@ function get_real_ip(): string
return request()->getServerParams()['remote_addr'] ?? ''; return request()->getServerParams()['remote_addr'] ?? '';
} }
/**
* 通过查询构造器读取分页数据
*
* @param QueryBuilder|ModelBuilder $model 查询构造器
* @param array $fields 查询字段
* @param int $page 当前分页
* @param int $size 分页大小
* @return array
*/
function toPaginate($model, array $fields = ['*'], int $page = 1, int $size = 15): array
{
$total = $model->count();
$data = [
'rows' => [],
'paginate' => [
'page' => $page,
'size' => $size,
'total' => $total,
]
];
if ($total > 0) $data['rows'] = $model->forPage($page, $size)->get($fields)->toArray();
return $data;
}