<?php
declare(strict_types=1);
namespace Core\Repositories\Admin;
use App\Constants\StatusCode;
use App\Exception\BusinessException;
use Core\Repositories\BaseRepository;
class MenuRepository extends BaseRepository
{
public function getUserMenuList()
{
$userInfo = $this->auth->check();
if (!isset($userInfo['id']) || !$userInfo['id']) {
throw new BusinessException(StatusCode::ERR_NOT_LOGIN);
}
$list = $this->menuService->getUserMenuList($userInfo['id']);
return $list;
}
public function getMenuList()
{
$list = $this->menuService->getMenuTreeList();
return $list;
}
public function getPermissionsList()
{
$list = $this->permissionsService->getPermissionsTreeList();
return $list;
}
public function saveMenu($data)
{
if ( !(isset($data['id']) && $data['id']) ) {
$data['order'] = $this->menuService->getMenuCount(['parent_id' => $data['parent_id']]);
}
return $this->menuService->saveMenu($data);
}
public function getInfo($id)
{
$info = $this->menuService->getInfo($id);
return $info;
}
public function getMenuPermissionList($pid)
{
if (!$pid) {
return [];
}
$arr = $this->permissionsService->getParentIds($pid);
$arr[] = $pid;
return $arr;
}
public function orderMenu($ids = [])
{
if (count($ids) <= 1) {
return true;
}
$order = 0; foreach ($ids as $v) {
$saveData = [
'id' => $v,
'order' => $order
];
$this->menuService->saveMenu($saveData);
$order++;
}
return true;
}
public function deleteInfo($id)
{
$count = $this->menuService->getMenuCount(['parent_id' => $id]);
if ($count) {
throw new BusinessException(StatusCode::ERR_EXCEPTION,'存在子节点不允许删除');
}
$info = $this->menuService->deleteInfo($id);
return $info;
}
}