动态菜单和动态路由的 antd pro

menu.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { isUrl } from '../utils/utils';
  2. // const menuData = [
  3. // {
  4. // name: 'dashboard',
  5. // icon: 'dashboard',
  6. // path: 'dashboard',
  7. // children: [
  8. // {
  9. // name: '分析页',
  10. // path: 'analysis',
  11. // },
  12. // {
  13. // name: '监控页',
  14. // path: 'monitor',
  15. // },
  16. // {
  17. // name: '工作台',
  18. // path: 'workplace',
  19. // // hideInBreadcrumb: true,
  20. // // hideInMenu: true,
  21. // },
  22. // ],
  23. // },
  24. // {
  25. // name: '表单页',
  26. // icon: 'form',
  27. // path: 'form',
  28. // children: [
  29. // {
  30. // name: '基础表单',
  31. // path: 'basic-form',
  32. // },
  33. // {
  34. // name: '分步表单',
  35. // path: 'step-form',
  36. // },
  37. // {
  38. // name: '高级表单',
  39. // authority: 'admin',
  40. // path: 'advanced-form',
  41. // },
  42. // ],
  43. // },
  44. // {
  45. // name: '列表页',
  46. // icon: 'table',
  47. // path: 'list',
  48. // children: [
  49. // {
  50. // name: '查询表格',
  51. // path: 'table-list',
  52. // },
  53. // {
  54. // name: '标准列表',
  55. // path: 'basic-list',
  56. // },
  57. // {
  58. // name: '卡片列表',
  59. // path: 'card-list',
  60. // },
  61. // {
  62. // name: '搜索列表',
  63. // path: 'search',
  64. // children: [
  65. // {
  66. // name: '搜索列表(文章)',
  67. // path: 'articles',
  68. // },
  69. // {
  70. // name: '搜索列表(项目)',
  71. // path: 'projects',
  72. // },
  73. // {
  74. // name: '搜索列表(应用)',
  75. // path: 'applications',
  76. // },
  77. // ],
  78. // },
  79. // ],
  80. // },
  81. // {
  82. // name: '详情页',
  83. // icon: 'profile',
  84. // path: 'profile',
  85. // children: [
  86. // {
  87. // name: '基础详情页',
  88. // path: 'basic',
  89. // },
  90. // {
  91. // name: '高级详情页',
  92. // path: 'advanced',
  93. // authority: 'admin',
  94. // },
  95. // ],
  96. // },
  97. // {
  98. // name: '结果页',
  99. // icon: 'check-circle-o',
  100. // path: 'result',
  101. // children: [
  102. // {
  103. // name: '成功',
  104. // path: 'success',
  105. // },
  106. // {
  107. // name: '失败',
  108. // path: 'fail',
  109. // },
  110. // ],
  111. // },
  112. // {
  113. // name: '异常页',
  114. // icon: 'warning',
  115. // path: 'exception',
  116. // children: [
  117. // {
  118. // name: '403',
  119. // path: '403',
  120. // },
  121. // {
  122. // name: '404',
  123. // path: '404',
  124. // },
  125. // {
  126. // name: '500',
  127. // path: '500',
  128. // },
  129. // {
  130. // name: '触发异常',
  131. // path: 'trigger',
  132. // hideInMenu: true,
  133. // },
  134. // ],
  135. // },
  136. // {
  137. // name: '账户',
  138. // icon: 'user',
  139. // path: 'user',
  140. // authority: 'guest',
  141. // children: [
  142. // {
  143. // name: '登录',
  144. // path: 'login',
  145. // },
  146. // {
  147. // name: '注册',
  148. // path: 'register',
  149. // },
  150. // {
  151. // name: '注册结果',
  152. // path: 'register-result',
  153. // },
  154. // ],
  155. // },
  156. // ];
  157. function formatter(data, parentPath = '/', parentAuthority) {
  158. return data.map(item => {
  159. let { path } = item;
  160. if (!isUrl(path)) {
  161. path = parentPath + item.path;
  162. }
  163. const result = {
  164. ...item,
  165. path,
  166. authority: item.authority || parentAuthority,
  167. };
  168. if (item.children) {
  169. result.children = formatter(item.children, `${parentPath}${item.path}/`, item.authority);
  170. }
  171. return result;
  172. });
  173. }
  174. export const getMenuData = (menuData) => formatter(menuData);