avue-router.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. let RouterPlugin = function () {
  2. this.$router = null
  3. this.$store = null
  4. }
  5. RouterPlugin.install = function (router, store) {
  6. this.$router = router
  7. this.$store = store
  8. function objToform (obj) {
  9. let result = []
  10. Object.keys(obj).forEach(ele => {
  11. result.push(`${ele}=${obj[ele]}`)
  12. })
  13. return result.join('&')
  14. }
  15. this.$router.$avueRouter = {
  16. // 全局配置
  17. $website: this.$store.getters.website,
  18. $defaultTitle: 'PigX微服务快速开发框架',
  19. routerList: [],
  20. group: '',
  21. safe: this,
  22. // 设置标题
  23. setTitle: function (title) {
  24. title = title ? `${title}——${this.$defaultTitle}` : this.$defaultTitle;
  25. document.title = title
  26. },
  27. closeTag: (value) => {
  28. const tag = value || this.$store.getters.tag
  29. this.$store.commit('DEL_TAG', tag)
  30. },
  31. // 处理路由
  32. getPath: function (params) {
  33. let { src } = params
  34. let result = src || '/'
  35. if (src.includes('http') || src.includes('https')) {
  36. result = `/myiframe/urlPath?${objToform(params)}`
  37. }
  38. return result
  39. },
  40. // 正则处理路由
  41. vaildPath: function (list, path) {
  42. let result = false
  43. list.forEach(ele => {
  44. if (new RegExp('^' + ele + '.*', 'g').test(path)) {
  45. result = true
  46. }
  47. })
  48. return result
  49. },
  50. // 设置路由值
  51. getValue: function (route) {
  52. let value = ''
  53. if (route.query.src) {
  54. value = route.query.src
  55. } else {
  56. value = route.path
  57. }
  58. return value
  59. },
  60. // 动态路由
  61. formatRoutes: function (aMenu = [], first) {
  62. const aRouter = []
  63. const propsConfig = this.$website.menu.props
  64. const propsDefault = {
  65. label: propsConfig.label || 'label',
  66. path: propsConfig.path || 'path',
  67. icon: propsConfig.icon || 'icon',
  68. children: propsConfig.children || 'children',
  69. meta: propsConfig.meta || 'meta'
  70. }
  71. if (aMenu.length === 0) return
  72. for (let i = 0; i < aMenu.length; i++) {
  73. const oMenu = aMenu[i]
  74. if (this.routerList.includes(oMenu[propsDefault.path])) return
  75. const path = (() => {
  76. if (first) {
  77. return oMenu[propsDefault.path].replace('/index', '')
  78. } else {
  79. return oMenu[propsDefault.path]
  80. }
  81. })()
  82. const component = oMenu.component
  83. const name = oMenu[propsDefault.label]
  84. const icon = oMenu[propsDefault.icon]
  85. const children = oMenu[propsDefault.children]
  86. const meta = {
  87. keepAlive: Number(oMenu['keepAlive']) === 0
  88. }
  89. const isChild = children.length !== 0
  90. const oRouter = {
  91. path: path,
  92. component (resolve) {
  93. // 判断是否为首路由
  94. if (first) {
  95. require(['../page/index'], resolve)
  96. // 判断是否为多层路由
  97. } else if (isChild && !first) {
  98. require(['../page/index/layout'], resolve)
  99. // 判断是否为最终的页面视图
  100. } else {
  101. require([`../${component}.vue`], resolve)
  102. }
  103. },
  104. name: name,
  105. icon: icon,
  106. meta: meta,
  107. redirect: (() => {
  108. if (!isChild && first) return `${path}/index`
  109. else return ''
  110. })(),
  111. // 处理是否为一级路由
  112. children: !isChild ? (() => {
  113. if (first) {
  114. oMenu[propsDefault.path] = `${path}/index`
  115. return [{
  116. component (resolve) { require([`../${component}.vue`], resolve) },
  117. icon: icon,
  118. name: name,
  119. meta: meta,
  120. path: 'index'
  121. }]
  122. }
  123. return []
  124. })() : (() => {
  125. return this.formatRoutes(children, false)
  126. })()
  127. }
  128. aRouter.push(oRouter)
  129. }
  130. if (first) {
  131. if (!this.routerList.includes(aRouter[0][propsDefault.path])) {
  132. this.safe.$router.addRoutes(aRouter)
  133. this.routerList.push(aRouter[0][propsDefault.path])
  134. }
  135. } else {
  136. return aRouter
  137. }
  138. }
  139. }
  140. }
  141. export default RouterPlugin