mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 19:27:48 +08:00
fix : update path And request
This commit is contained in:
80
app/Common/Repository/AdminMenuRepository.php
Normal file
80
app/Common/Repository/AdminMenuRepository.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Common\Repository;
|
||||
|
||||
use App\Constants\Model\AdminUser\AdminMenuStatusCode;
|
||||
use App\Model\AdminMenu;
|
||||
use Hyperf\Collection\Arr;
|
||||
use Hyperf\Collection\Collection;
|
||||
use Hyperf\Database\Model\Builder;
|
||||
|
||||
final class AdminMenuRepository extends BaseRepository
|
||||
{
|
||||
/**
|
||||
* @param AdminMenu $model
|
||||
*/
|
||||
public function __construct(protected readonly AdminMenu $model) {}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function enablePageOrderBy(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return Collection
|
||||
*/
|
||||
public function list(array $params = []): Collection
|
||||
{
|
||||
return $this->perQuery($this->getQuery(), $params)->orderBy('sort')->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder $query
|
||||
* @param array $params
|
||||
* @return Builder
|
||||
*/
|
||||
public function handleSearch(Builder $query, array $params): Builder
|
||||
{
|
||||
$whereInName = static function (Builder $query, array|string $code) {
|
||||
$query->whereIn('name', Arr::wrap($code));
|
||||
};
|
||||
return $query
|
||||
->when(Arr::get($params, 'sortable'), static function (Builder $query, array $sortable) {
|
||||
$query->orderBy(key($sortable), current($sortable));
|
||||
})
|
||||
->when(Arr::get($params, 'code'), $whereInName)
|
||||
->when(Arr::get($params, 'name'), $whereInName)
|
||||
->when(Arr::get($params, 'children'), static function (Builder $query) {
|
||||
$query->with('children');
|
||||
})->when(Arr::get($params, 'status'), static function (Builder $query, AdminMenuStatusCode $status) {
|
||||
$query->where('status', $status);
|
||||
})
|
||||
->when(Arr::has($params, 'parent_id'), static function (Builder $query) use ($params) {
|
||||
$query->where('parent_id', Arr::get($params, 'parent_id'));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Builder[]|\Hyperf\Database\Model\Collection
|
||||
*/
|
||||
public function allTree(): \Hyperf\Database\Model\Collection|array
|
||||
{
|
||||
return $this->model
|
||||
->newQuery()
|
||||
->where('parent_id', 0)
|
||||
->with('children')
|
||||
->get();
|
||||
}
|
||||
}
|
||||
18
app/Common/Repository/AdminRoleRepository.php
Normal file
18
app/Common/Repository/AdminRoleRepository.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Common\Repository;
|
||||
|
||||
use App\Model\AdminRole;
|
||||
|
||||
final class AdminRoleRepository extends BaseRepository
|
||||
{
|
||||
public function __construct(protected readonly AdminRole $model) {}
|
||||
}
|
||||
57
app/Common/Repository/AdminUserLoginLogRepository.php
Normal file
57
app/Common/Repository/AdminUserLoginLogRepository.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Common\Repository;
|
||||
|
||||
use App\Model\AdminUserLoginLog;
|
||||
use Hyperf\Collection\Arr;
|
||||
use Hyperf\Database\Model\Builder;
|
||||
|
||||
final class AdminUserLoginLogRepository extends BaseRepository
|
||||
{
|
||||
/**
|
||||
* @param AdminUserLoginLog $model
|
||||
*/
|
||||
public function __construct(protected readonly AdminUserLoginLog $model) {}
|
||||
|
||||
/**
|
||||
* @param Builder $query
|
||||
* @param array $params
|
||||
* @return Builder
|
||||
*/
|
||||
public function handleSearch(Builder $query, array $params): Builder
|
||||
{
|
||||
return $query
|
||||
->when(Arr::get($params, 'username'), static function (Builder $query, $username) {
|
||||
$query->where('username', $username);
|
||||
})
|
||||
->when(Arr::get($params, 'ip'), static function (Builder $query, $ip) {
|
||||
$query->where('ip', $ip);
|
||||
})
|
||||
->when(Arr::get($params, 'os'), static function (Builder $query, $os) {
|
||||
$query->where('os', $os);
|
||||
})
|
||||
->when(Arr::get($params, 'browser'), static function (Builder $query, $browser) {
|
||||
$query->where('browser', $browser);
|
||||
})
|
||||
->when(Arr::get($params, 'status'), static function (Builder $query, $status) {
|
||||
$query->where('status', $status);
|
||||
})
|
||||
->when(Arr::get($params, 'message'), static function (Builder $query, $message) {
|
||||
$query->where('message', $message);
|
||||
})
|
||||
->when(Arr::get($params, 'login_time'), static function (Builder $query, $login_time) {
|
||||
$query->whereBetween('login_time', $login_time);
|
||||
})
|
||||
->when(Arr::get($params, 'remark'), static function (Builder $query, $remark) {
|
||||
$query->where('remark', $remark);
|
||||
});
|
||||
}
|
||||
}
|
||||
18
app/Common/Repository/AdminUserOperationLogRepository.php
Normal file
18
app/Common/Repository/AdminUserOperationLogRepository.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Common\Repository;
|
||||
|
||||
use App\Model\AdminUserOperationLog;
|
||||
|
||||
final class AdminUserOperationLogRepository extends BaseRepository
|
||||
{
|
||||
public function __construct(protected readonly AdminUserOperationLog $model) {}
|
||||
}
|
||||
81
app/Common/Repository/AdminUserRepository.php
Normal file
81
app/Common/Repository/AdminUserRepository.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Common\Repository;
|
||||
|
||||
use App\Model\AdminUser;
|
||||
use Hyperf\Collection\Arr;
|
||||
use Hyperf\Database\Model\Builder;
|
||||
|
||||
/**
|
||||
* Class AdminUserRepository
|
||||
* @extends BaseRepository<AdminUser>
|
||||
*/
|
||||
final class AdminUserRepository extends BaseRepository
|
||||
{
|
||||
public function __construct(protected readonly AdminUser $model) {}
|
||||
|
||||
/**
|
||||
* @param string $username
|
||||
* @return AdminUser|null
|
||||
*/
|
||||
public function findByUserName(string $username): AdminUser|null
|
||||
{
|
||||
// @phpstan-ignore-next-line
|
||||
return $this->model->newQuery()
|
||||
->where('username', $username)
|
||||
->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder $query
|
||||
* @param array $params
|
||||
* @return Builder
|
||||
*/
|
||||
public function handleSearch(Builder $query, array $params): Builder
|
||||
{
|
||||
return $query
|
||||
->when(Arr::get($params, 'unique_username'), static function (Builder $query, $uniqueUsername) {
|
||||
$query->where('username', $uniqueUsername);
|
||||
})
|
||||
->when(Arr::get($params, 'username'), static function (Builder $query, $username) {
|
||||
$query->where('username', 'like', '%' . $username . '%');
|
||||
})
|
||||
->when(Arr::get($params, 'phone'), static function (Builder $query, $phone) {
|
||||
$query->where('phone', $phone);
|
||||
})
|
||||
->when(Arr::get($params, 'email'), static function (Builder $query, $email) {
|
||||
$query->where('email', $email);
|
||||
})
|
||||
->when(Arr::exists($params, 'status'), static function (Builder $query) use ($params) {
|
||||
$query->where('status', Arr::get($params, 'status'));
|
||||
})
|
||||
->when(Arr::exists($params, 'user_type'), static function (Builder $query) use ($params) {
|
||||
$query->where('user_type', Arr::get($params, 'user_type'));
|
||||
})
|
||||
->when(Arr::exists($params, 'nickname'), static function (Builder $query) use ($params) {
|
||||
$query->where('nickname', 'like', '%' . Arr::get($params, 'nickname') . '%');
|
||||
})
|
||||
->when(Arr::exists($params, 'created_at'), static function (Builder $query) use ($params) {
|
||||
$query->whereBetween('created_at', [
|
||||
Arr::get($params, 'created_at')[0] . ' 00:00:00',
|
||||
Arr::get($params, 'created_at')[1] . ' 23:59:59',
|
||||
]);
|
||||
})
|
||||
->when(Arr::get($params, 'user_ids'), static function (Builder $query, $userIds) {
|
||||
$query->whereIn('id', $userIds);
|
||||
})
|
||||
->when(Arr::get($params, 'role_id'), static function (Builder $query, $roleId) {
|
||||
$query->whereHas('roles', static function (Builder $query) use ($roleId) {
|
||||
$query->where('role_id', $roleId);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
151
app/Common/Repository/BaseRepository.php
Normal file
151
app/Common/Repository/BaseRepository.php
Normal file
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
|
||||
namespace App\Common\Repository;
|
||||
|
||||
use App\Common\Repository\Traits\BootTrait;
|
||||
use App\Common\Repository\Traits\RepositoryOrderByTrait;
|
||||
use Hyperf\Collection\Collection;
|
||||
use Hyperf\Contract\LengthAwarePaginatorInterface;
|
||||
use Hyperf\Database\Model\Builder;
|
||||
use Hyperf\Database\Model\Model;
|
||||
use Hyperf\DbConnection\Traits\HasContainer;
|
||||
use Hyperf\Paginator\AbstractPaginator;
|
||||
|
||||
/**
|
||||
* @template T of Model
|
||||
* @property Model $model
|
||||
*/
|
||||
abstract class BaseRepository
|
||||
{
|
||||
use BootTrait;
|
||||
use HasContainer;
|
||||
use RepositoryOrderByTrait;
|
||||
|
||||
public const string PER_PAGE_PARAM_NAME = 'per_page';
|
||||
public function handleSearch(Builder $query, array $params): Builder
|
||||
{
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function handleItems(Collection $items): Collection
|
||||
{
|
||||
return $items;
|
||||
}
|
||||
|
||||
public function handlePage(LengthAwarePaginatorInterface $paginator): array
|
||||
{
|
||||
if ($paginator instanceof AbstractPaginator) {
|
||||
$items = $paginator->getCollection();
|
||||
} else {
|
||||
$items = Collection::make($paginator->items());
|
||||
}
|
||||
$items = $this->handleItems($items);
|
||||
return [
|
||||
'list' => $items->toArray(),
|
||||
'total' => $paginator->total(),
|
||||
];
|
||||
}
|
||||
|
||||
public function list(array $params = []): Collection
|
||||
{
|
||||
return $this->handleItems($this->perQuery($this->getQuery(), $params)->get());
|
||||
}
|
||||
|
||||
public function count(array $params = []): int
|
||||
{
|
||||
return $this->perQuery($this->getQuery(), $params)->count();
|
||||
}
|
||||
|
||||
public function page(array $params = [], ?int $page = null, ?int $pageSize = null): array
|
||||
{
|
||||
$result = $this->perQuery($this->getQuery(), $params)->paginate(
|
||||
perPage: $pageSize,
|
||||
pageName: static::PER_PAGE_PARAM_NAME,
|
||||
page: $page,
|
||||
);
|
||||
return $this->handlePage($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Model
|
||||
*/
|
||||
public function create(array $data): mixed
|
||||
{
|
||||
// @phpstan-ignore-next-line
|
||||
return $this->getQuery()->create($data);
|
||||
}
|
||||
|
||||
public function updateById(mixed $id, array $data): bool
|
||||
{
|
||||
return (bool) $this->getQuery()->whereKey($id)->first()?->update($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|Model
|
||||
*/
|
||||
public function saveById(mixed $id, array $data): mixed
|
||||
{
|
||||
$model = $this->getQuery()->whereKey($id)->first();
|
||||
if ($model) {
|
||||
$model->fill($data)->save();
|
||||
return $model;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function deleteById(mixed $id): int
|
||||
{
|
||||
// @phpstan-ignore-next-line
|
||||
return $this->model::destroy($id);
|
||||
}
|
||||
|
||||
public function forceDeleteById(mixed $id): bool
|
||||
{
|
||||
return (bool) $this->getQuery()->whereKey($id)->forceDelete();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|Model
|
||||
*/
|
||||
public function findById(mixed $id): mixed
|
||||
{
|
||||
return $this->getQuery()->whereKey($id)->first();
|
||||
}
|
||||
|
||||
public function findByField(mixed $id, string $field): mixed
|
||||
{
|
||||
return $this->getQuery()->whereKey($id)->value($field);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|Model
|
||||
*/
|
||||
public function findByFilter(array $params): mixed
|
||||
{
|
||||
return $this->perQuery($this->getQuery(), $params)->first();
|
||||
}
|
||||
|
||||
public function perQuery(Builder $query, array $params): Builder
|
||||
{
|
||||
$this->startBoot($query, $params);
|
||||
return $this->handleSearch($query, $params);
|
||||
}
|
||||
|
||||
public function getQuery(): Builder
|
||||
{
|
||||
return $this->model->newQuery();
|
||||
}
|
||||
|
||||
public function existsById(mixed $id): bool
|
||||
{
|
||||
return (bool) $this->getQuery()->whereKey($id)->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Model
|
||||
*/
|
||||
public function getModel(): Model
|
||||
{
|
||||
return $this->model;
|
||||
}
|
||||
}
|
||||
20
app/Common/Repository/Traits/BootTrait.php
Normal file
20
app/Common/Repository/Traits/BootTrait.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Common\Repository\Traits;
|
||||
|
||||
use function Hyperf\Support\class_basename;
|
||||
use function Hyperf\Support\class_uses_recursive;
|
||||
|
||||
trait BootTrait
|
||||
{
|
||||
protected function startBoot(...$params): void
|
||||
{
|
||||
$traits = class_uses_recursive(static::class);
|
||||
foreach ($traits as $trait) {
|
||||
$method = 'boot' . class_basename($trait);
|
||||
if (method_exists($this, $method)) {
|
||||
$this->{$method}(...$params);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
38
app/Common/Repository/Traits/RepositoryOrderByTrait.php
Normal file
38
app/Common/Repository/Traits/RepositoryOrderByTrait.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Common\Repository\Traits;
|
||||
|
||||
use Hyperf\Database\Model\Builder;
|
||||
|
||||
trait RepositoryOrderByTrait
|
||||
{
|
||||
public function handleOrderBy(Builder $query, $params): Builder
|
||||
{
|
||||
if ($this->enablePageOrderBy()) {
|
||||
$orderByField = $params[$this->getOrderByParamName()] ?? $query->getModel()->getKeyName();
|
||||
$orderByDirection = $params[$this->getOrderByDirectionParamName()] ?? 'desc';
|
||||
$query->orderBy($orderByField, $orderByDirection);
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
protected function bootRepositoryOrderByTrait(Builder $query, array $params): void
|
||||
{
|
||||
$this->handleOrderBy($query, $params);
|
||||
}
|
||||
|
||||
protected function getOrderByParamName(): string
|
||||
{
|
||||
return 'order_by';
|
||||
}
|
||||
|
||||
protected function getOrderByDirectionParamName(): string
|
||||
{
|
||||
return 'order_by_direction';
|
||||
}
|
||||
|
||||
protected function enablePageOrderBy(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user