menu.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import XEUtils from "xe-utils"
  2. import {dynamicRoutes, staticRoutes} from "/@/router/route";
  3. /**
  4. * @description: 处理后端菜单数据格式
  5. * @param {Array} menuData
  6. * @return {*}
  7. */
  8. export const handleMenu = (menuData: Array<any>) => {
  9. // 先处理menu meta数据转换
  10. const handleMeta = (item: any) => {
  11. item.meta = {
  12. title: item.title,
  13. isLink: item.link_url,
  14. isHide: !item.visible,
  15. isKeepAlive: item.cache,
  16. isAffix: item.is_affix,
  17. isIframe: item.is_iframe,
  18. roles: ['admin'],
  19. icon: item.icon
  20. }
  21. item.name = item.component_name
  22. item.path = item.web_path
  23. return item
  24. }
  25. // 处理框架外的路由
  26. const handleFrame = (item: any) => {
  27. if (item.is_iframe) {
  28. item.meta = {
  29. title: item.title,
  30. isLink: item.link_url,
  31. isHide: !item.visible,
  32. isKeepAlive: item.cache,
  33. isAffix: item.is_affix,
  34. isIframe: item.is_iframe,
  35. roles: ['admin'],
  36. icon: item.icon
  37. }
  38. item.name = item.component_name
  39. item.path = item.web_path
  40. }
  41. return item
  42. }
  43. // 框架内路由
  44. const defaultRoutes:Array<any> = []
  45. // 框架外路由
  46. const iframeRoutes:Array<any> = []
  47. menuData.forEach((val) => {
  48. // if (val.is_iframe) {
  49. // // iframeRoutes.push(handleFrame(val))
  50. // } else {
  51. // defaultRoutes.push(handleMeta(val))
  52. // }
  53. defaultRoutes.push(handleMeta(val))
  54. })
  55. const data = XEUtils.toArrayTree(defaultRoutes, {
  56. parentKey: 'parent',
  57. strict: true,
  58. })
  59. const dynamicRoutes = [
  60. {
  61. path: '/home', name: 'home', component: '/system/home/index', meta: {
  62. title: 'message.router.home',
  63. isLink: '',
  64. isHide: false,
  65. isKeepAlive: true,
  66. isAffix: true,
  67. isIframe: false,
  68. roles: ['admin'],
  69. icon: 'iconfont icon-shouye'
  70. }
  71. },
  72. ...data
  73. ]
  74. return {frameIn:dynamicRoutes,frameOut:iframeRoutes}
  75. }