path-exists.ts 837 B

1234567891011121314151617181920212223242526272829
  1. import { eventHandler, getQuery } from 'h3';
  2. import { verifyAccessToken } from '~/utils/jwt-utils';
  3. import { MOCK_MENU_LIST } from '~/utils/mock-data';
  4. import { unAuthorizedResponse, useResponseSuccess } from '~/utils/response';
  5. const pathMap: Record<string, any> = { '/': 0 };
  6. function getPaths(menus: any[]) {
  7. menus.forEach((menu) => {
  8. pathMap[menu.path] = String(menu.id);
  9. if (menu.children) {
  10. getPaths(menu.children);
  11. }
  12. });
  13. }
  14. getPaths(MOCK_MENU_LIST);
  15. export default eventHandler(async (event) => {
  16. const userinfo = verifyAccessToken(event);
  17. if (!userinfo) {
  18. return unAuthorizedResponse(event);
  19. }
  20. const { id, path } = getQuery(event);
  21. return (path as string) in pathMap &&
  22. (!id || pathMap[path as string] !== String(id))
  23. ? useResponseSuccess(true)
  24. : useResponseSuccess(false);
  25. });