mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 18:17:49 +08:00
feat : admin menu
This commit is contained in:
70
app/Controller/Admin/AdminMenuController.php
Normal file
70
app/Controller/Admin/AdminMenuController.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Admin;
|
||||
|
||||
use App\Annotation\Permission;
|
||||
use App\Annotation\ResponseFormat;
|
||||
use App\Middleware\Admin\AdminTokenMiddleware;
|
||||
use App\Middleware\Admin\PermissionMiddleware;
|
||||
use App\Service\Admin\AdminUser\MenuService;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\Middleware;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
|
||||
|
||||
#[Controller(prefix: "admin/menu")]
|
||||
#[ResponseFormat('admin')]
|
||||
#[Middleware(middleware: AdminTokenMiddleware::class, priority: 100)]
|
||||
#[Middleware(middleware: PermissionMiddleware::class, priority: 99)]
|
||||
class AdminMenuController
|
||||
{
|
||||
/**
|
||||
* @var MenuService
|
||||
*/
|
||||
#[Inject]
|
||||
protected MenuService $service;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "list", methods: "GET")]
|
||||
#[Permission(code: 'permission:menu:index')]
|
||||
public function pageList(): array
|
||||
{
|
||||
return $this->service->handle();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "", methods: "POST")]
|
||||
#[Permission(code: 'permission:menu:create')]
|
||||
public function createMenu(): array
|
||||
{
|
||||
return $this->service->create();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "{id}", methods: "PUT")]
|
||||
#[Permission(code: 'permission:menu:save')]
|
||||
public function updateMenu(int $id): array
|
||||
{
|
||||
return $this->service->update();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "", methods: "DELETE")]
|
||||
#[Permission(code: 'permission:menu:delete')]
|
||||
public function deleteMenu(): array
|
||||
{
|
||||
return $this->service->delete();
|
||||
}
|
||||
}
|
||||
21
app/Controller/Admin/AdminRoleController.php
Normal file
21
app/Controller/Admin/AdminRoleController.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Admin;
|
||||
|
||||
use App\Annotation\ResponseFormat;
|
||||
use App\Middleware\Admin\AdminTokenMiddleware;
|
||||
use App\Middleware\Admin\PermissionMiddleware;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\Middleware;
|
||||
|
||||
|
||||
#[Controller(prefix: "admin/role")]
|
||||
#[ResponseFormat('admin')]
|
||||
#[Middleware(middleware: AdminTokenMiddleware::class, priority: 100)]
|
||||
#[Middleware(middleware: PermissionMiddleware::class, priority: 99)]
|
||||
class AdminRoleController
|
||||
{
|
||||
|
||||
}
|
||||
80
app/Repository/AdminMenuRepository.php
Normal file
80
app/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\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();
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ declare(strict_types=1);
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Model\AdminUser;
|
||||
use Hyperf\Collection\Arr;
|
||||
use Hyperf\Database\Concerns\BuildsQueries;
|
||||
use Hyperf\Database\Model\Builder;
|
||||
use Hyperf\Database\Model\Model;
|
||||
@@ -34,4 +35,49 @@ final class AdminUserRepository extends BaseRepository
|
||||
->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);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
136
app/Service/Admin/AdminUser/MenuService.php
Normal file
136
app/Service/Admin/AdminUser/MenuService.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\AdminUser;
|
||||
|
||||
use App\Exception\ErrException;
|
||||
use App\Model\AdminMenu;
|
||||
use App\Repository\AdminMenuRepository;
|
||||
use App\Service\Admin\BaseAdminService;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class MenuService extends BaseAdminService
|
||||
{
|
||||
/**
|
||||
* @var AdminMenuRepository
|
||||
*/
|
||||
#[Inject]
|
||||
protected AdminMenuRepository $adminMenuRepository;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
return $this->adminReturn->success('success',$this->adminMenuRepository->list([
|
||||
'children' => true,
|
||||
'parent_id' => 0
|
||||
])?->toArray() ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function create(): array
|
||||
{
|
||||
$data = array($this->getRequestData(),[
|
||||
'created_by' => $this->adminId
|
||||
]);
|
||||
/**
|
||||
* @var AdminMenu $model
|
||||
*/
|
||||
$model = $this->adminMenuRepository->create($data);
|
||||
|
||||
if (!$model) throw new ErrException('添加失败');
|
||||
|
||||
if ($data['meta']['type'] !== 'M' || empty($data['btnPermission'])) return $this->adminReturn->success('success',$model->toArray());
|
||||
|
||||
foreach ($data['btnPermission'] as $item) {
|
||||
$this->adminMenuRepository->create([
|
||||
'parent_id' => $model->id,
|
||||
'name' => $item['code'],
|
||||
'sort' => 0,
|
||||
'status' => 1,
|
||||
'meta' => [
|
||||
'title' => $item['title'],
|
||||
'i18n' => $item['i18n'],
|
||||
'type' => 'B'
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->adminReturn->success('success',$model->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function update(int $id): array
|
||||
{
|
||||
$data = array($this->getRequestData(),[
|
||||
'updated_by' => $this->adminId
|
||||
]);
|
||||
|
||||
$model = $this->adminMenuRepository->updateById($id, $data);
|
||||
|
||||
if (!$model) throw new ErrException('修改失败');
|
||||
|
||||
if ($data['meta']['type'] !== 'M' || !isset($data['btnPermission'])) return $this->adminReturn->success();
|
||||
|
||||
$existsBtnPermissions = array_flip(
|
||||
$this->adminMenuRepository
|
||||
->getQuery()
|
||||
->where('parent_id', $id)
|
||||
->whereJsonContains('meta->type', 'B')
|
||||
->pluck('id')
|
||||
->toArray()
|
||||
);
|
||||
|
||||
if (!empty($data['btnPermission'])) {
|
||||
foreach ($data['btnPermission'] as $item) {
|
||||
if (empty($item['type']) || $item['type'] !== 'B') continue;
|
||||
|
||||
$data = [
|
||||
'name' => $item['code'],
|
||||
'meta' => [
|
||||
'title' => $item['title'],
|
||||
'i18n' => $item['i18n'],
|
||||
'type' => 'B'
|
||||
],
|
||||
];
|
||||
|
||||
if (!empty($item['id'])) {
|
||||
$this->adminMenuRepository->updateById($item['id'], $data);
|
||||
unset($existsBtnPermissions[$item['id']]);
|
||||
} else {
|
||||
$data['parent_id'] = $id;
|
||||
$this->adminMenuRepository->create($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($existsBtnPermissions)) $this->adminMenuRepository->deleteById(array_keys($existsBtnPermissions));
|
||||
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function delete(): array
|
||||
{
|
||||
$res = $this->adminMenuRepository->deleteById($this->getRequestData());
|
||||
|
||||
if (!$res) throw new ErrException('删除失败');
|
||||
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
}
|
||||
21
app/Service/Admin/AdminUser/RoleService.php
Normal file
21
app/Service/Admin/AdminUser/RoleService.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\AdminUser;
|
||||
|
||||
use App\Service\Admin\BaseAdminService;
|
||||
|
||||
class RoleService extends BaseAdminService
|
||||
{
|
||||
public function handle()
|
||||
{
|
||||
//todo Write logic
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user