mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-02-08 10:20:16 +08:00
137 lines
3.8 KiB
PHP
137 lines
3.8 KiB
PHP
<?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\Common\Repository\AdminMenuRepository;
|
|
use App\Exception\ErrException;
|
|
use App\Model\AdminMenu;
|
|
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_merge($this->getRequestData(),[
|
|
'created_by' => $this->adminId
|
|
]);
|
|
if (empty($data['parent_id'])) $data['parent_id'] = 0;
|
|
/**
|
|
* @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_merge($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();
|
|
}
|
|
} |