avue-router.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 ? `${title}-${defaultTitle}` : 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. // window.console.log('aMenu')
  88. // window.console.log(aMenu)
  89. const aRouter = []
  90. // 获取到全局配置中的 props
  91. const propsConfig = this.$website.menu.props;
  92. // 设置 props默认值 作用就是将字段设置成配置的
  93. const propsDefault = {
  94. label: propsConfig.label || 'name',
  95. path: propsConfig.path || 'path',
  96. icon: propsConfig.icon || 'icon',
  97. children: propsConfig.children || 'children',
  98. meta: propsConfig.meta || 'meta',
  99. }
  100. // 如果没有权限菜单就结束
  101. if (aMenu.length === 0) return;
  102. // 开始处理menu
  103. for (let i = 0; i < aMenu.length; i++) {
  104. // 取到当前要处理的一项
  105. const oMenu = aMenu[i];
  106. // 这一块的赋值 也就是取到返回的值
  107. let path = (() => {
  108. if (first) {
  109. // 将 '/index' 替换为 ''
  110. return oMenu[propsDefault.path].replace('/index', '')
  111. } else {
  112. return oMenu[propsDefault.path]
  113. }
  114. })(),
  115. //特殊处理组件 执行完这个 component 也就是精确到具体的文件了 views文件夹下面就是具体的页面代码
  116. component = 'views' + oMenu.path,
  117. name = oMenu[propsDefault.label],
  118. icon = oMenu[propsDefault.icon],
  119. children = oMenu[propsDefault.children],
  120. meta = oMenu[propsDefault.meta] || {};
  121. // meta中 keepalive 的处理
  122. meta = Object.assign(meta, (function () {
  123. if (option.keepAlive === true) {
  124. return {
  125. keepAlive: true
  126. }
  127. }
  128. })());
  129. //是否有子路由
  130. const isChild = children.length !== 0;
  131. const oRouter = {
  132. path: path,
  133. component(resolve) {
  134. // 判断是否为首路由
  135. if (first) {
  136. require(['../page/index'], resolve)
  137. return
  138. // 判断是否为多层路由
  139. } else if (isChild && !first) {
  140. require(['../page/index/layout'], resolve)
  141. return
  142. // 判断是否为最终的页面视图
  143. } else {
  144. require([`../${component}.vue`], resolve)
  145. }
  146. },
  147. name: name,
  148. icon: icon,
  149. meta: meta,
  150. redirect: (() => {
  151. // 第一次进来但是没有子路由的 需要添加redirect
  152. if (!isChild && first && !isURL(path)) return `${path}/index`
  153. else return '';
  154. })(),
  155. // 整理子路由的route 配置
  156. // 处理是否为一级路由
  157. children: !isChild ? (() => {
  158. if (first) {
  159. // 这里的isURL判断,因为这个网站有使用 iframe。所以需要判断是否为网页链接
  160. if (!isURL(path)) oMenu[propsDefault.path] = `${path}/index`;
  161. return [{
  162. component(resolve) {
  163. require([`../${component}.vue`], resolve)
  164. },
  165. icon: icon,
  166. name: name,
  167. meta: meta,
  168. path: 'index'
  169. }]
  170. }
  171. return [];
  172. })() : (() => {
  173. /**
  174. * 这里是重点,当有子路由的时候 会再去执行 formatRoutes 方法,然后又会有一个新的 aMenu for循环。
  175. * 最后返回的是一个数组 aRouter 这个数组就会作为 childen的值被 return
  176. */
  177. return this.formatRoutes(children, false)
  178. })()
  179. }
  180. aRouter.push(oRouter)
  181. }
  182. // for循环结束
  183. // 这个first 卡的其实就是首路由
  184. if (first) {
  185. this.safe.$router.addRoutes(aRouter)
  186. } else {
  187. // 这里返回的是子组件
  188. return aRouter
  189. }
  190. }
  191. }
  192. }
  193. export default RouterPlugin;