menu.controller.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { sleep } from '@/utils';
  2. import { Controller, Get, HttpCode, HttpStatus, Request } from '@nestjs/common';
  3. @Controller('menu')
  4. export class MenuController {
  5. /**
  6. * 获取用户所有菜单
  7. */
  8. @Get('getAll')
  9. @HttpCode(HttpStatus.OK)
  10. async getAll(@Request() req: Request) {
  11. // 模拟请求延迟
  12. await sleep(500);
  13. // 请求用户的id
  14. const userId = req.user.id;
  15. // TODO: 改为表方式获取
  16. const dashboardMenus = [
  17. {
  18. component: 'BasicLayout',
  19. meta: {
  20. order: -1,
  21. title: 'page.dashboard.title',
  22. },
  23. name: 'Dashboard',
  24. path: '/',
  25. redirect: '/analytics',
  26. children: [
  27. {
  28. name: 'Analytics',
  29. path: '/analytics',
  30. component: '/dashboard/analytics/index',
  31. meta: {
  32. affixTab: true,
  33. title: 'page.dashboard.analytics',
  34. },
  35. },
  36. {
  37. name: 'Workspace',
  38. path: '/workspace',
  39. component: '/dashboard/workspace/index',
  40. meta: {
  41. title: 'page.dashboard.workspace',
  42. },
  43. },
  44. ],
  45. },
  46. ];
  47. const createDemosMenus = (role: 'admin' | 'super' | 'user') => {
  48. const roleWithMenus = {
  49. admin: {
  50. component: '/demos/access/admin-visible',
  51. meta: {
  52. icon: 'mdi:button-cursor',
  53. title: 'page.demos.access.adminVisible',
  54. },
  55. name: 'AccessAdminVisible',
  56. path: 'admin-visible',
  57. },
  58. super: {
  59. component: '/demos/access/super-visible',
  60. meta: {
  61. icon: 'mdi:button-cursor',
  62. title: 'page.demos.access.superVisible',
  63. },
  64. name: 'AccessSuperVisible',
  65. path: 'super-visible',
  66. },
  67. user: {
  68. component: '/demos/access/user-visible',
  69. meta: {
  70. icon: 'mdi:button-cursor',
  71. title: 'page.demos.access.userVisible',
  72. },
  73. name: 'AccessUserVisible',
  74. path: 'user-visible',
  75. },
  76. };
  77. return [
  78. {
  79. component: 'BasicLayout',
  80. meta: {
  81. icon: 'ic:baseline-view-in-ar',
  82. keepAlive: true,
  83. order: 1000,
  84. title: 'page.demos.title',
  85. },
  86. name: 'Demos',
  87. path: '/demos',
  88. redirect: '/demos/access',
  89. children: [
  90. {
  91. name: 'Access',
  92. path: 'access',
  93. meta: {
  94. icon: 'mdi:cloud-key-outline',
  95. title: 'page.demos.access.backendControl',
  96. },
  97. redirect: '/demos/access/page-control',
  98. children: [
  99. {
  100. name: 'AccessPageControl',
  101. path: 'page-control',
  102. component: '/demos/access/index',
  103. meta: {
  104. icon: 'mdi:page-previous-outline',
  105. title: 'page.demos.access.pageAccess',
  106. },
  107. },
  108. {
  109. name: 'AccessButtonControl',
  110. path: 'button-control',
  111. component: '/demos/access/button-control',
  112. meta: {
  113. icon: 'mdi:button-cursor',
  114. title: 'page.demos.access.buttonControl',
  115. },
  116. },
  117. {
  118. name: 'AccessMenuVisible403',
  119. path: 'menu-visible-403',
  120. component: '/demos/access/menu-visible-403',
  121. meta: {
  122. authority: ['no-body'],
  123. icon: 'mdi:button-cursor',
  124. menuVisibleWithForbidden: true,
  125. title: 'page.demos.access.menuVisible403',
  126. },
  127. },
  128. roleWithMenus[role],
  129. ],
  130. },
  131. ],
  132. },
  133. ];
  134. };
  135. const MOCK_MENUS = [
  136. {
  137. menus: [...dashboardMenus, ...createDemosMenus('super')],
  138. userId: 0,
  139. },
  140. {
  141. menus: [...dashboardMenus, ...createDemosMenus('admin')],
  142. userId: 1,
  143. },
  144. {
  145. menus: [...dashboardMenus, ...createDemosMenus('user')],
  146. userId: 2,
  147. },
  148. ];
  149. return MOCK_MENUS.find((item) => item.userId === userId)?.menus ?? [];
  150. }
  151. }