You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

154 lines
4.3 KiB

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