avue-router.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. let RouterPlugin = function () {
  2. this.$router = null
  3. this.$store = null
  4. }
  5. RouterPlugin.install = function (vue, option = {}) {
  6. this.$router = option.router
  7. this.$store = option.store
  8. this.$vue = new vue({ i18n: option.i18n })
  9. // 这个的作用是 为了检查出网页链接,因为本项目用到了 iframe
  10. function isURL(s) {
  11. if (s.includes('html')) return true
  12. return /^http[s]?:\/\/.*/.test(s)
  13. }
  14. // 将参数处理为参数的形式拼接
  15. function objToform(obj) {
  16. let result = []
  17. Object.keys(obj).forEach((ele) => {
  18. result.push(`${ele}=${obj[ele]}`)
  19. })
  20. return result.join('&')
  21. }
  22. this.$router.$avueRouter = {
  23. //全局配置
  24. $website: this.$store.getters.website,
  25. group: '',
  26. meta: {},
  27. safe: this,
  28. // 设置标题
  29. setTitle: (title) => {
  30. const defaultTitle = this.$vue.$t('title')
  31. title = title ? `${defaultTitle}-${title}` : defaultTitle
  32. document.title = title
  33. },
  34. closeTag: (value) => {
  35. let tag = value || this.$store.getters.tag
  36. if (typeof value === 'string') {
  37. tag = this.$store.getters.tagList.filter((ele) => ele.value === value)[0]
  38. }
  39. this.$store.commit('DEL_TAG', tag)
  40. },
  41. generateTitle: (title, key) => {
  42. if (!key) return title
  43. const hasKey = this.$vue.$te('route.' + key)
  44. if (hasKey) {
  45. // $t :this method from vue-i18n, inject in @/lang/index.js
  46. const translatedTitle = this.$vue.$t('route.' + key)
  47. return translatedTitle
  48. }
  49. return title
  50. },
  51. //处理路由
  52. getPath: function (params) {
  53. let { src } = params
  54. let result = src || '/'
  55. if (isURL(src)) {
  56. result = `/myiframe/urlPath?${objToform(params)}`
  57. }
  58. return result
  59. },
  60. //正则处理路由
  61. vaildPath: function (list, path) {
  62. let result = false
  63. list.forEach((ele) => {
  64. if (new RegExp('^' + ele + '.*', 'g').test(path)) {
  65. result = true
  66. }
  67. })
  68. return result
  69. },
  70. //设置路由值
  71. getValue: function (route) {
  72. let value = ''
  73. if (route.query.src) {
  74. value = route.query.src
  75. } else {
  76. value = route.path
  77. }
  78. return value
  79. },
  80. //动态路由
  81. // 路由是专门的一个接口获取
  82. /**
  83. * aMenu: 接受到的动态路由数据 menu的结构外层有父级path 里面有一个childen 记录页面的路由
  84. * first: 为了区分外界 调用formatRoutes 和 当前文件调用 formatRoutes
  85. */
  86. formatRoutes: function (aMenu = [], first) {
  87. const aRouter = []
  88. // 获取到全局配置中的 props
  89. const propsConfig = this.$website.menu.props
  90. // 设置 props默认值 作用就是将字段设置成配置的
  91. const propsDefault = {
  92. label: propsConfig.label || 'name',
  93. path: propsConfig.path || 'path',
  94. icon: propsConfig.icon || 'icon',
  95. children: propsConfig.children || 'children',
  96. meta: propsConfig.meta || 'meta',
  97. }
  98. // 如果没有权限菜单就结束
  99. if (aMenu.length === 0) return
  100. // 开始处理menu
  101. for (let i = 0; i < aMenu.length; i++) {
  102. // 取到当前要处理的一项
  103. const oMenu = aMenu[i]
  104. // 这一块的赋值 也就是取到返回的值
  105. let path = (() => {
  106. if (first) {
  107. // 将 '/index' 替换为 ''
  108. if (!oMenu[propsDefault.path]) {
  109. oMenu[propsDefault.path] = ''
  110. }
  111. return oMenu[propsDefault.path].replace('/index', '')
  112. } else {
  113. return oMenu[propsDefault.path]
  114. }
  115. })(),
  116. //特殊处理组件 执行完这个 component 也就是精确到具体的文件了 views文件夹下面就是具体的页面代码
  117. component = 'views' + oMenu.path,
  118. name = oMenu[propsDefault.label],
  119. icon = oMenu[propsDefault.icon],
  120. children = oMenu[propsDefault.children],
  121. meta = oMenu[propsDefault.meta] || {}
  122. // meta中 keepalive 的处理
  123. meta = Object.assign(
  124. meta,
  125. (function () {
  126. if (option.keepAlive === true) {
  127. return {
  128. keepAlive: true,
  129. }
  130. }
  131. })()
  132. )
  133. //是否有子路由
  134. const isChild = children && children.length !== 0
  135. const oRouter = {
  136. path: path,
  137. component(resolve) {
  138. // 判断是否为首路由
  139. if (first) {
  140. require(['../page/index'], resolve)
  141. return
  142. // 判断是否为多层路由
  143. } else if (isChild && !first) {
  144. require(['../page/index/layout'], resolve)
  145. return
  146. // 判断是否为最终的页面视图
  147. } else {
  148. require([`../${component}.vue`], resolve)
  149. }
  150. },
  151. name: name,
  152. icon: icon,
  153. meta: meta,
  154. redirect: (() => {
  155. // 第一次进来但是没有子路由的 需要添加redirect
  156. if (!isChild && first && !isURL(path)) return `${path}/index`
  157. else return ''
  158. })(),
  159. // 整理子路由的route 配置
  160. // 处理是否为一级路由
  161. children: !isChild
  162. ? (() => {
  163. if (first) {
  164. // 这里的isURL判断,因为这个网站有使用 iframe。所以需要判断是否为网页链接
  165. if (!isURL(path)) oMenu[propsDefault.path] = `${path}/index`
  166. return [
  167. {
  168. component(resolve) {
  169. require([`../${component}.vue`], resolve)
  170. },
  171. icon: icon,
  172. name: name,
  173. meta: meta,
  174. path: 'index',
  175. },
  176. ]
  177. }
  178. return []
  179. })()
  180. : (() => {
  181. /**
  182. * 这里是重点,当有子路由的时候 会再去执行 formatRoutes 方法,然后又会有一个新的 aMenu for循环。
  183. * 最后返回的是一个数组 aRouter 这个数组就会作为 childen的值被 return
  184. */
  185. return this.formatRoutes(children, false)
  186. })(),
  187. }
  188. aRouter.push(oRouter)
  189. }
  190. // for循环结束
  191. // 这个first 卡的其实就是首路由
  192. if (first) {
  193. this.safe.$router.addRoutes(aRouter)
  194. } else {
  195. // 这里返回的是子组件
  196. return aRouter
  197. }
  198. },
  199. }
  200. }
  201. export default RouterPlugin