|
@@ -12,6 +12,8 @@ use App\Enums\System\MenuType;
|
|
|
use App\Exceptions\ApiException;
|
|
|
use App\Http\Services\Service;
|
|
|
use App\Models\System\Menu;
|
|
|
+use Illuminate\Support\Arr;
|
|
|
+use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
class MenuService extends Service
|
|
|
{
|
|
@@ -19,17 +21,91 @@ class MenuService extends Service
|
|
|
/**
|
|
|
* @throws ApiException
|
|
|
*/
|
|
|
- public function createMenu($params)
|
|
|
+ public function createMenu($data)
|
|
|
{
|
|
|
// 校验父菜单存在
|
|
|
- self::validateParentMenu($params['parent_id'], null);
|
|
|
+ self::validateParentMenu($data['parentId'], null);
|
|
|
// 校验菜单(自己)
|
|
|
- self::validateMenu($params['parent_id'], $params['name'], null);
|
|
|
+ self::validateMenu($data['parentId'], $data['name'], null);
|
|
|
// 插入数据库
|
|
|
+ $menu = self::toModel($data, Menu::class);
|
|
|
+ self::initMenuProperty($menu);
|
|
|
+ return Menu::create($menu->getAttributes())->id;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @throws ApiException
|
|
|
+ */
|
|
|
+ public function updateMenu($params)
|
|
|
+ {
|
|
|
+ // 校验更新的菜单是否存在
|
|
|
+ !self::isExistMenu($params['id']) && self::error('MENU_NOT_EXISTS', Response::HTTP_UNPROCESSABLE_ENTITY);
|
|
|
+ // 校验父菜单存在
|
|
|
+ self::validateParentMenu($params['parentId'], $params['id']);
|
|
|
+ // 校验菜单(自己)
|
|
|
+ self::validateMenu($params['parentId'], $params['name'], $params['id']);
|
|
|
+
|
|
|
+ // 更新到数据库
|
|
|
$menu = self::toModel($params, Menu::class);
|
|
|
+
|
|
|
self::initMenuProperty($menu);
|
|
|
- $menu->save();
|
|
|
- return $menu->id;
|
|
|
+ $menu->update($menu->getAttributes());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @throws ApiException
|
|
|
+ */
|
|
|
+ public function deleteMenu($id)
|
|
|
+ {
|
|
|
+ // 校验是否还有子菜单
|
|
|
+ self::validateChildrenMenu($id);
|
|
|
+
|
|
|
+ // 校验删除的菜单是否存在
|
|
|
+ !self::isExistMenu($id) && self::error('MENU_NOT_EXISTS',Response::HTTP_UNPROCESSABLE_ENTITY);
|
|
|
+
|
|
|
+ $menu = self::toModel(['id' => $id], Menu::class);
|
|
|
+
|
|
|
+ // 标记删除
|
|
|
+ return $menu->delete();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getMenu($id)
|
|
|
+ {
|
|
|
+ $menu = Menu::query();
|
|
|
+ $select = ['id','name','permission','type','sort','parent_id as parentId','path','icon','component','component_name as componentName','status','visible','keep_alive as keepAlive','always_show as alwaysShow'];
|
|
|
+ return $menu->select($select)->find($id)->toArray();
|
|
|
+ }
|
|
|
+ public function getMenuList($params = []): array
|
|
|
+ {
|
|
|
+ $menu = Menu::query();
|
|
|
+ $select = ['id','name','permission','type','sort','parent_id as parentId','path','icon','component','component_name as componentName','status','visible','keep_alive as keepAlive','always_show as alwaysShow'];
|
|
|
+ !empty($params['name']) && $menu->whereLike('name', "%{$params['name']}%");
|
|
|
+ !empty($params['status']) && $menu->where('status', $params['status']);
|
|
|
+ return $menu->orderBy('sort')->select($select)->get()->toArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getSimpleMenuList(): array
|
|
|
+ {
|
|
|
+ $menus = $this->getMenuList(['status' => 0]);
|
|
|
+ $list = self::filterDisableMenus($menus);
|
|
|
+// list.sort(Comparator . comparing(MenuDO::getSort));
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected static function isExistMenu(int|array $condition): bool
|
|
|
+ {
|
|
|
+ $menu = Menu::query();
|
|
|
+ is_array($condition) && $menu->where($condition);
|
|
|
+ is_numeric($condition) && $menu->where('id', $condition);
|
|
|
+ return $menu->exists();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @throws ApiException
|
|
|
+ */
|
|
|
+ protected static function validateChildrenMenu($parentId): void
|
|
|
+ {
|
|
|
+ Menu::query()->where('parent_id', $parentId)->count() && self::error('MENU_EXISTS_CHILDREN', Response::HTTP_UNPROCESSABLE_ENTITY);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -39,14 +115,14 @@ class MenuService extends Service
|
|
|
{
|
|
|
if (!$parentId) return;
|
|
|
// 不能设置自己为父菜单
|
|
|
- $parentId === $childId && self::error('MENU_PARENT_ERROR');
|
|
|
+ $parentId === $childId && self::error('MENU_PARENT_ERROR',Response::HTTP_UNPROCESSABLE_ENTITY);
|
|
|
|
|
|
- $menu = Menu::query()->find($parentId);
|
|
|
+ $menu = Menu::query()->select('type')->find($parentId);
|
|
|
// 父菜单不存在
|
|
|
- !$menu && self::error('MENU_PARENT_NOT_EXISTS');
|
|
|
+ !$menu && self::error('MENU_PARENT_NOT_EXISTS', Response::HTTP_UNPROCESSABLE_ENTITY);
|
|
|
|
|
|
// 父菜单必须是目录或者菜单类型
|
|
|
- $menu->type != MenuType::DIR && $menu->type != MenuType::MENU && self::error('MENU_PARENT_NOT_DIR_OR_MENU');
|
|
|
+ $menu->type != MenuType::DIR && $menu->type != MenuType::MENU && self::error('MENU_PARENT_NOT_DIR_OR_MENU', Response::HTTP_UNPROCESSABLE_ENTITY);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -59,8 +135,8 @@ class MenuService extends Service
|
|
|
if (is_null($menu)) return;
|
|
|
|
|
|
// 如果 id 为空,说明不用比较是否为相同 id 的菜单
|
|
|
- !$id && self::error('MENU_NAME_DUPLICATE');
|
|
|
- $menu->id !== $id && self::error('MENU_NAME_DUPLICATE');
|
|
|
+ !$id && self::error('MENU_NAME_DUPLICATE', Response::HTTP_UNPROCESSABLE_ENTITY);
|
|
|
+ $menu->id !== $id && self::error('MENU_NAME_DUPLICATE', Response::HTTP_UNPROCESSABLE_ENTITY);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -78,7 +154,31 @@ class MenuService extends Service
|
|
|
$menu->icon = null;
|
|
|
$menu->path = null;
|
|
|
}
|
|
|
+ (isset($menu->permission) && empty($menu->permission) || !isset($menu->permission)) && ($menu->permission = null);
|
|
|
+ }
|
|
|
|
|
|
- isset($menu->permission) && ($menu->permission = $menu->permission ?: null);
|
|
|
+ private static function filterDisableMenus($menus): array
|
|
|
+ {
|
|
|
+ if (empty($menus)) return [];
|
|
|
+// $menuMap = convertMap($menus,'id');
|
|
|
+// $menuMap = collect($menus)->mapWithKeys(function ($menu) {
|
|
|
+// return [$menu['id'] => $menu];
|
|
|
+// })->all();
|
|
|
+
|
|
|
+ // 遍历 menu 菜单,查找不是禁用的菜单,添加到 enabledMenus 结果
|
|
|
+ $enabledMenus = [];
|
|
|
+ $disabledMenuCache = []; // 存下递归搜索过被禁用的菜单,防止重复的搜索
|
|
|
+
|
|
|
+ foreach ($menus as $menu) {
|
|
|
+ if($menu['status'] !== 0) continue;
|
|
|
+// if($menu['type'] === 3) continue;
|
|
|
+// if (isMenuDisabled($menu, $menuMap, $disabledMenuCache)) {
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+ $enabledMenus[] = ['id' => $menu['id'],'status' => $menu['status'], 'name' => $menu['name'], 'parentId' => $menu['parentId'], 'type' => $menu['type']];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $enabledMenus;
|
|
|
}
|
|
|
+
|
|
|
}
|