avue-router.js 4.4 KB

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