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:
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