generator-routers.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // eslint-disable-next-line
  2. import * as loginService from '@/api/login'
  3. // eslint-disable-next-line
  4. import { BasicLayout, BlankLayout, PageView, RouteView } from '@/layouts'
  5. // 前端路由表
  6. const constantRouterComponents = {
  7. // 基础页面 layout 必须引入
  8. BasicLayout: BasicLayout,
  9. BlankLayout: BlankLayout,
  10. RouteView: RouteView,
  11. PageView: PageView,
  12. '403': () => import(/* webpackChunkName: "error" */ '@/views/exception/403'),
  13. '404': () => import(/* webpackChunkName: "error" */ '@/views/exception/404'),
  14. '500': () => import(/* webpackChunkName: "error" */ '@/views/exception/500'),
  15. // 你需要动态引入的页面组件
  16. 'Workplace': () => import('@/views/dashboard/Workplace'),
  17. 'Analysis': () => import('@/views/dashboard/Analysis'),
  18. // form
  19. 'BasicForm': () => import('@/views/form/BasicForm'),
  20. 'StepForm': () => import('@/views/form/stepForm/StepForm'),
  21. 'AdvanceForm': () => import('@/views/form/advancedForm/AdvancedForm'),
  22. // list
  23. 'TableList': () => import('@/views/list/TableList'),
  24. 'StandardList': () => import('@/views/list/StandardList'),
  25. 'CardList': () => import('@/views/list/CardList'),
  26. 'SearchLayout': () => import('@/views/list/search/SearchLayout'),
  27. 'SearchArticles': () => import('@/views/list/search/Article'),
  28. 'SearchProjects': () => import('@/views/list/search/Projects'),
  29. 'SearchApplications': () => import('@/views/list/search/Applications'),
  30. 'ProfileBasic': () => import('@/views/profile/basic/Index'),
  31. 'ProfileAdvanced': () => import('@/views/profile/advanced/Advanced'),
  32. // result
  33. 'ResultSuccess': () => import(/* webpackChunkName: "result" */ '@/views/result/Success'),
  34. 'ResultFail': () => import(/* webpackChunkName: "result" */ '@/views/result/Error'),
  35. // exception
  36. 'Exception403': () => import(/* webpackChunkName: "fail" */ '@/views/exception/403'),
  37. 'Exception404': () => import(/* webpackChunkName: "fail" */ '@/views/exception/404'),
  38. 'Exception500': () => import(/* webpackChunkName: "fail" */ '@/views/exception/500'),
  39. // account
  40. 'AccountCenter': () => import('@/views/account/center/Index'),
  41. 'AccountSettings': () => import('@/views/account/settings/Index'),
  42. 'BaseSettings': () => import('@/views/account/settings/BaseSetting'),
  43. 'SecuritySettings': () => import('@/views/account/settings/Security'),
  44. 'CustomSettings': () => import('@/views/account/settings/Custom'),
  45. 'BindingSettings': () => import('@/views/account/settings/Binding'),
  46. 'NotificationSettings': () => import('@/views/account/settings/Notification'),
  47. 'TestWork': () => import(/* webpackChunkName: "TestWork" */ '@/views/dashboard/TestWork')
  48. }
  49. // 前端未找到页面路由(固定不用改)
  50. const notFoundRouter = {
  51. path: '*', redirect: '/404', hidden: true
  52. }
  53. // 根级菜单
  54. const rootRouter = {
  55. key: '',
  56. name: 'index',
  57. path: '',
  58. component: 'BasicLayout',
  59. redirect: '/dashboard',
  60. meta: {
  61. title: '首页'
  62. },
  63. children: []
  64. }
  65. /**
  66. * 动态生成菜单
  67. * @param token
  68. * @returns {Promise<Router>}
  69. */
  70. export const generatorDynamicRouter = (token) => {
  71. return new Promise((resolve, reject) => {
  72. loginService.getCurrentUserNav(token).then(res => {
  73. console.log('res', res)
  74. const { result } = res
  75. const menuNav = []
  76. const childrenNav = []
  77. // 后端数据, 根级树数组, 根级 PID
  78. listToTree(result, childrenNav, 0)
  79. rootRouter.children = childrenNav
  80. menuNav.push(rootRouter)
  81. console.log('menuNav', menuNav)
  82. const routers = generator(menuNav)
  83. routers.push(notFoundRouter)
  84. console.log('routers', routers)
  85. resolve(routers)
  86. }).catch(err => {
  87. reject(err)
  88. })
  89. })
  90. }
  91. /**
  92. * 格式化树形结构数据 生成 vue-router 层级路由表
  93. *
  94. * @param routerMap
  95. * @param parent
  96. * @returns {*}
  97. */
  98. export const generator = (routerMap, parent) => {
  99. return routerMap.map(item => {
  100. const { title, show, hideChildren, hiddenHeaderContent, target, icon } = item.meta || {}
  101. const currentRouter = {
  102. // 如果路由设置了 path,则作为默认 path,否则 路由地址 动态拼接生成如 /dashboard/workplace
  103. path: item.path || `${parent && parent.path || ''}/${item.key}`,
  104. // 路由名称,建议唯一
  105. name: item.name || item.key || '',
  106. // 该路由对应页面的 组件 :方案1
  107. // component: constantRouterComponents[item.component || item.key],
  108. // 该路由对应页面的 组件 :方案2 (动态加载)
  109. component: (constantRouterComponents[item.component || item.key]) || (() => import(`@/views/${item.component}`)),
  110. // meta: 页面标题, 菜单图标, 页面权限(供指令权限用,可去掉)
  111. meta: {
  112. title: title,
  113. icon: icon || undefined,
  114. hiddenHeaderContent: hiddenHeaderContent,
  115. target: target,
  116. permission: item.name
  117. }
  118. }
  119. // 是否设置了隐藏菜单
  120. if (show === false) {
  121. currentRouter.hidden = true
  122. }
  123. // 是否设置了隐藏子菜单
  124. if (hideChildren) {
  125. currentRouter.hideChildrenInMenu = true
  126. }
  127. // 为了防止出现后端返回结果不规范,处理有可能出现拼接出两个 反斜杠
  128. if (!currentRouter.path.startsWith('http')) {
  129. currentRouter.path = currentRouter.path.replace('//', '/')
  130. }
  131. // 重定向
  132. item.redirect && (currentRouter.redirect = item.redirect)
  133. // 是否有子菜单,并递归处理
  134. if (item.children && item.children.length > 0) {
  135. // Recursion
  136. currentRouter.children = generator(item.children, currentRouter)
  137. }
  138. return currentRouter
  139. })
  140. }
  141. /**
  142. * 数组转树形结构
  143. * @param list 源数组
  144. * @param tree 树
  145. * @param parentId 父ID
  146. */
  147. const listToTree = (list, tree, parentId) => {
  148. list.forEach(item => {
  149. // 判断是否为父级菜单
  150. if (item.parentId === parentId) {
  151. const child = {
  152. ...item,
  153. key: item.key || item.name,
  154. children: []
  155. }
  156. // 迭代 list, 找到当前菜单相符合的所有子菜单
  157. listToTree(list, child.children, item.id)
  158. // 删掉不存在 children 值的属性
  159. if (child.children.length <= 0) {
  160. delete child.children
  161. }
  162. // 加入到树中
  163. tree.push(child)
  164. }
  165. })
  166. }